Rest/Client.pm

filter: Statementstmt 72.8% 126 / 173 Branchbran 70.0% 28 / 40 Conditioncond 22.2% 4 / 18 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 74.2% 26 / 35 Podpod 0.0% 0 / 13 totaltotal 63.8% 184 / 288 CC 31 SCAR 15.8
CC
31
Cov
68%
__encode_request_body
13.5
AUTOLOAD
8.1
head
6.0
CRAP 4.8
« Rest/Product.pm ↑ Summary Rest/RequestAsync.pm »
1 package Eixo::Rest::Client;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 3 ▶use Eixo::Base::Clase;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use LWP::Protocol::https;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use JSON -convert_blessed_universally;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Eixo::Rest::RequestSync;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17  
18 3 my $REQ_PARSER = qr/\:\:([a-z]+)([A-Z]\w+?)$/;
19  
20 3 my $USER_AGENT_VERSION = 'EixoAgent/0.1';
21  
22 3 has(
23  
24 ua=>undef,
25 endpoint=>undef,
26 format=>'json',
27 error_callback => undef,
28 current_method => undef,
29 );
30  
31 3 ▶sub initialize{
Subroutine initialize: called CC=2 SCAR=7.0
Pod undocumented
32 3 my ($self, $endpoint, %opts) = @_;
33  
34 3 $self->endpoint($endpoint);
35  
36 3 ▶ die("API ENDPOINT NEEDED") unless($self->endpoint);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->endpoint 0 3 3
37  
38 3 $self->ua($USER_AGENT_VERSION, %opts);
39  
40 3 $self;
41 }
42  
43 10 ▶sub ua {
Subroutine ua: called CC=2 SCAR=6.9
Pod undocumented
44 10 my ($self, $ua_str, %opts) = @_;
45  
46 10 ▶ if($ua_str){
Branch: 1 branch
BranchTrueFalseTotal
if ($ua_str) 3 7 10
47 3 my $ua = LWP::UserAgent->new(%opts);
48 3 $ua->agent($ua_str);
49 3 $self->{ua} = $ua;
50 }
51  
52 10 return $self->{ua};
53 }
54  
55  
56  
57 8 ▶sub AUTOLOAD{
Subroutine AUTOLOAD: called CC=8 SCAR=20.9
58 8 my ($self, %args) = @_;
59  
60 8 my ($method, $entity) = our $AUTOLOAD =~ $REQ_PARSER;
61
62 # store current method for tracing
63 8 $self->current_method($method.$entity);
64  
65 8 $entity = lc($entity);
66  
67 8 ▶ unless(grep { $method eq $_ } qw(put get post delete patch)){
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$method eq $_;} "put", "get", "post", "delete", "patch") 1 7 8
68 1 confess(ref($self) . ': UNKNOW METHOD: ' . $method);
69 }
70  
71 7 my ($id, $action);
72  
73 7 ▶ if(exists($args{id})){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"id"}) 1 6 7
74 1 $id = $args{id};
75 1 delete($args{id});
76 }
77  
78 7 ▶ if(exists($args{action})){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"action"}) 2 5 7
79 2 $action = $args{action};
80 2 delete($args{action});
81 }
82  
83 7 ▶ if($args{__job_id}){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'__job_id'}) { } 0 7 7
84 0 $args{'__client_send_method'} = '__sendAsync';
85 }
86 else{
87 7 $args{'__client_send_method'} = '__send';
88 }
89  
90 7 ▶ if(!$args{__format}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"__format"}) 5 2 7
91 5 $args{__format} = $self->format;
92 }
93  
94 # set error_callback unless already established
95 7 ▶ unless(defined($args{PROCESS_DATA}->{onError})){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"PROCESS_DATA"}{"onError"}) 7 0 7
96
97 0 ▶ $args{PROCESS_DATA}->{onError} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
98
99 0 $self->remote_error(@_);
100
101 7 };
102 }
103  
104 7 my $uri;
105  
106 7 ▶ if($uri = $args{uri}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri = $args{'uri'}) { } 2 5 7
107 2 $uri = $self->build_predefined_uri($uri);
108 }
109 else{
110 5 $uri = $self->build_uri($entity, $id, $action, $args{__implicit_format});
111 }
112  
113 7 $self->$method($uri, %args);
114  
115 }
116  
117 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
118  
119 0 ▶sub head: Log {
Subroutine head: not called CC=2 SCAR=17.9
Pod undocumented
120 0 my ($self, $uri, %args) = @_;
121  
122 0 $uri->query_form($args{GET_DATA});
123  
124 0 my $req = HTTP::Request->new(GET => $uri);
125  
126 0 ▶ $self->set_headers($req, $args{HEADER_DATA} || {});
Condition0%0/2
$args{'HEADER_DATA'} || {}
l!l
00
MC/DC0%0/1
$args{'HEADER_DATA'} || {}
$args{'HEADER_DATA'}
Truth table0%0/2
A
A= $args{'HEADER_DATA'}
Aresult
11
00
127  
128 0 my $send_method = $args{__client_send_method};
129  
130 0 $self->$send_method($req, %args);
131 3 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
132  
133 5 ▶sub get: Log {
Subroutine get: called CC=2 SCAR=7.0
Pod undocumented
134  
135 5 my ($self, $uri, %args) = @_;
136  
137 5 $uri->query_form($args{GET_DATA});
138  
139 5 my $req = HTTP::Request->new(GET => $uri);
140  
141 5 ▶ $self->set_headers($req, $args{HEADER_DATA} || {});
Condition50%1/2
$args{'HEADER_DATA'} || {}
l!l
05
MC/DC0%0/1
$args{'HEADER_DATA'} || {}
$args{'HEADER_DATA'}
Truth table50%1/2
A
A= $args{'HEADER_DATA'}
Aresult
11
00
142  
143 5 my $send_method = $args{__client_send_method};
144  
145 5 $self->__encode_request_body($req,%args);
146  
147 5 $self->$send_method($req, %args);
148 3 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
149  
150 2 ▶sub post: Log {
Subroutine post: called CC=2 SCAR=7.0
Pod undocumented
151 2 my ($self,$uri,%args) = @_;
152  
153 # Is possible to add query string args to post requests
154 2 $uri->query_form($args{GET_DATA});
155  
156 2 my $req = HTTP::Request->new(POST => $uri);
157
158 2 ▶ $self->set_headers($req, $args{HEADER_DATA} || {});
Condition50%1/2
$args{'HEADER_DATA'} || {}
l!l
02
MC/DC0%0/1
$args{'HEADER_DATA'} || {}
$args{'HEADER_DATA'}
Truth table50%1/2
A
A= $args{'HEADER_DATA'}
Aresult
11
00
159  
160 2 my $send_method = $args{__client_send_method};
161  
162 2 $self->__encode_request_body($req,%args);
163  
164 2 $self->$send_method($req, %args);
165 3 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
166  
167 0 ▶sub delete: Log {
Subroutine delete: not called CC=2 SCAR=17.9
Pod undocumented
168  
169 0 my ($self, $uri, %args) = @_;
170  
171 0 $uri->query_form($args{GET_DATA});
172  
173 0 my $req = HTTP::Request->new(DELETE => $uri);
174  
175 0 ▶ $self->set_headers($req, $args{HEADER_DATA} || {});
Condition0%0/2
$args{'HEADER_DATA'} || {}
l!l
00
MC/DC0%0/1
$args{'HEADER_DATA'} || {}
$args{'HEADER_DATA'}
Truth table0%0/2
A
A= $args{'HEADER_DATA'}
Aresult
11
00
176  
177 0 my $send_method = $args{__client_send_method};
178  
179 0 $self->__encode_request_body($req,%args);
180  
181 0 $self->$send_method($req, %args);
182 3 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
183  
184 0 ▶sub patch: Log {
Subroutine patch: not called CC=2 SCAR=17.9
Pod undocumented
185 0 my ($self, $uri, %args) = @_;
186  
187 0 $uri->query_form($args{GET_DATA});
188  
189 0 my $req = HTTP::Request->new(PATCH => $uri);
190  
191 0 ▶ $self->set_headers($req, $args{HEADER_DATA} || {});
Condition0%0/2
$args{'HEADER_DATA'} || {}
l!l
00
MC/DC0%0/1
$args{'HEADER_DATA'} || {}
$args{'HEADER_DATA'}
Truth table0%0/2
A
A= $args{'HEADER_DATA'}
Aresult
11
00
192  
193 0 my $send_method = $args{__client_send_method};
194  
195 0 $self->__encode_request_body($req,%args);
196  
197 0 $self->$send_method($req, %args);
198  
199 3 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
200  
201 0 ▶sub put: Log {
Subroutine put: not called CC=2 SCAR=17.9
Pod undocumented
202 0 my ($self, $uri, %args) = @_;
203  
204 0 $uri->query_form($args{GET_DATA});
205  
206 0 my $req = HTTP::Request->new(PUT=> $uri);
207  
208 0 ▶ $self->set_headers($req, $args{HEADER_DATA} || {});
Condition0%0/2
$args{'HEADER_DATA'} || {}
l!l
00
MC/DC0%0/1
$args{'HEADER_DATA'} || {}
$args{'HEADER_DATA'}
Truth table0%0/2
A
A= $args{'HEADER_DATA'}
Aresult
11
00
209  
210 0 my $send_method = $args{__client_send_method};
211  
212 0 $self->__encode_request_body($req,%args);
213  
214 0 $self->$send_method($req, %args);
215 3 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
216  
217  
218 7 ▶sub __encode_request_body{
Subroutine __encode_request_body: called CC=8 SCAR=26.0
219 7 my ($self,$req, %args) = @_;
220  
221 7 ▶ return unless($args{POST_DATA});
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"POST_DATA"} 3 4 7
222  
223 4 my $content;
224 4 ▶ my $content_type = $req->header('Content-Type')
Condition50%1/2
$req->header('Content-Type') || 'application/json'
l!l
04
MC/DC0%0/1
$req->header('Content-Type') || 'application/json'
$req->header('Content-Type')
Truth table50%1/2
A
A= $req->header('Content-Type')
Aresult
11
00
225 || 'application/json';
226 #|| 'application/x-www-form-urlencoded';
227  
228 4 ▶ if($content_type eq "application/json"){
Branch: 1 branch
BranchTrueFalseTotal
if ($content_type eq 'application/json') { } 4 0 4
229  
230 $content = JSON->new->allow_blessed(1)
231 ->convert_blessed(1)#->utf8
232 4 ▶ ->encode($args{POST_DATA} || {});
Condition50%1/2
$args{'POST_DATA'} || {}
l!l
40
MC/DC0%0/1
$args{'POST_DATA'} || {}
$args{'POST_DATA'}
Truth table50%1/2
A
A= $args{'POST_DATA'}
Aresult
11
00
233 }
234 else{
235
236 # application/x-www-form-urlencoded
237 # raw stream
238 0 ▶ while(my ($key, $value) = each (%{$args{POST_DATA} || {}})){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$args{"POST_DATA"} or {};}) 0 0 0
Condition0%0/2
$args{'POST_DATA'} || {}
l!l
00
MC/DC0%0/1
$args{'POST_DATA'} || {}
$args{'POST_DATA'}
Truth table0%0/2
A
A= $args{'POST_DATA'}
Aresult
11
00
239 0 $content .= "$key=$value&";
240 }
241  
242 }
243  
244 4 $req->header("Content-Type", "$content_type; charset=utf-8");
245 4 ▶ $req->add_content_utf8($content) if($content);
Branch: 1 branch
BranchTrueFalseTotal
if $content 4 0 4
246  
247 }
248  
249  
250 5 ▶sub build_uri {
Subroutine build_uri: called CC=5 SCAR=16.1
Pod undocumented
251 5 my ($self, $entity, $id,$action, $implicit_format) = @_;
252  
253 5 my $uri = $self->{endpoint}.'/'.$entity;
254
255 5 ▶ $uri .= '/'.$id if(defined($id));
Branch: 1 branch
BranchTrueFalseTotal
if defined $id 1 4 5
256 5 ▶ $uri .= '/'.$action if(defined($action));
Branch: 1 branch
BranchTrueFalseTotal
if defined $action 2 3 5
257  
258 5 ▶ return URI->new($uri) if($implicit_format);
Branch: 1 branch
BranchTrueFalseTotal
if $implicit_format 2 3 5
259  
260 ($self->current_method =~ /^get/)?
261  
262 3 ▶ URI->new($uri.'/'.$self->{format}) :
Branch: 1 branch
BranchTrueFalseTotal
$self->current_method =~ /^get/ ? : 2 1 3
263  
264 URI->new($uri);
265 }
266  
267 2 ▶sub build_predefined_uri{
Subroutine build_predefined_uri: called CC=2 SCAR=7.0
Pod undocumented
268 2 my ($self, $uri) = @_;
269  
270 2 my $endpoint = $self->{endpoint};
271
272 2 ▶ $endpoint .= "/" unless($endpoint =~ /\/$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $endpoint =~ m[/$] 2 0 2
273  
274 2 $uri =~ s/^\///;
275  
276 2 return URI->new($endpoint . $uri)
277 }
278  
279  
280 0 ▶sub generate_query_str {
Subroutine generate_query_str: not called CC=1 SCAR=6.9
Pod undocumented
281 0 my ($self, %args) = @_;
282  
283 0 join '&', map {"$_=$args{$_}"} keys(%args);
284 }
285  
286  
287 7 ▶sub __send{
Subroutine __send: called CC=1 SCAR=0.0
288 7 my ($self, $req, %args) = @_;
289  
290 Eixo::Rest::RequestSync->new(
291  
292 callback=>$args{__callback},
293  
294 7 %{$args{PROCESS_DATA}},
295  
296 __format=>$args{__format}
297  
298 7 )->send(
299  
300 $self->ua(),
301  
302 $req
303  
304 );
305  
306 }
307  
308 0 ▶sub __sendAsync{
Subroutine __sendAsync: not called CC=2 SCAR=17.9
309 0 my ($self, $req, %args) = @_;
310  
311 0 ▶ die("unsupported: ERROR: This Perl not built to support threads\n") if (! $Config{'useithreads'});
Branch: 1 branch
BranchTrueFalseTotal
unless $Eixo::Rest::Client::Config{"useithreads"} 0 0 0
312  
313 0 eval 'use Eixo::Rest::RequestAsync';
314  
315 Eixo::Rest::RequestAsync->new(
316  
317 job_id=>$args{__job_id},
318  
319 api=>$args{api},
320  
321 callback=>$args{__callback},
322  
323 0 %{$args{PROCESS_DATA}},
324  
325 __format=>$args{__format}
326  
327 0 )->send(
328  
329 $self->ua(),
330  
331 $req
332  
333 );
334  
335 }
336  
337 0 ▶sub remote_error {
Subroutine remote_error: not called CC=2 SCAR=17.9
Pod undocumented
338 0 my ($self,$response) = @_;
339  
340 0 my $status = $response->code;
341 0 my $extra = $response->content;
342  
343 0 ▶ if(defined($self->error_callback)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->error_callback) { } 0 0 0
344  
345 0 &{$self->error_callback}(
346  
347 $self->current_method,
348  
349 'ERROR_CODE',
350  
351 $status,
352  
353 $extra,
354 #@extra_args
355
356 );
357 }
358 else{
359 0 die "Remote Api error: ($status). Details: $extra\n";
360 }
361 }
362  
363 7 ▶sub set_headers{
Subroutine set_headers: called CC=2 SCAR=6.9
Pod undocumented
364 7 my ($self, $req, $headers) = @_;
365  
366 7 $req->header($_, $headers->{$_}) foreach(keys %$headers);
367 }
368  
369 3 1;
« Rest/Product.pm ↑ Summary Rest/RequestAsync.pm »