NAME

HTTP::AnyUA::Middleware - A base class for HTTP::AnyUA middleware

VERSION

version 0.904

SYNOPSIS

    package HTTP::AnyUA::Middleware::MyMiddleware;

    use parent 'HTTP::AnyUA::Middleware';

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

        # Maybe do something with the request args here.

        # Let backend handle the response:
        my $response = $self->backend->request($method, $url, $args);

        my $handle_response = sub {
            my $response = shift;

            # Maybe do something with the response here.

            return $response;
        };

        if ($self->response_is_future) {
            $response = $response->transform(
                done => $handle_response,
                fail => $handle_response,
            );
        }
        else {
            $response = $handle_response->($response);
        }

        return $response;
    }

DESCRIPTION

This module provides an interface for an HTTP::AnyUA "middleware," which is a component that sits between an HTTP::AnyUA object and the backend (which may in fact be another middleware).

The easiest way to use middleware is to use "apply_middleware" in HTTP::AnyUA.

The middleware mechanism can be used to munge or react to requests and responses to and from the backend user agent. Middlewares are a completely optional part of HTTP::AnyUA. They can be wrapped around each other to create multiple layers and interesting possibilities. The functionality provided by middleware may be alternative to features provided by some of the supported user agents, themselves, but implementing functionality on this layer makes it work for all the user agents.

ATTRIBUTES

backend

Get the current backend that is wrapped.

ua

Get the backend user agent.

response_is_future

Get whether or not responses are Future objects. Default is whatever the backend returns.

This may be overridden by implementations.

METHODS

new

    $middleware = HTTP::AnyUA::Middleware::MyMiddleware->new($backend);
    $middleware = HTTP::AnyUA::Middleware::MyMiddleware->new($backend, %args);

Construct a new middleware.

init

Called by the default constructor with the middleware arguments.

This may be overridden by implementations instead of the constructor.

wrap

    $middleware = HTTP::AnyUA::Middleware::MyMiddleware->wrap($backend, %args);
    $middleware->wrap($backend);

Construct a new middleware or, when called on an instance, set a new backend on an existing middleware.

request

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

Make a request, get a response.

This should be overridden by implementations to do whatever they want with or to the request and/or response.

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.