| 1 | package WebService::Async::UserAgent; | ||
| 2 | # ABSTRACT: HTTP useragent abstraction for webservices | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | our $VERSION = '0.005'; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | WebService::Async::UserAgent - common API for making HTTP requests to webservices | ||
| 11 | |||
| 12 | =head1 VERSION | ||
| 13 | |||
| 14 | version 0.005 | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | use strict; | ||
| 19 | use warnings; | ||
| 20 | use WebService::Async::UserAgent::NaHTTP; | ||
| 21 | my $ua = WebService::Async::UserAgent::NaHTTP->new(loop => $loop); | ||
| 22 | eval { | ||
| 23 | print "was OK" if $ua->get('...')->get->code == 200; | ||
| 24 | } or warn "Failed - $@"; | ||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | This is an early release, most things are undocumented and subject to change. | ||
| 29 | |||
| 30 | The intention is to provide an abstraction for webservice API calls without | ||
| 31 | hardcoding a dependency on a specific HTTP client (such as L<Net::Async::HTTP>). | ||
| 32 | Although there is very basic support for sync clients such as L<LWP::UserAgent>, | ||
| 33 | they are untested and only there as an example. That may change in future. | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | use URI; | ||
| 38 | use HTTP::Request; | ||
| 39 | use HTTP::Response; | ||
| 40 | |||
| 41 | =head1 METHODS | ||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | =head2 new | ||
| 46 | |||
| 47 | Instantiate. | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | sub new { my $class = shift; bless { @_ }, $class } | ||
| 52 | |||
| 53 | |||
| 54 | sub parallel { 0 } | ||
| 55 | |||
| 56 | sub timeout { 60 } | ||
| 57 | |||
| 58 | sub request { ... } | ||
| 59 | |||
| 60 | sub GET { | ||
| 61 | my ($self, $uri) = @_; | ||
| 62 | $uri = URI->new($uri) unless ref $uri; | ||
| 63 | my $req = HTTP::Request->new( | ||
| 64 | GET => $uri | ||
| 65 | ); | ||
| 66 | $req->header(host => $uri->host); | ||
| 67 | $self->request($req) | ||
| 68 | } | ||
| 69 | |||
| 70 | # Back-compat | ||
| 71 | *get = \&GET; | ||
| 72 | |||
| 73 | sub user_agent { $_[0]->{user_agent} //= "Mozilla/5.0 (Perl) " . (ref $_[0] || $_[0]) } | ||
| 74 | |||
| 75 | sub ssl_args { () } | ||
| 76 | |||
| 77 | 1; | ||
| 78 | |||
| 79 | __END__ |