| 1 | package LWP::Simple::REST; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings FATAL => 'all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use Exporter qw( import ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | our @EXPORT_OK = qw/http_get http_post http_delete url/; | ||
| 8 | |||
| 9 | 1 | ▶ | use LWP::UserAgent; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use HTTP::Request; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use Try::Tiny; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use JSON; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | LWP::Simple::REST - A simple procedural interface do http verbs | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | This module is a simple wrapper for simple http requests. | ||
| 21 | |||
| 22 | This is a classical example, to post a information to a server. | ||
| 23 | |||
| 24 | use LWP::Simple::REST qw/http_post/; | ||
| 25 | |||
| 26 | my $foo = http_post( "http://example.org", { example => "1", show => "all" } ); | ||
| 27 | ... | ||
| 28 | |||
| 29 | =head1 SUBROUTINES/METHODS | ||
| 30 | |||
| 31 | =head2 http_get | ||
| 32 | |||
| 33 | Sends a http get to an url on parameters | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | sub http_get { | ||
| 38 | my ( $url, %arguments ) = @_; | ||
| 39 | |||
| 40 | my $ua = LWP::UserAgent->new; | ||
| 41 | $ua->agent('RESTClient'); | ||
| 42 | |||
| 43 | # Pass a url sanitazier | ||
| 44 | my @parameters; | ||
| 45 | while ( my ( $key, $value ) = each %arguments ) { | ||
| 46 | push @parameters, "$key=$value"; | ||
| 47 | } | ||
| 48 | my $parameters_for_url = join "&", @parameters; | ||
| 49 | |||
| 50 | my $response = $ua->get( $url . "?$parameters_for_url" ); | ||
| 51 | |||
| 52 | return $response; | ||
| 53 | } | ||
| 54 | |||
| 55 | =head2 http_post | ||
| 56 | |||
| 57 | Sends a http post to an url on parameters | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | sub http_post { | ||
| 62 | my ( $url, %arguments ) = @_; | ||
| 63 | |||
| 64 | my $ua = LWP::UserAgent->new; | ||
| 65 | $ua->agent('RESTClient'); | ||
| 66 | |||
| 67 | #wrong to do fix interface | ||
| 68 | if ( exists $arguments{ json } ){ | ||
| 69 | my $request = HTTP::Request->new( 'POST', $url ); | ||
| 70 | $request->header( 'Content-Type' => 'application/json' ); | ||
| 71 | $request->content( encode_json( $arguments{ json } )); | ||
| 72 | |||
| 73 | return answer( $ua->request( $request ) ); | ||
| 74 | }else{ | ||
| 75 | |||
| 76 | my $response = $ua->post( $url, | ||
| 77 | \%arguments, | ||
| 78 | ); | ||
| 79 | |||
| 80 | return $response; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | =head2 http_delete | ||
| 85 | |||
| 86 | Sends a delete request for the url | ||
| 87 | |||
| 88 | =cut | ||
| 89 | |||
| 90 | sub http_delete { | ||
| 91 | my ( $url, %arguments ) = @_; | ||
| 92 | |||
| 93 | my $ua = LWP::UserAgent->new; | ||
| 94 | $ua->agent('RESTClient'); | ||
| 95 | |||
| 96 | my @parameters; | ||
| 97 | while ( my ( $key, $value ) = each %arguments ) { | ||
| 98 | push @parameters, "$key=$value"; | ||
| 99 | } | ||
| 100 | |||
| 101 | my $parameters_for_url = join "&", @parameters; | ||
| 102 | |||
| 103 | my $response = $ua->delete( $url . "?$parameters_for_url" ); | ||
| 104 | |||
| 105 | return $response; | ||
| 106 | |||
| 107 | } | ||
| 108 | |||
| 109 | =head2 http_upload | ||
| 110 | |||
| 111 | Sends an Upload to url | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | # this interface is just plain terrible | ||
| 116 | sub http_upload { | ||
| 117 | my ( $url, $json, $filename ) = @_; | ||
| 118 | |||
| 119 | my $ua = LWP::UserAgent->new; | ||
| 120 | $ua->agent('RESTClient'); | ||
| 121 | |||
| 122 | my $response = $ua->post( | ||
| 123 | $url, | ||
| 124 | [ | ||
| 125 | meta => $json, | ||
| 126 | file => [ $filename ], | ||
| 127 | ], | ||
| 128 | 'Content_Type' => 'form-data', | ||
| 129 | ); | ||
| 130 | |||
| 131 | return answer( $response ); | ||
| 132 | |||
| 133 | } | ||
| 134 | |||
| 135 | |||
| 136 | =head2 answer | ||
| 137 | |||
| 138 | If you expects a JSON encoded response, this decode the response | ||
| 139 | |||
| 140 | =cut | ||
| 141 | |||
| 142 | sub answer { | ||
| 143 | my ( $response ) = @_; | ||
| 144 | |||
| 145 | my $http_code = $response->code(); | ||
| 146 | my $return = $response->decoded_content; | ||
| 147 | |||
| 148 | if ( $response->is_success ){ | ||
| 149 | my $answer; | ||
| 150 | if ( $http_code =~ /(2\d\d)/ ){ | ||
| 151 | if ( $1 == 204 ){ | ||
| 152 | return $return; | ||
| 153 | }else{ | ||
| 154 | return decode_json( $return ); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | } | ||
| 158 | my $status = $response->status_line; | ||
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | |||
| 163 | =head1 AUTHOR | ||
| 164 | |||
| 165 | GONCALES, C<< <italo.goncales at gmail.com> >> | ||
| 166 | RECSKY, C<< <cartas at frederico.me> >> | ||
| 167 | |||
| 168 | =head1 BUGS | ||
| 169 | |||
| 170 | Please report any bugs or feature requests to C<bug-lwp-simple-rest at rt.cpan.org>, or through | ||
| 171 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=LWP-Simple-REST>. I will be notified, and then you'll | ||
| 172 | automatically be notified of progress on your bug as I make changes. | ||
| 173 | |||
| 174 | |||
| 175 | =head1 SUPPORT | ||
| 176 | |||
| 177 | You can find documentation for this module with the perldoc command. | ||
| 178 | |||
| 179 | perldoc LWP::Simple::REST | ||
| 180 | |||
| 181 | |||
| 182 | You can also look for information at: | ||
| 183 | |||
| 184 | =over 4 | ||
| 185 | |||
| 186 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 187 | |||
| 188 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=LWP-Simple-REST> | ||
| 189 | |||
| 190 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 191 | |||
| 192 | L<http://annocpan.org/dist/LWP-Simple-REST> | ||
| 193 | |||
| 194 | =item * CPAN Ratings | ||
| 195 | |||
| 196 | L<http://cpanratings.perl.org/d/LWP-Simple-REST> | ||
| 197 | |||
| 198 | =item * Search CPAN | ||
| 199 | |||
| 200 | L<http://search.cpan.org/dist/LWP-Simple-REST/> | ||
| 201 | |||
| 202 | =back | ||
| 203 | |||
| 204 | =head1 LICENSE AND COPYRIGHT | ||
| 205 | |||
| 206 | Copyright 2014 GONCALES | ||
| 207 | Copyright 2014 RECSKY | ||
| 208 | |||
| 209 | This program is free software; you can redistribute it and/or modify it | ||
| 210 | under the terms of the the Artistic License (2.0). You may obtain a | ||
| 211 | copy of the full license at: | ||
| 212 | |||
| 213 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||
| 214 | |||
| 215 | Any use, modification, and distribution of the Standard or Modified | ||
| 216 | Versions is governed by this Artistic License. By using, modifying or | ||
| 217 | distributing the Package, you accept this license. Do not use, modify, | ||
| 218 | or distribute the Package, if you do not accept this license. | ||
| 219 | |||
| 220 | If your Modified Version has been derived from a Modified Version made | ||
| 221 | by someone other than you, you are nevertheless required to ensure that | ||
| 222 | your Modified Version complies with the requirements of this license. | ||
| 223 | |||
| 224 | This license does not grant you the right to use any trademark, service | ||
| 225 | mark, tradename, or logo of the Copyright Holder. | ||
| 226 | |||
| 227 | This license includes the non-exclusive, worldwide, free-of-charge | ||
| 228 | patent license to make, have made, use, offer to sell, sell, import and | ||
| 229 | otherwise transfer the Package with respect to any patent claims | ||
| 230 | licensable by the Copyright Holder that are necessarily infringed by the | ||
| 231 | Package. If you institute patent litigation (including a cross-claim or | ||
| 232 | counterclaim) against any party alleging that the Package constitutes | ||
| 233 | direct or contributory patent infringement, then this Artistic License | ||
| 234 | to you shall terminate on the date that such litigation is filed. | ||
| 235 | |||
| 236 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||
| 237 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||
| 238 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||
| 239 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||
| 240 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||
| 241 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||
| 242 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||
| 243 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 244 | |||
| 245 | |||
| 246 | =cut | ||
| 247 | |||
| 248 | 1; # End of LWP::Simple::REST | ||