ArangoDB2/HTTP/LWP.pm

filter: Statementstmt 34.4% 30 / 87 Branchbran 2.6% 1 / 38 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 50.0% 8 / 16 Podpod 100% 9 / 9 totaltotal 27.2% 48 / 176 CC 26 SCAR 37.6
CC
26
Cov
22%
response
132.0
put
20.0
post
20.0
CRAP 43.1
↑ Summary ArangoDB2/Base.pm »
1 package ArangoDB2::HTTP::LWP;
2  
3 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 22 ▶use base qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
7 ArangoDB2::HTTP
8 22 );
9  
10 22 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 22 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 22 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 22 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16  
17 22 my $JSON = JSON::XS->new->utf8;
18  
19  
20  
21 sub new
22 1 ▶{
Subroutine new: called CC=2 SCAR=7.1
Pod documented
23 1 my($class, $arango) = @_;
24 # we do not want to hold this reference if the
25 # parent goes out of scope
26 1 weaken $arango;
27 # create new instance
28 1 my $self = {
29 arango => $arango,
30 };
31 1 bless($self, $class);
32  
33 1 $self->{lwp} = LWP::UserAgent->new(
34 keep_alive => 1
35 );
36  
37 # set authentication is username is specified
38 1 ▶ if ($self->arango->username) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->arango->username) 0 1 1
39 0 $self->lwp->default_headers->authorization_basic(
40 $self->arango->username,
41 $self->arango->password,
42 );
43 }
44  
45 1 return $self;
46 }
47  
48 # delete
49 #
50 # make a DELETE request using the ArangoDB API uri along with
51 # the path and any args passed
52 sub delete
53 0 ▶{
Subroutine delete: not called CC=3 SCAR=24.8
Pod documented
54 0 my($self, $path, $args, $raw) = @_;
55 # get copy of ArangoDB API URI
56 0 my $uri = $self->arango->uri->clone;
57 # set path for request
58 0 $uri->path($path);
59 # set query params on URI if passed
60 0 ▶ $uri->query_form($args) if $args;
Branch: 1 branch
BranchTrueFalseTotal
if $args 0 0 0
61 # make request
62 0 my $response = $self->lwp->delete($uri);
63 # do not process response if raw requested
64 0 ▶ return $response if $raw;
Branch: 1 branch
BranchTrueFalseTotal
if $raw 0 0 0
65 # process response
66 0 return $self->response($response);
67 }
68  
69 # get
70 #
71 # make a GET request using the ArangoDB API uri along with
72 # the path and any args passed
73 sub get
74 0 ▶{
Subroutine get: not called CC=3 SCAR=24.8
Pod documented
75 0 my($self, $path, $args, $raw) = @_;
76 # get copy of ArangoDB API URI
77 0 my $uri = $self->arango->uri->clone;
78 # set path for request
79 0 $uri->path($path);
80 # set query params on URI if passed
81 0 ▶ $uri->query_form($args) if $args;
Branch: 1 branch
BranchTrueFalseTotal
if $args 0 0 0
82 # make request
83 0 my $response = $self->lwp->get($uri);
84 # do not process response if raw requested
85 0 ▶ return $response if $raw;
Branch: 1 branch
BranchTrueFalseTotal
if $raw 0 0 0
86 # process response
87 0 return $self->response($response);
88 }
89  
90 # head
91 #
92 # make a HEAD request using the ArangoDB API uri along with
93 # the path and any args passed
94 sub head
95 0 ▶{
Subroutine head: not called CC=3 SCAR=24.8
Pod documented
96 0 my($self, $path, $args, $raw) = @_;
97 # get copy of ArangoDB API URI
98 0 my $uri = $self->arango->uri->clone;
99 # set path for request
100 0 $uri->path($path);
101 # set query params on URI if passed
102 0 ▶ $uri->query_form($args) if $args;
Branch: 1 branch
BranchTrueFalseTotal
if $args 0 0 0
103 # make request
104 0 my $response = $self->lwp->head($uri);
105 # do not process response if raw requested
106 0 ▶ return $response if $raw;
Branch: 1 branch
BranchTrueFalseTotal
if $raw 0 0 0
107 # return code
108 0 return $response->code;
109 }
110  
111 # lwp
112 #
113 # LWP::UserAgent instance
114 0 ▶sub lwp { $_[0]->{lwp} }
Subroutine lwp: not called CC=1 SCAR=6.9
Pod documented
115  
116 # patch
117 #
118 # make a PATCH request using the ArangoDB API uri along with
119 # the path and any args passed
120 sub patch
121 0 ▶{
Subroutine patch: not called CC=3 SCAR=24.8
Pod documented
122 0 my($self, $path, $args, $patch, $raw) = @_;
123 # get copy of ArangoDB API URI
124 0 my $uri = $self->arango->uri->clone;
125 # set path for request
126 0 $uri->path($path);
127 # set query params on URI if passed
128 0 ▶ $uri->query_form($args) if $args;
Branch: 1 branch
BranchTrueFalseTotal
if $args 0 0 0
129 # build HTTP::Request
130 0 my $request = HTTP::Request->new('PATCH', $uri);
131 0 $request->content($patch);
132 # make request
133 0 my $response = $self->lwp->request($request);
134 # do not process response if raw requested
135 0 ▶ return $response if $raw;
Branch: 1 branch
BranchTrueFalseTotal
if $raw 0 0 0
136 # process response
137 0 return $self->response($response);
138 }
139  
140 # put
141 #
142 # make a PUT request using the ArangoDB API uri along with
143 # the path and any args passed
144 sub put
145 0 ▶{
Subroutine put: not called CC=4 SCAR=30.0
Pod documented
146 0 my($self, $path, $args, $put, $raw) = @_;
147 # get copy of ArangoDB API URI
148 0 my $uri = $self->arango->uri->clone;
149 # set path for request
150 0 $uri->path($path);
151 # set query params on URI if passed
152 0 ▶ $uri->query_form($args) if $args;
Branch: 1 branch
BranchTrueFalseTotal
if $args 0 0 0
153 # make request
154 0 ▶ my $response = ref $put
Branch: 1 branch
BranchTrueFalseTotal
ref $put ? : 0 0 0
155 # if put is hashref then treat as key/value pairs
156 # to be form encoded
157 ? $self->lwp->put($uri, $put)
158 # if put is string then put raw data
159 : $self->lwp->put($uri, Content => $put);
160 # do not process response if raw requested
161 0 ▶ return $response if $raw;
Branch: 1 branch
BranchTrueFalseTotal
if $raw 0 0 0
162 # process response
163 0 return $self->response($response);
164 }
165  
166 # post
167 #
168 # make a POST request using the ArangoDB API uri along with
169 # the path and any args passed
170 sub post
171 0 ▶{
Subroutine post: not called CC=4 SCAR=30.0
Pod documented
172 0 my($self, $path, $args, $post, $raw) = @_;
173 # get copy of ArangoDB API URI
174 0 my $uri = $self->arango->uri->clone;
175 # set path for request
176 0 $uri->path($path);
177 # set query params on URI if passed
178 0 ▶ $uri->query_form($args) if $args;
Branch: 1 branch
BranchTrueFalseTotal
if $args 0 0 0
179 # make request
180 0 ▶ my $response = ref $post
Branch: 1 branch
BranchTrueFalseTotal
ref $post ? : 0 0 0
181 # if post is hashref then treat as key/value pairs
182 # to be form encoded
183 ? $self->lwp->post($uri, $post)
184 # if post is string then post raw data
185 : $self->lwp->post($uri, Content => $post);
186 # do not process response if raw requested
187 0 ▶ return $response if $raw;
Branch: 1 branch
BranchTrueFalseTotal
if $raw 0 0 0
188 # process response
189 0 return $self->response($response);
190 }
191  
192 # response
193 #
194 # process LWP::UserAgent response
195 sub response
196 0 ▶{
Subroutine response: not called CC=11 SCAR=48.8
Pod documented
197 0 my($self, $response) = @_;
198  
199 0 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
200 0 my $res = eval { $JSON->decode($response->content) };
201 # if content is not valid JSON then return entire content
202 0 ▶ return $response->content unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
203 # res may be array in rare cases
204 0 ▶ if (!(ref $res eq 'HASH')) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $res eq 'HASH') { } 0 0 0
elsif ($res->{'result'} || $res->{'graph'} || $res->{'graphs'} and not $res->{'error'} and not defined $res->{'hasMore'}) { } 0 0 0
205 0 return $res;
206 }
207 # if there is a result object and no error and this is not a
208 # cursor result then only return the result object
209 ▶ elsif ( ($res->{result} || $res->{graph} || $res->{graphs})
Condition0%0/12
$res->{"result"} || $res->{"graph"} || $res->{"graphs"} and not $res->{"error"} and not defined $res->{"hasMore"}
!ll&&!rl&&r
000
$res->{"result"} || $res->{"graph"} || $res->{"graphs"} and not $res->{"error"}
!ll&&!rl&&r
000
$res->{"result"} or $res->{"graph"} or $res->{"graphs"}
l!l&&r!l&&!r
000
$res->{"result"} or $res->{"graph"}
l!l&&r!l&&!r
000
MC/DC0%0/5
$res->{"result"} || $res->{"graph"} || $res->{"graphs"} and not $res->{"error"} and not defined $res->{"hasMore"}
$res->{"result"} $res->{"graph"} $res->{"graphs"} not $res->{"error"} not defined $res->{"hasMore"}
Truth table0%0/10
A or B or C and not(D) and not(E)
A= $res->{"result"}B= $res->{"graph"}C= $res->{"graphs"}D= not $res->{"error"}E= not defined $res->{"hasMore"}
ABCDEresult
000XX0
1XX0X0
01X0X0
0010X0
1XX100
01X100
001100
1XX111
01X111
001111
210 && !$res->{error} && !defined $res->{hasMore} )
211 {
212 0 ▶ return $res->{result} || $res->{graph} || $res->{graphs};
Condition0%0/6
$res->{'result'} || $res->{'graph'} || $res->{'graphs'}
l!l&&r!l&&!r
000
$res->{"result"} or $res->{"graph"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$res->{'result'} || $res->{'graph'} || $res->{'graphs'}
$res->{"result"} $res->{"graph"} $res->{'graphs'}
Truth table0%0/4
A or B || C
A= $res->{"result"}B= $res->{"graph"}C= $res->{'graphs'}
ABCresult
1XX1
01X1
0011
0000
213 }
214 # otherwise return entire response
215 else {
216 0 return $res;
217 }
218 }
219 else {
220 # set error code
221 0 $self->error($response->code);
222 0 return;
223 }
224 }
225  
226 22 1;
227  
228 __END__
↑ Summary ArangoDB2/Base.pm »