blib/lib/Net/Fastly/Client.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/Net/Fastly.pm ↑ Summary blib/lib/Net/Fastly/Backend.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::Any;
Subroutine BEGIN: called CC=1 SCAR=2.6
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 my $class = shift;
62 my %opts = @_;
63 my $self = bless \%opts, $class;
64
65 my $base = $opts{base_url} ||= "api.fastly.com";
66 my $port = $opts{base_port} ||= 80;
67 $self->{user} ||= $self->{username};
68 $self->{_json} = JSON::Any->new;
69 $self->{_ua} = Net::Fastly::Client::UserAgent->new($base, $port, $opts{proxy});
70 return $self unless $self->fully_authed;
71  
72 # If we're fully authed (i.e username and password ) then we need to log in
73 my $res = $self->_ua->_post('/login', {}, user => $self->{user}, password => $self->{password});
74 unless ($res->is_success) {
75 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/;
76 die "Unauthorized" unless $res->is_success;
77 }
78 my $content = $self->_json->from_json($res->decoded_content);
79 $self->{_cookie} = $res->header('set-cookie');
80 return wantarray ? ($self, $content->{user}, $content->{customer}) : $self;
81 }
82  
83 sub _ua { shift->{_ua} }
84 sub _json { shift->{_json} }
85  
86 =head2 authed
87  
88 Whether or not we're authed at all by either username & password or API key
89  
90 =cut
91 sub authed {
92 my $self = shift;
93 defined $self->{api_key} || $self->fully_authed;
94 }
95  
96 =head2 fully_authed
97  
98 Whether or not we're fully (username and password) authed
99  
100 =cut
101 sub fully_authed {
102 my $self = shift;
103 defined $self->{user} && defined $self->{password};
104 }
105  
106 =head2 set_customer <customer id>
107  
108 Set the current customer to act as.
109  
110 B<NOTE>: this will only work if you're an admin
111  
112 =cut
113 sub set_customer {
114 my $self = shift;
115 my $id = shift;
116 $self->{explicit_customer} = $id;
117 }
118  
119  
120 # Get stuff from the stats API
121 sub _get_stats {
122 my $self = shift;
123 my $content = $self->_get(@_);
124 die $content->{msg} unless $content->{status} eq 'success';
125 return $content->{data};
126 }
127  
128 sub _get {
129 my $self = shift;
130 my $path = shift;
131 my %opts = @_;
132 my $res = $self->_ua->_get($path, $self->_headers, %opts);
133 return undef if 404 == $res->code;
134 $self->_raise_error($res) unless $res->is_success;
135 my $content = $self->_json->from_json($res->decoded_content);
136 return $content;
137 }
138  
139 sub _post {
140 my $self = shift;
141 my $path = shift;
142 my %params = @_;
143
144 my $res = $self->_ua->_post($path, $self->_headers, %params);
145 $self->_raise_error($res) unless $res->is_success;
146 my $content = $self->_json->from_json($res->decoded_content);
147 return $content;
148 }
149  
150 sub _put {
151 my $self = shift;
152 my $path = shift;
153 my %params = @_;
154
155 my $res = $self->_ua->_put($path, $self->_headers, %params);
156 $self->_raise_error($res) unless $res->is_success;
157 my $content = $self->_json->from_json($res->decoded_content);
158 return $content;
159 }
160  
161 sub _delete {
162 my $self = shift;
163 my $path = shift;
164 my $res = $self->_ua->_delete($path, $self->_headers);
165 $self->_raise_error($res) unless $res->is_success;
166 return 1;
167 }
168  
169 sub _headers {
170 my $self = shift;
171 my $params = $self->fully_authed ? { 'Cookie' => $self->{_cookie} } : { 'X-Fastly-Key' => $self->{api_key} };
172 $params->{'Fastly-Explicit-Customer'} = $self->{explicit_customer} if defined $self->{explicit_customer};
173 $params->{'Content-Accept'} = 'application/json';
174 return $params;
175 }
176  
177 sub _raise_error {
178 my $self = shift;
179 my $res = shift;
180  
181 my $content = eval { $self->_json->from_json($res->decoded_content) };
182 my $message = $content ? $content->{detail} || $content->{msg} : $res->status_line." ".$res->decoded_content;
183 die "$message\n";
184 }
185  
186  
187 package Net::Fastly::Client::UserAgent;
188  
189 use strict;
190 use URI;
191 use LWP::UserAgent;
192 BEGIN { # Compatibility fix for older versions of HTTP::Request::Common
193 require HTTP::Request::Common;
194 if (HTTP::Request::Common->can('DELETE')) {
195 HTTP::Request::Common->import(qw(GET HEAD PUT POST DELETE));
196 } elsif (my $_simple_req = HTTP::Request::Common->can('_simple_req')) {
197 HTTP::Request::Common->import(qw(GET HEAD PUT POST));
198 *DELETE = sub { $_simple_req->('DELETE', @_) };
199 } else {
200 die << 'END'
201 "DELETE" is not exported by the HTTP::Request::Common module
202 and its underlying _simple_req() method is not available.
203 END
204 }
205 }
206  
207 sub new {
208 my $class = shift;
209 my $base = shift;
210 my $port = shift;
211 my $proxy = shift;
212 my $ua = Net::Fastly::UA->new;
213 if ($proxy) {
214 $ua->proxy('https', $proxy);
215 } else {
216 $ua->env_proxy;
217 }
218 return bless { _base => $base, _port => $port, _ua => $ua }, $class;
219
220 }
221  
222 sub _ua { shift->{_ua} }
223  
224 sub _get {
225 my $self = shift;
226 my $path = shift;
227 my $headers = shift;
228 my %params = @_;
229 my $url = $self->_make_url($path, %params);
230 return $self->_ua->request(GET $url, %$headers);
231 }
232  
233 sub _post {
234 my $self = shift;
235 my $path = shift;
236 my $headers = shift;
237 my %params = @_;
238 my $url = $self->_make_url($path);
239 return $self->_ua->request(POST $url, [_make_params(%params)], %$headers);
240 }
241  
242 sub _put {
243 my $self = shift;
244 my $path = shift;
245 my $headers = shift;
246 my %params = @_;
247 $headers->{content_type} = "application/x-www-form-urlencoded";
248 my $url = $self->_make_url($path);
249 my $uri = URI->new('http');
250 $uri->query_form(_make_params(%params));
251 return $self->_ua->request(PUT $url, %$headers, Content => $uri->query || "");
252 }
253  
254 sub _delete {
255 my $self = shift;
256 my $path = shift;
257 my $headers = shift;
258 my %params = @_;
259 my $url = $self->_make_url($path, %params);
260 return $self->_ua->request(DELETE $url, %$headers);
261 }
262  
263 sub _make_url {
264 my $self = shift;
265 my $base = $self->{_base};
266 my $port = $self->{_port};
267 my $path = shift;
268 my %params = @_;
269  
270 my $prot = "https:";
271 if ($base =~ s!^(https?:)//!!) {
272 $prot = $1;
273 }
274 my $url = URI->new($prot);
275 $url->host($base);
276 $url->port($port) if $port != 80;
277 $url->path($path);
278 $url->query_form(_make_params(%params)) if keys %params;
279 return $url;
280 }
281  
282 sub _make_params {
283 my %in = @_;
284 my %out;
285
286 foreach my $key (keys %in) {
287 my $value = $in{$key};
288 next unless defined $value;
289 unless (ref($value) eq 'HASH') {
290 $out{$key} = $value;
291 next;
292 }
293 foreach my $sub_key (keys %$value) {
294 $out{$key."[".$sub_key."]"} = $value->{$sub_key};
295 }
296 }
297 return %out;
298 }
299  
300 package Net::Fastly::UA;
301  
302 use base qw(LWP::UserAgent);
303 use LWP::Protocol::https;
304 our $DEBUG=0;
305  
306 sub request {
307 my $self = shift;
308 my $req = shift;
309 print $req->as_string."\n------------------------\n\n" if $DEBUG;
310 my $res = $self->SUPER::request($req);
311 print $res->as_string."\n------------------------\n\n\n\n\n" if $DEBUG;
312 return $res;
313 }
314 1;
« blib/lib/Net/Fastly.pm ↑ Summary blib/lib/Net/Fastly/Backend.pm »