File Coverage

blib/lib/Blitz/API.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package Blitz::API;
2              
3 1     1   11 use strict;
  1         2  
  1         35  
4 1     1   6 use warnings;
  1         2  
  1         24  
5              
6 1     1   923 use LWP;
  1         60681  
  1         29  
7 1     1   9 use Blitz;
  1         1  
  1         21  
8 1     1   434 use JSON;
  0            
  0            
9             use MIME::Base64;
10              
11             =head1 NAME
12              
13             Blitz::API - Perl module for API access to Blitz
14              
15             =cut
16              
17             =head1 SUBROUTINES/METHODS
18              
19             =head2 client
20              
21             Create a blitz client object for executing sprints or rushes
22              
23             Required parameters are credentials (email address and api-key)
24              
25             See http://blitz.io for information regarding the api key.
26              
27             =cut
28              
29             sub client {
30             my $this = shift;
31             my $creds = shift;
32             my $self = {
33             credentials => $creds,
34             job_id => undef,
35             job_status => 'idle',
36             };
37             bless $self;
38             return $self;
39             }
40              
41             =head2 status
42              
43             status is a get/set method for the current status of a running exercise
44              
45             =cut
46              
47             sub status {
48             my $self = shift;
49             my $status = shift;
50             if ($status) {
51             $self->{job_status} = $status;
52             }
53            
54             return $self->{job_status};
55             }
56              
57             sub _make_url {
58             my $self = shift;
59             my $path = shift;
60             my $url = "http://" . $self->{credentials}{host}; # fix for https later?
61             $url .= ":$self->{credentials}{port}" if $self->{credentials}{port} != 80;
62             $url .= $path if $path;
63             return $url;
64             }
65              
66             sub _http_get {
67             my $self = shift;
68             my $path = shift;
69              
70             my $browser = LWP::UserAgent->new;
71             my $url = _make_url($self, $path);
72             my $response = $browser->get($url,
73             'X-API-User' => $self->{credentials}{username},
74             'X-API-Key' => $self->{credentials}{api_key},
75             'X-API-Client' => 'Perl',
76             );
77            
78             return $response;
79             }
80              
81             =head2 _decode_response
82              
83             Decodes the JSON result object from Blitz
84             Decodes base64 response in content areas of request and response
85              
86             =cut
87              
88             sub _decode_response {
89             my $self = shift;
90             my $response = shift;
91             my $return = {};
92             if ( $response->{_rc} != 200 ) {
93             $return->{error} = 'server';
94             $return->{cause} = $response->code();
95             }
96             else {
97             $return = decode_json($response->{_content});
98             for my $key ('request', 'response') {
99             if ($return->{result}) {
100             if ($return->{result}{$key} && $return->{result}{$key}{content}) {
101             $return->{result}{$key}{content} = decode_base64($return->{result}{$key}{content});
102             }
103             }
104             }
105             }
106              
107             return $return;
108             }
109              
110             =head2 login
111              
112             Sends the RESTful login request to blitz.io servers
113             When correctly executed, response contains a second api key that maintains
114             authentication permission for running tests.
115              
116             =cut
117              
118             sub login {
119             my $self = shift;
120             my $closure = shift;
121              
122             my $response = _http_get($self, '/login/api');
123             my $result = _decode_response($self, $response);
124              
125             if ($closure) {
126             &$closure($self, $result);
127             }
128             return $result;
129             }
130              
131             =head2 job_id
132              
133             Get/set method for the job_id attached to a given client object
134              
135             =cut
136              
137             sub job_id {
138             my $self = shift;
139             my $job_id = shift;
140             $self->{job_id} = $job_id if $job_id;
141             return $self->{job_id};
142             }
143              
144             =head2 job_status
145              
146             Sends RESTful request for job status of a given (known) job_id
147             Sets status of the job in the client object after response is returned
148              
149             =cut
150              
151             sub job_status {
152              
153             my $self = shift;
154             my $job_id = $self->job_id;
155             my $closure = $self->{callback};
156              
157             my $request = '/api/1/jobs/' . $job_id . '/status';
158             my $response = _http_get($self, $request);
159            
160             my $result = _decode_response($self, $response);
161              
162             $self->status($result->{status});
163            
164             if ($closure) {
165             &$closure($self, $result);
166             }
167              
168             return $result;
169             }
170              
171             =head2 start_job
172              
173             Executes an exercise (sprint or rush) on blitz.io
174              
175             When this method runs successfully, job_id is returned and
176             the progress of the job can be queried with job_status()
177              
178             =cut
179              
180             sub start_job {
181             my $self = shift;
182             my $data = shift;
183             my $closure = $self->{callback};
184            
185             my $json = encode_json($data);
186            
187             my $browser = LWP::UserAgent->new;
188             my $url = _make_url($self, '/api/1/curl/execute');
189              
190             my $response = $browser->post($url,
191             'X-API-User' => $self->{credentials}{username},
192             'X-API-Key' => $self->{credentials}{api_key},
193             'X-API-Client' => 'Perl',
194             'content-length' => length($json),
195             Content => $json,
196             );
197             my $result = _decode_response($self, $response);
198            
199             if ($closure) {
200             &$closure($self, $result);
201             }
202             return $result;
203             }
204              
205              
206             return 1;