line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::Simple; |
2
|
8
|
|
|
8
|
|
180897
|
use strict; |
|
8
|
|
|
|
|
18
|
|
|
8
|
|
|
|
|
320
|
|
3
|
8
|
|
|
8
|
|
39
|
use warnings; |
|
8
|
|
|
|
|
12
|
|
|
8
|
|
|
|
|
277
|
|
4
|
8
|
|
|
8
|
|
38
|
use base qw(LWP::UserAgent Class::Data::ConfigHash); |
|
8
|
|
|
|
|
16
|
|
|
8
|
|
|
|
|
5536
|
|
5
|
8
|
|
|
8
|
|
351221
|
use Class::Inspector; |
|
8
|
|
|
|
|
23613
|
|
|
8
|
|
|
|
|
294
|
|
6
|
8
|
|
|
8
|
|
4314
|
use Data::Dumper (); |
|
8
|
|
|
|
|
40157
|
|
|
8
|
|
|
|
|
191
|
|
7
|
8
|
|
|
8
|
|
53
|
use Digest::MD5 (); |
|
8
|
|
|
|
|
12
|
|
|
8
|
|
|
|
|
111
|
|
8
|
8
|
|
|
8
|
|
30
|
use URI::Escape; |
|
8
|
|
|
|
|
11
|
|
|
8
|
|
|
|
|
570
|
|
9
|
8
|
|
|
8
|
|
3202
|
use URI::QueryParam; |
|
8
|
|
|
|
|
3797
|
|
|
8
|
|
|
|
|
217
|
|
10
|
8
|
|
|
8
|
|
41
|
use HTTP::Message; |
|
8
|
|
|
|
|
8
|
|
|
8
|
|
|
|
|
179
|
|
11
|
8
|
|
|
8
|
|
3930
|
use Hash::MultiValue; |
|
8
|
|
|
|
|
15517
|
|
|
8
|
|
|
|
|
228
|
|
12
|
8
|
|
|
8
|
|
3267
|
use WebService::Simple::Response; |
|
8
|
|
|
|
|
15
|
|
|
8
|
|
|
|
|
193
|
|
13
|
8
|
|
|
8
|
|
3585
|
use UNIVERSAL::require; |
|
8
|
|
|
|
|
8920
|
|
|
8
|
|
|
|
|
69
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our $VERSION = '0.24'; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
__PACKAGE__->config( |
18
|
|
|
|
|
|
|
base_url => '', |
19
|
|
|
|
|
|
|
response_parser => { module => "XML::Simple" }, |
20
|
|
|
|
|
|
|
); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub new { |
23
|
7
|
|
|
7
|
1
|
1384
|
my $class = shift; |
24
|
7
|
|
|
|
|
29
|
my %args = @_; |
25
|
7
|
|
33
|
|
|
42
|
my $base_url = delete $args{base_url} |
26
|
|
|
|
|
|
|
|| $class->config->{base_url} |
27
|
|
|
|
|
|
|
|| Carp::croak("base_url is required"); |
28
|
7
|
|
|
|
|
61
|
$base_url = URI->new($base_url); |
29
|
7
|
|
100
|
|
|
47212
|
my $basic_params = delete $args{params} || delete $args{param} || {}; |
30
|
7
|
|
50
|
|
|
44
|
my $debug = delete $args{debug} || 0; |
31
|
|
|
|
|
|
|
|
32
|
7
|
|
66
|
|
|
104
|
my $response_parser = delete $args{response_parser} |
33
|
|
|
|
|
|
|
|| $class->config->{response_parser}; |
34
|
7
|
50
|
33
|
|
|
204
|
if ( !$response_parser |
35
|
7
|
|
|
|
|
208
|
|| !eval { $response_parser->isa('WebService::Simple::Parser') } ) |
36
|
|
|
|
|
|
|
{ |
37
|
7
|
|
33
|
|
|
22
|
my $config = $response_parser || $class->config->{response_parser}; |
38
|
7
|
100
|
|
|
|
24
|
if ( !ref $config ) { |
39
|
2
|
|
|
|
|
6
|
$config = { module => $config }; |
40
|
|
|
|
|
|
|
} |
41
|
7
|
|
|
|
|
16
|
my $module = $config->{module}; |
42
|
7
|
50
|
|
|
|
27
|
if ( $module !~ s/^\+// ) { |
43
|
7
|
|
|
|
|
18
|
$module = __PACKAGE__ . "::Parser::$module"; |
44
|
|
|
|
|
|
|
} |
45
|
7
|
50
|
|
|
|
53
|
if ( !Class::Inspector->loaded($module) ) { |
46
|
7
|
100
|
|
|
|
594
|
$module->require or die; |
47
|
|
|
|
|
|
|
} |
48
|
2
|
50
|
|
|
|
25
|
$response_parser = $module->new( %{ $config->{args} || {} } ); |
|
2
|
|
|
|
|
22
|
|
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
2
|
|
|
|
|
5
|
my $cache = delete $args{cache}; |
52
|
2
|
50
|
33
|
|
|
10
|
if ( !$cache || ref $cache eq 'HASH' ) { |
53
|
2
|
50
|
|
|
|
30
|
my $config = ref $cache eq 'HASH' ? $cache : $class->config->{cache}; |
54
|
2
|
50
|
|
|
|
68
|
if ($config) { |
55
|
0
|
0
|
|
|
|
0
|
if ( !ref $config ) { |
56
|
0
|
|
|
|
|
0
|
$config = { module => $config }; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
0
|
my $module = $config->{module}; |
60
|
0
|
0
|
|
|
|
0
|
if ( !Class::Inspector->loaded($module) ) { |
61
|
0
|
0
|
|
|
|
0
|
$module->require or die; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
$cache = |
64
|
0
|
|
|
|
|
0
|
$module->new( $config->{hashref_args} |
65
|
|
|
|
|
|
|
? $config->{args} |
66
|
0
|
0
|
|
|
|
0
|
: %{ $config->{args} } ); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} |
69
|
2
|
|
|
|
|
4
|
my $compression = delete $args{compression}; |
70
|
2
|
|
|
|
|
3
|
my $croak = delete $args{croak}; |
71
|
|
|
|
|
|
|
|
72
|
2
|
|
|
|
|
17
|
my $self = $class->SUPER::new(%args); |
73
|
2
|
|
|
|
|
6
|
$self->{base_url} = $base_url; |
74
|
2
|
|
|
|
|
7
|
$self->{basic_params} = $basic_params; |
75
|
2
|
|
|
|
|
4
|
$self->{response_parser} = $response_parser; |
76
|
2
|
|
|
|
|
4
|
$self->{cache} = $cache; |
77
|
2
|
|
|
|
|
3
|
$self->{compression} = $compression; |
78
|
2
|
|
|
|
|
3
|
$self->{croak} = $croak; |
79
|
2
|
|
|
|
|
4
|
$self->{debug} = $debug; |
80
|
2
|
|
|
|
|
6
|
return $self; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
2
|
|
|
2
|
|
134
|
sub _agent { "libwww-perl/$LWP::VERSION+". __PACKAGE__ .'/'.$VERSION } |
84
|
|
|
|
|
|
|
|
85
|
0
|
|
|
0
|
1
|
0
|
sub base_url { $_[0]->{base_url} } |
86
|
0
|
|
|
0
|
1
|
0
|
sub basic_params { $_[0]->{basic_params} } |
87
|
2
|
|
|
2
|
1
|
19
|
sub response_parser { $_[0]->{response_parser} } |
88
|
0
|
|
|
0
|
1
|
|
sub cache { $_[0]->{cache} } |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub __cache_get { |
91
|
0
|
|
|
0
|
|
|
my $self = shift; |
92
|
0
|
|
|
|
|
|
my $cache = $self->cache; |
93
|
0
|
0
|
|
|
|
|
return unless $cache; |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
|
my $key = $self->__cache_key(shift); |
96
|
0
|
|
|
|
|
|
return $cache->get( $key, @_ ); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub __cache_set { |
100
|
0
|
|
|
0
|
|
|
my $self = shift; |
101
|
0
|
|
|
|
|
|
my $cache = $self->cache; |
102
|
0
|
0
|
|
|
|
|
return unless $cache; |
103
|
|
|
|
|
|
|
|
104
|
0
|
|
|
|
|
|
my $key = $self->__cache_key(shift); |
105
|
0
|
|
|
|
|
|
return $cache->set( $key, @_ ); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub __cache_remove { |
109
|
0
|
|
|
0
|
|
|
my $self = shift; |
110
|
0
|
|
|
|
|
|
my $cache = $self->cache; |
111
|
0
|
0
|
|
|
|
|
return unless $cache; |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
my $key = $self->__cache_key(shift); |
114
|
0
|
|
|
|
|
|
return $cache->remove( $key, @_ ); |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub __cache_key { |
118
|
0
|
|
|
0
|
|
|
my $self = shift; |
119
|
0
|
|
|
|
|
|
local $Data::Dumper::Indent = 1; |
120
|
0
|
|
|
|
|
|
local $Data::Dumper::Terse = 1; |
121
|
0
|
|
|
|
|
|
local $Data::Dumper::Sortkeys = 1; |
122
|
0
|
|
|
|
|
|
return Digest::MD5::md5_hex( Data::Dumper::Dumper( $_[0] ) ); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub request_url { |
126
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
127
|
0
|
|
|
|
|
|
my %args = @_; |
128
|
|
|
|
|
|
|
|
129
|
0
|
0
|
|
|
|
|
my $uri = ref($args{url}) =~ m/^URI/ ? $args{url}->clone() : URI->new($args{url}); |
130
|
0
|
0
|
|
|
|
|
if ( my $extra_path = $args{extra_path} ) { |
131
|
0
|
|
|
|
|
|
$extra_path =~ s!^/!!; |
132
|
0
|
|
|
|
|
|
$uri->path( $uri->path . $extra_path ); |
133
|
|
|
|
|
|
|
} |
134
|
0
|
0
|
|
|
|
|
if($args{params}) { |
135
|
0
|
0
|
|
|
|
|
if(ref $args{params} eq 'Hash::MultiValue') { |
136
|
0
|
|
|
|
|
|
for my $key ($args{params}->keys) { |
137
|
0
|
|
|
|
|
|
$uri->query_param_append($key, $args{params}->get($key)); |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
}else{ |
140
|
0
|
|
|
|
|
|
$uri->query_form(%{$args{params}}); |
|
0
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
} |
143
|
0
|
|
|
|
|
|
return $uri; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub get { |
147
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
148
|
0
|
|
|
|
|
|
my ($url, $extra) = ("", {}); |
149
|
|
|
|
|
|
|
|
150
|
0
|
0
|
|
|
|
|
if ( ref $_[0] eq 'HASH' ) { |
151
|
0
|
|
|
|
|
|
$extra = shift @_; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
else { |
154
|
0
|
|
|
|
|
|
$url = shift @_; |
155
|
0
|
0
|
|
|
|
|
if ( ref $_[0] eq 'HASH' ) { |
156
|
0
|
|
|
|
|
|
$extra = shift @_; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
0
|
|
|
|
|
|
my $uri = $self->request_url( |
161
|
|
|
|
|
|
|
url => $self->base_url, |
162
|
|
|
|
|
|
|
extra_path => $url, |
163
|
0
|
|
|
|
|
|
params => Hash::MultiValue->new(%{$self->basic_params}, %$extra), |
164
|
|
|
|
|
|
|
); |
165
|
|
|
|
|
|
|
|
166
|
0
|
0
|
|
|
|
|
warn "Request URL is $uri\n" if $self->{debug}; |
167
|
|
|
|
|
|
|
|
168
|
0
|
|
|
|
|
|
my @headers = @_; |
169
|
0
|
0
|
0
|
|
|
|
unless(defined($self->{compression}) && !$self->{compression}){ |
170
|
0
|
|
|
|
|
|
my $can_accept = HTTP::Message::decodable(); |
171
|
0
|
|
|
|
|
|
push @headers, ('Accept-Encoding' => $can_accept) ; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
0
|
|
|
|
|
|
my $response; |
175
|
0
|
|
|
|
|
|
$response = $self->__cache_get( [ $uri, @headers ] ); |
176
|
0
|
0
|
|
|
|
|
if ($response) { |
177
|
0
|
0
|
|
|
|
|
if ($response->isa('WebService::Simple::Response')) { # backward compatibility |
178
|
0
|
|
|
|
|
|
return $response; |
179
|
|
|
|
|
|
|
} else { |
180
|
0
|
|
|
|
|
|
return WebService::Simple::Response->new_from_response( |
181
|
|
|
|
|
|
|
response => $response, |
182
|
|
|
|
|
|
|
parser => $self->response_parser |
183
|
|
|
|
|
|
|
); |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
0
|
|
|
|
|
|
$response = $self->SUPER::get( $uri, @headers ); |
188
|
|
|
|
|
|
|
|
189
|
0
|
0
|
|
|
|
|
if ( $response->is_success ) { |
190
|
0
|
|
|
|
|
|
$self->__cache_set( [ $uri, @headers ], $response ); |
191
|
0
|
|
|
|
|
|
$response = WebService::Simple::Response->new_from_response( |
192
|
|
|
|
|
|
|
response => $response, |
193
|
|
|
|
|
|
|
parser => $self->response_parser |
194
|
|
|
|
|
|
|
); |
195
|
|
|
|
|
|
|
}else{ |
196
|
0
|
0
|
0
|
|
|
|
Carp::croak("request to $uri failed") unless defined($self->{croak}) && !$self->{croak}; |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
0
|
|
|
|
|
|
return $response; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
sub post { |
203
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
204
|
0
|
|
|
|
|
|
my ( $url, $extra ) = ( '', {} ); |
205
|
|
|
|
|
|
|
|
206
|
0
|
0
|
|
|
|
|
if ( ref $_[0] eq 'HASH' ) { # post(\%arg [, @header ]) |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
207
|
0
|
|
|
|
|
|
$extra = shift @_; |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
elsif ( ref $_[1] eq 'HASH' ) { # post($url, \%arg [, @header ]) |
210
|
0
|
|
|
|
|
|
$url = shift @_; |
211
|
0
|
|
|
|
|
|
$extra = shift @_; |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
elsif ( @_ % 2 ) { # post($url [, @header ]) |
214
|
0
|
|
|
|
|
|
$url = shift @_; |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
0
|
|
|
|
|
|
my $uri = $self->request_url( |
218
|
|
|
|
|
|
|
url => $self->base_url, |
219
|
|
|
|
|
|
|
extra_path => $url, |
220
|
0
|
|
|
|
|
|
params => Hash::MultiValue->new(%{$self->basic_params}, %$extra), |
221
|
|
|
|
|
|
|
); |
222
|
0
|
|
|
|
|
|
my $content = $uri->query_form_hash(); |
223
|
0
|
|
|
|
|
|
$uri->query_form(undef); |
224
|
|
|
|
|
|
|
|
225
|
0
|
|
|
|
|
|
my @headers = @_; |
226
|
0
|
0
|
0
|
|
|
|
unless(defined($self->{compression}) && !$self->{compression}){ |
227
|
0
|
|
|
|
|
|
my $can_accept = HTTP::Message::decodable(); |
228
|
0
|
|
|
|
|
|
push @headers, ('Accept-Encoding' => $can_accept) ; |
229
|
|
|
|
|
|
|
} |
230
|
|
|
|
|
|
|
|
231
|
0
|
|
|
|
|
|
my $response = $self->SUPER::post( $uri, $content, @headers ); |
232
|
|
|
|
|
|
|
|
233
|
0
|
0
|
|
|
|
|
if ( $response->is_success ) { |
234
|
0
|
|
|
|
|
|
$response = WebService::Simple::Response->new_from_response( |
235
|
|
|
|
|
|
|
response => $response, |
236
|
|
|
|
|
|
|
parser => $self->response_parser |
237
|
|
|
|
|
|
|
); |
238
|
|
|
|
|
|
|
}else{ |
239
|
0
|
0
|
0
|
|
|
|
Carp::croak("request to $uri failed") unless defined($self->{croak}) && !$self->{croak}; |
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
|
242
|
0
|
|
|
|
|
|
return $response; |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
1; |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
__END__ |