blib/lib/Net/Fastly/Client.pm

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