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