line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package LWP::Simple::REST; |
2
|
|
|
|
|
|
|
|
3
|
8
|
|
|
8
|
|
49594
|
use strict; |
|
8
|
|
|
|
|
13
|
|
|
8
|
|
|
|
|
307
|
|
4
|
8
|
|
|
8
|
|
36
|
use warnings FATAL => 'all'; |
|
8
|
|
|
|
|
12
|
|
|
8
|
|
|
|
|
336
|
|
5
|
|
|
|
|
|
|
|
6
|
8
|
|
|
8
|
|
37
|
use Cwd; |
|
8
|
|
|
|
|
13
|
|
|
8
|
|
|
|
|
546
|
|
7
|
8
|
|
|
8
|
|
4586
|
use Data::Structure::Util qw( unbless ); |
|
8
|
|
|
|
|
57769
|
|
|
8
|
|
|
|
|
669
|
|
8
|
|
|
|
|
|
|
|
9
|
8
|
|
|
8
|
|
61
|
use Exporter qw( import ); |
|
8
|
|
|
|
|
16
|
|
|
8
|
|
|
|
|
443
|
|
10
|
|
|
|
|
|
|
our @EXPORT_OK = qw/ |
11
|
|
|
|
|
|
|
http_get |
12
|
|
|
|
|
|
|
http_post |
13
|
|
|
|
|
|
|
http_put |
14
|
|
|
|
|
|
|
http_delete |
15
|
|
|
|
|
|
|
http_head |
16
|
|
|
|
|
|
|
http_upload |
17
|
|
|
|
|
|
|
json_get |
18
|
|
|
|
|
|
|
json_post |
19
|
|
|
|
|
|
|
json_put |
20
|
|
|
|
|
|
|
json_head |
21
|
|
|
|
|
|
|
/; |
22
|
|
|
|
|
|
|
|
23
|
8
|
|
|
8
|
|
6116
|
use LWP::UserAgent; |
|
8
|
|
|
|
|
276969
|
|
|
8
|
|
|
|
|
277
|
|
24
|
8
|
|
|
8
|
|
93
|
use HTTP::Request; |
|
8
|
|
|
|
|
12
|
|
|
8
|
|
|
|
|
210
|
|
25
|
8
|
|
|
8
|
|
4556
|
use Try::Tiny; |
|
8
|
|
|
|
|
9405
|
|
|
8
|
|
|
|
|
444
|
|
26
|
8
|
|
|
8
|
|
3361
|
use JSON; |
|
8
|
|
|
|
|
51689
|
|
|
8
|
|
|
|
|
53
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
our $VERSION = '0.091'; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
my $user_agent = "LWP::Simple::REST"; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub http_get { |
33
|
1
|
|
|
1
|
1
|
2001436
|
my ( $url, $arguments ) = @_; |
34
|
|
|
|
|
|
|
|
35
|
1
|
|
|
|
|
43
|
my $ua = LWP::UserAgent->new; |
36
|
1
|
|
|
|
|
3228
|
$ua->agent($user_agent); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# Pass a url sanitizer |
39
|
1
|
|
|
|
|
66
|
my @parameters; |
40
|
1
|
|
|
|
|
3
|
while ( my ( $key, $value ) = each %{ $arguments } ) { |
|
2
|
|
|
|
|
11
|
|
41
|
1
|
|
|
|
|
3
|
push @parameters, "$key=$value"; |
42
|
|
|
|
|
|
|
} |
43
|
1
|
|
|
|
|
3
|
my $parameters_for_url = join "&", @parameters; |
44
|
1
|
|
|
|
|
11
|
my $response = $ua->get( $url . "?$parameters_for_url" ); |
45
|
|
|
|
|
|
|
|
46
|
1
|
|
|
|
|
127431
|
return $response->content; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub http_post { |
51
|
1
|
|
|
1
|
1
|
2001216
|
my ( $url, $arguments ) = @_; |
52
|
|
|
|
|
|
|
|
53
|
1
|
|
|
|
|
67
|
my $ua = LWP::UserAgent->new; |
54
|
1
|
|
|
|
|
3405
|
$ua->agent($user_agent); |
55
|
|
|
|
|
|
|
|
56
|
1
|
|
|
|
|
80
|
my $response = $ua->post( $url, |
57
|
|
|
|
|
|
|
$arguments, |
58
|
|
|
|
|
|
|
); |
59
|
|
|
|
|
|
|
|
60
|
1
|
|
|
|
|
107513
|
return $response->content; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub http_put { |
64
|
1
|
|
|
1
|
0
|
2001513
|
my ( $url, $arguments ) = @_; |
65
|
|
|
|
|
|
|
|
66
|
1
|
|
|
|
|
52
|
my $ua = LWP::UserAgent->new; |
67
|
1
|
|
|
|
|
4062
|
$ua->agent($user_agent); |
68
|
|
|
|
|
|
|
|
69
|
1
|
|
|
|
|
96
|
my $response = $ua->put( $url, |
70
|
|
|
|
|
|
|
$arguments, |
71
|
|
|
|
|
|
|
); |
72
|
|
|
|
|
|
|
|
73
|
1
|
|
|
|
|
143102
|
return $response->content; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub upload_post { |
77
|
0
|
|
|
0
|
0
|
0
|
my ( $url, $json, $filename ) = @_; |
78
|
|
|
|
|
|
|
|
79
|
0
|
|
|
|
|
0
|
my $ua = LWP::UserAgent->new; |
80
|
0
|
|
|
|
|
0
|
$ua->agent('RESTClient'); |
81
|
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
0
|
my $response = $ua->post( |
83
|
|
|
|
|
|
|
$url, |
84
|
|
|
|
|
|
|
[ |
85
|
|
|
|
|
|
|
file => [ $filename ], |
86
|
|
|
|
|
|
|
], |
87
|
|
|
|
|
|
|
'Content_Type' => 'form-data', |
88
|
|
|
|
|
|
|
); |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
0
|
return answer( $response ); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub http_delete { |
94
|
1
|
|
|
1
|
1
|
2001253
|
my ( $url, $arguments ) = @_; |
95
|
|
|
|
|
|
|
|
96
|
1
|
|
|
|
|
57
|
my $ua = LWP::UserAgent->new; |
97
|
1
|
|
|
|
|
3204
|
$ua->agent('RESTClient'); |
98
|
|
|
|
|
|
|
|
99
|
1
|
|
|
|
|
55
|
my @parameters; |
100
|
1
|
|
|
|
|
1
|
while ( my ( $key, $value ) = each %{ $arguments } ) { |
|
2
|
|
|
|
|
7
|
|
101
|
1
|
|
|
|
|
3
|
push @parameters, "$key=$value"; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
1
|
|
|
|
|
3
|
my $parameters_for_url = join "&", @parameters; |
105
|
|
|
|
|
|
|
|
106
|
1
|
|
|
|
|
13
|
my $response = $ua->delete( $url . "?$parameters_for_url" ); |
107
|
|
|
|
|
|
|
|
108
|
1
|
|
|
|
|
112159
|
return $response->content; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub http_head { |
113
|
1
|
|
|
1
|
1
|
2001473
|
my ( $url, $arguments ) = @_; |
114
|
|
|
|
|
|
|
|
115
|
1
|
|
|
|
|
66
|
my $ua = LWP::UserAgent->new; |
116
|
1
|
|
|
|
|
4904
|
$ua->agent($user_agent); |
117
|
|
|
|
|
|
|
|
118
|
1
|
|
|
|
|
108
|
my @parameters; |
119
|
1
|
|
|
|
|
7
|
while ( my ( $key, $value ) = each %{ $arguments } ){ |
|
2
|
|
|
|
|
12
|
|
120
|
1
|
|
|
|
|
3
|
push @parameters, "$key=$value"; |
121
|
|
|
|
|
|
|
} |
122
|
1
|
|
|
|
|
6
|
my $parameters_for_url = join "&", @parameters; |
123
|
1
|
|
|
|
|
12
|
my $response = $ua->head( $url . "?$parameters_for_url" ); |
124
|
|
|
|
|
|
|
|
125
|
1
|
|
|
|
|
110429
|
return $response->headers; |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub json_post { |
130
|
1
|
|
|
1
|
1
|
1353
|
my ( $url, $arguments ) = @_; |
131
|
|
|
|
|
|
|
|
132
|
1
|
|
|
|
|
67
|
my $ua = LWP::UserAgent->new; |
133
|
1
|
|
|
|
|
3456
|
$ua->agent($user_agent); |
134
|
|
|
|
|
|
|
|
135
|
1
|
|
|
|
|
105
|
my $response = $ua->post( $url, |
136
|
|
|
|
|
|
|
$arguments, |
137
|
|
|
|
|
|
|
); |
138
|
|
|
|
|
|
|
|
139
|
1
|
|
|
|
|
113288
|
return decode_json $response->content; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub json_put { |
143
|
0
|
|
|
0
|
0
|
0
|
my ( $url, $arguments ) = @_; |
144
|
|
|
|
|
|
|
|
145
|
0
|
|
|
|
|
0
|
my $ua = LWP::UserAgent->new; |
146
|
0
|
|
|
|
|
0
|
$ua->agent($user_agent); |
147
|
|
|
|
|
|
|
|
148
|
0
|
|
|
|
|
0
|
my $response = $ua->put( $url, |
149
|
|
|
|
|
|
|
$arguments, |
150
|
|
|
|
|
|
|
); |
151
|
|
|
|
|
|
|
|
152
|
0
|
|
|
|
|
0
|
return decode_json $response->content; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
sub json_get { |
156
|
1
|
|
|
1
|
1
|
2001903
|
my ( $url, $arguments ) = @_; |
157
|
|
|
|
|
|
|
|
158
|
1
|
|
|
|
|
49
|
my $ua = LWP::UserAgent->new; |
159
|
1
|
|
|
|
|
3362
|
$ua->agent($user_agent); |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
# Pass a url sanitazier |
162
|
1
|
|
|
|
|
78
|
my @parameters; |
163
|
1
|
|
|
|
|
3
|
while ( my ( $key, $value ) = each %{ $arguments } ) { |
|
1
|
|
|
|
|
17
|
|
164
|
0
|
|
|
|
|
0
|
push @parameters, "$key=$value"; |
165
|
|
|
|
|
|
|
} |
166
|
1
|
|
|
|
|
14
|
my $parameters_for_url = join "&", @parameters; |
167
|
1
|
|
|
|
|
18
|
my $response = $ua->get( $url . "?$parameters_for_url" ); |
168
|
|
|
|
|
|
|
|
169
|
1
|
|
|
|
|
130405
|
return decode_json $response->content; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
sub answer { |
173
|
0
|
|
|
0
|
0
|
|
my ( $response ) = @_; |
174
|
|
|
|
|
|
|
|
175
|
0
|
|
|
|
|
|
my $http_code = $response->code(); |
176
|
0
|
|
|
|
|
|
my $return = $response->decoded_content; |
177
|
|
|
|
|
|
|
|
178
|
0
|
0
|
|
|
|
|
if ( $response->is_success ){ |
179
|
0
|
|
|
|
|
|
my $answer; |
180
|
0
|
0
|
|
|
|
|
if ( $http_code =~ /(2\d\d)/ ){ |
181
|
0
|
0
|
|
|
|
|
if ( $1 == 204 ){ |
182
|
0
|
|
|
|
|
|
return $return; |
183
|
|
|
|
|
|
|
}else{ |
184
|
0
|
|
|
|
|
|
return decode_json( $return ); |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
} |
188
|
0
|
|
|
|
|
|
my $status = $response->status_line; |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head1 NAME |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
LWP::Simple::REST - A simple procedural interface do http verbs |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=head1 VERSION |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
Version 0.091 |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head1 SYNOPSIS |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
This module is a simple wrapper for simple http requests. It has two groups |
202
|
|
|
|
|
|
|
of wrappers, http_ and json_. The first are to use with plain answers, the |
203
|
|
|
|
|
|
|
second one assumes a json answer and already decode it. |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
This is a classical example, to post a information to a server. |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
use LWP::Simple::REST qw/http_post/; |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
my $foo = http_post( "http://example.org", { example => "1", show => "all" } ); |
210
|
|
|
|
|
|
|
... |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
All methods receive an url and a hashref with parameters. Now you can only send |
215
|
|
|
|
|
|
|
normal parameters, in future is possible to send json encoded parameters on the |
216
|
|
|
|
|
|
|
body. |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
Also there is a method to upload files to the server, really simple, just on |
219
|
|
|
|
|
|
|
hands for small files. |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=head2 http_get |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
Sends a http get and returns the content of this request |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=head2 http_post |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
Sends a http post and returns the content of this request |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=head2 http_delete |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
Sends a delete request for the url |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=head2 http_head |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
Sends a head request for the url, and unblesses the headers's object allowing access the header |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=head2 http_upload |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
Sends an Upload to url |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=head2 json_get |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
Sends a get request, expects a json response |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=head2 json_post |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
Sends a post request, expects a json response |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=head1 AUTHOR |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
GONCALES, C<< >> |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
RECSKY, C<< >> |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=head1 BUGS |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
258
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
259
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=head1 SUPPORT |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
perldoc LWP::Simple::REST |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
Usually we are on irc on irc.perl.org. |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
#sao-paulo.pm |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
=over 4 |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
L |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
L |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
=item * CPAN Ratings |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
L |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
=back |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
Copyright 2014 GONCALES |
292
|
|
|
|
|
|
|
Copyright 2014 RECSKY |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
295
|
|
|
|
|
|
|
under the terms of the the Artistic License (2.0). You may obtain a |
296
|
|
|
|
|
|
|
copy of the full license at: |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
L |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
=cut |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
1; # End of LWP::Simple::REST |