| 1 | package WebService::Async::UserAgent::LWP; | ||
| 2 | $WebService::Async::UserAgent::LWP::VERSION = '0.005'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use parent qw(WebService::Async::UserAgent); | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | WebService::Async::UserAgent::LWP - make requests using L<LWP::UserAgent> | ||
| 11 | |||
| 12 | =head1 VERSION | ||
| 13 | |||
| 14 | version 0.005 | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | Provides a L</request> method which will use L<LWP::UserAgent> to make | ||
| 19 | requests and return a L<Future> containing the result. Used internally by | ||
| 20 | L<WebService::Async::UserAgent>. | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | use Future; | ||
| 25 | use LWP::UserAgent; | ||
| 26 | |||
| 27 | =head2 new | ||
| 28 | |||
| 29 | Instantiate. | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | sub new { my $class = shift; bless {@_}, $class } | ||
| 34 | |||
| 35 | =head2 request | ||
| 36 | |||
| 37 | Issues the request. Expects a single L<HTTP::Request> object, | ||
| 38 | and returns a L<Future> which will resolve to the decoded | ||
| 39 | response content on success, or the failure reason on failure. | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | sub request { | ||
| 44 | my $self = shift; | ||
| 45 | my $req = shift; | ||
| 46 | my $resp = $self->ua->request($req); | ||
| 47 | return Future->new->done($resp->decoded_content) if $resp->is_success; | ||
| 48 | |||
| 49 | my $status = join ' ', $resp->code, $resp->message; | ||
| 50 | return Future->new->fail($status, $resp, $req) | ||
| 51 | } | ||
| 52 | |||
| 53 | =head2 ua | ||
| 54 | |||
| 55 | Returns the L<LWP::UserAgent> instance. | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | sub ua { shift->{ua} ||= LWP::UserAgent->new } | ||
| 60 | |||
| 61 | 1; | ||
| 62 | |||
| 63 | =head1 AUTHOR | ||
| 64 | |||
| 65 | Tom Molesworth <TEAM@cpan.org> | ||
| 66 | |||
| 67 | =head1 LICENSE | ||
| 68 | |||
| 69 | Copyright Tom Molesworth 2012-2015. Licensed under the same terms as Perl itself. |