Client/Request.pm

filter: Statementstmt 95.3% 103 / 108 Branchbran 90.6% 29 / 32 Conditioncond 88.2% 15 / 17 MC/DCmcdc 77.7% 7 / 9 Subroutinesub 100% 13 / 13 Podpod 100% 7 / 7 totaltotal 93.5% 174 / 186 CC 31 SCAR 18.4
CC
31
Cov
94%
parse_endpoint
9.1
endpoint
9.0
new
8.0
CRAP 6.3
« Client/Auth.pm ↑ Summary Client/Identity/Tagstore.pm »
1 package Net::OpenStack::Client::Request;
2 11 $Net::OpenStack::Client::Request::VERSION = '0.1.4';
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 11 Readonly our @SUPPORTED_METHODS => qw(DELETE GET PATCH POST PUT);
10 11 Readonly our @METHODS_REQUIRE_OPTIONS => qw(PATCH POST PUT);
11  
12 11 our @EXPORT = qw(mkrequest);
13 11 our @EXPORT_OK = qw(parse_endpoint @SUPPORTED_METHODS @METHODS_REQUIRE_OPTIONS $HDR_X_AUTH_TOKEN);
14  
15 11 ▶use overload bool => '_boolean';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 11 Readonly our $HDR_ACCEPT => 'Accept';
18 11 Readonly our $HDR_ACCEPT_ENCODING => 'Accept-Encoding';
19 11 Readonly our $HDR_CONTENT_TYPE => 'Content-Type';
20 11 Readonly our $HDR_X_AUTH_TOKEN => 'X-Auth-Token';
21 11 Readonly our $HDR_X_SUBJECT_TOKEN => 'X-Subject-Token';
22  
23  
24 11 Readonly my %DEFAULT_HEADERS => {
25 $HDR_ACCEPT => 'application/json, text/plain',
26 $HDR_ACCEPT_ENCODING => 'identity, gzip, deflate, compress',
27 $HDR_CONTENT_TYPE => 'application/json',
28 };
29  
30  
31 =head1 NAME
32  
33 Net::OpenStack::Client::Request is an request class for Net::OpenStack.
34  
35 Boolean logic is overloaded using C<_boolean> method (as inverse of C<is_error>).
36  
37 =head2 Public functions
38  
39 =over
40  
41 =item mkrequest
42  
43 A C<Net::OpenStack::Client::Request> factory
44  
45 =cut
46  
47 sub mkrequest
48 69 ▶{
Subroutine mkrequest: called CC=1 SCAR=0.0
Pod documented
49 69 return Net::OpenStack::Client::Request->new(@_);
50 }
51  
52 =item parse_endpoint
53  
54 Parse C<endpoint> and look for templates and parameters.
55  
56 Return (possibly modified) endpoint, arrayref of template names
57 and arrayref of parameter names.
58  
59 If C<logger> is passed, report an error and return; else die on failure.
60  
61 =cut
62  
63 sub parse_endpoint
64 65 ▶{
Subroutine parse_endpoint: called CC=7 SCAR=22.0
Pod documented
65 65 my ($origendpoint, $logger) = @_;
66  
67 # strip parameters
68 65 my ($endpoint, $paramtxt) = $origendpoint =~ /^([^?]+)(?:\?([^?]+))?$/;
69  
70 # List of key names that have to be passed
71 65 my (@templates, @params);
72 65 foreach my $template ($endpoint =~ m#\{([^/]+)}#g) {
73 # only add once; order is not that relevant
74 52 ▶ push(@templates, $template) if (!grep {$_ eq $template} @templates);
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $template;} @templates 45 7 52
75 };
76  
77 65 ▶ foreach my $kv (split(/&/, $paramtxt || '')) {
Condition100%2/2
$paramtxt || ''
l!l
3926
MC/DC100%1/1
$paramtxt || ''
$paramtxt
Truth table100%2/2
A
A= $paramtxt
Aresult
11
00
78 68 ▶ if ($kv =~ m/^([^=]+)=/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kv =~ /^([^=]+)=/) { } 68 0 68
79 68 push(@params, $1);
80 } else {
81 # invalid
82 0 my $msg = "invalid parameter kv $kv for origendpoint $origendpoint";
83 0 ▶ if ($logger) {
Branch: 1 branch
BranchTrueFalseTotal
if ($logger) { } 0 0 0
84 0 $logger->error($msg);
85 0 return;
86 } else {
87 0 die $msg;
88 }
89 }
90 }
91  
92 65 return $endpoint, \@templates, \@params;
93 }
94  
95  
96 =pod
97  
98 =back
99  
100 =head2 Public methods
101  
102 =over
103  
104 =item new
105  
106 Create new request instance from options for command C<endpoint>
107 and REST HTTP C<method>.
108  
109 The C<endpoint> is the URL to use (can be templated with C<tpls>)
110  
111 Options
112  
113 =over
114  
115 =item tpls: template names and values
116  
117 =item opts: optional arguments
118  
119 =item error: an error (no default)
120  
121 =item service: service name
122  
123 =item version: service version
124  
125 =item result: result path for the response
126  
127 =back
128  
129 =cut
130  
131 sub new
132 71 ▶{
Subroutine new: called CC=8 SCAR=20.8
Pod documented
133 71 my ($this, $endpoint, $method, %opts) = @_;
134 71 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
0710
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
135 my $self = {
136 endpoint => $endpoint,
137  
138 tpls => $opts{tpls} || {},
139 params => $opts{params} || {},
140 opts => $opts{opts} || {},
141 paths => $opts{paths} || {},
142 raw => $opts{raw},
143  
144 rest => $opts{rest} || {}, # options for rest
145  
146 error => $opts{error}, # no default
147  
148 service => $opts{service},
149 version => $opts{version},
150 result => $opts{result},
151 71 ▶ };
Condition100%10/10
$opts{'tpls'} || {}
l!l
3437
$opts{'params'} || {}
l!l
4625
$opts{'opts'} || {}
l!l
3338
$opts{'paths'} || {}
l!l
3338
$opts{'rest'} || {}
l!l
3338
MC/DC100%1/1
$opts{'tpls'} || {}
$opts{'tpls'}
MC/DC100%1/1
$opts{'params'} || {}
$opts{'params'}
MC/DC100%1/1
$opts{'opts'} || {}
$opts{'opts'}
MC/DC100%1/1
$opts{'paths'} || {}
$opts{'paths'}
MC/DC100%1/1
$opts{'rest'} || {}
$opts{'rest'}
Truth table100%2/2
A
A= $opts{'tpls'}
Aresult
11
00
Truth table100%2/2
A
A= $opts{'params'}
Aresult
11
00
Truth table100%2/2
A
A= $opts{'opts'}
Aresult
11
00
Truth table100%2/2
A
A= $opts{'paths'}
Aresult
11
00
Truth table100%2/2
A
A= $opts{'rest'}
Aresult
11
00
152  
153 71 ▶ if (grep {$method eq $_} @SUPPORTED_METHODS) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$method eq $_;} @Net::OpenStack::Client::Request::SUPPORTED_METHODS) { } 70 1 71
154 70 $self->{method} = $method;
155 } else {
156 1 $self->{error} = "Unsupported method $method";
157 }
158  
159 71 bless $self, $class;
160  
161 71 return $self;
162 };
163  
164 =item endpoint
165  
166 Parses the endpoint attribute, look for any templates, and replace them with values
167 from C<tpls> attribute hashref.
168 Any parameters defined in the endpoint are removed, and only those that
169 are present in the C<params> attribute are readded with the values from the attribute.
170  
171 The data can contain more keys than what is required
172 for templating, those keys and their values will be ignored.
173  
174 This does not modify the endpoint attribute.
175  
176 Return templated endpoint on success or undef on failure.
177  
178 If host is defined, try to make a full URL
179  
180 =over
181  
182 =item if you provide only fqdn, make a https://<fqdn>/v<version/<endpoint>
183  
184 =item if you provide URL, check for version suffix, return <url>/<endpoint>
185  
186 =back
187  
188 =cut
189  
190  
191 sub endpoint
192 62 ▶{
Subroutine endpoint: called CC=9 SCAR=22.0
Pod documented
193 62 my ($self, $host) = @_;
194  
195 # reset error attribute
196 62 $self->{error} = undef;
197  
198 62 my ($endpoint, $templates, $params) = parse_endpoint($self->{endpoint});
199 62 foreach my $template (@$templates) {
200 41 my $pattern = '\{' . $template . '\}';
201 41 ▶ if (exists($self->{tpls}->{$template})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'tpls'}{$template}) { } 40 1 41
202 40 $endpoint =~ s#$pattern#$self->{tpls}->{$template}#g;
203 } else {
204 1 $self->{error} = "Missing template $template data to replace endpoint $self->{endpoint}";
205 1 return;
206 }
207 }
208  
209 61 my @fparams;
210 61 foreach my $param (sort @$params) {
211 63 ▶ if (exists($self->{params}->{$param})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"params"}{$param}) 26 37 63
212 26 push(@fparams, "$param=".$self->{params}->{$param});
213 }
214 }
215  
216 61 ▶ if (@fparams) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fparams) 22 39 61
217 22 $endpoint =~ s/\/+$//;
218 22 $endpoint .= '?'.join('&', @fparams);
219 }
220  
221 61 ▶ if ($host) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host) 54 7 61
222 54 my $url = $host;
223  
224 54 my $version_suffix = "v$self->{version}";
225 54 $version_suffix =~ s/^v+/v/;
226  
227 54 ▶ if ($host !~ m/^http/) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $host =~ /^http/) { } 1 53 54
elsif (not $host =~ m[/v[\d.]+/?$]) { } 1 52 53
228 1 $url = "https://$url/$version_suffix";
229 } elsif ($host !~ m#/v[\d.]+/?$#) {
230 1 $url .= "/$version_suffix";
231 }
232  
233 54 $url =~ s#/+$##;
234 54 $endpoint =~ s#^/+##;
235 54 $endpoint = "$url/$endpoint";
236 }
237  
238 61 return $endpoint;
239 }
240  
241 =item opts_data
242  
243 Generate hashref from options, to be used for JSON encoding.
244 If C<raw> attribute is defined, ignore all options and return it.
245  
246 Returns empty hasref, even if no options existed.
247  
248 =cut
249  
250 sub opts_data
251 29 ▶{
Subroutine opts_data: called CC=5 SCAR=16.1
Pod documented
252 29 my ($self) = @_;
253  
254 29 my $root;
255  
256 29 ▶ if ($self->{raw}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'raw'}) { } 11 18 29
257 # ignore all options passed
258 11 $root = $self->{raw};
259 } else {
260 18 $root = {};
261 18 foreach my $key (sort keys %{$self->{opts}}) {
262 26 my @paths = @{$self->{paths}->{$key}};
263 26 my $lastpath = pop(@paths);
264 26 my $here = $root;
265 26 foreach my $path (@paths) {
266 # build tree
267 74 ▶ $here->{$path} = {} if !exists($here->{$path});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $here->{$path} 28 46 74
268 74 $here = $here->{$path};
269 }
270 # no intermediate variable with value
271 26 $here->{$lastpath} = $self->{opts}->{$key};
272 }
273 }
274  
275 29 return $root;
276 }
277  
278 =item headers
279  
280 Return headers for the request.
281  
282 Supported options:
283  
284 =over
285  
286 =item token: authentication token stored in X-Auth-Token
287  
288 =item headers: hashref with headers to add that take precedence over the defaults.
289 Headers with an undef value will be removed.
290  
291 =back
292  
293 =cut
294  
295 sub headers
296 56 ▶{
Subroutine headers: called CC=5 SCAR=16.1
Pod documented
297 56 my ($self, %opts) = @_;
298  
299 56 my $headers = {%DEFAULT_HEADERS};
300  
301 56 ▶ while (my ($hdr, $value) = each %{$opts{headers} || {}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$opts{"headers"} or {};}) 2 56 58
Condition100%2/2
$opts{'headers'} || {}
l!l
355
MC/DC100%1/1
$opts{'headers'} || {}
$opts{'headers'}
Truth table100%2/2
A
A= $opts{'headers'}
Aresult
11
00
302 2 ▶ if (defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 1 1 2
303 1 $headers->{$hdr} = $value;
304 } else {
305 1 delete $headers->{$hdr};
306 }
307 }
308  
309 56 ▶ $headers->{$HDR_X_AUTH_TOKEN} = $opts{token} if defined $opts{token};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"token"} 1 55 56
310  
311 56 return $headers;
312 }
313  
314  
315 =item is_error
316  
317 Test if this is an error or not (based on error attribute).
318  
319 =cut
320  
321 sub is_error
322 14 ▶{
Subroutine is_error: called CC=2 SCAR=6.9
Pod documented
323 14 my $self = shift;
324 14 ▶ return $self->{error} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'error'} ? : 8 6 14
325 }
326  
327 # Overloaded boolean, inverse of is_error
328 sub _boolean
329 11 ▶{
Subroutine _boolean: called CC=1 SCAR=0.0
330 11 my $self = shift;
331 11 return ! $self->is_error();
332 }
333  
334 =pod
335  
336 =back
337  
338 =cut
339  
340 11 1;
« Client/Auth.pm ↑ Summary Client/Identity/Tagstore.pm »