| 1 | package autobox::REST; |
|---|
| 2 | |
|---|
| 3 | use warnings; |
|---|
| 4 | use strict; |
|---|
| 5 | |
|---|
| 6 | use autobox; |
|---|
| 7 | use LWP::UserAgent; |
|---|
| 8 | use HTTP::Request; |
|---|
| 9 | |
|---|
| 10 | our $VERSION = '0.01'; |
|---|
| 11 | |
|---|
| 12 | sub SCALAR::get { |
|---|
| 13 | my $uri = shift; |
|---|
| 14 | my $request_content = shift; |
|---|
| 15 | |
|---|
| 16 | my $response = $uri->_request('GET',$request_content); |
|---|
| 17 | return $response; |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | sub SCALAR::post { |
|---|
| 21 | my $uri = shift; |
|---|
| 22 | my $request_content = shift; |
|---|
| 23 | |
|---|
| 24 | my $response = $uri->_request('POST',$request_content); |
|---|
| 25 | return $response; |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | sub SCALAR::put { |
|---|
| 29 | my $uri = shift; |
|---|
| 30 | my $request_content = shift; |
|---|
| 31 | |
|---|
| 32 | my $response = $uri->_request('PUT',$request_content); |
|---|
| 33 | return $response; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | sub SCALAR::delete { |
|---|
| 37 | my $uri = shift; |
|---|
| 38 | my $request_content = shift; |
|---|
| 39 | |
|---|
| 40 | my $response = $uri->_request('DELETE',$request_content); |
|---|
| 41 | return $response; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | sub SCALAR::_request{ |
|---|
| 45 | my $uri = shift; |
|---|
| 46 | my $request_method = shift; |
|---|
| 47 | my $request_content = shift; |
|---|
| 48 | |
|---|
| 49 | my $request = HTTP::Request->new($request_method => $uri); |
|---|
| 50 | |
|---|
| 51 | $request->content($request_content) |
|---|
| 52 | if( $request_content ); |
|---|
| 53 | |
|---|
| 54 | my $ua = LWP::UserAgent->new; |
|---|
| 55 | my $response = $ua->request($request); |
|---|
| 56 | if ( $response->is_success ) { |
|---|
| 57 | return $response; |
|---|
| 58 | }else{ |
|---|
| 59 | warn $response->status_line,"\n"; |
|---|
| 60 | return $response; |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | 1; |
|---|
| 66 | |
|---|
| 67 | =head1 NAME |
|---|
| 68 | |
|---|
| 69 | autobox::REST - |
|---|
| 70 | |
|---|
| 71 | =head1 SYNOPSIS |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | use autobox; |
|---|
| 75 | use autobox::REST; |
|---|
| 76 | |
|---|
| 77 | my $response = "http://example.com/rest_api/"->post("foo=1&bar=hoo") |
|---|
| 78 | |
|---|
| 79 | "http://example.com/rest_api/"->get(); |
|---|
| 80 | |
|---|
| 81 | "http://example.com/rest_api/"->put("foo=100"); |
|---|
| 82 | |
|---|
| 83 | "http://example.com/rest_api/"->delete(); |
|---|
| 84 | |
|---|
| 85 | =head1 DESCRIPTION |
|---|
| 86 | |
|---|
| 87 | This module calls the uri by HTTP METHOD(POST,GET,PUT,DELETE). |
|---|
| 88 | |
|---|
| 89 | =head1 METHODS |
|---|
| 90 | |
|---|
| 91 | =head2 post |
|---|
| 92 | |
|---|
| 93 | This method calls the uri by POST, and returns a L<HTTP::Response> object. |
|---|
| 94 | |
|---|
| 95 | =head2 get |
|---|
| 96 | |
|---|
| 97 | This method calls the uri by GET, and returns a L<HTTP::Response> object. |
|---|
| 98 | |
|---|
| 99 | =head2 put |
|---|
| 100 | |
|---|
| 101 | This method calls the uri by PUT, and returns a L<HTTP::Response> object. |
|---|
| 102 | |
|---|
| 103 | =head2 delete |
|---|
| 104 | |
|---|
| 105 | This method calls the uri by DELETE, and returns a L<HTTP::Response> object. |
|---|
| 106 | |
|---|
| 107 | =head1 |
|---|
| 108 | |
|---|
| 109 | This program is free software; you can redistribute |
|---|
| 110 | it and/or modify it under the same terms as Perl itself. |
|---|
| 111 | |
|---|
| 112 | =head1 AUTHOR |
|---|
| 113 | |
|---|
| 114 | Akihito Takeda, C<< <takeda.akihito at gmail.com> >> |
|---|
| 115 | |
|---|
| 116 | =cut |
|---|
| 117 | |
|---|