line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package LWP::Simple::REST; |
2
|
|
|
|
|
|
|
|
3
|
8
|
|
|
8
|
|
54837
|
use strict; |
|
8
|
|
|
|
|
10
|
|
|
8
|
|
|
|
|
286
|
|
4
|
8
|
|
|
8
|
|
32
|
use warnings FATAL => 'all'; |
|
8
|
|
|
|
|
12
|
|
|
8
|
|
|
|
|
358
|
|
5
|
|
|
|
|
|
|
|
6
|
8
|
|
|
8
|
|
59
|
use Cwd; |
|
8
|
|
|
|
|
16
|
|
|
8
|
|
|
|
|
627
|
|
7
|
|
|
|
|
|
|
|
8
|
8
|
|
|
8
|
|
42
|
use Exporter qw( import ); |
|
8
|
|
|
|
|
13
|
|
|
8
|
|
|
|
|
503
|
|
9
|
|
|
|
|
|
|
our @EXPORT_OK = qw/ |
10
|
|
|
|
|
|
|
HEAD |
11
|
|
|
|
|
|
|
GET |
12
|
|
|
|
|
|
|
POST |
13
|
|
|
|
|
|
|
DELETE |
14
|
|
|
|
|
|
|
PUT |
15
|
|
|
|
|
|
|
json |
16
|
|
|
|
|
|
|
plain |
17
|
|
|
|
|
|
|
headers |
18
|
|
|
|
|
|
|
response |
19
|
|
|
|
|
|
|
http_get |
20
|
|
|
|
|
|
|
http_post |
21
|
|
|
|
|
|
|
http_put |
22
|
|
|
|
|
|
|
http_delete |
23
|
|
|
|
|
|
|
http_head |
24
|
|
|
|
|
|
|
http_upload |
25
|
|
|
|
|
|
|
json_get |
26
|
|
|
|
|
|
|
json_post |
27
|
|
|
|
|
|
|
json_put |
28
|
|
|
|
|
|
|
json_head |
29
|
|
|
|
|
|
|
/; |
30
|
|
|
|
|
|
|
|
31
|
8
|
|
|
8
|
|
6282
|
use LWP::UserAgent; |
|
8
|
|
|
|
|
414845
|
|
|
8
|
|
|
|
|
335
|
|
32
|
8
|
|
|
8
|
|
85
|
use HTTP::Request; |
|
8
|
|
|
|
|
14
|
|
|
8
|
|
|
|
|
247
|
|
33
|
8
|
|
|
8
|
|
36
|
use Try::Tiny; |
|
8
|
|
|
|
|
12
|
|
|
8
|
|
|
|
|
571
|
|
34
|
8
|
|
|
8
|
|
4249
|
use JSON; |
|
8
|
|
|
|
|
60496
|
|
|
8
|
|
|
|
|
51
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
our $VERSION = '0.20'; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
my $user_agent = "LWP::Simple::REST"; |
39
|
|
|
|
|
|
|
my $lwp = LWP::UserAgent->new; |
40
|
|
|
|
|
|
|
my $response; |
41
|
|
|
|
|
|
|
|
42
|
0
|
|
|
0
|
0
|
0
|
sub user_agent { $lwp->agent( $_[0] ) } |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
0
|
0
|
0
|
sub response { $response } |
45
|
|
|
|
|
|
|
|
46
|
4
|
|
|
4
|
1
|
694848
|
sub plain { return ($_[0]->content ) } |
47
|
|
|
|
|
|
|
|
48
|
2
|
|
|
2
|
1
|
233435
|
sub json { return decode_json($_[0]->content ) } |
49
|
|
|
|
|
|
|
|
50
|
1
|
50
|
|
1
|
0
|
118463
|
sub headers { return $_[0] ? $_[0]->headers : $response->headers }; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub POST { |
53
|
2
|
|
|
2
|
1
|
15
|
my ( $url, $arguments, $content ) = @_; |
54
|
|
|
|
|
|
|
|
55
|
2
|
|
|
|
|
52
|
$response = $lwp->post( $url, $arguments ); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub PUT { |
59
|
1
|
|
|
1
|
1
|
6
|
my ( $url, $arguments ) = @_; |
60
|
|
|
|
|
|
|
|
61
|
1
|
|
|
|
|
20
|
$response = $lwp->put( $url, $arguments ); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub GET { |
65
|
2
|
|
|
2
|
1
|
14
|
my ( $url, $arguments ) = @_; |
66
|
|
|
|
|
|
|
|
67
|
2
|
|
|
|
|
26
|
$arguments = _parameters( $arguments ); |
68
|
|
|
|
|
|
|
|
69
|
2
|
|
|
|
|
45
|
$response = $lwp->get( $url, $arguments ); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub DELETE { |
73
|
1
|
|
|
1
|
|
7
|
my ( $url, $arguments ) = @_; |
74
|
|
|
|
|
|
|
|
75
|
1
|
|
|
|
|
7
|
$arguments = _parameters( $arguments ); |
76
|
|
|
|
|
|
|
|
77
|
1
|
|
|
|
|
31
|
$response = $lwp->delete( $url, $arguments ); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub HEAD { |
81
|
1
|
|
|
1
|
1
|
10
|
my ( $url, $arguments ) = @_; |
82
|
|
|
|
|
|
|
|
83
|
1
|
|
|
|
|
17
|
$arguments = _parameters( $arguments ); |
84
|
|
|
|
|
|
|
|
85
|
1
|
|
|
|
|
21
|
$response = $lwp->head( $url, $arguments ); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub _parameters { |
89
|
4
|
|
|
4
|
|
20
|
my ( $arguments ) = @_; |
90
|
4
|
|
|
|
|
20
|
my @parameters; |
91
|
4
|
|
|
|
|
28
|
while( my ( $key, $value ) = each %{ $arguments } ) { |
|
7
|
|
|
|
|
81
|
|
92
|
3
|
|
|
|
|
30
|
push @parameters, "$key=$value"; |
93
|
|
|
|
|
|
|
} |
94
|
4
|
|
|
|
|
37
|
return '?' . ( join '&', @parameters ); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub upload_post { |
98
|
0
|
|
|
0
|
0
|
0
|
my ( $url, $json, $filename ) = @_; |
99
|
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
0
|
my $ua = LWP::UserAgent->new; |
101
|
0
|
|
|
|
|
0
|
$ua->agent('RESTClient'); |
102
|
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
0
|
my $response = $ua->post( |
104
|
|
|
|
|
|
|
$url, |
105
|
|
|
|
|
|
|
[ |
106
|
|
|
|
|
|
|
file => [ $filename ], |
107
|
|
|
|
|
|
|
], |
108
|
|
|
|
|
|
|
'Content_Type' => 'form-data', |
109
|
|
|
|
|
|
|
); |
110
|
|
|
|
|
|
|
|
111
|
0
|
|
|
|
|
0
|
return answer( $response ); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# |
115
|
|
|
|
|
|
|
# The functions above are kept for the sake of compatibility |
116
|
|
|
|
|
|
|
# |
117
|
|
|
|
|
|
|
|
118
|
1
|
|
|
1
|
0
|
2001564
|
sub http_get { plain &GET } |
119
|
|
|
|
|
|
|
|
120
|
1
|
|
|
1
|
0
|
2001453
|
sub http_post { plain &POST } |
121
|
|
|
|
|
|
|
|
122
|
1
|
|
|
1
|
0
|
2001037
|
sub http_put { plain &PUT } |
123
|
|
|
|
|
|
|
|
124
|
1
|
|
|
1
|
0
|
2001228
|
sub http_delete { plain &DELETE } |
125
|
|
|
|
|
|
|
|
126
|
1
|
|
|
1
|
0
|
2001291
|
sub http_head { headers &HEAD } |
127
|
|
|
|
|
|
|
|
128
|
1
|
|
|
1
|
0
|
2001505
|
sub json_post { json &POST } |
129
|
|
|
|
|
|
|
|
130
|
0
|
|
|
0
|
0
|
0
|
sub json_put { json &PUT } |
131
|
|
|
|
|
|
|
|
132
|
1
|
|
|
1
|
0
|
2001330
|
sub json_get { json &GET } |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub answer { |
135
|
0
|
|
|
0
|
0
|
|
my ( $response ) = @_; |
136
|
|
|
|
|
|
|
|
137
|
0
|
|
|
|
|
|
my $http_code = $response->code(); |
138
|
0
|
|
|
|
|
|
my $return = $response->decoded_content; |
139
|
|
|
|
|
|
|
|
140
|
0
|
0
|
|
|
|
|
if ( $response->is_success ){ |
141
|
0
|
|
|
|
|
|
my $answer; |
142
|
0
|
0
|
|
|
|
|
if ( $http_code =~ /(2\d\d)/ ){ |
143
|
0
|
0
|
|
|
|
|
if ( $1 == 204 ){ |
144
|
0
|
|
|
|
|
|
return $return; |
145
|
|
|
|
|
|
|
}else{ |
146
|
0
|
|
|
|
|
|
return decode_json( $return ); |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
} |
150
|
0
|
|
|
|
|
|
my $status = $response->status_line; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head1 NAME |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
LWP::Simple::REST - A simple funcional interface to LWP::UserAgent, focused to |
156
|
|
|
|
|
|
|
quick use and test HTTP/REST apis |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head1 VERSION |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Version 0.2 |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head1 SYNOPSIS |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
This module is a simple wrapper for simple http requests. It provides functions |
165
|
|
|
|
|
|
|
to create clients to whatever http services, mainly REST ones. The goal is to be |
166
|
|
|
|
|
|
|
simple and straight forward. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
This version 0.2 tries to make it simpler, instead of have dozens of methods we just |
169
|
|
|
|
|
|
|
have the basic method and let you combine them as you need. The old ones are kept for |
170
|
|
|
|
|
|
|
compatibilty but are now deprecated. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
This is the actual main example: |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
use LWP::Simple::REST qw/POST json plain/; |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
my $foo = plain POST ( "http://example.org", { example => "1", show => "all" } ); |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
or decoding the interface |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
my $foo = json POST ( "http://example.org", { example => "1", show => "all" } ); |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
In fact, the old http_post routine is actually just a wrapper for plain POST |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
The http verbs are all caps, and normal methods are in low case. You need to ask |
185
|
|
|
|
|
|
|
to export them. |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
All http verbs methods receive an url and a hashref with parameters. The other methods |
190
|
|
|
|
|
|
|
have each one it own interface. |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=head2 GET, PUT, POST, DELETE, HEAD |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
They are the http verbs, they return an HTTP::Response object |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head2 plain |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
Receives an response and returns just the content, usually the calls will be like |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
my $var = plain POST ( $url, $arguments ) |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=head2 json |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
Same for above, but also decode_json the content |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head2 Old deprecated methods: |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
http_get, http_post, http_delete http_head http_upload json_get json_post |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
Are old methods kept just for compatibility, actually it will be preferred to |
211
|
|
|
|
|
|
|
use the new interface: |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
headers HEAD $url, $parameters |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=head1 AUTHOR |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
RECSKY, C<< >> |
218
|
|
|
|
|
|
|
GONCALES, C<< >> |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=head1 BUGS |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
223
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
224
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=head1 SUPPORT |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
perldoc LWP::Simple::REST |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
Usually we are on irc on irc.perl.org. |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
#sao-paulo.pm |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=over 4 |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
L |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
L |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
=item * CPAN Ratings |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
L |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=back |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
Copyright 2014 GONCALES |
255
|
|
|
|
|
|
|
Copyright 2014 RECSKY |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
258
|
|
|
|
|
|
|
under the terms of the the Artistic License (2.0). You may obtain a |
259
|
|
|
|
|
|
|
copy of the full license at: |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
L |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
=cut |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
1; # End of LWP::Simple::REST |