/
Simple.pm
394 lines (305 loc) · 9.73 KB
/
Simple.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# $Id$
package WebService::Simple;
use strict;
use warnings;
use base qw(LWP::UserAgent Class::Data::ConfigHash);
use Class::Inspector;
use Data::Dumper ();
use Digest::MD5 ();
use URI::Escape;
use WebService::Simple::Response;
use UNIVERSAL::require;
our $VERSION = '0.13';
__PACKAGE__->config(
base_url => '',
response_parser => { module => "XML::Simple" },
);
sub new {
my $class = shift;
my %args = @_;
my $base_url = delete $args{base_url}
|| $class->config->{base_url}
|| Carp::croak("base_url is required");
my $basic_params = delete $args{params} || delete $args{param} || {};
my $debug = delete $args{debug} || 0;
my $response_parser = delete $args{response_parser}
|| $class->config->{response_parser};
if ( !$response_parser
|| !eval { $response_parser->isa('WebService::Simple::Parser') } )
{
my $config = $response_parser || $class->config->{response_parser};
if ( !ref $config ) {
$config = { module => $config };
}
my $module = $config->{module};
if ( $module !~ s/^\+// ) {
$module = __PACKAGE__ . "::Parser::$module";
}
if ( !Class::Inspector->loaded($module) ) {
$module->require or die;
}
$response_parser = $module->new( %{ $config->{args} || {} } );
}
my $cache = delete $args{cache};
if ( !$cache || ref $cache eq 'HASH' ) {
my $config = ref $cache eq 'HASH' ? $cache : $class->config->{cache};
if ($config) {
if ( !ref $config ) {
$config = { module => $config };
}
my $module = $config->{module};
if ( !Class::Inspector->loaded($module) ) {
$module->require or die;
}
$cache =
$module->new( $config->{hashref_args}
? $config->{args}
: %{ $config->{args} } );
}
}
my $self = $class->SUPER::new(%args);
$self->{base_url} = URI->new($base_url);
$self->{basic_params} = $basic_params;
$self->{response_parser} = $response_parser;
$self->{cache} = $cache;
$self->{debug} = $debug;
return $self;
}
sub base_url { $_[0]->{base_url} }
sub basic_params { $_[0]->{basic_params} }
sub response_parser { $_[0]->{response_parser} }
sub cache { $_[0]->{cache} }
sub __cache_get {
my $self = shift;
my $cache = $self->cache;
return unless $cache;
my $key = $self->__cache_key(shift);
return $cache->get( $key, @_ );
}
sub __cache_set {
my $self = shift;
my $cache = $self->cache;
return unless $cache;
my $key = $self->__cache_key(shift);
return $cache->set( $key, @_ );
}
sub __cache_remove {
my $self = shift;
my $cache = $self->cache;
return unless $cache;
my $key = $self->__cache_key(shift);
return $cache->remove( $key, @_ );
}
sub __cache_key {
my $self = shift;
local $Data::Dumper::Indent = 1;
local $Data::Dumper::Terse = 1;
local $Data::Dumper::Sortkeys = 1;
return Digest::MD5::md5_hex( Data::Dumper::Dumper( $_[0] ) );
}
sub request_url {
my $self = shift;
my %args = @_;
my $uri = URI->new( $args{url} );
if ( my $extra_path = $args{extra_path} ) {
$extra_path =~ s!^/!!;
$uri->path( $uri->path . $extra_path );
}
$uri->query_form(%{$args{params}});
return $uri;
}
sub get {
my $self = shift;
my ( $url, %extra );
if ( ref $_[0] eq 'HASH' ) {
$url = "";
%extra = %{ shift @_ };
}
else {
$url = shift @_;
if ( ref $_[0] eq 'HASH' ) {
%extra = %{ shift @_ };
}
}
my $uri = $self->request_url(
url => $self->base_url,
extra_path => $url,
params => { %{ $self->basic_params }, %extra }
);
warn "Request URL is $uri\n" if $self->{debug};
my @headers = @_;
my $response;
$response = $self->__cache_get( [ $uri, @headers ] );
if ($response) {
return $response;
}
$response = $self->SUPER::get( $uri, @headers );
if ( !$response->is_success ) {
Carp::croak("request to $uri failed");
}
$response = WebService::Simple::Response->new_from_response(
response => $response,
parser => $self->response_parser
);
$self->__cache_set( [ $uri, @headers ], $response );
return $response;
}
sub post {
my $self = shift;
my ( $url, %extra );
if ( ref $_[0] eq 'HASH' ) {
$url = "";
%extra = %{ shift @_ };
}
else {
$url = shift @_;
if ( ref $_[0] eq 'HASH' ) {
%extra = %{ shift @_ };
}
}
my $uri = $self->request_url(
url => $self->base_url,
extra_path => $url,
);
warn "Request URL is $uri\n" if $self->{debug};
my @headers = @_;
if(defined $extra{file}){
my %file = %{$extra{file}};
delete $extra{file};
foreach my $key(keys(%file)){
$file{$key} = [$file{$key}] if ref $file{$key} ne "ARRAY";
}
%extra = (%file, %extra);
}
my $response;
$response = $self->SUPER::post( $uri, { %{ $self->basic_params }, %extra }, @headers );
if ( !$response->is_success ) {
Carp::croak( "request to $url failed: " . $response->status_line );
}
$response = WebService::Simple::Response->new_from_response(
response => $response,
parser => $self->response_parser
);
return $response;
}
1;
__END__
=head1 NAME
WebService::Simple - Simple Interface To Web Services APIs
=head1 SYNOPSIS
use WebService::Simple;
# Simple use case
my $flickr = WebService::Simple->new(
base_url => "http://api.flickr.com/services/rest/",
param => { api_key => "your_api_key", }
);
# send GET request to
# http://api.flickr.com/service/rest/?api_key=your_api_key&method=flickr.test.echo&name=value
$flickr->get( { method => "flickr.test.echo", name => "value" } );
# send GET request to
# http://api.flickr.com/service/rest/extra/path?api_key=your_api_key&method=flickr.test.echo&name=value
$flickr->get( "extra/path",
{ method => "flickr.test.echo", name => "value" });
=head1 DESCRIPTION
WebService::Simple is a simple class to interact with web services.
It's basically an LWP::UserAgent that remembers recurring api URLs and
parameters, plus sugar to parse the results.
=head1 METHODS
=over 4
=item new(I<%args>)
my $flickr = WebService::Simple->new(
base_url => "http://api.flickr.com/services/rest/",
param => { api_key => "your_api_key", },
# debug => 1
);
Create and return a new WebService::Simple object.
"new" Method requires a base_url of Web Service API.
If debug is set, dump a request URL in get or post method.
=item get(I<[$extra_path,] $args>)
my $response =
$flickr->get( { method => "flickr.test.echo", name => "value" } );
Send GET request, and you can get the WebService::Simple::Response object.
If you want to add a path to base URL, use an option parameter.
my $lingr = WebService::Simple->new(
base_url => "http://www.lingr.com/",
param => { api_key => "your_api_key", format => "xml" }
);
my $response = $lingr->get( 'api/session/create', {} );
=item post(I<[$extra_path,] $args>)
Send POST request.
## riywo patch
This is not supporting the cache.
=item request_url(I<$extra_path, $args>)
Return reequest URL.
=item base_url
=item basic_params
=item cache
=item response_parser
=back
=head1 SUBCLASSING
For better encapsulation, you can create subclass of WebService::Simple to
customize the behavior
package WebService::Simple::Flickr;
use base qw(WebService::Simple);
__PACKAGE__->config(
base_url => "http://api.flickr.com/services/rest/",
upload_url => "http://api.flickr.com/services/upload/",
);
sub test_echo
{
my $self = shift;
$self->get( { method => "flickr.test.echo", name => "value" } );
}
sub upload
{
my $self = shift;
local $self->{base_url} = $self->config->{upload_url};
$self->post(
Content_Type => "form-data",
Content => { title => "title", description => "...", photo => ... },
);
}
##riywo patch
To use new post method:
base_url => "http://api.flickr.com/services/";
$self->post('upload/',
{ title => "title", description => "...",
file => {
photo => 'localpath to photo'
}
},
Content_type => 'form-data'
);
=head1 PARSERS
Web services return their results in various different formats. Or perhaps
you require more sophisticated results parsing than what WebService::Simple
provides.
WebService::Simple by default uses XML::Simple, but you can easily override
that by providing a parser object to the constructor:
my $service = WebService::Simple->new(
response_parser => AVeryComplexParser->new,
...
);
my $response = $service->get( ... );
my $thing = $response->parse_response;
This allows great flexibility in handling different webservices
=head1 CACHING
You can cache the response of Web Service by using Cache object.
my $cache = Cache::File->new(
cache_root => '/tmp/mycache',
default_expires => '30 min',
);
my $flickr = WebService::Simple->new(
base_url => "http://api.flickr.com/services/rest/",
cache => $cache,
param => { api_key => "your_api_key, }
);
=head1 AUTHOR
Yusuke Wada C<< <yusuke@kamawada.com> >>
Daisuke Maki C<< <daisuke@endeworks.jp> >>
Matsuno Tokuhiro
=head1 COPYRIGHT AND LICENSE
This module is free software; you can redistribute it
and/or modify it under the same terms as Perl itself.
See L<perlartistic>.
=cut