| 1 | package WebService::Async::UserAgent::NaHTTP; | ||
| 2 | $WebService::Async::UserAgent::NaHTTP::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::NaHTTP - make requests using L<Net::Async::HTTP> | ||
| 11 | |||
| 12 | =head1 VERSION | ||
| 13 | |||
| 14 | version 0.005 | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | Provides a L</request> method which will use L<Net::Async::HTTP> 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 Net::Async::HTTP; | ||
| 26 | use HTTP::Cookies; | ||
| 27 | |||
| 28 | =head2 request | ||
| 29 | |||
| 30 | Issues the request. Expects a single L<HTTP::Request> object, | ||
| 31 | and returns a L<Future> which will resolve to the decoded | ||
| 32 | response content on success, or the failure reason on failure. | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | sub request { | ||
| 37 | my ($self, $req, %args) = @_; | ||
| 38 | my $host = delete $args{host}; | ||
| 39 | my $port = delete $args{port}; | ||
| 40 | unless(defined($host) && defined($port)) { | ||
| 41 | my ($h, $p) = split /:/, '' . $req->uri->host_port; | ||
| 42 | $host //= $h; | ||
| 43 | $port //= $p; | ||
| 44 | } | ||
| 45 | my $ssl = delete($args{ssl}) // ($req->uri->scheme eq 'https'); | ||
| 46 | $self->ua->do_request( | ||
| 47 | request => $req, | ||
| 48 | host => $host, | ||
| 49 | port => $port || ($ssl ? 443 : 80), | ||
| 50 | SSL => ($ssl ? 1 : 0), | ||
| 51 | %args, | ||
| 52 | )->transform( | ||
| 53 | done => sub { | ||
| 54 | shift->decoded_content | ||
| 55 | }, | ||
| 56 | ); | ||
| 57 | } | ||
| 58 | |||
| 59 | =head2 ua | ||
| 60 | |||
| 61 | Returns a L<Net::Async::HTTP> instance. | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | sub ua { | ||
| 66 | my $self = shift; | ||
| 67 | unless($self->{ua}) { | ||
| 68 | my $ua = Net::Async::HTTP->new( | ||
| 69 | user_agent => $self->user_agent, | ||
| 70 | max_connections_per_host => ($self->parallel // 1), | ||
| 71 | pipeline => 0, | ||
| 72 | fail_on_error => 1, | ||
| 73 | timeout => $self->timeout, | ||
| 74 | cookie_jar => ($self->{jar} ||= HTTP::Cookies->new), | ||
| 75 | decode_content => 1, | ||
| 76 | $self->ssl_args, | ||
| 77 | ); | ||
| 78 | $self->loop->add($ua); | ||
| 79 | $self->{ua} = $ua; | ||
| 80 | } | ||
| 81 | $self->{ua}; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub loop { shift->{loop} } | ||
| 85 | |||
| 86 | 1; | ||
| 87 | |||
| 88 | =head1 AUTHOR | ||
| 89 | |||
| 90 | Tom Molesworth <TEAM@cpan.org> | ||
| 91 | |||
| 92 | =head1 LICENSE | ||
| 93 | |||
| 94 | Copyright Tom Molesworth 2012-2015. Licensed under the same terms as Perl itself. |