| 1 | package WebAPI::DBIC::Resource::Role::SetInvoke; | ||||||||||||||||||||||||||||||||
| 2 | 19 | $WebAPI::DBIC::Resource::Role::SetInvoke::VERSION = '0.004002'; | |||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | 19 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 19 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | 19 | requires 'decode_json'; | |||||||||||||||||||||||||||||||
| 10 | 19 | requires 'encode_json'; | |||||||||||||||||||||||||||||||
| 11 | 19 | requires 'render_item_as_plain_hash'; | |||||||||||||||||||||||||||||||
| 12 | 19 | requires 'throwable'; | |||||||||||||||||||||||||||||||
| 13 | 19 | requires 'set'; | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 19 | has method => ( | |||||||||||||||||||||||||||||||
| 16 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 17 | required => 1, | ||||||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 5 | ▶ | sub post_is_create { return 0 } | ||||||||||||||||||||||||||||||
|
Subroutine
post_is_create: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | 5 | ▶ | around 'allowed_methods' => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 23 | 5 | return [ qw(POST) ]; | |||||||||||||||||||||||||||||||
| 24 | 19 | }; | |||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 5 | ▶ | sub process_post { | ||||||||||||||||||||||||||||||
|
Subroutine
process_post: called CC=11 SCAR=25.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 28 | 5 | my $self = shift; | |||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | # Here's we're calling a method on the set as a simple generic behaviour. | ||||||||||||||||||||||||||||||||
| 31 | # This is very limited because, for example, the method has no knowledge | ||||||||||||||||||||||||||||||||
| 32 | # that it's being called inside a web service, thus no way to do redirects | ||||||||||||||||||||||||||||||||
| 33 | # or provide HTTP specific rich-exceptions. | ||||||||||||||||||||||||||||||||
| 34 | # If anything more sophisticated is required then it should be implemented | ||||||||||||||||||||||||||||||||
| 35 | # as a specific resource class for the method (or perhaps a role if there's | ||||||||||||||||||||||||||||||||
| 36 | # a set of methods that require similar behaviour). | ||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | # The POST body content provides a data structure containing the method arguments | ||||||||||||||||||||||||||||||||
| 39 | # { args => [ (@_) ] } | ||||||||||||||||||||||||||||||||
| 40 | 5 | ▶ | $self->throwable->throw_bad_request(415, errors => "Request content-type not application/json") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 41 | unless $self->request->header('Content-Type') =~ 'application/.*?json'; | ||||||||||||||||||||||||||||||||
| 42 | 5 | my $invoke_body_data = $self->decode_json($self->request->content); | |||||||||||||||||||||||||||||||
| 43 | 5 | ▶ | $self->throwable->throw_bad_request(400, errors => "Request content not a JSON hash") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 44 | unless ref $invoke_body_data eq 'HASH'; | ||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | 4 | my @method_args; | |||||||||||||||||||||||||||||||
| 47 | 4 | ▶ | if (my $args = delete $invoke_body_data->{args}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 48 | 2 | ▶ | $self->throwable->throw_bad_request(400, errors => "The args must be an array") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 49 | if ref $args ne 'ARRAY'; | ||||||||||||||||||||||||||||||||
| 50 | 1 | @method_args = @$args; | |||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||
| 52 | 3 | ▶ | $self->throwable->throw_bad_request(400, errors => "Unknown attributes: @{[ keys %$invoke_body_data ]}") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 53 | if keys %$invoke_body_data; | ||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | 2 | my $method_name = $self->method; | |||||||||||||||||||||||||||||||
| 56 | # the method is expected to throw an exception on error. | ||||||||||||||||||||||||||||||||
| 57 | 2 | my $result_raw = $self->set->$method_name(@method_args); | |||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | 2 | my $result_rendered; | |||||||||||||||||||||||||||||||
| 60 | # return a DBIC resultset as array of hashes of ALL records (no paging) | ||||||||||||||||||||||||||||||||
| 61 | 2 | ▶ | if (blessed($result_raw) && $result_raw->isa('DBIx::Class::ResultSet')) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
builtin::blessed($result_raw) and $result_raw->isa("DBIx::Class::ResultSet")
MC/DC0%0/2
builtin::blessed($result_raw) and $result_raw->isa("DBIx::Class::ResultSet")
builtin::blessed($result_raw)
$result_raw->isa("DBIx::Class::ResultSet")
Truth table33%1/3
A and B
A= builtin::blessed($result_raw)B= $result_raw->isa("DBIx::Class::ResultSet")
| |||||||||||||||||||||||||||||||||
| 62 | 0 | $result_rendered = [ map { $self->render_item_as_plain_hash($_) } $result_raw->all ]; | |||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||
| 64 | # return a DBIC result row as a hash | ||||||||||||||||||||||||||||||||
| 65 | ▶ | elsif (blessed($result_raw) && $result_raw->isa('DBIx::Class::Row')) { | |||||||||||||||||||||||||||||||
|
Condition33%1/3
builtin::blessed($result_raw) and $result_raw->isa("DBIx::Class::Row")
MC/DC0%0/2
builtin::blessed($result_raw) and $result_raw->isa("DBIx::Class::Row")
builtin::blessed($result_raw)
$result_raw->isa("DBIx::Class::Row")
Truth table33%1/3
A and B
A= builtin::blessed($result_raw)B= $result_raw->isa("DBIx::Class::Row")
| |||||||||||||||||||||||||||||||||
| 66 | 0 | $result_rendered = $self->render_item_as_plain_hash($result_raw); | |||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||
| 68 | # return anything else as raw JSON wrapped in a hash | ||||||||||||||||||||||||||||||||
| 69 | else { | ||||||||||||||||||||||||||||||||
| 70 | # we shouldn't get an object here, but if we do then we | ||||||||||||||||||||||||||||||||
| 71 | # stringify it here to avoid exposing the guts | ||||||||||||||||||||||||||||||||
| 72 | 2 | ▶ | $result_rendered = { result => (blessed $result_raw) ? "$result_raw" : $result_raw }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 2 | $self->response->body( $self->encode_json($result_rendered) ); | |||||||||||||||||||||||||||||||
| 76 | 2 | return 200; | |||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | 19 | 1; | |||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | __END__ | ||||||||||||||||||||||||||||||||