root/lang/perl/autobox-REST/trunk/lib/autobox/REST.pm @ 5380

Revision 5380, 2.2 kB (checked in by akihito, 6 years ago)

lang/perl/autobox-REST: release version 0.01

Line 
1package autobox::REST;
2
3use warnings;
4use strict;
5
6use autobox;
7use LWP::UserAgent;
8use HTTP::Request;
9
10our $VERSION = '0.01';
11
12sub 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
20sub 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
28sub 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
36sub 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
44sub 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
651;
66
67=head1 NAME
68
69autobox::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
87This module calls the uri by HTTP METHOD(POST,GET,PUT,DELETE).
88
89=head1 METHODS
90
91=head2 post
92
93This method calls the uri by POST, and returns a L<HTTP::Response> object.
94
95=head2 get
96
97This method calls the uri by GET, and returns a L<HTTP::Response> object.
98
99=head2 put
100
101This method calls the uri by PUT, and returns a L<HTTP::Response> object.
102
103=head2 delete
104
105This method calls the uri by DELETE, and returns a L<HTTP::Response> object.
106
107=head1
108
109This program is free software; you can redistribute
110it and/or modify it under the same terms as Perl itself.
111
112=head1 AUTHOR
113
114Akihito Takeda, C<< <takeda.akihito at gmail.com> >>
115
116=cut
117
Note: See TracBrowser for help on using the browser.