1 package HTTP
::AnyUA
::Backend
::Mojo
::UserAgent
;
2 # ABSTRACT: A unified programming interface for Mojo::UserAgent
8 our $VERSION = '0.900'; # VERSION
10 use parent
'HTTP::AnyUA::Backend';
16 sub response_is_future
{ 1 }
20 my ($method, $url, $args) = @_;
22 my $future = Future-
>new;
24 my $tx = $self->_munge_request(@_);
26 $self->ua->start($tx => sub {
30 my $resp = $self->_munge_response($tx, $args->{data_callback
});
32 if ($resp->{success
}) {
50 my $headers = $args->{headers
} || {};
51 my $content = $args->{content
};
57 for my $header (keys %$headers) {
58 if (lc($header) eq 'content-length') {
59 $content_length = $headers->{$header};
64 # if we don't know the length we have to just read it all in
65 $content = HTTP
::AnyUA
::Util
::coderef_content_to_string
($content) if !$content_length;
67 push @content, $content if ref($content) ne 'CODE';
70 my $tx = $self->ua->build_tx($method => $url => $headers => @content);
72 if (ref($content) eq 'CODE') {
73 $tx->req->headers->content_length($content_length);
78 my $chunk = $content->() || '';
79 undef $drain if !$chunk;
80 $body->write($chunk, $drain);
82 $tx->req->content->$drain;
85 if (my $data_cb = $args->{data_callback
}) {
88 Scalar
::Util
::weaken
($tx_copy);
89 $tx->res->content->unsubscribe('read')->on(read => sub {
90 my ($content, $bytes) = @_;
91 my $resp = $self->_munge_response($tx_copy, undef);
92 $data_cb->($bytes, $resp);
106 success
=> !!$tx->res->is_success,
107 url
=> $tx->req->url->to_string,
108 status
=> $tx->res->code,
109 reason
=> $tx->res->message,
113 # lowercase header keys
114 my $headers = $tx->res->headers->to_hash;
115 for my $header (keys %$headers) {
116 $resp->{headers
}{lc($header)} = delete $headers->{$header};
119 my $version = $tx->res->version;
120 $resp->{protocol
} = "HTTP/$version" if $version;
123 for my $redirect (@{$tx->redirects}) {
124 push @{$resp->{redirects
} ||= []}, $self->_munge_response($redirect, undef, 1);
128 my $err = $tx->error;
129 if ($err and !$err->{code
}) {
130 return HTTP
::AnyUA
::Util
::internal_exception
($err->{message
}, $resp);
133 my $body = $tx->res->body;
134 $resp->{content
} = $body if $body && !$data_cb;
149 HTTP::AnyUA::Backend::Mojo::UserAgent - A unified programming interface for Mojo::UserAgent
157 This module adds support for the HTTP client L<Mojo::UserAgent> to be used with the unified
158 programming interface provided by L<HTTP::AnyUA>.
166 The C<url> field in the response has the auth portion (if any) removed from the URL.
176 L<HTTP::AnyUA::Backend>
182 Please report any bugs or feature requests on the bugtracker website
183 L<https://github.com/chazmcgarvey/HTTP-AnyUA/issues>
185 When submitting a bug or request, please include a test-file or a
186 patch to an existing test-file that illustrates the bug or desired
191 Charles McGarvey <chazmcgarvey@brokenzipper.com>
193 =head1 COPYRIGHT AND LICENSE
195 This software is copyright (c) 2017 by Charles McGarvey.
197 This is free software; you can redistribute it and/or modify it under
198 the same terms as the Perl 5 programming language system itself.