line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::HTTP::API; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: Easily create client for net API |
4
|
|
|
|
|
|
|
|
5
|
3
|
|
|
3
|
|
84431
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use Moose::Exporter; |
7
|
|
|
|
|
|
|
use Moose::Util::MetaRole; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.11'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
Moose::Exporter->setup_import_methods( |
12
|
|
|
|
|
|
|
with_meta => [qw/net_api_method net_api_declare/], |
13
|
|
|
|
|
|
|
also => [qw/Moose/] |
14
|
|
|
|
|
|
|
); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub net_api_method { |
17
|
|
|
|
|
|
|
my $meta = shift; |
18
|
|
|
|
|
|
|
my $name = shift; |
19
|
|
|
|
|
|
|
$meta->add_net_api_method($name, @_); |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub net_api_declare { |
23
|
|
|
|
|
|
|
my $meta = shift; |
24
|
|
|
|
|
|
|
my $name = shift; |
25
|
|
|
|
|
|
|
$meta->add_net_api_declare($name, @_); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub init_meta { |
29
|
|
|
|
|
|
|
my ($class, %options) = @_; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
my $for = $options{for_class}; |
32
|
|
|
|
|
|
|
Moose->init_meta(%options); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
my $meta = Moose::Util::MetaRole::apply_metaroles( |
35
|
|
|
|
|
|
|
for => $for, |
36
|
|
|
|
|
|
|
class_metaroles => { |
37
|
|
|
|
|
|
|
class => ['Net::HTTP::API::Meta::Class'], |
38
|
|
|
|
|
|
|
}, |
39
|
|
|
|
|
|
|
); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
Moose::Util::MetaRole::apply_base_class_roles( |
42
|
|
|
|
|
|
|
for => $for, |
43
|
|
|
|
|
|
|
roles => [ |
44
|
|
|
|
|
|
|
qw/ |
45
|
|
|
|
|
|
|
Net::HTTP::API::Role::UserAgent |
46
|
|
|
|
|
|
|
Net::HTTP::API::Role::Format |
47
|
|
|
|
|
|
|
Net::HTTP::API::Role::Authentication |
48
|
|
|
|
|
|
|
Net::HTTP::API::Role::Serialization |
49
|
|
|
|
|
|
|
Net::HTTP::API::Role::Request |
50
|
|
|
|
|
|
|
/ |
51
|
|
|
|
|
|
|
], |
52
|
|
|
|
|
|
|
); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
$meta; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
1; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
__END__ |
61
|
|
|
|
|
|
|
=pod |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head1 NAME |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
Net::HTTP::API - Easily create client for net API |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head1 VERSION |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
version 0.14 |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head1 SYNOPSIS |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
package My::Net::API; |
74
|
|
|
|
|
|
|
use Net::HTTP::API; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# we declare an API, the base_url is http://exemple.com/api |
77
|
|
|
|
|
|
|
# the format is json and it will be append to the query |
78
|
|
|
|
|
|
|
# You can set api_base_url later, calling $obj->api_base_url('http://..') |
79
|
|
|
|
|
|
|
net_api_declare my_api => ( |
80
|
|
|
|
|
|
|
api_base_url => 'http://exemple.com/api', |
81
|
|
|
|
|
|
|
api_format => 'json', |
82
|
|
|
|
|
|
|
api_format_mode => 'append', |
83
|
|
|
|
|
|
|
); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# declaring a users method |
86
|
|
|
|
|
|
|
# calling $obj->users will call http://exemple.com/api/users/france |
87
|
|
|
|
|
|
|
net_api_method users => ( |
88
|
|
|
|
|
|
|
description => 'this get a list of users', |
89
|
|
|
|
|
|
|
method => 'GET', |
90
|
|
|
|
|
|
|
path => '/users/:country', |
91
|
|
|
|
|
|
|
params => [qw/country/], |
92
|
|
|
|
|
|
|
); |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# you can create your own useragent (it must be a LWP::UserAgent object) |
95
|
|
|
|
|
|
|
net_api_declare my_api => ( |
96
|
|
|
|
|
|
|
... |
97
|
|
|
|
|
|
|
useragent => sub { |
98
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new; |
99
|
|
|
|
|
|
|
$ua->agent('MyUberAgent/0.23'); |
100
|
|
|
|
|
|
|
return $ua |
101
|
|
|
|
|
|
|
}, |
102
|
|
|
|
|
|
|
... |
103
|
|
|
|
|
|
|
); |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# if the API require authentification, the module will handle basic |
106
|
|
|
|
|
|
|
# authentication for you |
107
|
|
|
|
|
|
|
net_api_declare my_api => ( |
108
|
|
|
|
|
|
|
... |
109
|
|
|
|
|
|
|
authentication => 1, |
110
|
|
|
|
|
|
|
... |
111
|
|
|
|
|
|
|
); |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# if the authentication is more complex, you can delegate to your own method |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
1; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
my $obj = My::Net::API->new(); |
118
|
|
|
|
|
|
|
$obj->api_base_url('http://...'); |
119
|
|
|
|
|
|
|
$obj->foo(user => $user); |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head1 DESCRIPTION |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Net::HTTP::API is a module to help to easily create a client for a web API. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
This module is heavily inspired by what L<Net::Twitter> does. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
B<THIS MODULE IS IN ITS BETA QUALITY. THE API MAY CHANGE IN THE FUTURE> |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
The following roles are added to your class: |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=over 4 |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=item B<Net::HTTP::API::Role::UserAgent> |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=item B<Net::HTTP::API::Role::Format> |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=item B<Net::HTTP::API::Role::Authentication> |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=item B<Net::HTTP::API::Role::Serialization> |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=item B<Net::HTTP::API::Role::Request> |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=back |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
The following attributes are added to your class: |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=over 4 |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=item B<api_base_url> |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=item B<api_format> |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=item B<api_username> |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=item B<api_passord> |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=item B<authentication> |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=item B<authentication_method> |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=back |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
The following methods are added to your class: |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=over 4 |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=item B<http_request> |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=item B<get_content> |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=item B<serialize> |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=item B<deserialize> |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=item B<content_type> |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=back |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head2 METHODS |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=over 4 |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=item B<net_api_declare> |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
net_api_declare backtype => ( |
186
|
|
|
|
|
|
|
base_url => 'http://api....', |
187
|
|
|
|
|
|
|
format => 'json', |
188
|
|
|
|
|
|
|
format_mode => 'append', |
189
|
|
|
|
|
|
|
); |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=over 2 |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=item B<api_base_url> |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
The base url for all the API's calls. This will set the B<api_base_url> attribut in your class. Can be set at the object creation or before calling an API method. |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=item B<api_format> |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
The format for the API's calls. This will set the B<api_format> attribut to your class. Value can be: |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=over 2 |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=item B<json> |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=item B<yaml> |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=item B<xml> |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=back |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=item B<api_format_mode> |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
How the format is handled. B<append> will add B<.$format> to the query, B<content-type> will set the content-type information to the header of the request. Should be one the following value: |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=over 2 |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=item B<content-type> |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=item B<append> |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=back |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=item B<api_useragent> |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
A L<LWP::UserAgent> object. |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
useragent => sub { |
228
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new; |
229
|
|
|
|
|
|
|
$ua->agent( "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"); |
230
|
|
|
|
|
|
|
return $ua; |
231
|
|
|
|
|
|
|
} |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=item B<authentication> |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
This is a boolean to tell if we must authenticate to use this API. |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=item B<authentication_method> |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
The default authentication method only set an authorization header using the Basic Authentication Scheme. You can write your own authentication method: |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
net_api_declare foo => ( |
242
|
|
|
|
|
|
|
... |
243
|
|
|
|
|
|
|
authentication_method => 'my_auth_method', |
244
|
|
|
|
|
|
|
... |
245
|
|
|
|
|
|
|
); |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
sub my_auth_method { |
248
|
|
|
|
|
|
|
my ($self, $req) = @_; #$req is an HTTP::Request object |
249
|
|
|
|
|
|
|
... |
250
|
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=back |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=item B<net_api_method> |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=over 2 |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
=item B<description> |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
A string to describe the method (this is a documentation) |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=item B<method> |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
HTTP method (GET, POST, PUT, DELETE) |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
=item B<path> |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
path of the query. |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
If you defined your path and params like this |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
net_api_method user_comments => ( |
273
|
|
|
|
|
|
|
... |
274
|
|
|
|
|
|
|
path => '/user/:user/list/:date', |
275
|
|
|
|
|
|
|
params => [qw/user date foo bar/], |
276
|
|
|
|
|
|
|
... |
277
|
|
|
|
|
|
|
); |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
and you call |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
$obj->user_comments(user => 'franck', date => 'today', foo => 1, bar => 2); |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
the url generated will look like |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
/user/franck/list/today/?foo=1&bar=2 |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
=item B<params> |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
Arrayref of params. |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
=item B<required> |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
Arrayref of required params. |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
=item B<params_in_url> |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
When you do a post, the content may have to be sent as arguments in the url, and not as content in the header. |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
=back |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
=back |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
=head1 AUTHOR |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
franck cuny <franck@lumberjaph.net> |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
This software is copyright (c) 2010 by linkfluence. |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
312
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
=cut |
315
|
|
|
|
|
|
|
|