NAME

AnyEvent::UserAgent - AnyEvent::HTTP OO-wrapper

SYNOPSIS

use AnyEvent::UserAgent;
use Data::Dumper;

my $ua = AnyEvent::UserAgent->new;
my $cv = AE::cv;

$ua->get('http://example.com/', sub {
    my ($res) = @_;
    print(Dumper($res, $ua->cookie_jar));
    $cv->send();
});
$cv->recv();

DESCRIPTION

AnyEvent::UserAgent is a OO-wrapper around AnyEvent::HTTP with cookies support by HTTP::Cookies. Also request callback receives response as HTTP::Response object.

ATTRIBUTES

agent

The product token that is used to identify the user agent on the network. The agent value is sent as the User-Agent header in the requests.

The cookie jar object to use. The only requirement is that the cookie jar object must implement the extract_cookies($req) and add_cookie_header($res) methods. These methods will then be invoked by the user agent as requests are sent and responses are received. Normally this will be a HTTP::Cookies object or some subclass. Default cookie jar is the HTTP::Cookies object.

inactivity_timeout

Maximum time in seconds in which connection can be inactive before getting closed. Default timeout value is 20. Setting the value to 0 will allow connections to be inactive indefinitely.

max_redirects

Maximum number of redirects the user agent will follow before it gives up. The default value is 5.

request_timeout

Maximum time in seconds to establish a connection, send the request and receive a response. The request will be canceled when that time expires. Default timeout value is 0. Setting the value to 0 will allow the user agent to wait indefinitely. The timeout will reset for every followed redirect.

Other attributes

The following attributes are supported and they are all passed as options to the AnyEvent::HTTP::http_request calls made by this module: proxy, tls_ctx, session, timeout, on_prepare, tcp_connect, on_header, on_body, want_body_handle, persistent, keepalive, handle_params.

METHODS

new

my $ua = AnyEvent::UserAgent->new;
my $ua = AnyEvent::UserAgent->new(request_timeout => 60);

Constructor for the user agent. You can pass it either a hash or a hash reference with attribute values.

request

$ua->request(GET 'http://example.com/', sub { print($_[0]->code) });

This method will dispatch the given request object. Normally this will be an instance of the HTTP::Request class, but any object with a similar interface will do. The last argument must be a callback that will be called with a response object as first argument. Response will be an instance of the HTTP::Response class.

This method is a wrapper for the AnyEvent::HTTP::http_request() method. So you also can pass parameters for it, e.g.:

$ua->request(GET 'http://example.com/', want_body_handle => 0, sub { print($_[0]->code) });

Full parameter list see at the AnyEvent::HTTP documentation.

get

$ua->get('http://example.com/', sub { print($_[0]->code) });

This method is a wrapper for the request() method and the HTTP::Request::Common::GET() function. The last argument must be a callback.

This method is a wrapper for the request() method and the HTTP::Request::Common::HEAD() function. See get().

put

This method is a wrapper for the request() method and the HTTP::Request::Common::PUT() function. See get().

delete

This method is a wrapper for the request() method and the HTTP::Request::Common::DELETE() function. See get().

post

$ua->post('http://example.com/', [key => 'value'], sub { print($_[0]->code) });

This method is a wrapper for the request() method and the HTTP::Request::Common::POST() function. The last argument must be a callback.

patch

This method is a wrapper for the request() method and the HTTP::Request::Common::PATCH() function. The last argument must be a callback.

options

This method is a wrapper for the request() method and the HTTP::Request::Common::OPTIONS() function. The last argument must be a callback.

LIMITATIONS

Because of the handling of redirections done by this module as well as AnyEvent::HTTP, if you are connecting to a web server that does not implement persistent connections (which is common for test servers) then you should pass a persistent = 0> option to the AnyEvent::UserAgent constructor otherwise some requests might fail.

SEE ALSO

AnyEvent::HTTP, HTTP::Cookies, HTTP::Request::Common, HTTP::Request, HTTP::Response.

SUPPORT

AUTHOR

Denis Ibaev dionys@cpan.org for AdCamp.ru.

This module based on original AnyEvent::HTTP::Simple module by punytan punytan@gmail.com.

CONTRIBUTORS

Andrey Khozov avkhozov@cpan.org

Eric Johnson github@iijo.org

Mathias Kende mathias@cpan.org

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See http://dev.perl.org/licenses/ for more information.