| 1 | # ABSTRACT: Client to a {JSON:API} service (http://jsonapi.org/) v1.0 | ||
| 2 | package PONAPI::Client; | ||
| 3 | |||
| 4 | our $VERSION = '0.002011'; | ||
| 5 | |||
| 6 | use Moose; | ||
| 7 | |||
| 8 | use JSON::MaybeXS qw( decode_json ); | ||
| 9 | |||
| 10 | use PONAPI::Client::Request::Create; | ||
| 11 | use PONAPI::Client::Request::CreateRelationships; | ||
| 12 | use PONAPI::Client::Request::Retrieve; | ||
| 13 | use PONAPI::Client::Request::RetrieveAll; | ||
| 14 | use PONAPI::Client::Request::RetrieveRelationships; | ||
| 15 | use PONAPI::Client::Request::RetrieveByRelationship; | ||
| 16 | use PONAPI::Client::Request::Update; | ||
| 17 | use PONAPI::Client::Request::UpdateRelationships; | ||
| 18 | use PONAPI::Client::Request::Delete; | ||
| 19 | use PONAPI::Client::Request::DeleteRelationships; | ||
| 20 | |||
| 21 | has host => ( | ||
| 22 | is => 'ro', | ||
| 23 | isa => 'Str', | ||
| 24 | default => sub { 'localhost' }, | ||
| 25 | ); | ||
| 26 | |||
| 27 | has port => ( | ||
| 28 | is => 'ro', | ||
| 29 | isa => 'Num', | ||
| 30 | default => sub { 5000 }, | ||
| 31 | ); | ||
| 32 | |||
| 33 | has send_version_header => ( | ||
| 34 | is => 'ro', | ||
| 35 | isa => 'Bool', | ||
| 36 | default => sub { 1 }, | ||
| 37 | ); | ||
| 38 | |||
| 39 | has send_escape_values_header => ( | ||
| 40 | is => 'ro', | ||
| 41 | isa => 'Bool', | ||
| 42 | default => sub { 1 }, | ||
| 43 | ); | ||
| 44 | |||
| 45 | has 'uri_base' => ( | ||
| 46 | is => 'ro', | ||
| 47 | isa => 'Maybe[Str]', | ||
| 48 | default => sub { '' }, | ||
| 49 | ); | ||
| 50 | |||
| 51 | has ua => ( | ||
| 52 | is => 'ro', | ||
| 53 | does => 'PONAPI::Client::Role::UA', | ||
| 54 | lazy => 1, | ||
| 55 | builder => '_build_hijk_ua', | ||
| 56 | ); | ||
| 57 | |||
| 58 | ### public methods | ||
| 59 | |||
| 60 | sub create { | ||
| 61 | my ($self) = @_; | ||
| 62 | my $request = PONAPI::Client::Request::Create->new( _args(@_) ); | ||
| 63 | return $self->_send_ponapi_request( $request->request_params ); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub create_relationships { | ||
| 67 | my ($self) = @_; | ||
| 68 | my $request = PONAPI::Client::Request::CreateRelationships->new( _args(@_) ); | ||
| 69 | return $self->_send_ponapi_request( $request->request_params ); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub retrieve_all { | ||
| 73 | my ($self) = @_; | ||
| 74 | my $request = PONAPI::Client::Request::RetrieveAll->new( _args(@_) ); | ||
| 75 | return $self->_send_ponapi_request( $request->request_params ); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub retrieve { | ||
| 79 | my ($self) = @_; | ||
| 80 | my $request = PONAPI::Client::Request::Retrieve->new( _args(@_) ); | ||
| 81 | return $self->_send_ponapi_request( $request->request_params ); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub retrieve_relationships { | ||
| 85 | my ($self) = @_; | ||
| 86 | my $request = PONAPI::Client::Request::RetrieveRelationships->new( _args(@_) ); | ||
| 87 | return $self->_send_ponapi_request( $request->request_params ); | ||
| 88 | } | ||
| 89 | |||
| 90 | sub retrieve_by_relationship { | ||
| 91 | my ($self) = @_; | ||
| 92 | my $request = PONAPI::Client::Request::RetrieveByRelationship->new( _args(@_) ); | ||
| 93 | return $self->_send_ponapi_request( $request->request_params ); | ||
| 94 | } | ||
| 95 | |||
| 96 | sub update { | ||
| 97 | my ($self) = @_; | ||
| 98 | my $request = PONAPI::Client::Request::Update->new( _args(@_) ); | ||
| 99 | return $self->_send_ponapi_request( $request->request_params ); | ||
| 100 | } | ||
| 101 | |||
| 102 | sub update_relationships { | ||
| 103 | my ($self) = @_; | ||
| 104 | my $request = PONAPI::Client::Request::UpdateRelationships->new( _args(@_) ); | ||
| 105 | return $self->_send_ponapi_request( $request->request_params ); | ||
| 106 | } | ||
| 107 | |||
| 108 | sub delete : method { | ||
| 109 | my ($self) = @_; | ||
| 110 | my $request = PONAPI::Client::Request::Delete->new( _args(@_) ); | ||
| 111 | return $self->_send_ponapi_request( $request->request_params ); | ||
| 112 | } | ||
| 113 | |||
| 114 | sub delete_relationships { | ||
| 115 | my ($self) = @_; | ||
| 116 | my $request = PONAPI::Client::Request::DeleteRelationships->new( _args(@_) ); | ||
| 117 | return $self->_send_ponapi_request( $request->request_params ); | ||
| 118 | } | ||
| 119 | |||
| 120 | ### private methods | ||
| 121 | |||
| 122 | sub _build_hijk_ua { | ||
| 123 | require PONAPI::Client::UA::Hijk; | ||
| 124 | return PONAPI::Client::UA::Hijk->new(); | ||
| 125 | } | ||
| 126 | |||
| 127 | sub _args { | ||
| 128 | my $self = shift; | ||
| 129 | my %args = @_ == 1 ? %{ $_[0] } : @_; | ||
| 130 | $args{uri_base} = $self->uri_base; | ||
| 131 | return %args; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub _http_request { | ||
| 135 | my ($self) = @_; | ||
| 136 | return $self->ua->send_http_request($_[1]); | ||
| 137 | } | ||
| 138 | |||
| 139 | my %DEFAULT_HEADER_OVERRIDE; | ||
| 140 | |||
| 141 | # $self->add_to_default_headers([ header1 => value1 ], [header2 => value2], ...) | ||
| 142 | |||
| 143 | sub add_to_default_headers { | ||
| 144 | my ($self, @headers) = @_; | ||
| 145 | |||
| 146 | $DEFAULT_HEADER_OVERRIDE{$_->[0]} = $_->[1] for @headers; | ||
| 147 | } | ||
| 148 | |||
| 149 | sub _send_ponapi_request { | ||
| 150 | my $self = shift; | ||
| 151 | my %args = @_; | ||
| 152 | |||
| 153 | my @mt_header = ( | ||
| 154 | ( $args{body} ? 'Content-Type' : 'Accept' ), | ||
| 155 | 'application/vnd.api+json' | ||
| 156 | ); | ||
| 157 | |||
| 158 | my ($status, $content, $failed, $e); | ||
| 159 | ($status, $content) = do { | ||
| 160 | local $@; | ||
| 161 | eval { | ||
| 162 | |||
| 163 | my $request = { | ||
| 164 | %args, | ||
| 165 | host => $self->host, | ||
| 166 | port => $self->port, | ||
| 167 | head => [ | ||
| 168 | @mt_header, | ||
| 169 | ( $self->send_version_header | ||
| 170 | ? ( 'X-PONAPI-Client-Version' => '1.0' ) | ||
| 171 | : () | ||
| 172 | ), | ||
| 173 | ( $self->send_escape_values_header | ||
| 174 | ? ( 'X-PONAPI-Escaped-Values' => '1' ) | ||
| 175 | : () | ||
| 176 | ), | ||
| 177 | %DEFAULT_HEADER_OVERRIDE, | ||
| 178 | ], | ||
| 179 | }; | ||
| 180 | |||
| 181 | $self->ua->before_request($request); | ||
| 182 | |||
| 183 | my $res = $self->_http_request($request); | ||
| 184 | $status = $res->{status}; | ||
| 185 | |||
| 186 | $self->ua->after_request($res); | ||
| 187 | |||
| 188 | $content = $res->{body} ? decode_json( $res->{body} ) : ''; | ||
| 189 | 1; | ||
| 190 | } | ||
| 191 | or do { | ||
| 192 | ($failed, $e) = (1, $@||'Unknown error'); | ||
| 193 | }; | ||
| 194 | |||
| 195 | if ( $failed ) { | ||
| 196 | $status ||= 400; | ||
| 197 | $content = { | ||
| 198 | errors => [ { detail => $e, status => $status } ], | ||
| 199 | }; | ||
| 200 | } | ||
| 201 | |||
| 202 | ($status, $content); | ||
| 203 | }; | ||
| 204 | |||
| 205 | return ($status, $content); | ||
| 206 | } | ||
| 207 | |||
| 208 | __PACKAGE__->meta->make_immutable; | ||
| 209 | no Moose; 1; | ||
| 210 | |||
| 211 | __END__ | ||
| 212 | |||
| 213 | =pod | ||
| 214 | |||
| 215 | =encoding UTF-8 | ||
| 216 | |||
| 217 | =head1 NAME | ||
| 218 | |||
| 219 | PONAPI::Client - Client to a {JSON:API} service (http://jsonapi.org/) v1.0 | ||
| 220 | |||
| 221 | =head1 VERSION | ||
| 222 | |||
| 223 | version 0.002011 | ||
| 224 | |||
| 225 | =head1 SYNOPSIS | ||
| 226 | |||
| 227 | use PONAPI::Client; | ||
| 228 | my $client = PONAPI::Client->new( | ||
| 229 | host => $host, | ||
| 230 | port => $port, | ||
| 231 | ); | ||
| 232 | |||
| 233 | $client->retrieve_all( type => $type ); | ||
| 234 | |||
| 235 | $client->retrieve( | ||
| 236 | type => $type, | ||
| 237 | id => $id, | ||
| 238 | ); | ||
| 239 | |||
| 240 | $client->retrieve_relationships( | ||
| 241 | type => $type, | ||
| 242 | id => $id, | ||
| 243 | rel_type => $rel_type, | ||
| 244 | ); | ||
| 245 | |||
| 246 | $client->retrieve_by_relationship( | ||
| 247 | type => $type, | ||
| 248 | id => $id, | ||
| 249 | rel_type => $rel_type, | ||
| 250 | ); | ||
| 251 | |||
| 252 | $client->create( | ||
| 253 | type => $type, | ||
| 254 | data => { | ||
| 255 | attributes => { ... }, | ||
| 256 | relationships => { ... }, | ||
| 257 | }, | ||
| 258 | ); | ||
| 259 | |||
| 260 | $client->delete( | ||
| 261 | type => $type, | ||
| 262 | id => $id, | ||
| 263 | ); | ||
| 264 | |||
| 265 | $client->update( | ||
| 266 | type => $type, | ||
| 267 | id => $id, | ||
| 268 | data => { | ||
| 269 | type => $type, | ||
| 270 | id => $id, | ||
| 271 | attributes => { ... }, | ||
| 272 | relationships => { ... }, | ||
| 273 | } | ||
| 274 | ); | ||
| 275 | |||
| 276 | $client->delete_relationships( | ||
| 277 | type => $type, | ||
| 278 | id => $id, | ||
| 279 | rel_type => $rel_type, | ||
| 280 | data => [ | ||
| 281 | { type => $rel_type, id => $rel_id }, | ||
| 282 | ... | ||
| 283 | ], | ||
| 284 | ); | ||
| 285 | |||
| 286 | $client->create_relationships( | ||
| 287 | type => $type, | ||
| 288 | id => $id, | ||
| 289 | rel_type => $rel_type, | ||
| 290 | data => [ | ||
| 291 | { type => $rel_type, id => $rel_id }, | ||
| 292 | ... | ||
| 293 | ], | ||
| 294 | ); | ||
| 295 | |||
| 296 | $client->update_relationships( | ||
| 297 | type => $type, | ||
| 298 | id => $id, | ||
| 299 | rel_type => $rel_type, | ||
| 300 | # for a one-to-one: | ||
| 301 | data => { type => $rel_type, id => $rel_id }, | ||
| 302 | # or for a one-to-many: | ||
| 303 | data => [ | ||
| 304 | { type => $rel_type, id => $rel_id }, | ||
| 305 | ... | ||
| 306 | ], | ||
| 307 | ); | ||
| 308 | |||
| 309 | # If the endpoint uses an uncommon url format: | ||
| 310 | $client->retrieve( | ||
| 311 | type => 'foo', | ||
| 312 | id => 43, | ||
| 313 | # Will generate a request to | ||
| 314 | # host:port/type_foo_id_43 | ||
| 315 | uri_template => "type_{type}_id_{id}", | ||
| 316 | ); | ||
| 317 | |||
| 318 | =head1 DESCRIPTION | ||
| 319 | |||
| 320 | C<PONAPI::Client> is a L<{JSON:API}|http://jsonapi.org/> compliant client; | ||
| 321 | it should be able to communicate with any API-compliant service. | ||
| 322 | |||
| 323 | The client does a handful of checks required by the spec, then uses L<Hijk> | ||
| 324 | to communicate with the service. | ||
| 325 | |||
| 326 | In most cases, all API methods return a response document: | ||
| 327 | |||
| 328 | my $response = $client->retrieve(...); | ||
| 329 | |||
| 330 | In list context however, all api methods will return the request status and | ||
| 331 | the document: | ||
| 332 | |||
| 333 | my ($status, $response) = $client->retrieve(...) | ||
| 334 | |||
| 335 | Response documents will look something like these: | ||
| 336 | |||
| 337 | # Successful retrieve(type => 'articles', id => 2) | ||
| 338 | { | ||
| 339 | jsonapi => { version => "1.0" }, | ||
| 340 | links => { self => "/articles/2" }, | ||
| 341 | data => { ... }, | ||
| 342 | meta => { ... }, # May not be there | ||
| 343 | included => [ ... ], # May not be there, see C<include> | ||
| 344 | } | ||
| 345 | |||
| 346 | # Successful retrieve_all( type => 'articles' ) | ||
| 347 | { | ||
| 348 | jsonapi => { version => "1.0" }, | ||
| 349 | links => { self => "/articles" }, # May include pagination links | ||
| 350 | data => [ | ||
| 351 | { ... }, | ||
| 352 | { ... }, | ||
| 353 | ... | ||
| 354 | ], | ||
| 355 | meta => { ... }, # May not be there | ||
| 356 | included => [ ... ], # May not be there, see C<include> | ||
| 357 | } | ||
| 358 | |||
| 359 | # Successful create(type => 'foo', data => { ... }) | ||
| 360 | { | ||
| 361 | jsonapi => { version => "1.0" }, | ||
| 362 | links => { self => "/foo/$created_id" }, | ||
| 363 | data => { type => 'foo', id => $created_id }, | ||
| 364 | } | ||
| 365 | |||
| 366 | # Successful update(type => 'foo', id => 2, data => { ... }) | ||
| 367 | { | ||
| 368 | jsonapi => { version => "1.0" }, | ||
| 369 | links => { self => "/foo/2" }, # may not be there | ||
| 370 | meta => { ... }, # may not be there | ||
| 371 | } | ||
| 372 | |||
| 373 | # Error, see http://jsonapi.org/format/#error-objects | ||
| 374 | { | ||
| 375 | jsonapi => { version => "1.0" }, | ||
| 376 | errors => [ | ||
| 377 | { ... }, # error 1 | ||
| 378 | ... # potentially others | ||
| 379 | ], | ||
| 380 | } | ||
| 381 | |||
| 382 | However, there are situations where the server may respond with a | ||
| 383 | C<204 No Content> and no response document; depending on the situation, | ||
| 384 | it might be worth checking the status. | ||
| 385 | |||
| 386 | =for TODO Do we want to explain how to create your own subclass of the client? | ||
| 387 | |||
| 388 | =head1 METHODS | ||
| 389 | |||
| 390 | =head2 new | ||
| 391 | |||
| 392 | Creates a new C<PONAPI::Client> object. Takes a couple of attributes: | ||
| 393 | |||
| 394 | =over 4 | ||
| 395 | |||
| 396 | =item host | ||
| 397 | |||
| 398 | The hostname (or IP address) of the service. Defaults to localhost. | ||
| 399 | |||
| 400 | =item port | ||
| 401 | |||
| 402 | Port of the service. Defaults to 5000. | ||
| 403 | |||
| 404 | =item send_version_header | ||
| 405 | |||
| 406 | Sends a C<X-PONAPI-Client-Version> header set to the {JSON:API} version the | ||
| 407 | client supports. Defaults to true. | ||
| 408 | |||
| 409 | =back | ||
| 410 | |||
| 411 | =head2 retrieve_all | ||
| 412 | |||
| 413 | retrieve_all( type => $type, %optional_arguments ) | ||
| 414 | |||
| 415 | Retrieves B<all> resources of the given type. In SQL, this is similar to | ||
| 416 | C<SELECT * FROM $type>. | ||
| 417 | |||
| 418 | This handles several arguments: | ||
| 419 | |||
| 420 | =over 4 | ||
| 421 | |||
| 422 | =item fields | ||
| 423 | |||
| 424 | L<Spec|http://jsonapi.org/format/#fetching-sparse-fieldsets>. | ||
| 425 | |||
| 426 | Instead of returning every attribute and relationship from a given resource, | ||
| 427 | C<fields> can be used to specify exactly what is returned. | ||
| 428 | |||
| 429 | This excepts a hashref of arrayrefs, where the keys are types, and the values | ||
| 430 | are either attribute names, or relationship names. | ||
| 431 | |||
| 432 | $client->retrieve_all( | ||
| 433 | type => 'people', | ||
| 434 | fields => { people => [ 'name', 'age' ] } | ||
| 435 | ) | ||
| 436 | |||
| 437 | Note that an attribute not being in fields means the opposite to | ||
| 438 | an attribute having empty fields: | ||
| 439 | |||
| 440 | # No attributes or relationships for both people and comments | ||
| 441 | $client->retrieve_all( | ||
| 442 | type => 'people', | ||
| 443 | fields => { people => [], comments => [] }, | ||
| 444 | ); | ||
| 445 | |||
| 446 | # No attributes or relationships for comments, but | ||
| 447 | # ALL attributes and relationships for people | ||
| 448 | $client->retrieve_all( | ||
| 449 | type => 'people', | ||
| 450 | fields => { comments => [] }, | ||
| 451 | ); | ||
| 452 | |||
| 453 | =item include | ||
| 454 | |||
| 455 | L<Spec|http://jsonapi.org/format/#fetching-includes>. | ||
| 456 | |||
| 457 | C<include> can be used to fetch related resources. | ||
| 458 | The example below is fetching both all the people, and all comments made by | ||
| 459 | those people: | ||
| 460 | |||
| 461 | my $response = $client->retrieve_all( | ||
| 462 | type => 'people', | ||
| 463 | include => ['comments'] | ||
| 464 | ); | ||
| 465 | |||
| 466 | C<include> expects an arrayref of relationship names. In the response, | ||
| 467 | the resources fetched will be in an arrayref under the top-level C<included> | ||
| 468 | key: | ||
| 469 | |||
| 470 | say $_->{attributes}{body} for @{ $response->{included} } | ||
| 471 | |||
| 472 | =item page | ||
| 473 | |||
| 474 | L<Spec|http://jsonapi.org/format/#fetching-pagination>. | ||
| 475 | |||
| 476 | Requests that the server paginate the results. Each endpoint may have different | ||
| 477 | pagination rules. | ||
| 478 | |||
| 479 | =item sort | ||
| 480 | |||
| 481 | L<Spec|http://jsonapi.org/format/#fetching-sorting>. | ||
| 482 | |||
| 483 | Requests that the server sort the results in a given way: | ||
| 484 | |||
| 485 | $client->retrieve_all( | ||
| 486 | type => 'people', | ||
| 487 | sort => [qw/ age /], # sort by age, ascending | ||
| 488 | ); | ||
| 489 | |||
| 490 | $client->retrieve_all( | ||
| 491 | type => 'people', | ||
| 492 | sort => [qw/ -age /], # sort by age, descending | ||
| 493 | ); | ||
| 494 | |||
| 495 | Although not all endpoints will support this, it may be possible to sort by | ||
| 496 | a relationship's attribute: | ||
| 497 | |||
| 498 | $client->retrieve_all( | ||
| 499 | type => 'people', | ||
| 500 | sort => [qw/ -comments.created_date /], | ||
| 501 | ); | ||
| 502 | |||
| 503 | =item filter | ||
| 504 | |||
| 505 | L<Spec|http://jsonapi.org/format/#fetching-filtering>. | ||
| 506 | |||
| 507 | This one is entirely dependent on the endpoint. It's usually employed | ||
| 508 | to act as a C<WHERE> clause: | ||
| 509 | |||
| 510 | $client->retrieve_all( | ||
| 511 | type => 'people', | ||
| 512 | filter => { | ||
| 513 | id => [ 1, 2, 3, 4, 6 ], # IN ( 1, 2, ... ) | ||
| 514 | age => 34, # age = 34 | ||
| 515 | }, | ||
| 516 | ); | ||
| 517 | |||
| 518 | Sadly, more complex filters are currently not available. | ||
| 519 | |||
| 520 | =back | ||
| 521 | |||
| 522 | =head2 retrieve | ||
| 523 | |||
| 524 | retrieve( type => $type, id => $id, %optional_arguments ) | ||
| 525 | |||
| 526 | Similar to C<retrieve_all>, but retrieves a single resource. | ||
| 527 | |||
| 528 | =head2 retrieve_relationships | ||
| 529 | |||
| 530 | retrieve_relationships( type => $type, id => $id, rel_type => $rel_type, %optional_arguments ) | ||
| 531 | |||
| 532 | Retrieves all of C<$id>'s relationships to C<$rel_type> as resource identifiers; | ||
| 533 | that is, as hashrefs that contain only C<type> and C<id>: | ||
| 534 | |||
| 535 | # retrieve_relationships(type=>'people', id=>2, rel_type=>'comments') | ||
| 536 | { | ||
| 537 | jsonapi => { version => "1.0" }, | ||
| 538 | data => [ | ||
| 539 | { type => 'comments', id => 4 }, | ||
| 540 | { type => 'comments', id => 9 }, | ||
| 541 | { type => 'comments', id => 14 }, | ||
| 542 | ] | ||
| 543 | } | ||
| 544 | |||
| 545 | These two do roughly the same thing: | ||
| 546 | |||
| 547 | my $response = $client->retrieve( type => $type, id => $id ); | ||
| 548 | my $relationships = $response->{data}{relationships}{$rel_type}; | ||
| 549 | say join ", ", map $_->{id}, @$relationships; | ||
| 550 | |||
| 551 | my $response = $client->retrieve_relationships( | ||
| 552 | type => $type, | ||
| 553 | id => $id, | ||
| 554 | rel_type => $rel_type, | ||
| 555 | ); | ||
| 556 | my $relationships = $response->{data}; | ||
| 557 | say join ", ", map $_->{id}, @$relationships; | ||
| 558 | |||
| 559 | However, C<retrieve_relationships> also allows you to page those | ||
| 560 | relationships, which may be quite useful. | ||
| 561 | |||
| 562 | Keep in mind that C<retrieve_relationships> will return an arrayref for | ||
| 563 | one-to-many relationships, and a hashref for one-to-ones. | ||
| 564 | |||
| 565 | =head2 retrieve_by_relationship | ||
| 566 | |||
| 567 | retrieve_by_relationship( type => $type, id => $id, rel_type => $rel_type, %optional_arguments ) | ||
| 568 | |||
| 569 | C<retrieve_relationships> on steroids. It behaves the same way, but will | ||
| 570 | retrieve full resources, not just resource identifiers; because of this, | ||
| 571 | you can also potentially apply more complex filters and sorts. | ||
| 572 | |||
| 573 | =head2 create | ||
| 574 | |||
| 575 | create( type => $type, data => { ... }, id => $optional ) | ||
| 576 | |||
| 577 | Create a resource of type C<$type> using C<$data> to populate it. | ||
| 578 | Data B<must> include the type, and may include two other keys: C<attributes> | ||
| 579 | and C<relationships>: | ||
| 580 | |||
| 581 | $client->create( | ||
| 582 | type => 'comments', | ||
| 583 | data => { | ||
| 584 | type => 'comments', | ||
| 585 | attributes => { body => 'abc' }, | ||
| 586 | relationships => { | ||
| 587 | author => { type => 'people', id => 55 }, | ||
| 588 | liked_by => [ | ||
| 589 | { type => 'people', id => 55 }, | ||
| 590 | { type => 'people', id => 577 }, | ||
| 591 | ], | ||
| 592 | } | ||
| 593 | } | ||
| 594 | } | ||
| 595 | |||
| 596 | An optional C<id> may be provided, in which case the server may choose to use | ||
| 597 | it when creating the new resource. | ||
| 598 | |||
| 599 | =head2 update | ||
| 600 | |||
| 601 | update( type => $type, id => $id, data => { ... } ) | ||
| 602 | |||
| 603 | Can be used to update the resource. Data B<must> have C<type> and C<id> keys: | ||
| 604 | |||
| 605 | $client->create( | ||
| 606 | type => 'comments', | ||
| 607 | id => 5, | ||
| 608 | data => { | ||
| 609 | type => 'comments', | ||
| 610 | id => 5, | ||
| 611 | attributes => { body => 'new body!' }, | ||
| 612 | relationships => { | ||
| 613 | author => undef, # no author | ||
| 614 | liked_by => [ | ||
| 615 | { type => 'people', id => 79 }, | ||
| 616 | ], | ||
| 617 | } | ||
| 618 | } | ||
| 619 | } | ||
| 620 | |||
| 621 | An empty arrayref (C<[]>) can be used to clear one-to-many relationships, and | ||
| 622 | C<undef> to clear one-to-one relationships. | ||
| 623 | |||
| 624 | A successful C<update> will always return a response document; see the spec | ||
| 625 | for more details. | ||
| 626 | |||
| 627 | L<Spec|http://jsonapi.org/format/#crud-updating>. | ||
| 628 | |||
| 629 | =head2 delete | ||
| 630 | |||
| 631 | delete( type => $type, id => $id ) | ||
| 632 | |||
| 633 | Deletes the resource. | ||
| 634 | |||
| 635 | =head2 update_relationships | ||
| 636 | |||
| 637 | update_relationships( type => $type, id => $id, rel_type => $rel_type, data => $data ) | ||
| 638 | |||
| 639 | Update a resource's relationships. Basically a shortcut to using C<update>. | ||
| 640 | |||
| 641 | For one-to-one relationships, C<data> can be either a single hashref, or undef. | ||
| 642 | For one-to-many relationships, C<data> can be an arrayref; an empty arrayref | ||
| 643 | means 'clear the relationship'. | ||
| 644 | |||
| 645 | =head2 create_relationships | ||
| 646 | |||
| 647 | create_relationships( type => $type, id => $id, rel_type => $rel_type, data => [{ ... }] ) | ||
| 648 | |||
| 649 | Adds to the specified one-to-many relationship. | ||
| 650 | |||
| 651 | =head2 delete_relationships | ||
| 652 | |||
| 653 | delete_relationships( type => $type, id => $id, rel_type => $rel_type, data => [{ ... }] ) | ||
| 654 | |||
| 655 | Deletes from the specified one-to-many relationship. | ||
| 656 | |||
| 657 | =head1 Endpoint URI format | ||
| 658 | |||
| 659 | By default, C<PONAPI::Client> assumes urls on the endpoint are in this format: | ||
| 660 | |||
| 661 | retrieve_all: /$type | ||
| 662 | retrieve: /$type/$id | ||
| 663 | retrieve_by_relationships: /$type/$id/$rel_type | ||
| 664 | retrieve_relationships: /$type/$id/relationships/$rel_type | ||
| 665 | |||
| 666 | create: /$type or /$type/$id | ||
| 667 | delete: /$type/$id | ||
| 668 | update: /$type/$id | ||
| 669 | |||
| 670 | update_relationships: /$type/$id/relationships/$rel_type | ||
| 671 | create_relationships: /$type/$id/relationships/$rel_type | ||
| 672 | delete_relationships: /$type/$id/relationships/$rel_type | ||
| 673 | |||
| 674 | # Will generate a request to /foo/99 | ||
| 675 | $client->retrieve( | ||
| 676 | type => 'foo', | ||
| 677 | id => 99, | ||
| 678 | ); | ||
| 679 | |||
| 680 | However, if another format is needed, two approaches are possible: | ||
| 681 | |||
| 682 | =head2 URI paths have a common prefix | ||
| 683 | |||
| 684 | If all the endpoint urls have a common prefix, ala C</v1/articles> | ||
| 685 | instead of simply C</articles>, then you can just set C<uri_base> | ||
| 686 | as needed: | ||
| 687 | |||
| 688 | $client->retrieve( | ||
| 689 | type => 'foo', | ||
| 690 | id => 99, | ||
| 691 | uri_base => '/v1' | ||
| 692 | ); | ||
| 693 | |||
| 694 | We can also set this when creating the client; if done this way, | ||
| 695 | all requests generated from this client will include the base: | ||
| 696 | |||
| 697 | my $new_client = PONAPI::Client->new( | ||
| 698 | uri_base => '/v1', | ||
| 699 | ... | ||
| 700 | ); | ||
| 701 | |||
| 702 | # This will generate a request to /v1/foo/99 | ||
| 703 | $new_client->retrieve( | ||
| 704 | type => 'foo', | ||
| 705 | id => 99, | ||
| 706 | ); | ||
| 707 | |||
| 708 | =head2 Completely different uris | ||
| 709 | |||
| 710 | If the endpoint's expected formats are wildly different, you can specify | ||
| 711 | C<uri_template> with your request: | ||
| 712 | |||
| 713 | # Will generate a request to id_here_99_and_type_there/foo | ||
| 714 | $client->retrieve( | ||
| 715 | type => 'foo', | ||
| 716 | id => 99, | ||
| 717 | uri_template => 'id_here_{id}_and_type_there/{type}' | ||
| 718 | ); | ||
| 719 | |||
| 720 | These placeholders are recognized: | ||
| 721 | |||
| 722 | =over 4 | ||
| 723 | |||
| 724 | =item * type | ||
| 725 | |||
| 726 | =item * id | ||
| 727 | |||
| 728 | =item * rel_type | ||
| 729 | |||
| 730 | =back | ||
| 731 | |||
| 732 | This can only be done on a per-request basis. | ||
| 733 | |||
| 734 | =head1 AUTHORS | ||
| 735 | |||
| 736 | =over 4 | ||
| 737 | |||
| 738 | =item * | ||
| 739 | |||
| 740 | Mickey Nasriachi <mickey@cpan.org> | ||
| 741 | |||
| 742 | =item * | ||
| 743 | |||
| 744 | Stevan Little <stevan@cpan.org> | ||
| 745 | |||
| 746 | =item * | ||
| 747 | |||
| 748 | Brian Fraser <hugmeir@cpan.org> | ||
| 749 | |||
| 750 | =back | ||
| 751 | |||
| 752 | =head1 COPYRIGHT AND LICENSE | ||
| 753 | |||
| 754 | This software is copyright (c) 2019 by Mickey Nasriachi, Stevan Little, Brian Fraser. | ||
| 755 | |||
| 756 | This is free software; you can redistribute it and/or modify it under | ||
| 757 | the same terms as the Perl 5 programming language system itself. | ||
| 758 | |||
| 759 | =cut |