MT#7841 API callforwards: improve documentation examples

gjungwirth/voicemail_number
Gerhard Jungwirth 12 years ago
parent b0235d97d1
commit 6dd1562252

@ -30,6 +30,19 @@ class_has 'query_params' => (
]},
);
class_has 'documentation_sample' => (
is => 'ro',
default => sub { {
cfb => [{
"destinationset" => "quickset_cfb",
"timeset" => undef,
}],
cfna => [],
cft => [],
cft_ringtimeout => "200",
cfu => [],
} },
);
with 'NGCP::Panel::Role::API::CFMappings';

@ -30,6 +30,22 @@ class_has 'query_params' => (
]},
);
class_has 'documentation_sample' => (
is => 'ro',
default => sub { {
cfb => { "destinations" => [{
"destination" => "voicebox",
"priority" => "1",
"timeout" => "300",
}],
"times" => [],
},
cfna => {},
cft => { "ringtimeout" => "199" },
cfu => {},
} },
);
with 'NGCP::Panel::Role::API::CallForwards';
class_has('resource_name', is => 'ro', default => 'callforwards');

@ -7,6 +7,7 @@ use HTTP::Response qw();
use HTTP::Status qw(:constants);
use MooseX::ClassAttribute qw(class_has);
use File::Find::Rule;
use JSON qw(to_json);
BEGIN { extends 'Catalyst::Controller'; }
require Catalyst::ActionRole::ACL;
require Catalyst::ActionRole::CheckTrailingSlash;
@ -82,7 +83,7 @@ sub GET : Allow {
$sorting_cols = [$item_rs->result_source->columns];
}
$c->stash->{collections}->{$rel} = {
$c->stash->{collections}->{$rel} = {
name => $mod,
description => $full_mod->api_description,
fields => $form ? $self->get_collection_properties($form) : [],
@ -90,6 +91,10 @@ sub GET : Allow {
actions => $actions,
item_actions => $item_actions,
sorting_cols => $sorting_cols,
uri => "/api/$rel/",
sample => $full_mod->can('documentation_sample') # generate pretty json, but without outer brackets (this is tricky though)
? to_json($full_mod->documentation_sample, {pretty => 1}) =~ s/(^\s*{\s*)|(\s*}\s*$)//rg =~ s/\n /\n/rg
: undef,
};
}

@ -55,7 +55,7 @@
{ level = 2, id = 'relations', title = 'Resources', },
];
FOREACH col IN collections.keys.sort;
chapters.push({ level = 3, id = col, title = collections.$col.name, uri = '/api/' _ col _ '/' });
chapters.push({ level = 3, id = col, title = collections.$col.name, uri = collections.$col.uri });
END;
chapters.push({ level = 2, id = 'definitions', title = 'Definitions' });
-%]

@ -12,6 +12,35 @@
[% col.description %]
[%-
#props_plain = '"id" : 1';
props_creation_first_flag = 1;
FOREACH p IN col.fields;
NEXT IF p.types.0 == 'null';
IF props_creation_first_flag;
props_creation_first_flag = 0;
ELSE;
props_plain = props_plain _ ",\n";
END;
IF p.types.0 == "Number";
val = 4;
ELSIF p.types.0 == "String";
val = '"test"';
ELSIF p.types.0 == "Boolean";
val = 'true';
ELSIF p.types.0 == "Array";
val = '[]';
ELSE;
val = '"missing"';
END;
props_plain = props_plain _ '"' _ p.name _ '" : ' _ val;
END;
props_plain = props_plain _ "\n";
IF col.sample;
props_plain = col.sample _ "\n";
END;
%]
<h[% level + 1 %]>Collection Actions</h[% level + 1%]>
Allowed methods for the collection as in <span>METHOD [% uri %]</span>:
[% UNLESS col.actions.size -%]
@ -66,7 +95,7 @@ See description how to obtain properties, if any.
<h[% level + 2 %]>Request available HTTP methods on the URI</h[% level + 2 %]>
<p>
[%
cmd = 'curl -i -X OPTIONS -H \'Connection: close\' --cert NGCP-API-client-certificate.pem --cacert ca-cert.pem https://example.org:1443/api/' _ id _ '/';
cmd = 'curl -i -X OPTIONS -H \'Connection: close\' --cert NGCP-API-client-certificate.pem --cacert ca-cert.pem https://example.org:1443' _ uri;
INCLUDE helpers/api_command.tt cmd=cmd level=level+3;
request =
@ -93,26 +122,7 @@ Accept-Post: application/hal+json; profile="http://purl.org/sipwise/ngcp-api/#re
cmd = 'curl -i -X GET -H \'Connection: close\' --cert NGCP-API-client-certificate.pem --cacert ca-cert.pem \'https://example.org:1443/api/' _ id _ '/?page=1&rows=1\'';
INCLUDE helpers/api_command.tt cmd=cmd level=level+3;
props = ' "id" : 1';
FOREACH p IN col.fields;
NEXT IF p.types.0 == 'null';
props = props _ ',
';
IF p.types.0 == "Number";
val = 4;
ELSIF p.types.0 == "String";
val = '"test"';
ELSIF p.types.0 == "Boolean";
val = 'true';
ELSIF p.types.0 == "Array";
val = '[]';
ELSE;
val = '"missing"';
END;
props = props _ ' "' _ p.name _ '" : ' _ val;
END;
props = props _ '
';
props = props_plain | indent(12);
request =
'GET /api/' _ id _ '/?page=1&rows=1 HTTP/1.1';
@ -180,26 +190,8 @@ Content-Type: application/hal+json; profile="http://purl.org/sipwise/ngcp-api/"
cmd = 'curl -i -X GET -H \'Connection: close\' --cert NGCP-API-client-certificate.pem --cacert ca-cert.pem \'https://example.org:1443/api/' _ id _ '/1\'';
INCLUDE helpers/api_command.tt cmd=cmd level=level+3;
props = ' "id" : 1';
FOREACH p IN col.fields;
NEXT IF p.types.0 == 'null';
props = props _ ',
';
IF p.types.0 == "Number";
val = 4;
ELSIF p.types.0 == "String";
val = '"test"';
ELSIF p.types.0 == "Boolean";
val = 'true';
ELSIF p.types.0 == "Array";
val = '[]';
ELSE;
val = '"missing"';
END;
props = props _ ' "' _ p.name _ '" : ' _ val;
END;
props = props _ '
';
props = props_plain | indent(3);
request =
'GET /api/' _ id _ '/1 HTTP/1.1';
response =
@ -237,54 +229,19 @@ Link: </api/' _ id _ '/1>; rel="item self"
<h[% level + 2 %]>Create a new <i>[% id %]</i> item</h[% level + 2 %]>
<p>
[%
props = '';
FOREACH p IN col.fields;
NEXT IF p.types.0 == 'null';
IF p.types.0 == "Number";
val = 4;
ELSIF p.types.0 == "String";
val = '"test"';
ELSIF p.types.0 == "Boolean";
val = 'true';
ELSIF p.types.0 == "Array";
val = '[]';
ELSE;
val = '"missing"';
END;
props = props _ '"' _ p.name _ '" : ' _ val;
props = props _ ', ';
END;
props = props.substr(0, props.length - 2);
props = props_plain | collapse;
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 = '';
FOREACH p IN col.fields;
NEXT IF p.types.0 == 'null';
IF p.types.0 == "Number";
val = 4;
ELSIF p.types.0 == "String";
val = '"test"';
ELSIF p.types.0 == "Boolean";
val = 'true';
ELSIF p.types.0 == "Array";
val = '[]';
ELSE;
val = '"missing"';
END;
props = props _ ' "' _ p.name _ '" : ' _ val;
props = props _ ',
';
END;
props = props.substr(0, props.length - 2);
props = props_plain | indent(3);
request =
'POST /api/' _ id _ '/ HTTP/1.1
Content-Type: application/json
{
' _ props _ '
}';
' _ props _ '}';
response =
'HTTP/1.1 201 Created
Location: /api/' _ id _ '/2';
@ -298,55 +255,19 @@ Location: /api/' _ id _ '/2';
<h[% level + 2 %]>Update an existing <i>[% id %]</i> item</h[% level + 2 %]>
<p>
[%
props = '';
FOREACH p IN col.fields;
NEXT IF p.types.0 == 'null';
IF p.types.0 == "Number";
val = 4;
ELSIF p.types.0 == "String";
val = '"test"';
ELSIF p.types.0 == "Boolean";
val = 'true';
ELSIF p.types.0 == "Array";
val = '[]';
ELSE;
val = '"missing"';
END;
props = props _ '"' _ p.name _ '" : ' _ val;
props = props _ ', ';
END;
props = props.substr(0, props.length - 2);
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 = '';
FOREACH p IN col.fields;
NEXT IF p.types.0 == 'null';
IF p.types.0 == "Number";
val = 4;
ELSIF p.types.0 == "String";
val = '"test"';
ELSIF p.types.0 == "Boolean";
val = 'true';
ELSIF p.types.0 == "Array";
val = '[]';
ELSE;
val = '"missing"';
END;
props = props _ ' "' _ p.name _ '" : ' _ val;
props = props _ ',
';
END;
props = props.substr(0, props.length - 2);
props = props_plain | indent(3);
request =
'PUT /api/' _ id _ '/2 HTTP/1.1
Content-Type: application/json
Prefer: return=minimal
{
' _ props _ '
}';
' _ props _ '}';
response =
'HTTP/1.1 204 No Content
Preference-Applied: return=minimal';

Loading…
Cancel
Save