| 1 | package HTTP::AnyUA::Backend::Mojo::UserAgent; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: A unified programming interface for Mojo::UserAgent | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | our $VERSION = '0.904'; # VERSION | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use parent 'HTTP::AnyUA::Backend'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Future; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Scalar::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | my $future_class; | |||||||||||||||||||||||||||
| 17 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.9
| |||||||||||||||||||||||||||||
| 18 | 1 | $future_class = 'Future'; | |||||||||||||||||||||||||||
| 19 | 0 | ▶ | eval 'use Future::Mojo'; ## no critic | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | $future_class = 'Future::Mojo' if !$@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub response_is_future { 1 } | ||||||||||||||||||||||||||
|
Subroutine
response_is_future: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | sub request { | ||||||||||||||||||||||||||
|
Subroutine
request: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 28 | 0 | my ($method, $url, $args) = @_; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | my $future = $future_class->new; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | my $tx = $self->_munge_request(@_); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | $self->ua->start($tx => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 35 | 0 | my $ua = shift; | |||||||||||||||||||||||||||
| 36 | 0 | my $tx = shift; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | my $resp = $self->_munge_response($tx, $args->{data_callback}); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | if ($resp->{success}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | $future->done($resp); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | else { | ||||||||||||||||||||||||||||
| 44 | 0 | $future->fail($resp); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 0 | }); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | return $future; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sub _munge_request { | ||||||||||||||||||||||||||
|
Subroutine
_munge_request: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||
| 53 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 54 | 0 | my $method = shift; | |||||||||||||||||||||||||||
| 55 | 0 | my $url = shift; | |||||||||||||||||||||||||||
| 56 | 0 | my $args = shift; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | my $headers = $args->{headers} || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args->{'headers'} || {}
MC/DC0%0/1
$args->{'headers'} || {}
$args->{'headers'}
Truth table0%0/2
A
A= $args->{'headers'}
| |||||||||||||||||||||||||||||
| 59 | 0 | my $content = $args->{content}; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | my @content; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | my $content_length; | ||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if ($content) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | for my $header (keys %$headers) { | |||||||||||||||||||||||||||
| 66 | 0 | ▶ | if (lc($header) eq 'content-length') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | $content_length = $headers->{$header}; | |||||||||||||||||||||||||||
| 68 | 0 | last; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # if we don't know the length we have to just read it all in | ||||||||||||||||||||||||||||
| 73 | 0 | ▶ | $content = HTTP::AnyUA::Util::coderef_content_to_string($content) if !$content_length; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | push @content, $content if ref($content) ne 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | my $tx = $self->ua->build_tx($method => $url => $headers => @content); | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if (ref($content) eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | $tx->req->headers->content_length($content_length); | |||||||||||||||||||||||||||
| 82 | # stream the request | ||||||||||||||||||||||||||||
| 83 | 0 | my $drain; | |||||||||||||||||||||||||||
| 84 | 0 | ▶ | $drain = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 85 | 0 | my $body = shift; | |||||||||||||||||||||||||||
| 86 | 0 | ▶ | my $chunk = $content->() || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
&$content() || ''
MC/DC0%0/1
&$content() || ''
&$content()
Truth table0%0/2
A
A= &$content()
| |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | undef $drain if !$chunk; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | $body->write($chunk, $drain); | |||||||||||||||||||||||||||
| 89 | 0 | }; | |||||||||||||||||||||||||||
| 90 | 0 | $tx->req->content->$drain; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if (my $data_cb = $args->{data_callback}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | # stream the response | ||||||||||||||||||||||||||||
| 95 | 0 | my $tx_copy = $tx; | |||||||||||||||||||||||||||
| 96 | 0 | Scalar::Util::weaken($tx_copy); | |||||||||||||||||||||||||||
| 97 | 0 | ▶ | $tx->res->content->unsubscribe('read')->on(read => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 98 | 0 | my ($content, $bytes) = @_; | |||||||||||||||||||||||||||
| 99 | 0 | my $resp = $self->_munge_response($tx_copy, undef); | |||||||||||||||||||||||||||
| 100 | 0 | $data_cb->($bytes, $resp); | |||||||||||||||||||||||||||
| 101 | 0 | }); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | return $tx; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | sub _munge_response { | ||||||||||||||||||||||||||
|
Subroutine
_munge_response: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||
| 108 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 109 | 0 | my $tx = shift; | |||||||||||||||||||||||||||
| 110 | 0 | my $data_cb = shift; | |||||||||||||||||||||||||||
| 111 | 0 | my $recurse = shift; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | my $resp = { | |||||||||||||||||||||||||||
| 114 | success => !!$tx->res->is_success, | ||||||||||||||||||||||||||||
| 115 | url => $tx->req->url->to_string, | ||||||||||||||||||||||||||||
| 116 | status => $tx->res->code, | ||||||||||||||||||||||||||||
| 117 | reason => $tx->res->message, | ||||||||||||||||||||||||||||
| 118 | headers => {}, | ||||||||||||||||||||||||||||
| 119 | }; | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # lowercase header keys | ||||||||||||||||||||||||||||
| 122 | 0 | my $headers = $tx->res->headers->to_hash; | |||||||||||||||||||||||||||
| 123 | 0 | for my $header (keys %$headers) { | |||||||||||||||||||||||||||
| 124 | 0 | $resp->{headers}{lc($header)} = delete $headers->{$header}; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | my $version = $tx->res->version; | |||||||||||||||||||||||||||
| 128 | 0 | ▶ | $resp->{protocol} = "HTTP/$version" if $version; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | if (!$recurse) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | for my $redirect (@{$tx->redirects}) { | |||||||||||||||||||||||||||
| 132 | 0 | ▶ | push @{$resp->{redirects} ||= []}, $self->_munge_response($redirect, undef, 1); | ||||||||||||||||||||||||||
|
Condition0%0/2
$resp->{"redirects"} ||= []
MC/DC0%0/1
$resp->{"redirects"} ||= []
$resp->{"redirects"}
Truth table0%0/2
A
A= $resp->{"redirects"}
| |||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | my $err = $tx->error; | |||||||||||||||||||||||||||
| 137 | 0 | ▶ | if ($err && !$err->{code}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$err and not $err->{"code"}
MC/DC0%0/2
$err and not $err->{"code"}
$err
not $err->{"code"}
Truth table0%0/3
A and not(B)
A= $errB= not $err->{"code"}
| |||||||||||||||||||||||||||||
| 138 | 0 | return HTTP::AnyUA::Util::internal_exception($err->{message}, $resp); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | my $body = $tx->res->body; | |||||||||||||||||||||||||||
| 142 | 0 | ▶ | $resp->{content} = $body if $body && !$data_cb; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$body and not $data_cb
MC/DC0%0/2
$body and not $data_cb
$body
not $data_cb
Truth table0%0/3
A and not(B)
A= $bodyB= not $data_cb
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | return $resp; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 1 | 1; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | __END__ | ||||||||||||||||||||||||||||