| 1 | package WWW::VastAI::Role::HTTP; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 10 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Shared synchronous HTTP client role for Vast.ai API consumers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use JSON::MaybeXS qw(decode_json encode_json); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use Log::Any qw($log); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 10 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 10 | ▶ | use WWW::VastAI::HTTPRequest; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 10 | ▶ | use WWW::VastAI::LWPIO; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 10 | requires 'api_key'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 10 | requires 'base_url'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | has io => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | is => 'lazy', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | ▶ | builder => sub { WWW::VastAI::LWPIO->new }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_io: not called CC=1 SCAR=6.9
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 10 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 15 | ▶ | sub get { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 15 | my ($self, $path, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 15 | return $self->_request('GET', $path, %opts); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 12 | ▶ | sub post { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
post: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 12 | my ($self, $path, $body, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 12 | return $self->_request('POST', $path, %opts, body => $body); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 10 | ▶ | sub put { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
put: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 10 | my ($self, $path, $body, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 10 | return $self->_request('PUT', $path, %opts, body => $body); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 9 | ▶ | sub delete { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 9 | my ($self, $path, $body, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 9 | ▶ | return $self->_request('DELETE', $path, %opts, (defined $body ? (body => $body) : ())); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 46 | ▶ | sub _set_auth { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_auth: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 46 | my ($self, $headers) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 46 | $headers->{Authorization} = 'Bearer ' . $self->api_key; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 2 | ▶ | sub _build_query_uri { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_query_uri: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 2 | my ($self, $url, $params) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 2 | my $uri = URI->new($url); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 2 | ▶ | for my $name (sort keys %{$params || {}}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$params || {}
MC/DC0%0/1
$params || {}
$params
Truth table50%1/2
A
A= $params
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 3 | my $value = $params->{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 3 | ▶ | next unless defined $value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 3 | ▶ | $value = encode_json($value) if ref $value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 3 | $uri->query_param($name => $value); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 2 | return $uri->as_string; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 46 | ▶ | sub _build_request { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_request: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 46 | my ($self, $method, $path, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 46 | ▶ | my $base_url = $opts{base_url} || $self->base_url; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$opts{'base_url'} || $self->base_url
MC/DC0%0/2
$opts{'base_url'} || $self->base_url
$opts{'base_url'}
$self->base_url
Truth table33%1/3
A || B
A= $opts{'base_url'}B= $self->base_url
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 46 | my $url = $base_url . $path; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 46 | ▶ | $url = $self->_build_query_uri($url, $opts{params}) if $opts{params}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 46 | my %headers = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 'Content-Type' => 'application/json', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 46 | $self->_set_auth(\%headers); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 46 | my %args = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | method => $method, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | url => $url, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | headers => \%headers, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 46 | ▶ | if (exists $opts{body}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 25 | $args{content} = encode_json($opts{body}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 25 | $log->debugf('%s %s body=%s', $method, $url, $args{content}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 21 | $log->debugf('%s %s', $method, $url); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 46 | return WWW::VastAI::HTTPRequest->new(%args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 46 | ▶ | sub _parse_response { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_response: called CC=12 SCAR=40.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 46 | my ($self, $response, $method, $path) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 46 | my $content = $response->content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 46 | my $data; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 46 | ▶ | if (defined $content && $content =~ /^\s*[\{\[]/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
defined $content and $content =~ /^\s*[\{\[]/
MC/DC0%0/2
defined $content and $content =~ /^\s*[\{\[]/
defined $content
$content =~ /^\s*[\{\[]/
Truth table33%1/3
A and B
A= defined $contentB= $content =~ /^\s*[\{\[]/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 46 | $data = decode_json($content); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | ▶ | elsif (defined $content && length $content) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
defined $content and length $content
MC/DC0%0/2
defined $content and length $content
defined $content
length $content
Truth table0%0/3
A and B
A= defined $contentB= length $content
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | $data = $content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 46 | ▶ | if ($response->status < 200 || $response->status >= 300) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$response->status < 200 or $response->status >= 300
MC/DC0%0/2
$response->status < 200 or $response->status >= 300
$response->status < 200
$response->status >= 300
Truth table33%1/3
A or B
A= $response->status < 200B= $response->status >= 300
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | my $message = ref $data eq 'HASH' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | ? ($data->{msg} || $data->{message} || $data->{error} || $response->status) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/9
$data->{"msg"} or $data->{"message"} or $data->{"error"} or $response->status
$data->{"msg"} or $data->{"message"} or $data->{"error"}
$data->{"msg"} or $data->{"message"}
MC/DC0%0/4
$data->{"msg"} or $data->{"message"} or $data->{"error"} or $response->status
$data->{"msg"}
$data->{"message"}
$data->{"error"}
$response->status
Truth table0%0/5
A or B or C or D
A= $data->{"msg"}B= $data->{"message"}C= $data->{"error"}D= $response->status
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | : (defined $data ? $data : $response->status); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | croak "Vast.ai API error: $message"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 46 | $log->infof('%s %s -> %s', $method, $path, $response->status); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 46 | return $data; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 46 | ▶ | sub _request { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_request: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 46 | my ($self, $method, $path, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 46 | ▶ | croak "No Vast.ai API key configured" unless $self->api_key; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 46 | my $req = $self->_build_request($method, $path, %opts); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 46 | my $response = $self->io->call($req); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 46 | return $self->_parse_response($response, $method, $path); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 10 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||