File Coverage

lib/Wing/Client.pm
Criterion Covered Total %
statement 56 56 100.0
branch 6 6 100.0
condition n/a
subroutine 17 17 100.0
pod 4 4 100.0
total 83 83 100.0


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