HTTP/MultiGet/Role.pm

filter: Statementstmt 87.0% 121 / 139 Branchbran 61.1% 22 / 36 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 90.9% 30 / 33 Podpod 72.7% 8 / 11 totaltotal 81.6% 187 / 229 CC 21 SCAR 10.8
CC
21
Cov
81%
parse_response
8.0
add_ids_for_blocking
6.0
block_on_ids
3.8
CRAP 2.9
« HTTP/MultiGet.pm ↑ Summary AnyEvent/HTTP/MultiGet.pm »
1 package HTTP::MultiGet::Role;
2  
3 2 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use MooX::Types::MooseLike::Base qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use AnyEvent::HTTP::MultiGet;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use JSON qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Ref::Util qw(is_plain_arrayref);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 with 'Log::LogMethods','Data::Result::Moo';
17 }
18  
19 2 our $AUTOLOAD;
20  
21 =head1 NAME
22  
23 HTTP::MultiGet::Role - Role for building blocking/non-blocking AnyEvent friendly REST Clients
24  
25 =head1 SYNOPSIS
26  
27 package My::Rest::Class;
28  
29 use Modern::Perl;
30 use Moo;
31 BEGIN { with 'HTTP::MultiGet::Role' }
32  
33 sub que_some_request {
34 my ($self,$cb)=@_;
35 my $request=HTTP::Request->new(GET=>'https://some_json_endpoint');
36 return $self->queue_request($request,$cb);
37 }
38  
39  
40 Blocking Example
41  
42 # blocking context
43 use My::Rest::Class;
44  
45 my $self=new My::Rest::Class;
46 my $result=$self->some_request;
47 die $result unless $result;
48  
49  
50 NonBlocking Example
51  
52 # non blocking
53 use AnyEvent::Loop;
54 use My::Rest::Class;
55  
56 my $self=new My::Rest::Class;
57 my $id=$self->some_request(sub {
58 my ($self,$id,$result,$request,$response)=@_;
59 });
60  
61 $obj->agent->run_next;
62 AnyEvent::Loop::run;
63  
64 =head1 DESCRIPTION
65  
66 In the real world we are often confronted with a situation of needing and or wanting blocking and non-blocking code, but we normally only have time to develop one or the other. This class provided an AnyEvent friendly framework that solves some of the issues involved in creating both with 1 code base.
67  
68 The solution presented by this module is to simply develop the non blocking interface and dynamically AUTOLOAD the blocking interface as needed. One of the major advantages of this model of coding is it becomes possible to create asyncronous calls in what looks like syncronous code.
69  
70 More documentation comming soon.. time permitting.
71  
72 =cut
73  
74 2 our %MULTIGET_ARRGS=(
75 timeout=>300,
76 max_que_count=>20,
77 );
78  
79 2 our $VERSION=$HTTP::MultiGet::VERSION;
80  
81 =head1 OO Declarations
82  
83 This section documents the Object Declarations. ALl of these arguments are optional and autogenerated on demand if not passed into the constructor.
84  
85 agnet: AnyEvent::HTTP::MultiGet object
86 json: JSON object
87  
88 Run Time State Settings ( modify at your own risk!! )
89  
90 is_blocking: Boolean ( denotes if we are in a blocking context or not )
91 block_for_more: array ref of additoinal ids to block for in a blocking context
92 pending: hash ref that outbound request objects
93 result_map: hash ref that contains the inbound result objects
94 jobs: anonymous hash, used to keep our results that never hit IO
95  
96 Success Range for parsing json
97  
98 As of version 1.017 a range of codes can now be set to validate if the response should be parsed as json
99  
100 code_parse_start: 199 # if the response code is greater than
101 code_parse_end: 300 # if the response code is less than
102  
103 =cut
104  
105 has agent=>(
106 is=>'ro',
107 isa=>Object,
108 required=>1,
109 2 ▶ default=>sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
110 2 new AnyEvent::HTTP::MultiGet(%MULTIGET_ARRGS)
111 },
112 2 lazy=>1,
113 );
114  
115  
116 has jobs=>(
117 is=>'ro',
118 0 ▶ default=>sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
119 2 lazy=>1,
120 );
121  
122 2 has is_blocking=>(
123 is=>'rw',
124 isa=>Bool,
125 default=>0,
126 lazy=>1,
127 );
128  
129 has block_for_more=>(
130 is=>'rw',
131 isa=>ArrayRef,
132 2 ▶ default=>sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
133 2 lazy=>1,
134 );
135  
136 has json =>(
137 is=>'ro',
138 isa=>Object,
139 required=>1,
140 lazy=>1,
141 2 ▶ default=>sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
142 2 my $json=JSON->new->allow_nonref(&JSON::true)->utf8->relaxed(&JSON::true);
143 2 return $json;
144 },
145 2 );
146  
147 has pending=>(
148 is=>'ro',
149 isa=>HashRef,
150 required=>1,
151 1 ▶ default=>sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
152 2 lazy=>1,
153 );
154  
155 has result_map=>(
156 is=>'ro',
157 isa=>HashRef,
158 required=>1,
159 2 ▶ default=>sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
160 2 lazy=>1,
161 );
162  
163 2 has code_parse_start=>(
164 is=>'rw',
165 isa=>Int,
166 default=>199
167 );
168  
169 2 has code_parse_end=>(
170 is=>'rw',
171 isa=>Int,
172 default=>300
173 );
174  
175 =head1 OO Methods
176  
177 =over 4
178  
179 =item * my $result=$self->new_true({qw( some data )});
180  
181 Returns a new true Data::Result object.
182  
183 =item * my $result=$self->new_false("why this failed")
184  
185 Returns a new false Data::Result object
186  
187 =item * my $code=$self->cb;
188  
189 Internal object used to construct the global callback used for all http responses. You may need to overload this method in your own class.
190  
191 =cut
192  
193 2 ▶sub cb {
Subroutine cb: called CC=2 SCAR=7.1
Pod documented
194 2 my ($self)=@_;
195 2 ▶ return $self->{cb} if exists $self->{cb};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"cb"} 1 1 2
196 2 ▶ my $code=sub {
Subroutine __ANON__: called CC=3 SCAR=11.8
197 2 my ($mg,$ref,$response)=@_;
198 2 ▶ my $request=is_plain_arrayref($ref) ? $ref->[0] : $ref;
Branch: 1 branch
BranchTrueFalseTotal
is_plain_arrayref($ref) ? : 0 2 2
199 2 ▶ unless(exists $self->pending->{$request}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->pending->{$request}) 0 2 2
200  
201 0 $self->log_error("Request wasn't found!");
202 0 croak "Request Object wasn't found!";
203 }
204 2 my ($id,$cb)=@{delete $self->pending->{$request}};
205 2 my $result=$self->parse_response($request,$response);
206 2 $cb->($self,$id,$result,$request,$response);
207 1 };
208 1 $self->{cb}=$code;
209 1 return $code;
210 }
211  
212 =item * my $result=$self->parse_response($request,$response);
213  
214 Returns a Data::Result object, if true it contains the parsed result object, if false it contains why it failed. If you are doing anything other than parsing json on a 200 to 299 response you will need to overload this method.
215  
216 =cut
217  
218 309 ▶sub parse_response {
Subroutine parse_response: called CC=7 SCAR=20.7
Pod documented
219 309 my ($self,$request,$response)=@_;
220  
221 309 my $content=$response->decoded_content;
222 309 ▶ $content='' unless defined($content);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 309 309
223 309 ▶ if($response->code >$self->code_parse_start && $response->code <$self->code_parse_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code > $self->code_parse_start and $response->code < $self->code_parse_end) { } 301 8 309
Condition100%3/3
$response->code > $self->code_parse_start and $response->code < $self->code_parse_end
!ll&&!rl&&r
17301
MC/DC100%2/2
$response->code > $self->code_parse_start and $response->code < $self->code_parse_end
$response->code > $self->code_parse_start $response->code < $self->code_parse_end
Truth table100%3/3
A and B
A= $response->code > $self->code_parse_startB= $response->code < $self->code_parse_end
ABresult
0X0
100
111
224 301 ▶ if(length($content)!=0 and $content=~ /^\s*[\[\{\"]/s) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $content != 0 and $content =~ /^\s*[\[\{\"]/su) { } 301 0 301
Condition33%1/3
length $content != 0 and $content =~ /^\s*[\[\{\"]/su
!ll&&!rl&&r
00301
MC/DC0%0/2
length $content != 0 and $content =~ /^\s*[\[\{\"]/su
length $content != 0 $content =~ /^\s*[\[\{\"]/su
Truth table33%1/3
A and B
A= length $content != 0B= $content =~ /^\s*[\[\{\"]/su
ABresult
0X0
100
111
225 301 my $data=eval {$self->json->decode($content)};
226 301 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 301 301
227 0 return $self->new_false("Code: [".$response->code."] JSON Decode error [$@] Content: $content");
228 } else {
229 301 return $self->new_true($data);
230 }
231 } else {
232 0 return $self->new_true($content,$response);
233 }
234 } else {
235 8 return $self->new_false("Code: [".$response->code."] http error [".$response->status_line."] Content: $content");
236 }
237 }
238  
239 =item * my $id=$self->queue_request($request,$cb|undef);
240  
241 Returns an Id for the qued request. If $cb is undef then the default internal blocking callback is used.
242  
243 =cut
244  
245 2 ▶sub queue_request {
Subroutine queue_request: called CC=3 SCAR=11.2
Pod documented
246 2 my ($self,$request,$cb)=@_;
247 2 ▶ $cb=$self->get_block_cb unless defined($cb);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cb 0 2 2
248 2 my $id=$self->agent->add_cb($request,$self->cb);
249 2 ▶ my $req=is_plain_arrayref($request) ? $request->[0] : $request;
Branch: 1 branch
BranchTrueFalseTotal
is_plain_arrayref($request) ? : 0 2 2
250 2 $self->pending->{$req}=[$id,$cb];
251 2 return $id;
252 }
253  
254 =item * my $id=$self->queue_result($cb,$result);
255  
256 Alows for result objects to look like they were placed in the the job que but wern't.
257  
258 Call back example
259  
260 sub {
261 my ($self,$id,$result,undef,undef)=@_;
262 # 0 Current object class
263 # 1 fake_id
264 # 2 Data::Result Object ( passed into $self->queue_result )
265 # 3 undef
266 # 4 undef
267 };
268  
269 =cut
270  
271 4 ▶sub queue_result {
Subroutine queue_result: called CC=3 SCAR=11.0
Pod documented
272 4 my ($self,$cb,$result)=@_;
273 4 ▶ $cb=\&block_cb unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 1 3 4
274 4 ▶ $result=$self->new_false("unknown error") unless defined($result);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 4 4
275 4 my $id;
276 4 ▶ $id=$self->agent->add_result(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
277 4 $cb->($self,$id,$result,undef,undef);
278 4 });
279 }
280  
281 0 ▶sub has_fake_jobs {
Subroutine has_fake_jobs: not called CC=1 SCAR=6.9
Pod undocumented
282 0 return $_[0]->agent->has_fake_jobs;
283 }
284  
285 =item * my $results=$self->block_on_ids(@ids);
286  
287 Scalar context returns an array ref.
288  
289 =item * my @results=$self->block_on_ids(@ids);
290  
291 Returns a list of array refrences.
292  
293 Each List refrence contains the follwing
294  
295 0: Data::Result
296 1: HTTP::Request
297 2: HTTP::Result
298  
299 Example
300  
301 my @results=$self->block_on_ids(@ids);
302 foreach my $set (@results) {
303 my ($result,$request,$response)=@{$set};
304 if($result)
305 ...
306 } else {
307 ...
308 }
309 }
310  
311 =cut
312  
313 6 ▶sub block_on_ids {
Subroutine block_on_ids: called CC=3 SCAR=13.4
Pod documented
314 6 my ($self,@ids)=@_;
315 6 my @init=@ids;
316  
317 6 $self->agent->block_for_results_by_id(@ids);
318 6 my $ref={};
319  
320 6 ▶ while($#{$self->block_for_more}!=-1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$self->block_for_more;} != -1) 0 6 6
321 0 @ids=@{$self->block_for_more};
322 0 @{$self->block_for_more}=();
323 0 $self->agent->run_next;
324 0 $self->agent->block_for_results_by_id(@ids);
325 }
326  
327 6 my $results=[delete @{$self->result_map}{@init}];
328 0 ▶ return wantarray ? @{$results} : $results;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 6 6
329 }
330  
331 =item * $self->add_ids_for_blocking(@ids);
332  
333 This method solves the chicken and the egg senerio when a calback generates other callbacks. In a non blocking context this is fine, but in a blocking context there are 2 things to keep in mind: 1. The jobs created by running the inital request didn't exist when the id was created. 2. The outter most callback id must always be used when processing the final callback or things get wierd.
334  
335 The example here is a litteral copy paste from L<Net::AppDynamics::REST>
336  
337 sub que_walk_all {
338 my ($self,$cb)=@_;
339  
340 my $state=1;
341 my $data={};
342 my $total=0;
343 my @ids;
344  
345 my $app_cb=sub {
346 my ($self,$id,$result,$request,$response)=@_;
347  
348 if($result) {
349 foreach my $obj (@{$result->get_data}) {
350 $data->{ids}->{$obj->{id}}=$obj;
351 $obj->{our_type}='applications';
352 $data->{applications}->{$obj->{name}}=[] unless exists $data->{applications}->{$obj->{name}};
353 push @{$data->{applications}->{$obj->{name}}},$obj->{id};
354 foreach my $method (qw(que_list_nodes que_list_tiers que_list_business_transactions)) {
355 ++$total;
356 my $code=sub {
357 my ($self,undef,$result,$request,$response)=@_;
358 return unless $state;
359 return ($cb->($self,$id,$result,$request,$response,$method,$obj),$state=0) unless $result;
360 --$total;
361 foreach my $sub_obj (@{$result->get_data}) {
362 my $target=$method;
363 $target=~ s/^que_list_//;
364  
365 foreach my $field (qw(name machineName)) {
366 next unless exists $sub_obj->{$field};
367 my $name=uc($sub_obj->{$field});
368 $data->{$target}->{$name}=[] unless exists $data->{$target}->{$name};
369 push @{$data->{$target}->{$name}},$sub_obj->{id};
370 }
371 $sub_obj->{ApplicationId}=$obj->{id};
372 $sub_obj->{ApplicationName}=$obj->{name};
373 $sub_obj->{our_type}=$target;
374 $data->{ids}->{$sub_obj->{id}}=$sub_obj;
375 }
376  
377 if($total==0) {
378 return ($cb->($self,$id,$self->new_true($data),$request,$response,'que_walk_all',$obj),$state=0)
379 }
380 };
381 push @ids,$self->$method($code,$obj->{id});
382 }
383 }
384 } else {
385 return $cb->($self,$id,$result,$request,$response,'que_list_applications',undef);
386 }
387 $self->add_ids_for_blocking(@ids);
388 };
389  
390 return $self->que_list_applications($app_cb);
391 }
392  
393 =cut
394  
395 0 ▶sub add_ids_for_blocking {
Subroutine add_ids_for_blocking: not called CC=2 SCAR=17.9
Pod documented
396 0 my ($self,@ids)=@_;
397 0 ▶ return unless $self->is_blocking;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_blocking 0 0 0
398 0 push @{$self->block_for_more},@ids;
399 }
400  
401 =item * my $code=$self->block_cb($id,$result,$request,$response);
402  
403 For internal use Default callback method used for all que_ methods.
404  
405 =cut
406  
407 6 ▶sub block_cb {
Subroutine block_cb: called CC=1 SCAR=0.0
Pod documented
408 6 my ($self,$id,$result,$request,$response)=@_;
409 6 $self->result_map->{$id}=[$result,$request,$response];
410 }
411  
412 =item * my $cb=$self->get_block_cb
413  
414 For Internal use, Returns the default blocking callback: \&block_cbblock_cb
415  
416 =cut
417  
418 5 ▶sub get_block_cb {
Subroutine get_block_cb: called CC=1 SCAR=0.0
Pod documented
419 5 return \&block_cb;
420 }
421  
422 =back
423  
424 =head1 Non-Blocking Interfaces
425  
426 Every Non-Blocking method has a contrasting blocking method that does not accept a code refrence. All of the blocking interfaces are auto generated using AUTOLOAD. This section documents the non blocking interfaces.
427  
428 All Non Blocking methods provide the following arguments to the callback.
429  
430 my $code=sub {
431 my ($self,$id,$result,$request,$response)=@_;
432 if($result) {
433 print Dumper($result->get_data);
434 } else {
435 warn $result;
436 }
437 }
438  
439 $self->que_xxx($code,$sql);
440  
441 The code refrence $code will be calld when the HTTP::Response has been recived.
442  
443 Callback variables
444  
445 $self
446 This Net::AppDynamics::REST Object
447 $id
448 The Job ID ( used internally )
449 $result
450 A Data::Result Object, when true it contains the results, when false it contains why things failed
451 $request
452 HTTP::Requst Object that was sent to SolarWinds to make this request
453 $response
454 HTTP::Result Object that represents the response from SolarWinds
455  
456 =head1 Blocking Interfaces
457  
458 All Blocking interfaces are generated with the AUTOLOAD method. Each method that begins with que_xxx can be calld in a blocking method.
459  
460 Example:
461  
462 # my $id=$self->que_list_applications(sub {});
463  
464 # can called as a blocking method will simply return the Data::Result object
465 my $result=$self->list_applications;
466  
467 =cut
468  
469 5 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=11.1
470 5 my ($self,@args)=@_;
471  
472 5 AnyEvent->now_update;
473 5 my $method=$AUTOLOAD;
474 5 $method=~ s/^.*:://s;
475 5 ▶ return if $method eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 0 5 5
476  
477 5 $self->is_blocking(1);
478 5 my $que_method="que_$method";
479 5 ▶ unless($self->can($que_method)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can($que_method)) 0 5 5
480 0 croak "Undefined subroutine $method";
481 }
482  
483 5 my @ids=$self->$que_method($self->get_block_cb,@args);
484 5 $self->agent->run_next;
485 5 my $result=$self->block_on_ids(@ids)->[0]->[0];
486  
487 5 $self->is_blocking(0);
488 5 return $result;
489 }
490  
491 22 ▶sub can {
Subroutine can: called CC=3 SCAR=11.0
Pod undocumented
492 22 my ($self,$method)=@_;
493 22 my $sub=$self->SUPER::can($method);
494  
495 22 ▶ return $sub if $sub;
Branch: 1 branch
BranchTrueFalseTotal
if $sub 18 4 22
496  
497 4 my $que_method="que_$method";
498 4 ▶ return undef unless $self->SUPER::can($que_method);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->SUPER::can($que_method) 3 1 4
499  
500 1 ▶ $sub=sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
501 1 $AUTOLOAD=$method;
502 1 $self->AUTOLOAD(@_);
503 1 };
504  
505 1 return $sub;
506 }
507  
508 1 ▶sub DEMOLISH { }
Subroutine DEMOLISH: called CC=1 SCAR=0.0
Pod undocumented
509  
510 =head1 See Also
511  
512 L<https://docs.appdynamics.com/display/PRO43/AppDynamics+APIs>
513  
514 L<AnyEvent::HTTP::MultiGet>
515  
516 =head1 AUTHOR
517  
518 Michael Shipper L<mailto:AKALINUX@CPAN.ORG>
519  
520 =cut
521  
522 2 1;
« HTTP/MultiGet.pm ↑ Summary AnyEvent/HTTP/MultiGet.pm »