The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

HTTP::AnyUA::Backend - A base class for HTTP::AnyUA backends

VERSION

version 0.904

SYNOPSIS

package HTTP::AnyUA::Backend::MyUserAgent;

use parent 'HTTP::AnyUA::Backend';

sub response_is_future { 0 }

sub request {
    my ($self, $method, $url, $args) = @_;

    my $ua = $self->ua;

    # Here is where you transform the arguments into a request that $ua
    # understands, make the request against $ua and get a response, and
    # transform the response to the expected hashref form.

    my $resp = $ua->make_request();

    return $resp;
}

### Non-blocking user agents are expected to return Future objects:

use Future;

sub response_is_future { 1 }

sub request {
    my ($self, $method, $url, $args) = @_;

    my $ua = $self->ua;

    my $future = Future->new;

    # Again, this example glosses over transforming the request and response
    # to and from the actual user agent, but such details are the whole
    # point of a backend.

    $ua->nonblocking_callback(sub {
        my $resp = shift;

        if ($resp->{success}) {
            $future->done($resp);
        }
        else {
            $future->fail($resp);
        }
    });

    return $future;
}

DESCRIPTION

This module provides an interface for an HTTP::AnyUA "backend," which is an adapter that adds support for using a type of user agent with HTTP::AnyUA.

This class should not be instantiated directly, but it may be convenient for backend implementations to subclass it.

At its core, a backend simply takes a set of standard arguments that represent an HTTP request, transforms that request into a form understood by an underlying user agent, calls upon the user agent to make the request and get a response, and then transforms that response into a standard form. The standard forms for the request and response are based on HTTP::Tiny's arguments and return value to and from its request method.

ATTRIBUTES

ua

Get the user agent that was passed to "new".

response_is_future

Get whether or not responses are Future objects. Default is false.

This may be overridden by implementations.

METHODS

new

$backend = HTTP::AnyUA::Backend::MyUserAgent->new($my_user_agent);

Construct a new backend.

request

$response = $backend->request($method => $url, \%options);

Make a request, get a response.

This must be overridden by implementations.

SEE ALSO

BUGS

Please report any bugs or feature requests on the bugtracker website https://github.com/chazmcgarvey/HTTP-AnyUA/issues

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

AUTHOR

Charles McGarvey <chazmcgarvey@brokenzipper.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2019 by Charles McGarvey.

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