| 1 | package WebService::Async::UserAgent::MojoUA; | ||
| 2 | $WebService::Async::UserAgent::MojoUA::VERSION = '0.004'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use parent qw(WebService::Async::UserAgent); | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | WebService::Async::UserAgent::MojoUA - make requests using L<Mojo::UserAgent> | ||
| 11 | |||
| 12 | =head1 VERSION | ||
| 13 | |||
| 14 | version 0.004 | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | Provides a L</request> method which will use L<Mojo::UA> 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 Mojo::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 $method = lc $req->method; | ||
| 47 | my $resp = $self->ua->$method(''.$req->uri => { map {; $_ => ''.$req->header($_) } $req->header_field_names } => $req->content); | ||
| 48 | if(my $res = $resp->success) { | ||
| 49 | return Future->done($res->body) | ||
| 50 | } | ||
| 51 | |||
| 52 | my $status = join ' ', $resp->error; | ||
| 53 | return Future->fail($status, $resp, $req) | ||
| 54 | } | ||
| 55 | |||
| 56 | =head2 ua | ||
| 57 | |||
| 58 | Returns the L<LWP::UserAgent> instance. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | sub ua { shift->{ua} ||= Mojo::UserAgent->new } | ||
| 63 | |||
| 64 | 1; | ||
| 65 | |||
| 66 | =head1 AUTHOR | ||
| 67 | |||
| 68 | Tom Molesworth <cpan@perlsite.co.uk> | ||
| 69 | |||
| 70 | =head1 LICENSE | ||
| 71 | |||
| 72 | Copyright Tom Molesworth 2012-2015. Licensed under the same terms as Perl itself. |