File Coverage

lib/Wing/Client.pm
Criterion Covered Total %
statement 51 62 82.2
branch 6 10 60.0
condition n/a
subroutine 15 17 88.2
pod 4 4 100.0
total 76 93 81.7


line stmt bran cond sub pod time code
1 2     2   674 use strict;
  2         2  
  2         46  
2 2     2   5 use warnings;
  2         3  
  2         67  
3             package Wing::Client;
4             $Wing::Client::VERSION = '1.0000';
5 2     2   7 use HTTP::Thin;
  2         5  
  2         39  
6 2     2   815 use HTTP::Request::Common;
  2         5184  
  2         101  
7 2     2   752 use HTTP::CookieJar;
  2         38356  
  2         69  
8 2     2   14 use JSON;
  2         3  
  2         14  
9 2     2   223 use URI;
  2         3  
  2         34  
10 2     2   6 use Ouch;
  2         2  
  2         106  
11 2     2   1012 use Moo;
  2         18200  
  2         8  
12              
13              
14             =head1 NAME
15              
16             Wing::Client - A simple client to Wing's web services.
17              
18             =head1 VERSION
19              
20             version 1.0000
21              
22             =head1 SYNOPSIS
23              
24             use Wing::Client;
25              
26             my $wing = Wing::Client->new(uri => 'https://www.thegamecrafter.com');
27              
28             my $game = $wing->get('game/528F18A2-F2C4-11E1-991D-40A48889CD00');
29            
30             my $session = $wing->post('session', { username => 'me', password => '123qwe', api_key_id => 'abcdefghijklmnopqrztuz' });
31              
32             $game = $wing->put('game/528F18A2-F2C4-11E1-991D-40A48889CD00', { session_id => $session->{id}, name => 'Lacuna Expanse' });
33              
34             my $status = $wing->delete('game/528F18A2-F2C4-11E1-991D-40A48889CD00', { session_id => $session->{id} });
35              
36             =head1 DESCRIPTION
37              
38             A light-weight wrapper for Wing's (L) RESTful API (an example of which can be found at: L). This wrapper basically hides the request cycle from you so that you can get down to the business of using the API. It doesn't attempt to manage the data structures or objects the web service interfaces with.
39              
40             =head1 METHODS
41              
42             The following methods are available.
43              
44             =head2 new ( params )
45              
46             Constructor.
47              
48             =over
49              
50             =item params
51              
52             A hash of parameters.
53              
54             =over
55              
56             =item uri
57              
58             The base URI of the service you're interacting with. Example: C.
59              
60             =cut
61              
62             has uri => (
63             is => 'rw',
64             required => 1,
65             );
66              
67             =item agent
68              
69             A LWP::UserAgent object used to keep a persistent cookie_jar across requests.
70              
71             =back
72              
73             =back
74              
75             =cut
76              
77             has agent => (
78             is => 'ro',
79             required => 0,
80             lazy => 1,
81             builder => '_build_agent',
82             );
83              
84             sub _build_agent {
85 2     2   791 return HTTP::Thin->new( cookie_jar => HTTP::CookieJar->new() )
86             }
87              
88             =head2 get(path, params)
89              
90             Performs a C request, which is used for reading data from the service.
91              
92             =over
93              
94             =item path
95              
96             The path to the REST interface you wish to call. You can abbreviate and leave off the C part if you wish.
97              
98             =item params
99              
100             A hash reference of parameters you wish to pass to the web service.
101              
102             =back
103              
104             =cut
105              
106             sub get {
107 3     3 1 85872 my ($self, $path, $params) = @_;
108 3         12 my $uri = $self->_create_uri($path);
109 3         12236 $uri->query_form($params);
110 3         203 return $self->_process_request( GET $uri );
111             }
112              
113             =head2 delete(path, params)
114              
115             Performs a C request, deleting data from the service.
116              
117             =over
118              
119             =item path
120              
121             The path to the REST interface you wish to call. You can abbreviate and leave off the C part if you wish.
122              
123             =item params
124              
125             A hash reference of parameters you wish to pass to the web service.
126              
127             =back
128              
129             =cut
130              
131             sub delete {
132 0     0 1 0 my ($self, $path, $params) = @_;
133 0         0 my $uri = $self->_create_uri($path);
134 0         0 return $self->_process_request(POST $uri->as_string, $params, 'X-HTTP-Method' => 'DELETE', Content_Type => 'form-data', Content => $params );
135             }
136              
137             =head2 put(path, params, options)
138              
139             Performs a C request, which is used for updating data in the service.
140              
141             =over
142              
143             =item path
144              
145             The path to the REST interface you wish to call. You can abbreviate and leave off the C part if you wish.
146              
147             =item params
148              
149             A hash reference of parameters you wish to pass to the web service.
150              
151             =item options
152              
153             =over
154              
155             =item upload
156              
157             Defaults to 0. If 1 then when you pass a param that is an array reference, the value of that array reference will be assumed to be a file name and will attempt to be uploaded per the inner workings of L.
158              
159             =back
160              
161             =back
162              
163             =cut
164              
165             sub put {
166 1     1 1 1984 my ($self, $path, $params, $options) = @_;
167 1         3 my $uri = $self->_create_uri($path);
168 1         47 my %headers = ( 'X-HTTP-Method' => 'PUT',Content => $params );
169 1 50       3 if ($options->{upload}) {
170 0         0 $headers{Content_Type} = 'form-data';
171             }
172 1         8 return $self->_process_request( POST $uri->as_string, %headers);
173             }
174              
175             =head2 post(path, params, options)
176              
177             Performs a C request, which is used for creating data in the service.
178              
179             =over
180              
181             =item path
182              
183             The path to the REST interface you wish to call. You can abbreviate and leave off the C part if you wish.
184              
185             =item params
186              
187             A hash reference of parameters you wish to pass to the web service.
188              
189             =item options
190              
191             =over
192              
193             =item upload
194              
195             Defaults to 0. If 1 then when you pass a param that is an array reference, the value of that array reference will be assumed to be a file name and will attempt to be uploaded per the inner workings of L.
196              
197             =back
198              
199             =back
200              
201             =cut
202              
203             sub post {
204 0     0 1 0 my ($self, $path, $params, $options) = @_;
205 0         0 my $uri = $self->_create_uri($path);
206 0         0 my %headers = ( Content => $params );
207 0 0       0 if ($options->{upload}) {
208 0         0 $headers{Content_Type} = 'form-data';
209             }
210 0         0 return $self->_process_request( POST $uri->as_string, %headers );
211             }
212              
213             sub _create_uri {
214 4     4   9 my $self = shift;
215 4         6 my $path = shift;
216 4 100       17 unless ($path =~ m/^\/api/) {
217 3         7 $path = '/api/'.$path;
218             }
219 4         50 return URI->new($self->uri.$path);
220             }
221              
222             sub _process_request {
223 4     4   715 my $self = shift;
224 4         55 $self->_process_response($self->agent->request( @_ ));
225             }
226              
227             sub _process_response {
228 5     5   3780 my $self = shift;
229 5         7 my $response = shift;
230 5         5 my $result = eval { from_json($response->decoded_content) };
  5         26  
231 5 100       3606 if ($@) {
    50          
232 4         15 ouch 500, 'Server returned unparsable content.', { error => $@, content => $response->decoded_content };
233             }
234             elsif ($response->is_success) {
235 1         10 return $result->{result};
236             }
237             else {
238 0           ouch $result->{error}{code}, $result->{error}{message}, $result->{error}{data};
239             }
240             }
241              
242             =head1 PREREQS
243              
244             L
245             L
246             L
247             L
248             L
249             L
250             L
251              
252             =head1 SUPPORT
253              
254             =over
255              
256             =item Repository
257              
258             L
259              
260             =item Bug Reports
261              
262             L
263              
264             =back
265              
266             =head1 AUTHOR
267              
268             JT Smith
269              
270             =head1 LEGAL
271              
272             This module is Copyright 2013 Plain Black Corporation. It is distributed under the same terms as Perl itself.
273              
274             =cut
275              
276             1;