Client.pm

filter: Statementstmt 94.6% 159 / 168 Branchbran 66.6% 32 / 48 Conditioncond 59.0% 13 / 22 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 95.4% 42 / 44 Podpod 90.0% 9 / 10 totaltotal 84.5% 258 / 305 CC 34 SCAR 14.4
CC
34
Cov
86%
req
16.1
_content
5.1
_encode_params
4.0
CRAP 4.2
« Client/Response.pm ↑ Summary
1 package WebService::Client;
2 2 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 our $VERSION = '1.0100'; # VERSION
5  
6 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Ref::Util qw(is_hashref is_plain_arrayref is_plain_coderef);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use HTTP::Request::Common qw(DELETE GET HEAD OPTIONS PATCH POST PUT);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use LWP::UserAgent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use WebService::Client::Response ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 has base_url => (
16 is => 'rw',
17 0 ▶ default => sub { '' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
18 2 );
19  
20 has ua => (
21 is => 'ro',
22 lazy => 1,
23 0 ▶ default => sub { LWP::UserAgent->new(timeout => shift->timeout) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 2 );
25  
26 has timeout => (
27 is => 'ro',
28 14 ▶ default => sub { 10 },
Subroutine __ANON__: called CC=1 SCAR=0.0
29 2 );
30  
31 has retries => (
32 is => 'ro',
33 14 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 14 ▶ isa => sub {
Subroutine __ANON__: called CC=3 SCAR=13.1
35 14 my $r = shift;
36 14 ▶ die 'retries must be a nonnegative integer'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r and $r =~ /^\d+$/ 0 14 14
Condition33%1/3
defined $r and $r =~ /^\d+$/
!ll&&!rl&&r
0014
MC/DC0%0/2
defined $r and $r =~ /^\d+$/
defined $r $r =~ /^\d+$/
Truth table33%1/3
A and B
A= defined $rB= $r =~ /^\d+$/
ABresult
0X0
100
111
37 unless defined $r and $r =~ /^\d+$/;
38 },
39 2 );
40  
41 2 has logger => ( is => 'ro' );
42  
43 has log_method => (
44 is => 'ro',
45 14 ▶ default => sub { 'DEBUG' },
Subroutine __ANON__: called CC=1 SCAR=0.0
46 2 );
47  
48 has content_type => (
49 is => 'rw',
50 14 ▶ default => sub { 'application/json' },
Subroutine __ANON__: called CC=1 SCAR=0.0
51 2 );
52  
53 has deserializer => (
54 is => 'ro',
55 lazy => 1,
56 11 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
57 11 my $self = shift;
58 11 my $json = $self->json;
59 25 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
60 25 my ($res, %args) = @_;
61 25 return $json->decode($res->decoded_content);
62 }
63 11 },
64 2 );
65  
66 has serializer => (
67 is => 'ro',
68 lazy => 1,
69 4 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
70 4 my $self = shift;
71 4 my $json = $self->json;
72 6 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
73 6 my ($data, %args) = @_;
74 # TODO: remove the next line after clients are updated to inject
75 # custom serializers that will handle this logic
76 6 ▶ return $data unless _content_type($args{headers}) =~ /json/;
Branch: 1 branch
BranchTrueFalseTotal
unless _content_type($args{"headers"}) =~ /json/ 0 6 6
77 6 return $json->encode($data);
78 }
79 4 }
80 2 );
81  
82 has json => (
83 is => 'ro',
84 lazy => 1,
85 11 ▶ default => sub { JSON::MaybeXS->new() },
Subroutine __ANON__: called CC=1 SCAR=0.0
86 2 );
87  
88 has mode => (
89 is => 'ro',
90 14 ▶ default => sub { '' },
Subroutine __ANON__: called CC=1 SCAR=0.0
91 2 );
92  
93 has array_query_style => (
94 is => 'ro',
95 13 ▶ default => sub { 'php' },
Subroutine __ANON__: called CC=1 SCAR=0.0
96 14 ▶ isa => sub {
Subroutine __ANON__: called CC=3 SCAR=11.7
97 14 my $s = shift;
98 14 ▶ die q{array_query_style must be "php" or "rfc"}
Branch: 1 branch
BranchTrueFalseTotal
unless $s eq "php" or $s eq "rfc" 0 14 14
Condition66%2/3
$s eq "php" or $s eq "rfc"
l!l&&r!l&&!r
1310
MC/DC0%0/2
$s eq "php" or $s eq "rfc"
$s eq "php" $s eq "rfc"
Truth table66%2/3
A or B
A= $s eq "php"B= $s eq "rfc"
ABresult
1X1
011
000
99 unless $s eq 'php' or $s eq 'rfc';
100 },
101 2 );
102  
103 17 ▶sub get { shift->_request_with_params(\&GET, @_) }
Subroutine get: called CC=1 SCAR=0.0
Pod documented
104 3 ▶sub head { shift->_request_with_params(\&HEAD, @_) }
Subroutine head: called CC=1 SCAR=0.0
Pod documented
105  
106 20 ▶sub _request_with_params {
Subroutine _request_with_params: called CC=2 SCAR=6.9
107 20 my ($self, $method, $path, $params, %args) = @_;
108 20 ▶ $params //= {};
Condition100%2/2
$params //= {}
l!l
146
MC/DC100%1/1
$params //= {}
$params
Truth table100%2/2
A
A= $params
Aresult
11
00
109 20 my $headers = $self->_headers(\%args);
110 18 my $req = $method->($self->_build_url($path, $params), %$headers);
111 18 return $self->req($req, %args);
112 }
113  
114 1 ▶sub post {
Subroutine post: called CC=1 SCAR=0.0
Pod documented
115 1 my ($self, $path, $data, %args) = @_;
116 1 my $headers = $self->_headers_content_type(\%args);
117 1 my $url = $self->_url($path);
118 1 my $req = POST $url, %$headers, $self->_content($data, %args);
119 1 return $self->req($req, %args);
120 }
121  
122 1 ▶sub put {
Subroutine put: called CC=1 SCAR=0.0
Pod documented
123 1 my ($self, $path, $data, %args) = @_;
124 1 my $headers = $self->_headers_content_type(\%args);
125 1 my $url = $self->_url($path);
126 1 my $req = PUT $url, %$headers, $self->_content($data, %args);
127 1 return $self->req($req, %args);
128 }
129  
130 2 ▶sub patch {
Subroutine patch: called CC=1 SCAR=0.0
Pod documented
131 2 my ($self, $path, $data, %args) = @_;
132 2 my $headers = $self->_headers_content_type(\%args);
133 2 my $url = $self->_url($path);
134 2 my $req = PATCH $url, %$headers, $self->_content($data, %args);
135 2 return $self->req($req, %args);
136 }
137  
138 2 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
139 2 my ($self, $path, %args) = @_;
140 2 my $headers = $self->_headers(\%args);
141 2 my $url = $self->_url($path);
142 2 my $req = DELETE $url, %$headers;
143 2 return $self->req($req, %args);
144 }
145  
146 3 ▶sub options {
Subroutine options: called CC=2 SCAR=6.9
Pod documented
147 3 my ($self, $path, $data, %args) = @_;
148 3 ▶ my $headers = defined $data
Branch: 1 branch
BranchTrueFalseTotal
defined $data ? : 2 1 3
149 ? $self->_headers_content_type(\%args)
150 : $self->_headers(\%args);
151 3 my $url = $self->_url($path);
152 3 my $req = OPTIONS $url, %$headers, $self->_content($data, %args);
153 3 return $self->req($req, %args);
154 }
155  
156 27 ▶sub req {
Subroutine req: called CC=11 SCAR=27.8
Pod documented
157 27 my ($self, $req, %args) = @_;
158 27 $self->_log_request($req);
159 27 my $res = $self->ua->request($req);
160 27 $self->_log_response($res);
161  
162 27 my $retries = $self->retries;
163 27 ▶ while ($res->code =~ /^5/ and $retries--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code =~ /^5/ and $retries--) 0 27 27
Condition33%1/3
$res->code =~ /^5/ and $retries--
!ll&&!rl&&r
2700
MC/DC0%0/2
$res->code =~ /^5/ and $retries--
$res->code =~ /^5/ $retries--
Truth table33%1/3
A and B
A= $res->code =~ /^5/B= $retries--
ABresult
0X0
100
111
164 0 sleep 1;
165 0 $res = $self->ua->request($req);
166 0 $self->_log_response($res);
167 }
168  
169 27 $self->prepare_response($res);
170  
171 27 ▶ if ($self->mode eq 'v2') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq "v2") 0 27 27
172 0 return WebService::Client::Response->new(
173 res => $res,
174 json => $self->json,
175 );
176 }
177  
178 27 ▶ return if $req->method eq 'GET' and $res->code =~ /404|410/;
Branch: 1 branch
BranchTrueFalseTotal
if $req->method eq "GET" and $res->code =~ /404|410/ 0 27 27
Condition66%2/3
$req->method eq "GET" and $res->code =~ /404|410/
!ll&&!rl&&r
12150
MC/DC0%0/2
$req->method eq "GET" and $res->code =~ /404|410/
$req->method eq "GET" $res->code =~ /404|410/
Truth table66%2/3
A and B
A= $req->method eq "GET"B= $res->code =~ /404|410/
ABresult
0X0
100
111
179 27 ▶ die $res unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 27 27
180 27 ▶ return 1 unless $res->content;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->content 2 25 27
181 25 my $des = $self->deserializer;
182 25 ▶ $des = $args{deserializer} if exists $args{deserializer};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"deserializer"} 0 25 25
183 25 ▶ if ($des) {
Branch: 1 branch
BranchTrueFalseTotal
if ($des) { } 25 0 25
184 25 ▶ die 'deserializer must be a coderef or undef'
Branch: 1 branch
BranchTrueFalseTotal
unless is_plain_coderef($des) 0 25 25
185 unless is_plain_coderef($des);
186 25 return $des->($res, %args);
187 }
188 else {
189 0 return $res->content;
190 }
191 }
192  
193 54 ▶sub log {
Subroutine log: called CC=2 SCAR=9.2
Pod documented
194 54 my ($self, $msg) = @_;
195 54 ▶ return unless $self->logger;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->logger 54 0 54
196 0 my $log_method = $self->log_method;
197 0 $self->logger->$log_method($msg);
198 }
199  
200 27 ▶sub prepare_response {
Subroutine prepare_response: called CC=1 SCAR=0.0
Pod undocumented
201 27 my ($self, $res) = @_;
202 27 Moo::Role->apply_roles_to_object($res, 'HTTP::Response::Stringable');
203 27 return;
204 }
205  
206 27 ▶sub _url {
Subroutine _url: called CC=3 SCAR=11.1
207 27 my ($self, $path) = @_;
208 27 ▶ croak 'The path is missing' unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 27 27
209 27 ▶ return $path =~ m{^https?://} ? $path : $self->base_url . $path;
Branch: 1 branch
BranchTrueFalseTotal
$path =~ m[^https?://] ? : 1 26 27
210 }
211  
212 29 ▶sub _headers {
Subroutine _headers: called CC=3 SCAR=11.0
213 29 my ($self, $args) = @_;
214 29 ▶ my $headers = $args->{headers} // {};
Condition100%2/2
$args->{"headers"} // {}
l!l
920
MC/DC100%1/1
$args->{"headers"} // {}
$args->{"headers"}
Truth table100%2/2
A
A= $args->{"headers"}
Aresult
11
00
215 29 ▶ croak 'The headers param must be a hashref'
Branch: 1 branch
BranchTrueFalseTotal
unless is_hashref($headers) 2 27 29
216 unless is_hashref($headers);
217 27 return $headers;
218 }
219  
220 6 ▶sub _headers_content_type {
Subroutine _headers_content_type: called CC=2 SCAR=7.0
221 6 my ($self, $args) = @_;
222 6 my $headers = $self->_headers($args);
223 6 ▶ unless (_content_type($headers)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_content_type($headers)) 6 0 6
224 6 $headers = { %$headers, content_type => $self->content_type };
225 6 $args->{headers} = $headers;
226 }
227 6 return $headers;
228 }
229  
230 27 ▶sub _log_request {
Subroutine _log_request: called CC=1 SCAR=0.0
231 27 my ($self, $req) = @_;
232 27 $self->log(ref($self) . " REQUEST:\n" . $req->as_string);
233 }
234  
235 27 ▶sub _log_response {
Subroutine _log_response: called CC=1 SCAR=0.0
236 27 my ($self, $res) = @_;
237 27 $self->log(ref($self) . " RESPONSE:\n" . $res->as_string);
238 }
239  
240 12 ▶sub _content_type {
Subroutine _content_type: called CC=3 SCAR=12.5
241 12 my ($headers) = @_;
242 return $headers->{'Content-Type'}
243 || $headers->{'content-type'}
244 12 ▶ || $headers->{content_type};
Condition50%3/6
$headers->{'Content-Type'} || $headers->{'content-type'} || $headers->{'content_type'}
l!l&&r!l&&!r
066
$headers->{"Content-Type"} or $headers->{"content-type"}
l!l&&r!l&&!r
0012
MC/DC33%1/3
$headers->{'Content-Type'} || $headers->{'content-type'} || $headers->{'content_type'}
$headers->{"Content-Type"} $headers->{"content-type"} $headers->{'content_type'}
Truth table50%2/4
A or B || C
A= $headers->{"Content-Type"}B= $headers->{"content-type"}C= $headers->{'content_type'}
ABCresult
1XX1
01X1
0011
0000
245 }
246  
247 7 ▶sub _content {
Subroutine _content: called CC=5 SCAR=16.3
248 7 my ($self, $data, %args) = @_;
249 7 my @content;
250 7 ▶ if (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) 6 1 7
251 6 my $ser = $self->serializer;
252 6 ▶ $ser = $args{serializer} if exists $args{serializer};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"serializer"} 0 6 6
253 6 ▶ if ($ser) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ser) 6 0 6
254 6 ▶ die 'serializer must be a coderef or undef'
Branch: 1 branch
BranchTrueFalseTotal
unless is_plain_coderef($ser) 0 6 6
255 unless is_plain_coderef($ser);
256 6 $data = $ser->($data, %args);
257 }
258 6 @content = ( content => $data );
259 }
260 7 return @content;
261 }
262  
263 10 ▶sub _encode_params {
Subroutine _encode_params: called CC=4 SCAR=13.9
264 10 my ($self, $params) = @_;
265 10 ▶ return $params unless $self->array_query_style eq 'php';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->array_query_style eq "php" 3 7 10
266 7 my %php;
267 7 for my $key (keys %$params) {
268 10 my $value = $params->{$key};
269 10 ▶ if (is_plain_arrayref($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_plain_arrayref($value)) { } 3 7 10
270 3 $php{"$key\[]"} = $value;
271 }
272 else {
273 7 $php{$key} = $value;
274 }
275 }
276 7 return \%php;
277 }
278  
279 18 ▶sub _build_url {
Subroutine _build_url: called CC=2 SCAR=6.9
280 18 my ($self, $path, $params) = @_;
281 18 my $uri = URI->new($self->_url($path));
282 18 ▶ if (keys %$params) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$params) 10 8 18
283 10 $uri->query_form_hash($self->_encode_params($params));
284 }
285 18 return $uri->as_string;
286 }
287  
288 # ABSTRACT: A base role for creating web service clients
289  
290  
291 2 1;
292  
293 __END__
« Client/Response.pm ↑ Summary