Fastly/Client.pm

filter: Statementstmt 25.0% 50 / 200 Branchbran 6.6% 4 / 60 Conditioncond 12.0% 3 / 25 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 36.1% 13 / 36 Podpod 100% 6 / 6 totaltotal 22.2% 76 / 341 CC 44 SCAR 27.3
CC
44
Cov
20%
new
39.6
_headers
30.0
_make_params
30.0
CRAP 15.3
« Fastly.pm ↑ Summary Fastly/Invoice.pm »
1 package Net::Fastly::Client;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Carp qw( carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Net::Fastly::Client - communicate with the Fastly HTTP API
11  
12 =head1 SYNOPSIS
13  
14 =head1 PROXYING
15  
16 There are two ways to proxy:
17  
18 The first method is to pass a proxy option into the constructor
19  
20 my $client = Net::Fastly::Client->new(user => $username, password => $password, proxy => "http://localhost:8080");
21
22 The second is to set your C<https_proxy> environment variable. So, in Bash
23  
24 % export https_proxy=http://localhost:8080
25
26 or in CSH or TCSH
27  
28 % setenv https_proxy=http://localhost:8080
29  
30 =head1 METHODS
31  
32 =cut
33  
34  
35 =head2 new <opt[s]>
36  
37 Create a new Fastly user agent. Options are
38  
39 =over 4
40  
41 =item user
42  
43 The login to use
44  
45 =item password
46  
47 Your password
48  
49 =item api_key
50  
51 Alternatively use the API Key (only some commands are available)
52  
53 =item proxy
54  
55 Optionally pass in an https proxy to use.
56  
57 =back
58  
59  
60 =cut
61 1 ▶sub new {
Subroutine new: called CC=10 SCAR=36.8
Pod documented
62 1 my $class = shift;
63 1 my %opts = @_;
64 1 my $self = bless \%opts, $class;
65
66 1 ▶ my $base = $opts{base_url} ||= "api.fastly.com";
Condition50%1/2
$opts{"base_url"} ||= "api.fastly.com"
l!l
01
MC/DC0%0/1
$opts{"base_url"} ||= "api.fastly.com"
$opts{"base_url"}
Truth table50%1/2
A
A= $opts{"base_url"}
Aresult
11
00
67 1 ▶ my $port = $opts{base_port} ||= 80;
Condition50%1/2
$opts{"base_port"} ||= 80
l!l
01
MC/DC0%0/1
$opts{"base_port"} ||= 80
$opts{"base_port"}
Truth table50%1/2
A
A= $opts{"base_port"}
Aresult
11
00
68 1 ▶ $self->{user} ||= $self->{username};
Condition33%1/3
$self->{"user"} ||= $self->{"username"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->{"user"} ||= $self->{"username"}
$self->{"user"} $self->{"username"}
Truth table33%1/3
A ||= B
A= $self->{"user"}B= $self->{"username"}
ABresult
1X1
011
000
69 1 $self->{_ua} = Net::Fastly::Client::UserAgent->new($base, $port, $opts{proxy});
70 1 ▶ return $self unless $self->fully_authed;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->fully_authed 1 0 1
71  
72 # If we're fully authed (i.e username and password ) then we need to log in
73 0 my $res = $self->_ua->_post('/login', {}, user => $self->{user}, password => $self->{password});
74  
75 0 carp "DEPRECATION WARNING: Username/password authentication is deprecated and will not be available starting September 2020; please migrate to API tokens as soon as possible.";
76  
77 0 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
78 0 ▶ die "You must have IO::Socket::SSL or Crypt::SSLeay installed in order to do SSL requests\n" if $res->code == 501 && $res->status_line =~ /Protocol scheme 'https' is not supported/;
Branch: 1 branch
BranchTrueFalseTotal
if $res->code == 501 and $res->status_line =~ /Protocol scheme 'https' is not supported/ 0 0 0
Condition0%0/3
$res->code == 501 and $res->status_line =~ /Protocol scheme 'https' is not supported/
!ll&&!rl&&r
000
MC/DC0%0/2
$res->code == 501 and $res->status_line =~ /Protocol scheme 'https' is not supported/
$res->code == 501 $res->status_line =~ /Protocol scheme 'https' is not supported/
Truth table0%0/3
A and B
A= $res->code == 501B= $res->status_line =~ /Protocol scheme 'https' is not supported/
ABresult
0X0
100
111
79 0 ▶ die "Unauthorized" unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
80 }
81 0 my $content = decode_json($res->decoded_content);
82 0 $self->{_cookie} = $res->header('set-cookie');
83 0 ▶ return wantarray ? ($self, $content->{user}, $content->{customer}) : $self;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
84 }
85  
86 0 ▶sub _ua { shift->{_ua} }
Subroutine _ua: not called CC=1 SCAR=6.9
87  
88 =head2 authed
89  
90 Whether or not we're authed at all by either API key or username & password
91  
92 =cut
93 0 ▶sub authed {
Subroutine authed: not called CC=2 SCAR=17.9
Pod documented
94 0 my $self = shift;
95 0 ▶ $self->key_authed || $self->fully_authed;
Branch: 1 branch
BranchTrueFalseTotal
$self->key_authed or $self->fully_authed 0 0 0
96 }
97  
98 =head2 key_authed
99  
100 Whether or not we're authed by API key
101  
102 =cut
103 0 ▶sub key_authed {
Subroutine key_authed: not called CC=1 SCAR=6.9
Pod documented
104 0 my $self = shift;
105 defined $self->{api_key}
106 0 }
107  
108 =head2 fully_authed
109  
110 Whether or not we're authed by username & password
111  
112 =cut
113 1 ▶sub fully_authed {
Subroutine fully_authed: called CC=2 SCAR=7.2
Pod documented
114 1 my $self = shift;
115 1 ▶ defined $self->{user} && defined $self->{password};
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"user"} and defined $self->{"password"} 0 1 1
116 }
117  
118 =head2 set_customer <customer id>
119  
120 Set the current customer to act as.
121  
122 B<NOTE>: this will only work if you're an admin
123  
124 =cut
125 0 ▶sub set_customer {
Subroutine set_customer: not called CC=1 SCAR=6.9
Pod documented
126 0 my $self = shift;
127 0 my $id = shift;
128 0 $self->{explicit_customer} = $id;
129 }
130  
131 =head2 timeout <number>
132  
133 Get or set the timeout value in seconds. The default value is 180 seconds.
134  
135 =cut
136  
137 0 ▶sub timeout {
Subroutine timeout: not called CC=1 SCAR=6.9
Pod documented
138 0 my $self = shift;
139 0 $self->_ua->_ua->timeout(@_);
140 }
141  
142 # Get stuff from the stats API
143 0 ▶sub _get_stats {
Subroutine _get_stats: not called CC=2 SCAR=17.9
144 0 my $self = shift;
145 0 my $content = $self->_get(@_);
146 0 ▶ die $content->{msg} unless $content->{status} eq 'success';
Branch: 1 branch
BranchTrueFalseTotal
unless $content->{"status"} eq "success" 0 0 0
147 0 return $content->{data};
148 }
149  
150 0 ▶sub _get {
Subroutine _get: not called CC=4 SCAR=30.0
151 0 my $self = shift;
152 0 my $path = shift;
153 0 my %opts = @_;
154 0 ▶ my $headers = delete $opts{headers} || {};
Condition0%0/2
delete $opts{'headers'} || {}
l!l
00
MC/DC0%0/1
delete $opts{'headers'} || {}
delete $opts{'headers'}
Truth table0%0/2
A
A= delete $opts{'headers'}
Aresult
11
00
155  
156 0 my $res = $self->_ua->_get($path, $self->_headers($headers), %opts);
157 0 ▶ return undef if 404 == $res->code;
Branch: 1 branch
BranchTrueFalseTotal
if 404 == $res->code 0 0 0
158 0 ▶ $self->_raise_error($res) unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
159 0 my $content = decode_json($res->decoded_content);
160 0 return $content;
161 }
162  
163 0 ▶sub _post {
Subroutine _post: not called CC=3 SCAR=24.8
164 0 my $self = shift;
165 0 my $path = shift;
166 0 my %params = @_;
167 0 ▶ my $headers = delete $params{headers} || {};
Condition0%0/2
delete $params{'headers'} || {}
l!l
00
MC/DC0%0/1
delete $params{'headers'} || {}
delete $params{'headers'}
Truth table0%0/2
A
A= delete $params{'headers'}
Aresult
11
00
168  
169 0 my $res = $self->_ua->_post($path, $self->_headers($headers), %params);
170 0 ▶ $self->_raise_error($res) unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
171 0 my $content = decode_json($res->decoded_content);
172 0 return $content;
173 }
174  
175 0 ▶sub _purge {
Subroutine _purge: not called CC=4 SCAR=30.0
176 0 my $self = shift;
177 0 my $url = shift;
178 0 my %params = @_;
179 0 ▶ my $headers = delete $params{headers} || {};
Condition0%0/2
delete $params{'headers'} || {}
l!l
00
MC/DC0%0/1
delete $params{'headers'} || {}
delete $params{'headers'}
Truth table0%0/2
A
A= delete $params{'headers'}
Aresult
11
00
180  
181 0 my $method = "_purge";
182 0 ▶ if ($self->{use_old_purge_method}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"use_old_purge_method"}) 0 0 0
183 0 $method = "_post";
184 0 $url = "/purge/$url";
185 }
186  
187 0 my $res = $self->_ua->$method($url, $self->_headers($headers), %params);
188 0 ▶ $self->_raise_error($res) unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
189 0 my $content = decode_json($res->decoded_content);
190 0 return $content;
191 }
192  
193 0 ▶sub _put {
Subroutine _put: not called CC=3 SCAR=24.8
194 0 my $self = shift;
195 0 my $path = shift;
196 0 my %params = @_;
197 0 ▶ my $headers = delete $params{headers} || {};
Condition0%0/2
delete $params{'headers'} || {}
l!l
00
MC/DC0%0/1
delete $params{'headers'} || {}
delete $params{'headers'}
Truth table0%0/2
A
A= delete $params{'headers'}
Aresult
11
00
198  
199 0 my $res = $self->_ua->_put($path, $self->_headers($headers), %params);
200 0 ▶ $self->_raise_error($res) unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
201 0 my $content = decode_json($res->decoded_content);
202 0 return $content;
203 }
204  
205 0 ▶sub _delete {
Subroutine _delete: not called CC=3 SCAR=24.8
206 0 my $self = shift;
207 0 my $path = shift;
208 0 my %params = @_;
209 0 ▶ my $headers = delete $params{headers} || {};
Condition0%0/2
delete $params{'headers'} || {}
l!l
00
MC/DC0%0/1
delete $params{'headers'} || {}
delete $params{'headers'}
Truth table0%0/2
A
A= delete $params{'headers'}
Aresult
11
00
210  
211 0 my $res = $self->_ua->_delete($path, $self->_headers($headers));
212 0 ▶ $self->_raise_error($res) unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
213 0 return 1;
214 }
215  
216 0 ▶sub _headers {
Subroutine _headers: not called CC=5 SCAR=34.0
217 0 my $self = shift;
218 0 my $extras = shift;
219 0 ▶ my $params = $self->fully_authed ? { 'Cookie' => $self->{_cookie} } : { 'Fastly-Key' => $self->{api_key} };
Branch: 1 branch
BranchTrueFalseTotal
$self->fully_authed ? : 0 0 0
220 0 ▶ $params->{'Fastly-Explicit-Customer'} = $self->{explicit_customer} if defined $self->{explicit_customer};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"explicit_customer"} 0 0 0
221 0 $params->{'Content-Accept'} = 'application/json';
222 0 $params->{'User-Agent'} = "fastly-perl-v$Net::Fastly::VERSION";
223 0 ▶ while (my ($key, $value) = each %$extras) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$extras) 0 0 0
224 0 ▶ $params->{$key} = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
225 }
226 0 return $params;
227 }
228  
229 0 ▶sub _raise_error {
Subroutine _raise_error: not called CC=3 SCAR=24.8
230 0 my $self = shift;
231 0 my $res = shift;
232  
233 0 my $content = eval { decode_json($res->decoded_content) };
234 0 ▶ my $message = $content ? $content->{detail} || $content->{msg} : $res->status_line." ".$res->decoded_content;
Branch: 1 branch
BranchTrueFalseTotal
$content ? : 0 0 0
Condition0%0/3
$content->{"detail"} or $content->{"msg"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$content->{"detail"} or $content->{"msg"}
$content->{"detail"} $content->{"msg"}
Truth table0%0/3
A or B
A= $content->{"detail"}B= $content->{"msg"}
ABresult
1X1
011
000
235 0 die "$message\n";
236 }
237  
238  
239 package Net::Fastly::Client::UserAgent;
240  
241 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
242 5 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
243 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
244 5 ▶BEGIN { # Compatibility fix for older versions of HTTP::Request::Common
Subroutine BEGIN: called CC=3 SCAR=16.7
245 5 require HTTP::Request::Common;
246 5 ▶ if (HTTP::Request::Common->can('DELETE')) {
Branch: 2 branches
BranchTrueFalseTotal
if ('HTTP::Request::Common'->can('DELETE')) { } 5 0 5
elsif (my $_simple_req = 'HTTP::Request::Common'->can('_simple_req')) { } 0 0 0
247 5 HTTP::Request::Common->import(qw(GET HEAD PUT POST DELETE));
248 } elsif (my $_simple_req = HTTP::Request::Common->can('_simple_req')) {
249 0 HTTP::Request::Common->import(qw(GET HEAD PUT POST));
250 0 ▶ *DELETE = sub { $_simple_req->('DELETE', @_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
251 } else {
252 0 die << 'END'
253 "DELETE" is not exported by the HTTP::Request::Common module
254 and its underlying _simple_req() method is not available.
255 END
256 }
257 }
258  
259 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
260 1 my $class = shift;
261 1 my $base = shift;
262 1 my $port = shift;
263 1 my $proxy = shift;
264 1 my $ua = Net::Fastly::UA->new;
265 1 ▶ if ($proxy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proxy) { } 0 1 1
266 0 $ua->proxy('https', $proxy);
267 } else {
268 1 $ua->env_proxy;
269 }
270 1 return bless { _base => $base, _port => $port, _ua => $ua }, $class;
271
272 }
273  
274 0 ▶sub _ua { shift->{_ua} }
Subroutine _ua: not called CC=1 SCAR=6.9
275  
276 0 ▶sub _get {
Subroutine _get: not called CC=1 SCAR=6.9
277 0 my $self = shift;
278 0 my $path = shift;
279 0 my $headers = shift;
280 0 my %params = @_;
281 0 my $url = $self->_make_url($path, %params);
282 0 return $self->_ua->request(GET $url, %$headers);
283 }
284  
285 0 ▶sub _post {
Subroutine _post: not called CC=1 SCAR=6.9
286 0 my $self = shift;
287 0 my $path = shift;
288 0 my $headers = shift;
289 0 my %params = @_;
290 0 my $url = $self->_make_url($path);
291 0 return $self->_ua->request(POST $url, [_make_params(%params)], %$headers);
292 }
293  
294 0 ▶sub _purge {
Subroutine _purge: not called CC=1 SCAR=6.9
295 0 my $self = shift;
296 0 my $url = shift;
297 0 my $headers = shift;
298 0 my %params = @_;
299 0 return $self->_ua->request(HTTP::Request->new("PURGE", $url, [%$headers]));
300 }
301  
302 0 ▶sub _put {
Subroutine _put: not called CC=2 SCAR=17.9
303 0 my $self = shift;
304 0 my $path = shift;
305 0 my $headers = shift;
306 0 my %params = @_;
307 0 $headers->{content_type} = "application/x-www-form-urlencoded";
308 0 my $url = $self->_make_url($path);
309 0 my $uri = URI->new('http');
310 0 $uri->query_form(_make_params(%params));
311 0 ▶ return $self->_ua->request(PUT $url, %$headers, Content => $uri->query || "");
Condition0%0/2
$uri->query || ''
l!l
00
MC/DC0%0/1
$uri->query || ''
$uri->query
Truth table0%0/2
A
A= $uri->query
Aresult
11
00
312 }
313  
314 0 ▶sub _delete {
Subroutine _delete: not called CC=1 SCAR=6.9
315 0 my $self = shift;
316 0 my $path = shift;
317 0 my $headers = shift;
318 0 my %params = @_;
319 0 my $url = $self->_make_url($path, %params);
320 0 return $self->_ua->request(DELETE $url, %$headers);
321 }
322  
323 0 ▶sub _make_url {
Subroutine _make_url: not called CC=4 SCAR=30.0
324 0 my $self = shift;
325 0 my $base = $self->{_base};
326 0 my $port = $self->{_port};
327 0 my $path = shift;
328 0 my %params = @_;
329  
330 0 my $prot = "https:";
331 0 ▶ if ($base =~ s!^(https?:)//!!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base =~ s[^(https?:)//][]) 0 0 0
332 0 $prot = $1;
333 }
334 0 my $url = URI->new($prot);
335 0 $url->host($base);
336 0 ▶ $url->port($port) if $port != 80;
Branch: 1 branch
BranchTrueFalseTotal
if $port != 80 0 0 0
337 0 $url->path($path);
338 0 ▶ $url->query_form(_make_params(%params)) if keys %params;
Branch: 1 branch
BranchTrueFalseTotal
if keys %params 0 0 0
339 0 return $url;
340 }
341  
342 0 ▶sub _make_params {
Subroutine _make_params: not called CC=5 SCAR=34.0
343 0 my %in = @_;
344 0 my %out;
345
346 0 foreach my $key (keys %in) {
347 0 my $value = $in{$key};
348 0 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
349 0 ▶ unless (ref($value) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value eq "HASH") 0 0 0
350 0 $out{$key} = $value;
351 0 next;
352 }
353 0 foreach my $sub_key (keys %$value) {
354 0 $out{$key."[".$sub_key."]"} = $value->{$sub_key};
355 }
356 }
357 0 return %out;
358 }
359  
360 package Net::Fastly::UA;
361  
362 5 ▶use base qw(LWP::UserAgent);
Subroutine BEGIN: called CC=1 SCAR=0.0
363 5 ▶use LWP::Protocol::https;
Subroutine BEGIN: called CC=1 SCAR=0.0
364 5 our $DEBUG=0;
365  
366 0 ▶sub request {
Subroutine request: not called CC=3 SCAR=24.8
367 0 my $self = shift;
368 0 my $req = shift;
369 0 ▶ print $req->as_string."\n------------------------\n\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Fastly::UA::DEBUG 0 0 0
370 0 my $res = $self->SUPER::request($req);
371 0 ▶ print $res->as_string."\n------------------------\n\n\n\n\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Fastly::UA::DEBUG 0 0 0
372 0 return $res;
373 }
374 5 1;
« Fastly.pm ↑ Summary Fastly/Invoice.pm »