« blib/lib/SignalWire/Agents/Server/AgentServer.pm
↑ Summary
blib/lib/SignalWire/Agents/Relay/Client.pm »
| 1 | package SignalWire::Agents::REST::HttpClient; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use JSON qw(encode_json decode_json); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use MIME::Base64 qw(encode_base64); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 4 | has 'project' => ( is => 'ro', required => 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 11 | 4 | has 'token' => ( is => 'ro', required => 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 12 | 4 | has 'host' => ( is => 'ro', required => 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 13 | 4 | has 'base_url' => ( is => 'lazy' ); | |||||||||||||||||||||||||||||||||||||||||
| 14 | 4 | has '_ua' => ( is => 'lazy' ); | |||||||||||||||||||||||||||||||||||||||||
| 15 | 4 | has '_auth_header' => ( is => 'lazy' ); | |||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | sub _build_base_url { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_base_url: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | return 'https://' . $self->host; | |||||||||||||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub _build__ua { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build__ua: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | return HTTP::Tiny->new( | |||||||||||||||||||||||||||||||||||||||||
| 25 | agent => 'signalwire-agents-perl-rest/1.0', | ||||||||||||||||||||||||||||||||||||||||||
| 26 | default_headers => { | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 'Content-Type' => 'application/json', | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 'Accept' => 'application/json', | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 'Authorization' => $self->_auth_header, | ||||||||||||||||||||||||||||||||||||||||||
| 30 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 31 | timeout => 30, | ||||||||||||||||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 1 | ▶ | sub _build__auth_header { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build__auth_header: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 1 | my $credentials = $self->project . ':' . $self->token; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | return 'Basic ' . encode_base64($credentials, ''); | |||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | sub _request { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_request: not called CC=13 SCAR=52.0
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | my ($self, $method, $path, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | my $url = $self->base_url . $path; | |||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | # Add query params to URL | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | if ($opts{params} && ref $opts{params} eq 'HASH' && %{$opts{params}}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$opts{"params"} and ref $opts{"params"} eq "HASH" and %{$opts{"params"};}
$opts{"params"} and ref $opts{"params"} eq "HASH"
MC/DC0%0/3
$opts{"params"} and ref $opts{"params"} eq "HASH" and %{$opts{"params"};}
$opts{"params"}
ref $opts{"params"} eq "HASH"
%{$opts{"params"};}
Truth table0%0/4
A and B and C
A= $opts{"params"}B= ref $opts{"params"} eq "HASH"C= %{$opts{"params"};}
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | my @pairs; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | for my $key (sort keys %{$opts{params}}) { | |||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | my $val = $opts{params}{$key} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opts{"params"}{$key} // ""
MC/DC0%0/1
$opts{"params"}{$key} // ""
$opts{"params"}{$key}
Truth table0%0/2
A
A= $opts{"params"}{$key}
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | push @pairs, _uri_encode($key) . '=' . _uri_encode($val); | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | $url .= '?' . join('&', @pairs); | |||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | my %request_opts; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if ($opts{body}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | $request_opts{content} = encode_json($opts{body}); | |||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | my $response = $self->_ua->request($method, $url, \%request_opts); | |||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | unless ($response->{success}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | my $body = $response->{content} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$response->{"content"} // ""
MC/DC0%0/1
$response->{"content"} // ""
$response->{"content"}
Truth table0%0/2
A
A= $response->{"content"}
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | my $parsed; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | eval { $parsed = decode_json($body) }; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | $parsed = $body if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | die SignalWire::Agents::REST::HttpClient::Error->new( | ||||||||||||||||||||||||||||||||||||||||||
| 68 | status_code => $response->{status}, | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | body => $parsed, | |||||||||||||||||||||||||||||||||||||||||
| 70 | url => $path, | ||||||||||||||||||||||||||||||||||||||||||
| 71 | method => $method, | ||||||||||||||||||||||||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | # 204 No Content or empty body | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if ($response->{status} == 204 || !$response->{content}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$response->{"status"} == 204 or not $response->{"content"}
MC/DC0%0/2
$response->{"status"} == 204 or not $response->{"content"}
$response->{"status"} == 204
not $response->{"content"}
Truth table0%0/3
A or not(B)
A= $response->{"status"} == 204B= not $response->{"content"}
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | return {}; | |||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my $result; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | eval { $result = decode_json($response->{content}) }; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | return { raw => $response->{content} }; | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | return $result; | |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | sub get { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | my ($self, $path, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | return $self->_request('GET', $path, params => $opts{params}); | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | sub post { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
post: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | my ($self, $path, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | return $self->_request('POST', $path, body => $opts{body}, params => $opts{params}); | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub put { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
put: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | my ($self, $path, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | return $self->_request('PUT', $path, body => $opts{body}); | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | sub patch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
patch: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | my ($self, $path, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | return $self->_request('PATCH', $path, body => $opts{body}); | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub delete_request { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete_request: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | my ($self, $path) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | return $self->_request('DELETE', $path); | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | # Simple URI encoding | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | sub _uri_encode { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_uri_encode: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | my ($str) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | $str =~ s/([^A-Za-z0-9\-_.~])/sprintf("%%%02X", ord($1))/ge; | |||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | return $str; | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | # --- Error class --- | ||||||||||||||||||||||||||||||||||||||||||
| 121 | package SignalWire::Agents::REST::HttpClient::Error; | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 4 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 4 | ▶ | use JSON qw(encode_json); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 4 | has 'status_code' => ( is => 'ro', required => 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | has 'body' => ( is => 'ro', default => sub { '' } ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | has 'url' => ( is => 'ro', default => sub { '' } ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | has 'method' => ( is => 'ro', default => sub { 'GET' } ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 4 | ▶ | use overload '""' => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 132 | 2 | ▶ | my $body = ref $self->body ? encode_json($self->body) : ($self->body // ''); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->body // ""
MC/DC0%0/1
$self->body // ""
$self->body
Truth table50%1/2
A
A= $self->body
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 2 | return sprintf('%s %s returned %s: %s', | |||||||||||||||||||||||||||||||||||||||||
| 134 | $self->method, $self->url, $self->status_code, $body); | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 4 | }; | |||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||