| 1 | package WebAPI::DBIC::Resource::Role::DBIC; | ||||||||||
| 2 | 25 | $WebAPI::DBIC::Resource::Role::DBIC::VERSION = '0.004002'; | |||||||||
| 3 | |||||||||||
| 4 | 25 | ▶ | use Carp qw(croak confess); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 25 | ▶ | use Devel::Dwarn; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 25 | ▶ | use JSON::MaybeXS qw(JSON); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 25 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | |||||||||||
| 11 | 25 | requires 'uri_for'; | |||||||||
| 12 | 25 | requires 'throwable'; | |||||||||
| 13 | 25 | requires 'request'; | |||||||||
| 14 | 25 | requires 'response'; | |||||||||
| 15 | 25 | requires 'get_url_for_item_relationship'; | |||||||||
| 16 | 25 | requires 'id_kvs_for_item'; | |||||||||
| 17 | |||||||||||
| 18 | |||||||||||
| 19 | 25 | has set => ( | |||||||||
| 20 | is => 'rw', | ||||||||||
| 21 | required => 1, | ||||||||||
| 22 | ); | ||||||||||
| 23 | |||||||||||
| 24 | |||||||||||
| 25 | has prefetch => ( | ||||||||||
| 26 | is => 'rw', | ||||||||||
| 27 | 149 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 25 | ); | |||||||||
| 29 | |||||||||||
| 30 | |||||||||||
| 31 | # XXX perhaps shouldn't be a role, just functions, or perhaps a separate rendering object | ||||||||||
| 32 | # default render for DBIx::Class item | ||||||||||
| 33 | # https://metacpan.org/module/DBIx::Class::Manual::ResultClass | ||||||||||
| 34 | # https://metacpan.org/module/DBIx::Class::InflateColumn | ||||||||||
| 35 | 355 | ▶ | sub render_item_as_plain_hash { | ||||||||
|
Subroutine
render_item_as_plain_hash: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 36 | 355 | my ($self, $item) = @_; | |||||||||
| 37 | 355 | my $data = { $item->get_columns }; # XXX ? | |||||||||
| 38 | # DateTimes | ||||||||||
| 39 | 355 | return $data; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | |||||||||||
| 43 | 171 | ▶ | sub path_for_item { | ||||||||
|
Subroutine
path_for_item: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 44 | 171 | my ($self, $item) = @_; | |||||||||
| 45 | |||||||||||
| 46 | 171 | my $result_source = $item->result_source; | |||||||||
| 47 | |||||||||||
| 48 | 171 | my @id_kvs = $self->id_kvs_for_item($item); | |||||||||
| 49 | |||||||||||
| 50 | 171 | ▶ | my $url = $self->uri_for( @id_kvs, result_class => $result_source->result_class) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | or confess sprintf("panic: no route found to result_class %s (%s)", | ||||||||||
| 52 | $result_source->result_class, join(", ", @id_kvs) | ||||||||||
| 53 | ); | ||||||||||
| 54 | |||||||||||
| 55 | 171 | return $url; | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | |||||||||||
| 59 | |||||||||||
| 60 | # used for recursive rendering | ||||||||||
| 61 | 16 | ▶ | sub web_machine_resource { | ||||||||
|
Subroutine
web_machine_resource: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 62 | 16 | my ($self, %resource_args) = @_; | |||||||||
| 63 | |||||||||||
| 64 | # XXX shouldn't hard-code GenericItem here (should use router?) | ||||||||||
| 65 | my $resource_class = ($resource_args{item}) | ||||||||||
| 66 | 16 | ▶ | ? 'WebAPI::DBIC::Resource::GenericItem' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | : 'WebAPI::DBIC::Resource::GenericSet'; | ||||||||||
| 68 | |||||||||||
| 69 | 16 | my $resource = $resource_class->new( | |||||||||
| 70 | request => $self->request, | ||||||||||
| 71 | response => $self->request->new_response, | ||||||||||
| 72 | throwable => $self->throwable, | ||||||||||
| 73 | prefetch => [], # don't propagate prefetch by default | ||||||||||
| 74 | set => undef, | ||||||||||
| 75 | # XXX others? which and why? generalize | ||||||||||
| 76 | %resource_args | ||||||||||
| 77 | ); | ||||||||||
| 78 | |||||||||||
| 79 | 16 | return $resource; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | |||||||||||
| 83 | 16 | ▶ | sub render_item_into_body { | ||||||||
|
Subroutine
render_item_into_body: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 84 | 16 | my ($self, %resource_args) = @_; | |||||||||
| 85 | |||||||||||
| 86 | 16 | my $item_resource = $self; | |||||||||
| 87 | # if an item has been specified then we assume that it's not $self->item | ||||||||||
| 88 | # and probably relates to a different resource, so we create one for it | ||||||||||
| 89 | # that doesn't have the request params set, eg prefetch | ||||||||||
| 90 | 16 | ▶ | if ($resource_args{item}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | 8 | $item_resource = $self->web_machine_resource( %resource_args ); | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | # XXX temporary hack | ||||||||||
| 95 | 16 | my $body; | |||||||||
| 96 | 16 | ▶ | if ($self->request->headers->header('Accept') =~ /hal\+json/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 7 | $body = $item_resource->to_json_as_hal; | |||||||||
| 98 | } | ||||||||||
| 99 | else { | ||||||||||
| 100 | 9 | $body = $item_resource->to_json_as_plain; | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 16 | $self->response->body($body); | |||||||||
| 104 | |||||||||||
| 105 | 16 | return; | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | |||||||||||
| 109 | |||||||||||
| 110 | 443 | ▶ | sub add_params_to_url { # XXX this is all a bit suspect | ||||||||
|
Subroutine
add_params_to_url: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||
| 111 | 443 | my ($self, $base, $passthru_params, $override_params) = @_; | |||||||||
| 112 | 443 | ▶ | $base || croak "no base"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | |||||||||||
| 114 | 443 | my $req_params = $self->request->query_parameters; | |||||||||
| 115 | 443 | my @params = (%$override_params); | |||||||||
| 116 | |||||||||||
| 117 | # XXX turns 'foo~json' into 'foo', and 'me.bar' into 'me'. | ||||||||||
| 118 | 443 | my %override_param_basenames = map { (split(/\W/,$_,2))[0] => 1 } keys %$override_params; | |||||||||
| 119 | |||||||||||
| 120 | # TODO this logic should live elsewhere | ||||||||||
| 121 | 443 | for my $param (sort keys %$req_params) { | |||||||||
| 122 | |||||||||||
| 123 | # ignore request params that we have an override for | ||||||||||
| 124 | 711 | my $param_basename = (split(/\W/,$param,2))[0]; | |||||||||
| 125 | 711 | ▶ | next if defined $override_param_basenames{$param_basename}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | |||||||||||
| 127 | 693 | ▶ | next unless $passthru_params->{$param_basename}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | |||||||||||
| 129 | 7 | push @params, $param => $req_params->get($param); | |||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | 443 | my $uri = URI->new($base); | |||||||||
| 133 | 443 | $uri->query_form(@params); | |||||||||
| 134 | |||||||||||
| 135 | 443 | return $uri; | |||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | |||||||||||
| 139 | 25 | 1; | |||||||||
| 140 | |||||||||||
| 141 | __END__ | ||||||||||