diff --git a/lib/NGCP/Panel/Controller/API/CFMappings.pm b/lib/NGCP/Panel/Controller/API/CFMappings.pm
index ee7985401c..9fd853c96a 100644
--- a/lib/NGCP/Panel/Controller/API/CFMappings.pm
+++ b/lib/NGCP/Panel/Controller/API/CFMappings.pm
@@ -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';
diff --git a/lib/NGCP/Panel/Controller/API/CallForwards.pm b/lib/NGCP/Panel/Controller/API/CallForwards.pm
index c9cebdcbe2..d9d7fa7539 100644
--- a/lib/NGCP/Panel/Controller/API/CallForwards.pm
+++ b/lib/NGCP/Panel/Controller/API/CallForwards.pm
@@ -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');
diff --git a/lib/NGCP/Panel/Controller/API/Root.pm b/lib/NGCP/Panel/Controller/API/Root.pm
index a51a555805..c3e2de1983 100644
--- a/lib/NGCP/Panel/Controller/API/Root.pm
+++ b/lib/NGCP/Panel/Controller/API/Root.pm
@@ -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,
};
}
diff --git a/share/templates/api/root.tt b/share/templates/api/root.tt
index 98cbbcd37d..0a7ddca3ed 100644
--- a/share/templates/api/root.tt
+++ b/share/templates/api/root.tt
@@ -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' });
-%]
diff --git a/share/templates/api/root/collection.tt b/share/templates/api/root/collection.tt
index 2cec7c7760..5b24d14889 100644
--- a/share/templates/api/root/collection.tt
+++ b/share/templates/api/root/collection.tt
@@ -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;
+%]
+
[%
- 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: ; rel="item self"
[%
- 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';
[% - 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';