Consumer.pm

filter: Statementstmt 94.8% 130 / 137 Branchbran 84.0% 42 / 50 Conditioncond 62.5% 5 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 90.9% 20 / 22 Podpod n/a totaltotal 89.1% 198 / 222 CC 30 SCAR 17.1
CC
30
Cov
91%
request_path
12.5
call
7.0
consumer
5.1
CRAP 5.5
↑ Summary Consumer/Response.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package MooseX::Role::REST::Consumer;
5  
6 1 ▶use MooseX::Role::Parameterized;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use MooseX::Role::REST::Consumer::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use REST::Consumer;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our $VERSION = '0.003';
15  
16 1 my @HTTP_METHODS = qw(get put post delete);
17  
18 1 parameter service_host => (
19 lazy => 1,
20 default => '',
21 );
22  
23 1 parameter service_port => ();
24  
25 parameter resource_path => (
26 lazy => 1,
27 0 ▶ default => sub { undef },
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 1 );
29  
30 1 parameter content_type => (
31 isa => 'Str',
32 required => 1,
33 default => 'application/json',
34 );
35  
36 parameter header_exclude => (
37 isa => 'HashRef',
38 8 ▶ default => sub {{}},
Subroutine __ANON__: called CC=1 SCAR=0.0
39 1 );
40  
41 1 parameter retry => (
42 required => 1,
43 default => 0,
44 );
45  
46 1 parameter timeout => (
47 default => 1,
48 );
49  
50 parameter query_params_mapping => (
51 isa => 'HashRef',
52 7 ▶ default => sub {{}},
Subroutine __ANON__: called CC=1 SCAR=0.0
53 1 );
54  
55 1 parameter useragent_class => (
56 isa => 'Str',
57 );
58  
59 8 ▶role {
Subroutine __ANON__: called CC=2 SCAR=7.0
60 8 my $p = shift;
61  
62 8 my $service_host = $p->service_host;
63 8 my $service_port = $p->service_port;
64 8 my $resource_path = $p->resource_path;
65 8 my $content_type = $p->content_type;
66 8 my $retry = $p->retry;
67 8 my $timeout = $p->timeout;
68 8 my $query_params_mapping = $p->query_params_mapping;
69 8 my $useragent_class = $p->useragent_class;
70  
71 #Note: since this is a parametrized role then only one class will be closing
72 #over this variable and this instance only depends on role parameters
73 8 my $consumer_instance;
74  
75 #Note: $class varible in methods of this role can be an instance and a class name
76 #For GET requests this is usually a class name, for POST requests
77 #this would usually be an instance.
78 #Be carefull!
79  
80 13 ▶ method 'consumer' => sub {
Subroutine consumer: called CC=5 SCAR=16.3
81 13 my ($class) = @_;
82 13 ▶ return $consumer_instance if($consumer_instance);
Branch: 1 branch
BranchTrueFalseTotal
if $consumer_instance 6 7 13
83  
84 7 my $user_agent;
85  
86 7 ▶ if($useragent_class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($useragent_class) 1 6 7
87 1 Module::Load::load($useragent_class);
88 0 $user_agent = $useragent_class->new;
89 };
90  
91 6 ▶ my $client = REST::Consumer->new(
Branch: 2 branches
BranchTrueFalseTotal
$user_agent ? : 0 6 6
$service_port ? : 0 6 6
92 host => $service_host,
93 timeout => $timeout,
94 ($user_agent ? (ua => $user_agent) : ()),
95 ($service_port ? (port => $service_port) : ())
96 );
97  
98 6 $client->user_agent->use_eval(0);
99  
100 6 $consumer_instance = $client;
101 6 return $client;
102 8 };
103  
104 11 ▶ method 'call' => sub {
Subroutine call: called CC=7 SCAR=19.5
105 11 my ($class, $method, %params) = @_;
106  
107 my %request_headers = $class->request_headers(
108 header_exclude => $p->header_exclude,
109 headers => $params{headers},
110 11 content_type => $p->content_type,
111 method => $method,
112 );
113  
114 11 $content_type = delete $request_headers{'Content-Type'};
115 11 ▶ if ($params{access_token}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"access_token"}) 1 10 11
116 1 $request_headers{Authorization} = 'Bearer ' . $params{access_token};
117 }
118  
119 my %query_params = $class->query_params(
120 params => $params{params},
121 11 query_params_mapping => $query_params_mapping,
122 );
123  
124 my $path = $class->request_path(
125 resource_path => $resource_path,
126 route_params => delete $params{route_params},
127 path => delete $params{path},
128 11 );
129  
130 11 my ($data, $error, $timeout_error) = (undef, '', 0);
131  
132 my %request = (
133 params => \%query_params,
134 content => $params{content},
135 11 ▶ path => $path,
Branch: 1 branch
BranchTrueFalseTotal
%request_headers ? : 2 9 11
136 content_type => $content_type,
137 headers => ( %request_headers ? [ %request_headers ] : undef ),
138 );
139  
140 #my $logger = $class->get_logger
141 #$logger->start_request(
142 # type => $method,
143 # path => $path,
144 # response => $data,
145 # request => \%request
146 #);
147  
148 11 my $consumer = $class->consumer; #we want same instance throughout the whole process
149 10 ▶ $consumer->host(delete $params{host}) if $params{host};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"host"} 1 9 10
150  
151 10 ▶ if (my $timeout_override = delete $params{timeout} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $timeout_override = delete $params{"timeout"}) 1 9 10
152 1 $consumer->timeout($timeout_override);
153 }
154  
155 10 my $try = 0;
156 10 ▶ while ($try <= $retry) {
Branch: 1 branch
BranchTrueFalseTotal
if ($try <= $retry) 12 2 14
157 12 my $is_success;
158 12 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
159 12 $try++;
160 12 $data = $consumer->$method(%request);
161 8 $is_success = 1;
162 4 ▶ } catch {
Subroutine __ANON__: called CC=2 SCAR=6.9
163 4 $error = $_;
164 4 ▶ $timeout_error++ if $error =~ /read timeout/;
Branch: 1 branch
BranchTrueFalseTotal
if $error =~ /read timeout/ 3 1 4
165 12 };
166 12 ▶ last if $is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $is_success 8 4 12
167 }
168  
169 10 $consumer->timeout($timeout);
170 10 $consumer->host($service_host);
171  
172 #$logger->finish($log_entry, {
173 # type => $method,
174 # path => $path,
175 # response => $data,
176 # request => \%request
177 #});
178  
179 # TODO: It's confusing as to how we handle errors.
180 # ie: message should be called "error_message" and
181 # we should inspect the response content for possible error messages
182 10 return MooseX::Role::REST::Consumer::Response->new(
183 data => $data,
184 is_success => !$error,
185 error_message => "$error",
186 request => $consumer->last_request,
187 content_type => $content_type,
188 response => $consumer->last_response,
189 );
190 8 };
191  
192 # Note: REST::Consumer doesn't support OPTIONS/PATCH
193 8 for my $method ( @HTTP_METHODS ) {
194 11 ▶ method $method => sub {
Subroutine post: called CC=1 SCAR=0.0
195 11 my $class = shift;
196 11 $class->call($method, @_);
197 }
198 32 }
199  
200 11 ▶ method 'request_headers' => sub {
Subroutine request_headers: called CC=4 SCAR=14.3
201 11 my ($class, %params) = @_;
202 11 ▶ my %request_headers = ($params{headers} ? %{delete $params{headers}} : ());
Branch: 1 branch
BranchTrueFalseTotal
$params{'headers'} ? : 2 9 11
203 # Strange mutation going on here:
204 # 1. First we set the content_type in the headers if we have one set in parameter definition
205 # 2. However, we won't override anything that is passed explicitly into a method call Class->post
206 # 3. Next we delete anything that needs to be removed from the header
207 # 4. Finally we explicltly pull out content-type from the request_headers
208 # to make REST::Consumer happy
209 11 ▶ $request_headers{'Content-Type'} = $params{content_type} unless $request_headers{'Content-Type'};
Branch: 1 branch
BranchTrueFalseTotal
unless $request_headers{"Content-Type"} 11 0 11
210 0 ▶ delete @request_headers{@{$params{header_exclude}->{$params{method}}}} if $params{header_exclude}->{$params{method}};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"header_exclude"}{$params{"method"}} 0 11 11
211 11 return %request_headers;
212 8 };
213 22 ▶ method 'request_path' => sub {
Subroutine request_path: called CC=12 SCAR=25.3
214 22 my ($class, %params) = @_;
215  
216 22 my $resource_path = $params{resource_path};
217 22 my $route_params = $params{route_params};
218 22 my $params_path = $params{path};
219 22 ▶ my @path = (defined $resource_path ? $resource_path : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $resource_path ? : 22 0 22
220  
221 22 ▶ if($params_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params_path) 9 13 22
222 9 ▶ if(ref($params_path) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params_path eq 'ARRAY') { } 0 9 9
223 0 push(@path, @$params_path);
224 } else {
225 9 push(@path, $params_path);
226 }
227 }
228 22 ▶ my $path = File::Spec->catfile(@path) . ( $resource_path && $resource_path =~ m{[^/]+/$} ? '/' : '');
Branch: 1 branch
BranchTrueFalseTotal
$resource_path && $resource_path =~ m[[^/]+/$] ? : 6 16 22
Condition66%2/3
$resource_path and $resource_path =~ m[[^/]+/$]
!ll&&!rl&&r
0166
MC/DC50%1/2
$resource_path and $resource_path =~ m[[^/]+/$]
$resource_path $resource_path =~ m[[^/]+/$]
Truth table66%2/3
A and B
A= $resource_pathB= $resource_path =~ m[[^/]+/$]
ABresult
0X0
100
111
229 #We support two ways of substituting params here:
230 # /:param/ - name has to have '/' or end of string after it
231 # /has_:{param}_value - surround param name with '{}'
232 #Note: we go through complete incremental parsing/fetching url params to avoid
233 #params substituted values being mached against other params names
234 #We also verify that all parameters have been substituted
235 22 my $result = '';
236 22 ▶ while($path =~ /\G(.*?)(:(?:\{([^}]+?)\}|(\w+)(?=\W|$)))/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /\G(.*?)(:(?:\{([^}]+?)\}|(\w+)(?=\W|$)))/cg) 19 21 40
237 19 $result .= $1;
238 19 ▶ if($2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($2) 19 0 19
239 19 ▶ my $name = $3 || $4; # only one of them could match
Condition66%2/3
$3 || $4
l!l&&r!l&&!r
5140
MC/DC0%0/2
$3 || $4
$3 $4
Truth table66%2/3
A || B
A= $3B= $4
ABresult
1X1
011
000
240 19 ▶ if(exists $route_params->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $route_params->{$name}) { } 18 1 19
241 18 ▶ $result .= URI::Escape::uri_escape_utf8($route_params->{$name} // '');
Condition50%1/2
$route_params->{$name} // ""
l!l
180
MC/DC0%0/1
$route_params->{$name} // ""
$route_params->{$name}
Truth table50%1/2
A
A= $route_params->{$name}
Aresult
11
00
242 }
243 else {
244 1 die "Found parameter $name in path but it wasn't set in parameters hash";
245 }
246 }
247 }
248 21 ▶ if($path =~ /\G(.+)$/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /\G(.+)$/g) 17 4 21
249 17 $result .= $1;
250 }
251 21 return $result;
252 8 };
253  
254 11 ▶ method 'query_params' => sub {
Subroutine query_params: called CC=4 SCAR=14.1
255 11 my ($class, %params) = @_;
256  
257 11 my %query_params;
258 11 ▶ if($params{params}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"params"}) 1 10 11
259 # TODO: I don't think having this strict mapping in place
260 # makes a lot of sense. Ideally we should be able to pass in
261 # any query params that we want
262 1 ▶ if ( $params{query_params_mapping} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'query_params_mapping'}) { } 1 0 1
263 1 ▶ while(my ($name, $url_name) = each(%{$params{query_params_mapping}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$params{"query_params_mapping"};}) 1 1 2
264 # Check for method canness here probably
265 1 $query_params{$url_name} = delete($params{params}->{$name});
266 }
267 } else {
268 0 %query_params = %{$params{params}};
269 }
270 }
271 11 return %query_params;
272 8 };
273  
274 0 ▶ method 'parameters' => sub { $p };
Subroutine parameters: not called CC=1 SCAR=1.2
275  
276 1 };
277  
278 __END__
↑ Summary Consumer/Response.pm »