| 1 | package WWW::Hetzner::LWPIO; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # ABSTRACT: Synchronous HTTP backend using LWP::UserAgent | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 60 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 60 | ▶ | use LWP::UserAgent; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 60 | ▶ | use HTTP::Request; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 60 | ▶ | use JSON::MaybeXS qw(encode_json); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 60 | ▶ | use WWW::Hetzner::HTTPResponse; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 60 | with 'WWW::Hetzner::Role::IO'; | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 60 | our $VERSION = '0.102'; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 60 | has timeout => (is => 'ro', default => 30); | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | has ua => ( | ||||||||||||||||||||
| 20 | is => 'lazy', | ||||||||||||||||||||
| 21 | 0 | ▶ | builder => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Subroutine
_build_ua: called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 22 | 0 | my $self = shift; | |||||||||||||||||||
| 23 | 0 | LWP::UserAgent->new( | |||||||||||||||||||
| 24 | agent => 'WWW-Hetzner/' . $VERSION, | ||||||||||||||||||||
| 25 | timeout => $self->timeout, | ||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||
| 27 | }, | ||||||||||||||||||||
| 28 | 60 | ); | |||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 1 | ▶ | sub call { | ||||||||||||||||||
|
Subroutine
call: called CC=7 SCAR=20.6
Pod
documented
| |||||||||||||||||||||
| 32 | 1 | my ($self, $req) = @_; | |||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 1 | my $http_req = HTTP::Request->new($req->method => $req->url); | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 1 | my $headers = $req->headers; | |||||||||||||||||||
| 37 | 1 | for my $header (keys %$headers) { | |||||||||||||||||||
| 38 | 2 | $http_req->header($header => $headers->{$header}); | |||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 1 | ▶ | $http_req->content($req->content) if $req->has_content; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 1 | my $response = $self->ua->request($http_req); | |||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 1 | ▶ | my $content = $response->decoded_content // ''; | ||||||||||||||||||
|
Condition50%1/2
$response->decoded_content // ""
MC/DC0%0/1
$response->decoded_content // ""
$response->decoded_content
Truth table50%1/2
A
A= $response->decoded_content
| |||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | # LWP synthesises a 5xx response when the request never reached the | ||||||||||||||||||||
| 48 | # server (connection refused, DNS, TLS, timeout). Report its reason in | ||||||||||||||||||||
| 49 | # the API error shape instead of a bare status code. | ||||||||||||||||||||
| 50 | 1 | ▶ | if (($response->header('Client-Warning') // '') eq 'Internal response') { | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$response->header("Client-Warning") // ""
MC/DC0%0/1
$response->header("Client-Warning") // ""
$response->header("Client-Warning")
Truth table50%1/2
A
A= $response->header("Client-Warning")
| |||||||||||||||||||||
| 51 | 1 | $content =~ s/\s+$//; | |||||||||||||||||||
| 52 | 1 | ▶ | $content = encode_json({ | ||||||||||||||||||
|
Condition33%1/3
$content || $response->message
MC/DC0%0/2
$content || $response->message
$content
$response->message
Truth table33%1/3
A || B
A= $contentB= $response->message
| |||||||||||||||||||||
| 53 | error => { code => 'transport_error', message => $content || $response->message }, | ||||||||||||||||||||
| 54 | }); | ||||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 1 | return WWW::Hetzner::HTTPResponse->new( | |||||||||||||||||||
| 58 | status => $response->code, | ||||||||||||||||||||
| 59 | content => $content, | ||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 60 | 1; | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | __END__ | ||||||||||||||||||||