MT#11731 Consider different content types for API actions

Change-Id: I3edc66cf091ac1cf4575fd12302c1f6befe71e6d
changes/61/3961/19
Irina Peshinskaya 9 years ago
parent 81bb0509dc
commit 36c08c6eb8

@ -22,7 +22,7 @@ class_has 'api_description' => (
is => 'ro',
isa => 'Str',
default =>
'Defines the actual recording of fax messages. It is referred to by the <a href="#faxs">Faxs</a> relation. A GET on an item returns the binary blob of the recording with Content-Type "audio/x-wav".',
'Defines the actual recording of fax messages. It is referred to by the <a href="#faxes">Faxes</a> relation. A GET on an item returns the binary blob of the recording with Content-Type "audio/x-wav".',
);
with 'NGCP::Panel::Role::API::FaxRecordings';

@ -68,7 +68,8 @@ __PACKAGE__->config(
Does => [qw(ACL CheckTrailingSlash RequireSSL)],
Method => $_,
Path => __PACKAGE__->dispatch_path,
} } @{ __PACKAGE__->allowed_methods }
('POST' eq $_) ? (ContentType => ['multipart/form-data']) : (),
} } @{ __PACKAGE__->allowed_methods },
},
action_roles => [qw(HTTPMethods)],
);

@ -153,11 +153,13 @@ sub GET : Allow {
if ($item_rs) {
$sorting_cols = [$item_rs->result_source->columns];
}
my ($form_fields,$form_fields_upload) = $form ? $self->get_collection_properties($form) : ([],[]);
$c->stash->{collections}->{$rel} = {
name => $mod,
description => $full_mod->api_description,
fields => $form ? $self->get_collection_properties($form) : [],
fields => $form_fields,
uploads => $form_fields_upload,
config => $full_mod->config ,
query_params => $query_params,
actions => $actions,
item_actions => $item_actions,
@ -354,7 +356,7 @@ sub get_field_poperties :Private{
unless (defined $desc && length($desc) > 0) {
$desc = 'to be described ...';
}
return { name => $name, description => $desc, types => \@types };
return { name => $name, description => $desc, types => \@types, type_original => $field->type };
}
sub get_collection_properties {
my ($self, $form) = @_;
@ -363,10 +365,14 @@ sub get_collection_properties {
my %renderlist = defined $renderlist ? map { $_ => 1 } @{$renderlist} : ();
my @props = ();
my @uploads = ();
foreach my $f($form->fields) {
my $name = $f->name;
next if (defined $renderlist && !exists $renderlist{$name});
push @props, $self->get_field_poperties($f);
my $field_spec = $self->get_field_poperties($f);
next if !$field_spec;
push @props, $field_spec;
push @uploads, $field_spec if $f->type =~/Upload/;
if(my $spec = $f->element_attr->{implicit_parameter}){
my $f_implicit = clone($f);
foreach my $field_attribute (keys %{$spec}){
@ -376,7 +382,7 @@ sub get_collection_properties {
}
}
@props = sort{$a->{name} cmp $b->{name}} @props;
return \@props;
return (\@props,\@uploads);
}
sub end : Private {

@ -17,6 +17,7 @@
props_creation_first_flag = 1;
FOREACH p IN col.fields;
NEXT IF p.types.0 == 'null';
NEXT IF p.type_original == 'Upload';
IF props_creation_first_flag;
props_creation_first_flag = 0;
ELSE;
@ -40,6 +41,13 @@
props_plain = col.sample _ "\n";
END;
%]
[%-
FOREACH p IN col.uploads;
#NEXT IF p.types.0 == 'null';
uploads_form = uploads_plain _ ' --form \'' _ p.name _ '=@/path/to/' _ p.name _ '/file\'';
uploads_data = uploads_data _ '"' _ p.name _ '" => [\'/path/to/' _ p.name _ '/file\']' _ ( !loop.last() ? ",\n" : "" );
END;
%]
<h[% level + 1 %]>Collection Actions</h[% level + 1%]>
Allowed methods for the collection as in <span>METHOD [% uri %]</span>:
@ -67,7 +75,6 @@ Allowed methods for a collection item as in <span>METHOD [% uri %]id</span>:
<h[% level + 1 %]>Properties</h[% level + 1%]>
[% UNLESS col.fields.size -%]
See description how to obtain properties, if any.
[% ELSE -%]
@ -225,30 +232,58 @@ Link: </api/' _ id _ '/1>; rel="item self"
</p>
[% END -%]
[% IF col.actions.grep('^POST$').size -%]
<h[% level + 2 %]>Create a new <i>[% id %]</i> item</h[% level + 2 %]>
<p>
[%
[% MACRO request_macro BLOCK ;
props = props_plain | collapse;
uploads_form = uploads_form | collapse;
content_type = col.config.action.${http_request}.ContentType.0 ? col.config.action.${http_request}.ContentType.0 : 'application/json';
IF content_type == 'multipart/form-data' ;
request_data_plain = '\'json={' _ props _ '}\' ';
props = props_plain | indent(6);
uploads_data = uploads_data | indent(3);
request_data = "{\n json => {\n" _ props _ ( uploads_data ? " }\n" _ uploads_data : "" ) _ "\n}";
request_form = ' --form ' _ request_data_plain _ uploads_form ;
ELSE;
request_data_plain = '\'{' _ props _ '}\'';
props = props_plain | indent(3);
uploads_data = uploads_data | indent(0);
request_data = "{\n" _ props _ ( uploads_data ? "\n" _ uploads_data : "" ) _ "}";
request_form = ' --data-binary ' _ request_data_plain _ uploads_form;
END;
cmd = 'curl -i -X POST -H \'Connection: close\' -H \'Content-Type: application/json\' --cert NGCP-API-client-certificate.pem --cacert ca-cert.pem \'https://example.org:1443/api/' _ id _ '/\' --data-binary \'{ ' _ props _ ' }\'';
INCLUDE helpers/api_command.tt cmd=cmd level=level+3;
props = props_plain | indent(3);
IF http_request == 'POST' ;
response = 'HTTP/1.1 201 Created';
IF !col.properties.asynchronous ;
response = response _ '
Location: /api/' _ id _ '/2' ;
END ;
ELSIF http_request == 'PUT' ;
item_id = 2;
request_headers = "\nPrefer: return=minimal";
request_headers_form = ' -H \'Prefer: return=minimal\'';
response =
'HTTP/1.1 204 No Content
Preference-Applied: return=minimal';
END;
cmd = 'curl -i -X ' _ http_request _ ' -H \'Connection: close\' -H \'Content-Type: ' _ content_type _ '\'' _ request_headers_form _ ' --cert NGCP-API-client-certificate.pem --cacert ca-cert.pem \'https://example.org:1443/api/' _ id _ '/' _ item_id _ '\' ' _ request_form ;
request =
'POST /api/' _ id _ '/ HTTP/1.1
Content-Type: application/json
http_request _ ' /api/' _ id _ '/' _ item_id _ ' HTTP/1.1
Content-Type: ' _ content_type _ request_headers _ '
{
' _ props _ '}';
response = 'HTTP/1.1 201 Created';
' _ request_data;
IF !col.properties.asynchronous ;
response = response _ '
Location: /api/' _ id _ '/2' ;
END ;
INCLUDE helpers/api_command.tt cmd=cmd level=level+3 extended=http_request == 'PUT';
INCLUDE helpers/api_req_res.tt request=request response=response level=level+3;
END%]
[% IF col.actions.grep('^POST$').size -%]
<h[% level + 2 %]>Create a new <i>[% id %]</i> item</h[% level + 2 %]>
<p>
[%
request_macro(http_request = 'POST');
%]
</p>
[% END -%]
@ -257,24 +292,7 @@ Location: /api/' _ id _ '/2' ;
<h[% level + 2 %]>Update an existing <i>[% id %]</i> item</h[% level + 2 %]>
<p>
[%
props = props_plain | collapse;
cmd = 'curl -i -X PUT -H \'Connection: close\' -H \'Content-Type: application/json\' -H \'Prefer: return=minimal\' --cert NGCP-API-client-certificate.pem --cacert ca-cert.pem \'https://example.org:1443/api/' _ id _ '/2\' --data-binary \'{ ' _ props _ ' }\'';
INCLUDE helpers/api_command.tt cmd=cmd extended=1 level=level+3;
props = props_plain | indent(3);
request =
'PUT /api/' _ id _ '/2 HTTP/1.1
Content-Type: application/json
Prefer: return=minimal
{
' _ props _ '}';
response =
'HTTP/1.1 204 No Content
Preference-Applied: return=minimal';
INCLUDE helpers/api_req_res.tt request=request response=response level=level+3;
request_macro(http_request = 'PUT');
-%]
</p>
[% END -%]

Loading…
Cancel
Save