| 1 | package LWP::Simple::REST; | ||||||||||
| 2 | |||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 8 | ▶ | use warnings FATAL => 'all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 8 | ▶ | use Cwd; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 8 | ▶ | use Exporter qw( import ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 8 | 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 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 8 | ▶ | use HTTP::Request; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | 8 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | 8 | ▶ | use JSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | |||||||||||
| 36 | 8 | our $VERSION = '0.20'; | |||||||||
| 37 | |||||||||||
| 38 | 8 | my $user_agent = "LWP::Simple::REST"; | |||||||||
| 39 | 8 | my $lwp = LWP::UserAgent->new; | |||||||||
| 40 | 8 | my $response; | |||||||||
| 41 | |||||||||||
| 42 | 0 | ▶ | sub user_agent { $lwp->agent( $_[0] ) } | ||||||||
|
Subroutine
user_agent: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 43 | |||||||||||
| 44 | 0 | ▶ | sub response { $response } | ||||||||
|
Subroutine
response: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 45 | |||||||||||
| 46 | 4 | ▶ | sub plain { return ($_[0]->content ) } | ||||||||
|
Subroutine
plain: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 47 | |||||||||||
| 48 | 2 | ▶ | sub json { return decode_json($_[0]->content ) } | ||||||||
|
Subroutine
json: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 49 | |||||||||||
| 50 | 1 | ▶ | sub headers { return $_[0] ? $_[0]->headers : $response->headers }; | ||||||||
|
Subroutine
headers: called CC=2 SCAR=7.6
Pod
undocumented
Branch: 1 branch
| |||||||||||
| 51 | |||||||||||
| 52 | 2 | ▶ | sub POST { | ||||||||
|
Subroutine
POST: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 53 | 2 | my ( $url, $arguments, $content ) = @_; | |||||||||
| 54 | |||||||||||
| 55 | 2 | $response = $lwp->post( $url, $arguments ); | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | 1 | ▶ | sub PUT { | ||||||||
|
Subroutine
PUT: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 59 | 1 | my ( $url, $arguments ) = @_; | |||||||||
| 60 | |||||||||||
| 61 | 1 | $response = $lwp->put( $url, $arguments ); | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 2 | ▶ | sub GET { | ||||||||
|
Subroutine
GET: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 65 | 2 | my ( $url, $arguments ) = @_; | |||||||||
| 66 | |||||||||||
| 67 | 2 | $arguments = _parameters( $arguments ); | |||||||||
| 68 | |||||||||||
| 69 | 2 | $response = $lwp->get( $url, $arguments ); | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 1 | ▶ | sub DELETE { | ||||||||
|
Subroutine
DELETE: called CC=1 SCAR=0.0
| |||||||||||
| 73 | 1 | my ( $url, $arguments ) = @_; | |||||||||
| 74 | |||||||||||
| 75 | 1 | $arguments = _parameters( $arguments ); | |||||||||
| 76 | |||||||||||
| 77 | 1 | $response = $lwp->delete( $url, $arguments ); | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 1 | ▶ | sub HEAD { | ||||||||
|
Subroutine
HEAD: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 81 | 1 | my ( $url, $arguments ) = @_; | |||||||||
| 82 | |||||||||||
| 83 | 1 | $arguments = _parameters( $arguments ); | |||||||||
| 84 | |||||||||||
| 85 | 1 | $response = $lwp->head( $url, $arguments ); | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 4 | ▶ | sub _parameters { | ||||||||
|
Subroutine
_parameters: called CC=2 SCAR=6.9
| |||||||||||
| 89 | 4 | my ( $arguments ) = @_; | |||||||||
| 90 | 4 | my @parameters; | |||||||||
| 91 | 4 | ▶ | while( my ( $key, $value ) = each %{ $arguments } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 3 | push @parameters, "$key=$value"; | |||||||||
| 93 | } | ||||||||||
| 94 | 4 | return '?' . ( join '&', @parameters ); | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 0 | ▶ | sub upload_post { | ||||||||
|
Subroutine
upload_post: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 98 | 0 | my ( $url, $json, $filename ) = @_; | |||||||||
| 99 | |||||||||||
| 100 | 0 | my $ua = LWP::UserAgent->new; | |||||||||
| 101 | 0 | $ua->agent('RESTClient'); | |||||||||
| 102 | |||||||||||
| 103 | 0 | my $response = $ua->post( | |||||||||
| 104 | $url, | ||||||||||
| 105 | [ | ||||||||||
| 106 | file => [ $filename ], | ||||||||||
| 107 | ], | ||||||||||
| 108 | 'Content_Type' => 'form-data', | ||||||||||
| 109 | ); | ||||||||||
| 110 | |||||||||||
| 111 | 0 | return answer( $response ); | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | # | ||||||||||
| 115 | # The functions above are kept for the sake of compatibility | ||||||||||
| 116 | # | ||||||||||
| 117 | |||||||||||
| 118 | 1 | ▶ | sub http_get { plain &GET } | ||||||||
|
Subroutine
http_get: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 119 | |||||||||||
| 120 | 1 | ▶ | sub http_post { plain &POST } | ||||||||
|
Subroutine
http_post: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 121 | |||||||||||
| 122 | 1 | ▶ | sub http_put { plain &PUT } | ||||||||
|
Subroutine
http_put: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 123 | |||||||||||
| 124 | 1 | ▶ | sub http_delete { plain &DELETE } | ||||||||
|
Subroutine
http_delete: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 125 | |||||||||||
| 126 | 1 | ▶ | sub http_head { headers &HEAD } | ||||||||
|
Subroutine
http_head: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 127 | |||||||||||
| 128 | 1 | ▶ | sub json_post { json &POST } | ||||||||
|
Subroutine
json_post: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 129 | |||||||||||
| 130 | 0 | ▶ | sub json_put { json &PUT } | ||||||||
|
Subroutine
json_put: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 131 | |||||||||||
| 132 | 1 | ▶ | sub json_get { json &GET } | ||||||||
|
Subroutine
json_get: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 133 | |||||||||||
| 134 | 0 | ▶ | sub answer { | ||||||||
|
Subroutine
answer: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 135 | 0 | my ( $response ) = @_; | |||||||||
| 136 | |||||||||||
| 137 | 0 | my $http_code = $response->code(); | |||||||||
| 138 | 0 | my $return = $response->decoded_content; | |||||||||
| 139 | |||||||||||
| 140 | 0 | ▶ | if ( $response->is_success ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | 0 | my $answer; | |||||||||
| 142 | 0 | ▶ | if ( $http_code =~ /(2\d\d)/ ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 143 | 0 | ▶ | if ( $1 == 204 ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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<< <recsky at cpan.org> >> | ||||||||||
| 218 | GONCALES, C<< <italo.goncales at gmail.com> >> | ||||||||||
| 219 | |||||||||||
| 220 | =head1 BUGS | ||||||||||
| 221 | |||||||||||
| 222 | Please report any bugs or feature requests to C<bug-lwp-simple-rest at rt.cpan.org>, or through | ||||||||||
| 223 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=LWP-Simple-REST>. 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=LWP-Simple-REST> | ||||||||||
| 241 | |||||||||||
| 242 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 243 | |||||||||||
| 244 | L<http://annocpan.org/dist/LWP-Simple-REST> | ||||||||||
| 245 | |||||||||||
| 246 | =item * CPAN Ratings | ||||||||||
| 247 | |||||||||||
| 248 | L<http://cpanratings.perl.org/d/LWP-Simple-REST> | ||||||||||
| 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<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||
| 262 | |||||||||||
| 263 | =cut | ||||||||||
| 264 | |||||||||||
| 265 | 8 | 1; # End of LWP::Simple::REST | |||||||||