File Coverage

blib/lib/LWP/Simple/REST.pm
Criterion Covered Total %
statement 81 102 79.4
branch 0 6 0.0
condition n/a
subroutine 16 19 84.2
pod 6 10 60.0
total 103 137 75.1


line stmt bran cond sub pod time code
1             package LWP::Simple::REST;
2              
3 8     8   44350 use strict;
  8         15  
  8         279  
4 8     8   35 use warnings FATAL => 'all';
  8         8  
  8         293  
5              
6 8     8   30 use Cwd;
  8         12  
  8         482  
7 8     8   4514 use Data::Structure::Util qw( unbless );
  8         54098  
  8         697  
8              
9 8     8   64 use Exporter qw( import );
  8         14  
  8         403  
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   8851 use LWP::UserAgent;
  8         281369  
  8         273  
24 8     8   62 use HTTP::Request;
  8         12  
  8         164  
25 8     8   4413 use Try::Tiny;
  8         9452  
  8         430  
26 8     8   3484 use JSON;
  8         58553  
  8         55  
27              
28             our $VERSION = '0.092';
29              
30             my $user_agent = "LWP::Simple::REST";
31              
32             sub http_get {
33 1     1 1 2001501 my ( $url, $arguments ) = @_;
34              
35 1         56 my $ua = LWP::UserAgent->new;
36 1         4630 $ua->agent($user_agent);
37              
38             # Pass a url sanitizer
39 1         101 my @parameters;
40 1         10 while ( my ( $key, $value ) = each %{ $arguments } ) {
  2         18  
41 1         5 push @parameters, "$key=$value";
42             }
43 1         7 my $parameters_for_url = join "&", @parameters;
44 1         17 my $response = $ua->get( $url . "?$parameters_for_url" );
45              
46 1         151411 return $response->content;
47             }
48              
49              
50             sub http_post {
51 1     1 1 2001922 my ( $url, $arguments ) = @_;
52              
53 1         58 my $ua = LWP::UserAgent->new;
54 1         3572 $ua->agent($user_agent);
55              
56 1         74 my $response = $ua->post( $url,
57             $arguments,
58             );
59              
60 1         109736 return $response->content;
61             }
62              
63             sub http_put {
64 1     1 0 2001153 my ( $url, $arguments ) = @_;
65              
66 1         51 my $ua = LWP::UserAgent->new;
67 1         4446 $ua->agent($user_agent);
68              
69 1         91 my $response = $ua->put( $url,
70             $arguments,
71             );
72              
73 1         119301 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 2001108 my ( $url, $arguments ) = @_;
95              
96 1         38 my $ua = LWP::UserAgent->new;
97 1         4655 $ua->agent('RESTClient');
98              
99 1         51 my @parameters;
100 1         2 while ( my ( $key, $value ) = each %{ $arguments } ) {
  2         12  
101 1         6 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         141882 return $response->content;
109              
110             }
111              
112             sub http_head {
113 1     1 1 2001397 my ( $url, $arguments ) = @_;
114              
115 1         48 my $ua = LWP::UserAgent->new;
116 1         3112 $ua->agent($user_agent);
117              
118 1         68 my @parameters;
119 1         3 while ( my ( $key, $value ) = each %{ $arguments } ){
  2         8  
120 1         4 push @parameters, "$key=$value";
121             }
122 1         2 my $parameters_for_url = join "&", @parameters;
123 1         19 my $response = $ua->head( $url . "?$parameters_for_url" );
124              
125 1         100166 return $response->headers;
126              
127             }
128              
129             sub json_post {
130 1     1 1 2001567 my ( $url, $arguments ) = @_;
131              
132 1         45 my $ua = LWP::UserAgent->new;
133 1         3431 $ua->agent($user_agent);
134              
135 1         76 my $response = $ua->post( $url,
136             $arguments,
137             );
138              
139 1         128087 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 2001306 my ( $url, $arguments ) = @_;
157              
158 1         33 my $ua = LWP::UserAgent->new;
159 1         3169 $ua->agent($user_agent);
160              
161             # Pass a url sanitazier
162 1         48 my @parameters;
163 1         2 while ( my ( $key, $value ) = each %{ $arguments } ) {
  1         5  
164 0         0 push @parameters, "$key=$value";
165             }
166 1         2 my $parameters_for_url = join "&", @parameters;
167 1         10 my $response = $ua->get( $url . "?$parameters_for_url" );
168              
169 1         154652 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.092
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