Client/REST.pm

filter: Statementstmt 92.8% 129 / 139 Branchbran 66.6% 32 / 48 Conditioncond 64.2% 18 / 28 MC/DCmcdc 33.3% 5 / 15 Subroutinesub 100% 13 / 13 Podpod 100% 1 / 1 totaltotal 81.1% 198 / 244 CC 40 SCAR 24.5
CC
40
Cov
83%
rest
16.4
_call
16.2
_page_paths
11.2
CRAP 11.6
« Client/API/Magic.pm ↑ Summary Client/Identity/v3.pm »
1 package Net::OpenStack::Client::REST;
2 7 $Net::OpenStack::Client::REST::VERSION = '0.1.4';
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use Net::OpenStack::Client::Request qw(@METHODS_REQUIRE_OPTIONS $HDR_X_AUTH_TOKEN);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Net::OpenStack::Client::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use REST::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # Map with HTTP return codes indicating success
15 # if method is missing (only) 200 is conisdered success
16 # if method is present, 200 is not considered success by default
17 7 Readonly my %SUCCESS => {
18 POST => [201],
19 PUT => [200, 201, 204],
20 DELETE => [204, 201], # yes, 201 when deleting a token
21 };
22  
23  
24 # JSON::XS instance
25 # sort the keys, to create reproducable results
26 7 my $json = JSON::XS->new()->canonical(1);
27  
28 =head1 methods
29  
30 =over
31  
32 =cut
33  
34 sub _new_client
35 6 ▶{
Subroutine _new_client: called CC=1 SCAR=0.0
36 6 my ($self) = @_;
37  
38 6 my $browser = LWP::UserAgent->new();
39 # Temporary cookie_jar
40 6 $browser->cookie_jar( {} );
41  
42 6 my $rc = REST::Client->new(
43 useragent => $browser,
44 );
45  
46 6 $self->{rc} = $rc;
47 }
48  
49 # Actual REST::Client call
50 # Returns tuple repsonse, repsonse headers and error message.
51 # Processes the repsonse code, including possible JSON decoding
52 # Reports error and returns err (with repsonse undef)
53 sub _call
54 54 ▶{
Subroutine _call: called CC=13 SCAR=27.8
55 54 my ($self, $method, $url, @args) = @_;
56  
57 54 my $err;
58 54 my $rc = $self->{rc};
59  
60 # make the call
61 54 $rc->$method($url, @args);
62  
63 54 my $code = $rc->responseCode();
64 54 my $content = $rc->responseContent();
65 54 my $rheaders = {map {$_ => $rc->responseHeader($_)} $rc->responseHeaders};
66 54 ▶ my $success = grep {$code == $_} @{$SUCCESS{$method} || [200]};
Condition100%2/2
$SUCCESS{$method} || [200]
l!l
2826
MC/DC100%1/1
$SUCCESS{$method} || [200]
$SUCCESS{$method}
Truth table100%2/2
A
A= $SUCCESS{$method}
Aresult
11
00
67  
68 54 my $response;
69 54 ▶ my $type = $rheaders->{'Content-Type'} || 'RESPONSE_WO_CONTENT_TYPE_HEADER';
Condition50%1/2
$rheaders->{'Content-Type'} || 'RESPONSE_WO_CONTENT_TYPE_HEADER'
l!l
540
MC/DC0%0/1
$rheaders->{'Content-Type'} || 'RESPONSE_WO_CONTENT_TYPE_HEADER'
$rheaders->{'Content-Type'}
Truth table50%1/2
A
A= $rheaders->{'Content-Type'}
Aresult
11
00
70 54 ▶ if ($type =~ qr{^application/json}i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ qr"^application/json"i) { } 54 0 54
71 54 local $@;
72 54 eval {
73 54 $response = $json->decode($content);
74 };
75 54 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 54 54
76 0 ▶ my $report = $success ? 'error' : 'verbose';
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
77 0 ▶ $self->$report("REST $method with ".($success ? 'success' : 'error').
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
78 " failure to decode JSON content $content: $@");
79 }
80 } else {
81 0 $response = $content;
82 }
83  
84 54 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) { } 53 1 54
85 53 $self->verbose("Successful REST $method url $url type $type");
86 53 ▶ if ($self->{debugapi}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debugapi"}) 53 0 53
87 # might contain sensitive data, eg security token
88 53 my $headers_txt = join(',', map {"$_=$rheaders->{$_}"} sort keys %$rheaders);
89 53 $self->debug("REST $method full response headers $headers_txt");
90 53 $self->debug("REST $method full response content $content");
91 }
92 } else {
93 1 my $errmsg = "$method failed (url $url code $code)";
94 1 ▶ if (ref($response) eq 'HASH' &&
Branch: 1 branch
BranchTrueFalseTotal
if (ref $response eq 'HASH' and $response->{'error'}) { } 1 0 1
Condition33%1/3
ref $response eq "HASH" and $response->{"error"}
!ll&&!rl&&r
001
MC/DC0%0/2
ref $response eq "HASH" and $response->{"error"}
ref $response eq "HASH" $response->{"error"}
Truth table33%1/3
A and B
A= ref $response eq "HASH"B= $response->{"error"}
ABresult
0X0
100
111
95 $response->{error}) {
96 1 $err = $response->{error};
97 1 $err->{url} = $url;
98 1 $err->{method} = $method;
99 1 ▶ $err->{code} = $code if !exists($err->{code});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $err->{"code"} 1 0 1
100 } else {
101 0 $err = $errmsg;
102 }
103  
104 1 ▶ $content = '<undef>' if ! defined($content);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 1 1
105 1 $errmsg = "$errmsg: $content";
106 1 $self->error("REST $errmsg");
107 }
108  
109 54 return $response, $rheaders, $err;
110 }
111  
112 # Handle pagination: https://developer.openstack.org/api-guide/compute/paginated_collections.html
113 # For any decoded reply, walk the tree
114 # look for <key>+<key>_links combos
115 # <key> should be a list (a collection)
116 # the <key>_links part should have a rel=next, href=newurl
117 # follow it, and merge the results with the original key list
118 # lets assume they are in the same relative path
119 # Aaaargh who came up with this crap
120  
121 # Return array of paths that have to processed for paging
122 # Each element is a tuple of path (as arrayref of subpaths)
123 # and url to follow
124 # Assumes that all responses can be retrieved from
125 # and be joined using the same path in JSON
126 # Only pass hashref as data.
127 # TODO: support lookup of links in arrays?
128 sub _page_paths
129 73 ▶{
Subroutine _page_paths: called CC=11 SCAR=24.2
130 73 my ($self, $data) = @_;
131  
132 73 my @paths;
133  
134 73 foreach my $key (sort keys %$data) {
135 88 my $lkey = $key."_links";
136 88 my $ref = ref($data->{$key});
137 88 ▶ if (exists($data->{$lkey}) &&
Branch: 2 branches
BranchTrueFalseTotal
if (exists $data->{$lkey} and $ref eq 'ARRAY' and ref $data->{$lkey} eq 'ARRAY') { } 6 82 88
elsif ($ref eq 'HASH') { } 18 64 82
Condition66%4/6
exists $data->{$lkey} and $ref eq "ARRAY" and ref $data->{$lkey} eq "ARRAY"
!ll&&!rl&&r
8206
exists $data->{$lkey} and $ref eq "ARRAY"
!ll&&!rl&&r
8206
MC/DC33%1/3
exists $data->{$lkey} and $ref eq "ARRAY" and ref $data->{$lkey} eq "ARRAY"
exists $data->{$lkey} $ref eq "ARRAY" ref $data->{$lkey} eq "ARRAY"
Truth table50%2/4
A and B and C
A= exists $data->{$lkey}B= $ref eq "ARRAY"C= ref $data->{$lkey} eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
138 $ref eq 'ARRAY' &&
139 ref($data->{$lkey}) eq 'ARRAY'
140 ) {
141 6 foreach my $link (@{$data->{$lkey}}) {
142 6 ▶ if (exists($link->{rel}) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $link->{"rel"} and $link->{"rel"} eq "next" and exists $link->{"href"}) 4 2 6
Condition66%4/6
exists $link->{"rel"} and $link->{"rel"} eq "next" and exists $link->{"href"}
!ll&&!rl&&r
204
exists $link->{"rel"} and $link->{"rel"} eq "next"
!ll&&!rl&&r
024
MC/DC33%1/3
exists $link->{"rel"} and $link->{"rel"} eq "next" and exists $link->{"href"}
exists $link->{"rel"} $link->{"rel"} eq "next" exists $link->{"href"}
Truth table50%2/4
A and B and C
A= exists $link->{"rel"}B= $link->{"rel"} eq "next"C= exists $link->{"href"}
ABCresult
0XX0
10X0
1100
1111
143 $link->{rel} eq 'next' &&
144 exists($link->{href})) {
145 # only first one
146 4 push(@paths, [[$key], $link->{href}]);
147 4 last;
148 }
149 }
150 } elsif ($ref eq 'HASH') {
151 18 foreach my $rpath_tuple ($self->_page_paths($data->{$key})) {
152 # add current key to path element (i.e. the path is relative to $key)
153 6 unshift(@{$rpath_tuple->[0]}, $key);
154 6 push(@paths, $rpath_tuple);
155 }
156 }
157 }
158  
159 73 return @paths;
160 }
161  
162 # only hashrefs
163 sub _page
164 54 ▶{
Subroutine _page: called CC=4 SCAR=14.0
165 54 my ($self, $method, $response, $headers) = @_;
166  
167 54 my $err;
168  
169 54 foreach my $path_tuple ($self->_page_paths($response)) {
170 # No body, this is GET only
171 # We only care about the response headers of the first batch
172 2 $self->debug("_page method $method url $path_tuple->[1]");
173 2 my ($tresponse, $trheaders, $terr) = $self->_call($method, $path_tuple->[1], $headers);
174 2 ▶ if ($terr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($terr) { } 0 2 2
175 # no temp err here, a failure in the paged GET repsonse is a failure nonetheless
176 0 $err = $terr;
177 0 last;
178 } else {
179 2 my @path = @{$path_tuple->[0]};
180 # extend path of tresponse in path of response
181 2 my $rarray = $response;
182 2 foreach my $p (@path) {
183 5 $rarray = $rarray->{$p};
184 5 $tresponse = $tresponse->{$p};
185 }
186 2 push(@$rarray, @$tresponse);
187 };
188 }
189  
190 54 return $response, $err;
191 }
192  
193 =item rest
194  
195 Given a Request instance C<req>, perform this request.
196 All options are passed to the headers method.
197 The token option is added if the token attribute exists and
198 if not token option was already in the options.
199  
200 =cut
201  
202 sub rest
203 54 ▶{
Subroutine rest: called CC=15 SCAR=27.9
Pod documented
204 54 my ($self, $req, %opts) = @_;
205  
206 # methods that require options, must pass said option as body
207 # general call is $rc->$method($url, [body if options], $headers)
208  
209 54 my $method = $req->{method};
210 54 my $rservice = $req->{service};
211 54 my $service;
212 54 ▶ if ($rservice) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rservice) { } 54 0 54
213 54 $service = $self->{services}->{$rservice};
214 54 ▶ if (!$service) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($service) 3 51 54
215 3 $self->debug("REST $method request endpoint $req->{endpoint} service $rservice has no known service");
216 }
217 } else {
218 0 $self->debug("REST $method request endpoint $req->{endpoint} has no service");
219 }
220  
221 # url
222 54 my $url = $req->endpoint($service);
223 54 ▶ if (!$url) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url) 0 54 54
224 0 ▶ my $msg = "REST $method request endpoint $req->{endpoint} ".
Branch: 1 branch
BranchTrueFalseTotal
$service ? : 0 0 0
225 ($service ? "service $service" : "no service")." has no endpoint url";
226 0 $self->error($msg);
227 0 return mkresponse(error => $msg);
228 }
229  
230 54 my @args = ($url);
231  
232 # body if needed
233 54 my $body;
234 54 ▶ if (grep {$method eq $_} @METHODS_REQUIRE_OPTIONS) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$method eq $_;} @Net::OpenStack::Client::REST::METHODS_REQUIRE_OPTIONS) 27 27 54
235 27 my $data = $req->opts_data;
236 27 $body = $json->encode($data);
237 27 push(@args, $body);
238 }
239  
240 # headers
241 54 ▶ $opts{token} = $self->{token} if (exists($self->{token}) && !exists $opts{token});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"token"} and not exists $opts{"token"} 48 6 54
Condition66%2/3
exists $self->{"token"} and not exists $opts{"token"}
!ll&&!rl&&r
6048
MC/DC50%1/2
exists $self->{"token"} and not exists $opts{"token"}
exists $self->{"token"} not exists $opts{"token"}
Truth table66%2/3
A and not(B)
A= exists $self->{"token"}B= not exists $opts{"token"}
ABresult
0X0
100
111
242 54 my $headers = $req->headers(%opts);
243 54 push(@args, $headers);
244  
245 54 ▶ $self->debug("REST $method url $url, ".(defined $body ? '' : 'no ')."body, headers ".join(',', sort keys %$headers));
Branch: 1 branch
BranchTrueFalseTotal
defined $body ? : 27 27 54
246 54 ▶ if ($self->{debugapi}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debugapi"}) 54 0 54
247 # might contain sensitive data, eg security token
248 54 my $headers_txt = join(',', map {"$_=$headers->{$_}"} sort keys %$headers);
249 54 $self->debug("REST $method full headers $headers_txt");
250 54 ▶ $self->debug("REST $method full body $body") if $body;
Branch: 1 branch
BranchTrueFalseTotal
if $body 27 27 54
251 }
252  
253 54 my ($response, $rheaders, $err) = $self->_call($method, @args);
254 # The err here could be a failure in the paged GET repsonse
255 54 ▶ ($response, $err) = $self->_page($method, $response, $headers) if (!$err && $response && ref($response) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if not $err and $response and ref $response eq "HASH" 53 1 54
Condition66%4/6
not $err and $response and ref $response eq "HASH"
!ll&&!rl&&r
1053
not $err and $response
!ll&&!rl&&r
1053
MC/DC33%1/3
not $err and $response and ref $response eq "HASH"
not $err $response ref $response eq "HASH"
Truth table50%2/4
not(A) and B and C
A= not $errB= $responseC= ref $response eq "HASH"
ABCresult
0XX0
10X0
1100
1111
256  
257 54 my %ropts = (
258 data => $response,
259 headers => $rheaders,
260 error => $err,
261 );
262 54 ▶ $ropts{result_path} = $req->{result} if defined $req->{result};
Branch: 1 branch
BranchTrueFalseTotal
if defined $req->{"result"} 27 27 54
263 54 return mkresponse(%ropts);
264 }
265  
266 =pod
267  
268 =back
269  
270 =cut
271  
272 7 1;
« Client/API/Magic.pm ↑ Summary Client/Identity/v3.pm »