| 1 | package Strehler::API; | ||||||||||||||||||||||||||||
| 2 | 5 | $Strehler::API::VERSION = '1.6.8'; | |||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use Dancer2 0.166001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use Dancer2::Serializer::JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Strehler::Helpers; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Strehler::Meta::Category; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 5 | prefix '/api/v1'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 5 | set layout => undef; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 5 | my $root_path = __FILE__; | |||||||||||||||||||||||||||
| 14 | 5 | $root_path =~ s/API\.pm//; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 5 | set views => $root_path . 'views'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | get '/reference' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 19 | 0 | my @entities = Strehler::Helpers::entities_list(); | |||||||||||||||||||||||||||
| 20 | 0 | my @single_calls; | |||||||||||||||||||||||||||
| 21 | my @plural_calls; | ||||||||||||||||||||||||||||
| 22 | 0 | foreach my $e (@entities) | |||||||||||||||||||||||||||
| 23 | { | ||||||||||||||||||||||||||||
| 24 | 0 | my $class = Strehler::Helpers::class_from_entity($e); | |||||||||||||||||||||||||||
| 25 | 0 | ▶ | next if ! $class->exposed(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 0 | push @single_calls, '/api/v1/' . $e . '/<id>'; | |||||||||||||||||||||||||||
| 27 | 0 | push @single_calls, '/api/v1/' . $e . '/slug/<slug>'; | |||||||||||||||||||||||||||
| 28 | 0 | push @plural_calls, '/api/v1/' . $class->plural() . '/'; | |||||||||||||||||||||||||||
| 29 | 0 | push @plural_calls, '/api/v1/' . $class->plural() . '/<category>/<subcategory>/'; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | template "api/reference", { page_title => "API reference", single_calls => \@single_calls, plural_calls => \@plural_calls }, { layout => 'light-admin' }; | |||||||||||||||||||||||||||
| 33 | 5 | }; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 2 | ▶ | get '/:entity/slug/:slug' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=26.4
| |||||||||||||||||||||||||||||
| 36 | 2 | my $entity = params->{entity}; | |||||||||||||||||||||||||||
| 37 | 2 | my $slug = params->{'slug'}; | |||||||||||||||||||||||||||
| 38 | 2 | ▶ | my $callback = params->{'callback'} || undef; | ||||||||||||||||||||||||||
|
Condition50%1/2
params()->{'callback'} || undef
MC/DC0%0/1
params()->{'callback'} || undef
params()->{'callback'}
Truth table50%1/2
A
A= params()->{'callback'}
| |||||||||||||||||||||||||||||
| 39 | 2 | ▶ | my $lang = params->{'lang'} || config->{'Strehler'}->{'default_language'}; | ||||||||||||||||||||||||||
|
Condition33%1/3
params()->{'lang'} || config()->{'Strehler'}{'default_language'}
MC/DC0%0/2
params()->{'lang'} || config()->{'Strehler'}{'default_language'}
params()->{'lang'}
config()->{'Strehler'}{'default_language'}
Truth table33%1/3
A || B
A= params()->{'lang'}B= config()->{'Strehler'}{'default_language'}
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 2 | my $class = Strehler::Helpers::class_from_entity($entity); | |||||||||||||||||||||||||||
| 42 | 2 | ▶ | return pass if ! $class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 2 | ▶ | return pass if ! $class->exposed(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 2 | ▶ | return pass if ! $class->slugged(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 2 | my $obj = $class->get_by_slug($slug, $lang); | |||||||||||||||||||||||||||
| 46 | 2 | ▶ | if(! $obj) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | { | ||||||||||||||||||||||||||||
| 48 | 0 | return error_handler(404, "Element doesn't exists"); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 2 | ▶ | if($obj->publishable() && ! $obj->get_attr('published')) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$obj->publishable and not $obj->get_attr("published")
MC/DC0%0/2
$obj->publishable and not $obj->get_attr("published")
$obj->publishable
not $obj->get_attr("published")
Truth table66%2/3
A and not(B)
A= $obj->publishableB= not $obj->get_attr("published")
| |||||||||||||||||||||||||||||
| 51 | { | ||||||||||||||||||||||||||||
| 52 | 0 | return error_handler(404, "Element doesn't exists"); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 2 | my %data = $obj->get_json_data($lang); | |||||||||||||||||||||||||||
| 55 | 2 | ▶ | if($callback) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | { | ||||||||||||||||||||||||||||
| 57 | 0 | content_type('application/javascript'); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | else | ||||||||||||||||||||||||||||
| 60 | { | ||||||||||||||||||||||||||||
| 61 | 2 | content_type('application/json'); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 2 | return serialize(\%data, $callback); | |||||||||||||||||||||||||||
| 64 | 5 | }; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 1 | ▶ | get '/:entity/schema' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 67 | 1 | my $entity = params->{entity}; | |||||||||||||||||||||||||||
| 68 | 1 | ▶ | my $callback = params->{'callback'} || undef; | ||||||||||||||||||||||||||
|
Condition50%1/2
params()->{'callback'} || undef
MC/DC0%0/1
params()->{'callback'} || undef
params()->{'callback'}
Truth table50%1/2
A
A= params()->{'callback'}
| |||||||||||||||||||||||||||||
| 69 | 1 | my $class = Strehler::Helpers::class_from_entity($entity); | |||||||||||||||||||||||||||
| 70 | 1 | ▶ | return pass if ! $class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 1 | my @basic = $class->data_fields(); | |||||||||||||||||||||||||||
| 72 | 1 | @basic = grep { $_ ne 'published' } @basic; | |||||||||||||||||||||||||||
| 73 | 1 | my @multilanguage = $class->multilang_data_fields(); | |||||||||||||||||||||||||||
| 74 | 1 | my %data = ( 'basic' => \@basic, | |||||||||||||||||||||||||||
| 75 | 'multilanguage' => \@multilanguage ); | ||||||||||||||||||||||||||||
| 76 | 1 | ▶ | if($callback) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | { | ||||||||||||||||||||||||||||
| 78 | 0 | content_type('application/javascript'); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | else | ||||||||||||||||||||||||||||
| 81 | { | ||||||||||||||||||||||||||||
| 82 | 1 | content_type('application/json'); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 1 | return serialize(\%data, $callback); | |||||||||||||||||||||||||||
| 85 | 5 | }; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 5 | ▶ | get '/:entity/:id' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.1
| |||||||||||||||||||||||||||||
| 88 | 5 | my $entity = params->{entity}; | |||||||||||||||||||||||||||
| 89 | 5 | my $id = params->{'id'}; | |||||||||||||||||||||||||||
| 90 | 5 | ▶ | my $callback = params->{'callback'} || undef; | ||||||||||||||||||||||||||
|
Condition100%2/2
params()->{'callback'} || undef
MC/DC100%1/1
params()->{'callback'} || undef
params()->{'callback'}
Truth table100%2/2
A
A= params()->{'callback'}
| |||||||||||||||||||||||||||||
| 91 | 5 | ▶ | my $lang = params->{'lang'} || config->{'Strehler'}->{'default_language'}; | ||||||||||||||||||||||||||
|
Condition33%1/3
params()->{'lang'} || config()->{'Strehler'}{'default_language'}
MC/DC0%0/2
params()->{'lang'} || config()->{'Strehler'}{'default_language'}
params()->{'lang'}
config()->{'Strehler'}{'default_language'}
Truth table33%1/3
A || B
A= params()->{'lang'}B= config()->{'Strehler'}{'default_language'}
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 5 | my $class = Strehler::Helpers::class_from_entity($entity); | |||||||||||||||||||||||||||
| 94 | 5 | ▶ | return pass if ! $class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 5 | ▶ | return pass if ! $class->exposed(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 5 | my $obj = $class->new($id); | |||||||||||||||||||||||||||
| 97 | 5 | ▶ | if(! $obj->exists()) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | { | ||||||||||||||||||||||||||||
| 99 | 1 | return error_handler(404, "Element doesn't exists"); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 4 | ▶ | if($obj->publishable() && ! $obj->get_attr('published')) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$obj->publishable and not $obj->get_attr("published")
MC/DC100%2/2
$obj->publishable and not $obj->get_attr("published")
$obj->publishable
not $obj->get_attr("published")
Truth table100%3/3
A and not(B)
A= $obj->publishableB= not $obj->get_attr("published")
| |||||||||||||||||||||||||||||
| 102 | { | ||||||||||||||||||||||||||||
| 103 | 1 | return error_handler(404, "Element doesn't exists"); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 3 | my %data = $obj->get_json_data($lang); | |||||||||||||||||||||||||||
| 106 | 3 | ▶ | if($callback) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | { | ||||||||||||||||||||||||||||
| 108 | 1 | content_type('application/javascript'); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | else | ||||||||||||||||||||||||||||
| 111 | { | ||||||||||||||||||||||||||||
| 112 | 2 | content_type('application/json'); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | 3 | return serialize(\%data, $callback); | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 5 | }; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 6 | ▶ | get '/**/' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=23.4
| |||||||||||||||||||||||||||||
| 119 | 6 | my ($attributes) = splat; | |||||||||||||||||||||||||||
| 120 | 6 | my $entities = shift @{$attributes}; | |||||||||||||||||||||||||||
| 121 | 6 | my $category; | |||||||||||||||||||||||||||
| 122 | 6 | ▶ | if(@{$attributes}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | { | ||||||||||||||||||||||||||||
| 124 | 1 | $category = join('/', @{$attributes}); | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | else | ||||||||||||||||||||||||||||
| 127 | { | ||||||||||||||||||||||||||||
| 128 | 5 | $category = undef; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 6 | ▶ | my $callback = params->{'callback'} || undef; | ||||||||||||||||||||||||||
|
Condition50%1/2
params()->{'callback'} || undef
MC/DC0%0/1
params()->{'callback'} || undef
params()->{'callback'}
Truth table50%1/2
A
A= params()->{'callback'}
| |||||||||||||||||||||||||||||
| 131 | 6 | ▶ | my $lang = params->{'lang'} || config->{'Strehler'}->{'default_language'}; | ||||||||||||||||||||||||||
|
Condition66%2/3
params()->{'lang'} || config()->{'Strehler'}{'default_language'}
MC/DC0%0/2
params()->{'lang'} || config()->{'Strehler'}{'default_language'}
params()->{'lang'}
config()->{'Strehler'}{'default_language'}
Truth table66%2/3
A || B
A= params()->{'lang'}B= config()->{'Strehler'}{'default_language'}
| |||||||||||||||||||||||||||||
| 132 | 6 | my $order = params->{'order'}; | |||||||||||||||||||||||||||
| 133 | 6 | my $order_by = params->{'order_by'}; | |||||||||||||||||||||||||||
| 134 | 6 | my $page = params->{'page'}; | |||||||||||||||||||||||||||
| 135 | 6 | ▶ | my $entries_per_page = params->{'entries_per_page'} || 20; | ||||||||||||||||||||||||||
|
Condition100%2/2
params()->{'entries_per_page'} || 20
MC/DC100%1/1
params()->{'entries_per_page'} || 20
params()->{'entries_per_page'}
Truth table100%2/2
A
A= params()->{'entries_per_page'}
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 6 | my $class = Strehler::Helpers::class_from_plural($entities); | |||||||||||||||||||||||||||
| 138 | 6 | ▶ | return pass if ! $class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 6 | ▶ | return pass if ! $class->exposed(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 6 | my $category_id = undef; | |||||||||||||||||||||||||||
| 142 | 6 | my $ancestor = undef; | |||||||||||||||||||||||||||
| 143 | 6 | ▶ | if($category) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | { | ||||||||||||||||||||||||||||
| 145 | 1 | my $category_obj = Strehler::Meta::Category->explode_name($category); | |||||||||||||||||||||||||||
| 146 | 1 | ▶ | if(! $category_obj->exists()) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | { | ||||||||||||||||||||||||||||
| 148 | 0 | return error_handler(404, "Category doesn't exists"); | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | 1 | $category_id = $category_obj->get_attr('id'); | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | 6 | my $parameters = { | |||||||||||||||||||||||||||
| 153 | order => $order, | ||||||||||||||||||||||||||||
| 154 | order_by => $order_by, | ||||||||||||||||||||||||||||
| 155 | language => $lang, | ||||||||||||||||||||||||||||
| 156 | entries_per_page => $entries_per_page, | ||||||||||||||||||||||||||||
| 157 | page => $page, | ||||||||||||||||||||||||||||
| 158 | json => 1, | ||||||||||||||||||||||||||||
| 159 | published => 1, | ||||||||||||||||||||||||||||
| 160 | category_id => undef, | ||||||||||||||||||||||||||||
| 161 | ancestor => $category_id | ||||||||||||||||||||||||||||
| 162 | }; | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 6 | my $data = $class->get_list($parameters); | |||||||||||||||||||||||||||
| 165 | 6 | ▶ | if($callback) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | { | ||||||||||||||||||||||||||||
| 167 | 0 | content_type('application/javascript'); | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | else | ||||||||||||||||||||||||||||
| 170 | { | ||||||||||||||||||||||||||||
| 171 | 6 | content_type('application/json'); | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 6 | return serialize($data, $callback); | |||||||||||||||||||||||||||
| 174 | 5 | }; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | sub error_handler | ||||||||||||||||||||||||||||
| 177 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
error_handler: called CC=5 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 178 | 2 | ▶ | my $code = shift || 500; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 500
MC/DC0%0/1
shift() || 500
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 179 | 2 | ▶ | my $message = shift || "Generic error"; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 'Generic error'
MC/DC0%0/1
shift() || 'Generic error'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 180 | 2 | ▶ | my $callback = shift || undef; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || undef
MC/DC0%0/1
shift() || undef
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 181 | 2 | my $error_content = { error => $code, message => $message }; | |||||||||||||||||||||||||||
| 182 | 2 | my $serializer = Dancer2::Serializer::JSON->new(); | |||||||||||||||||||||||||||
| 183 | 2 | my $serialized = $serializer->serialize($error_content); | |||||||||||||||||||||||||||
| 184 | 2 | ▶ | if($callback) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | { | ||||||||||||||||||||||||||||
| 186 | 0 | content_type('application/javascript'); | |||||||||||||||||||||||||||
| 187 | 0 | $serialized = $callback . '( '. $serialized . ')'; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | else | ||||||||||||||||||||||||||||
| 190 | { | ||||||||||||||||||||||||||||
| 191 | 2 | content_type('application/json'); | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | 2 | send_error($message, $code); | |||||||||||||||||||||||||||
| 194 | 0 | return $serialized; | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | sub serialize | ||||||||||||||||||||||||||||
| 198 | 12 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
serialize: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 199 | 12 | my $content = shift; | |||||||||||||||||||||||||||
| 200 | 12 | my $callback = shift;; | |||||||||||||||||||||||||||
| 201 | 12 | my $serializer = Dancer2::Serializer::JSON->new(); | |||||||||||||||||||||||||||
| 202 | 12 | my $serialized = $serializer->serialize($content); | |||||||||||||||||||||||||||
| 203 | 12 | ▶ | if($callback) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | { | ||||||||||||||||||||||||||||
| 205 | 1 | $serialized = $callback . '( '. $serialized . ')'; | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | 12 | return $serialized; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =encoding utf8 | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =head1 NAME | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | Strehler::API - App that gives a RESTful interface to Strehler data | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | Strehler::API is an out-of-the-box API system designed to give back contents created with Strehler backend in a JSON (or JSONp) shape. | ||||||||||||||||||||||||||||
| 219 | It's main purpose is to make Strehler a complete server for client-side applications designed using advanced javascript, as L<Angular.js|https://angularjs.org/>. | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | Articles and Images are exposed through API by default, all the other custom entities can be exposed as well. Exposed flag was created for that purpose. | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | All the API calls are under B</api/v1/> | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =head1 API REFERENCE | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | Strehler::API are read-only API so there're just two calls you can do, both GET, to obtain data. | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | =over 4 | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =item /api/v1/$entity/$entity_id | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | This API just return all the data related to entity $entity with id $entity_id. | ||||||||||||||||||||||||||||
| 234 | Data is always the extended data from get_ext_data in L<Strehler::Element>. You can pass to the call B<lang> as parameter to obtain data in a certain language. If no lang parameter is provided, data is returned using the default language. | ||||||||||||||||||||||||||||
| 235 | If the entity is publishable, only data from published articles is returned. Calling for an unpublished article return 404. | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | B<Example>: /api/v1/article/15 | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | =item /api/v1/$entity/slug/$slug | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | As the previous API, using article slug instead of ID. It works only with slugged entities, entities that has L<Strehler::Element::Role:Slugged> as L<Strehler::Element::Article>. | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | =item /api/v1/$plural_entity/$category/$subcategory/ | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | This API returns in a JSON format a call to get_list sub from L<Strehler::Element>. So data structure is: | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | { | ||||||||||||||||||||||||||||
| 248 | page => 1, #page retrieved | ||||||||||||||||||||||||||||
| 249 | last_page => 3, #maximum callable page number | ||||||||||||||||||||||||||||
| 250 | to_view => $objects #list of objects, all returned with their extended data. | ||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | $plural_entity is the plural name of the entity. You can let Strehler derives it by itself from entity name using L<Lingua::EN::Inflect> or you can configure it in the config.yml or in the class as all other attributes. | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | $category and $subcategory are optional. If you call the API using them you'll retrieve all the elements under $category/$subcategory category. Pay attention that if you just call /api/v1/$plural_entity/$category/ system will return the objects under $category and all the objects under $category's subcategories. | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | API output is controlled by many parameters (a subset of get_list available parameters): | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | lang: output language, as for single item API | ||||||||||||||||||||||||||||
| 261 | order, order_by: to change the way elements are ordered | ||||||||||||||||||||||||||||
| 262 | page: to control pagination | ||||||||||||||||||||||||||||
| 263 | entries_per_page: to say how many elements display every page | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | B<Example>: /api/v1/articles/foo/bar/ | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | =item /api/v1/reference | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | Returns a web page where all the available APIs are listed. Automatically generated. | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | =back | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | =head2 JSONP CALLBACK | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | Adding B<callback> parameter to any API, return format will be JSONp instead of JSON. | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | =cut | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | 5 | 1; | |||||||||||||||||||||||||||