1 package HTTP
::AnyUA
::Middleware
;
2 # ABSTRACT: A base class for HTTP::AnyUA middleware
6 package HTTP::AnyUA::Middleware::MyMiddleware;
8 use parent 'HTTP::AnyUA::Middleware';
11 my ($self, $method, $url, $args) = @_;
13 # Maybe do something with the request args here.
15 # Let backend handle the response:
16 my $response = $self->backend->request($method, $url, $args);
18 my $handle_response = sub {
21 # Maybe do something with the response here.
26 if ($self->response_is_future) {
28 done => $handle_response,
29 fail => $handle_response,
33 $response = $handle_response->($response);
41 This module provides an interface for an L<HTTP::AnyUA> "middleware," which is a component that sits
42 between an L<HTTP::AnyUA> object and the L<backend|HTTP::AnyUA::Backend> (which may in fact be
45 The easiest way to use middleware is to use L<HTTP::AnyUA/apply_middleware>.
47 The middleware mechanism can be used to munge or react to requests and responses to and from the
48 backend user agent. Middlewares are a completely optional part of L<HTTP::AnyUA>. They can be
49 wrapped around each other to create multiple layers and interesting possibilities. The functionality
50 provided by middleware may be alternative to features provided by some of the supported user agents,
51 themselves, but implementing functionality on this layer makes it work for I<all> the user agents.
58 our $VERSION = '9999.999'; # VERSION
60 sub _croak
{ require Carp
; Carp
::croak
(@_) }
61 sub _usage
{ _croak
("Usage: @_\n") }
66 $middleware = HTTP
::AnyUA
::Middleware
::MyMiddleware-
>new($backend);
67 $middleware = HTTP
::AnyUA
::Middleware
::MyMiddleware-
>new($backend, %args);
69 Construct a new middleware
.
75 my $backend = shift or die 'Backend is required';
76 my $self = bless {backend
=> $backend}, $class;
83 Called by the
default constructor with the middleware arguments
.
85 This may be overridden by implementations instead of the constructor
.
93 $middleware = HTTP
::AnyUA
::Middleware
::MyMiddleware-
>wrap($backend, %args);
94 $middleware->wrap($backend);
96 Construct a new middleware
or, when called on an instance
, set a new backend on an existing
103 my $backend = shift or _usage
($self . q{->wrap($backend, %args)});
106 $self->{backend
} = $backend;
109 $self = $self->new($backend, @_);
117 $response = $middleware->request($method => $url, \
%options);
119 Make a request
, get a response
.
121 This should be overridden by implementations to
do whatever they want with
or to the request
and/or
126 sub request
{ shift-
>backend->request(@_) }
130 Get the current backend that
is wrapped
.
134 sub backend
{ shift-
>{backend
} }
138 Get the backend user agent
.
142 sub ua
{ shift-
>backend->ua(@_) }
144 =attr response_is_future
146 Get whether
or not responses are L
<Future
> objects
. Default
is whatever the backend returns
.
148 This may be overridden by implementations
.
152 sub response_is_future
{ shift-
>backend->response_is_future(@_) }