NAME

MojoX::UserAgent::Transaction - Basic building block of MojoX::UserAgent, encapsulates a single HTTP exchange.

SYNOPSIS

my $tx = MojoX::UserAgent::Transaction->new(
    {   url     => 'http://www.some.host.com/bla/',
        method  => 'POST',
        ua      => $ua,
        id      => '123456',
        headers => {
            'Expect'       => '100-continue',
            'Content-Type' => 'text/plain'
        },
        body     => 'Hello!',
        callback => sub {
            my ($ua, $tx) = @_;
            ok(!$tx->has_error, 'Completed');
            is($tx->id, '123456', 'Request ID');
            is($tx->res->code, 200, 'Status 200');
        }
    }
};

$ua->spool($tx);

DESCRIPTION

A subclass of Mojo::Transaction::Single, this class simply adds the few extra elements that are needed by MojoX::UserAgent.

ATTRIBUTES

This class inherits all the attributes of Mojo::Transaction::Single, and adds the following.

done_cb

The subroutine that will be called once the transaction is completed. When invoked, this sub is passed two arguments: the UserAgent object that performed the transaction and the transaction itself.

hops

The number of hops (ie redirects) that this transaction has gone through.

id

An optional transaction identifier. Not used internally by the class, but preserved across redirects and accessible to the callback.

original_req

If the transaction is redirected, this holds the original request object.

ua

A pointer back to the MojoX::UserAgent to which this transaction was spooled.

METHODS

MojoX::UserAgent::Transaction inherits all methods from Mojo::Transaction::Single and implements the following new ones.

new

Constructor that accepts a reference to a hash of named arguments. This hash must contain the following key/value pairs:

  • key: 'url' value: either a string or a Mojo::URL object;

  • key: 'ua' value: a reference to the Mojox::UserAgent object to which this transaction belongs.

It may also contain any/all of the following:

  • key: 'callback' value: the callback subroutine that will be called when this transaction is finished (see done_cb above);

  • key: 'headers' value: a reference to a hash of request headers (see Mojo::Message::Request);

  • key: 'method' value: the HTTP method to be used in the request;

  • key: 'body' value: the contents of the body of the request;

  • key: 'id' value: the value of the id attribute (see above);

  • key: 'hops' value: the value of the hops attribute (see above, should only be set by the User-Agent);

  • key: 'original_req' value: the original Mojo::Message::Request object iff hops isn't 0.

client_connect

Called when the transaction is about to be sent out, this method is used to add the User-Agent and request cookies to the outgoing request.