SDK/HTTPClient.pm

filter: Statementstmt 40.9% 43 / 105 Branchbran 7.8% 3 / 38 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 80.0% 12 / 15 Podpod 100% 3 / 3 totaltotal 35.2% 61 / 173 CC 27 SCAR 45.0
CC
27
Cov
30%
request
210.0
_handle_error
72.0
request_raw
30.0
CRAP 89.9
↑ Summary SDK/Resource/Polling.pm »
1 package Apertur::SDK::HTTPClient;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use HTTP::Request::Common qw(POST);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use JSON qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Apertur::SDK::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Apertur::SDK::Error::Authentication;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Apertur::SDK::Error::NotFound;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Apertur::SDK::Error::RateLimit;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Apertur::SDK::Error::Validation;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
18 3 my ($class, %args) = @_;
19 3 my $base_url = $args{base_url};
20 3 $base_url =~ s{/+$}{};
21  
22 3 my $auth_header = '';
23 3 ▶ if ($args{api_key}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'api_key'}) { } 2 1 3
elsif ($args{'oauth_token'}) { } 1 0 1
24 2 $auth_header = "Bearer $args{api_key}";
25 }
26 elsif ($args{oauth_token}) {
27 1 $auth_header = "Bearer $args{oauth_token}";
28 }
29  
30 3 my $ua = LWP::UserAgent->new(
31 agent => 'Apertur-SDK-Perl/0.01',
32 timeout => 30,
33 );
34  
35 3 return bless {
36 base_url => $base_url,
37 auth_header => $auth_header,
38 ua => $ua,
39 }, $class;
40 }
41  
42 0 ▶sub request {
Subroutine request: not called CC=14 SCAR=53.5
Pod documented
43 0 my ($self, $method, $path, %opts) = @_;
44  
45 0 my $url = $self->{base_url} . $path;
46 0 ▶ my $headers = $opts{headers} || {};
Condition0%0/2
$opts{'headers'} || {}
l!l
00
MC/DC0%0/1
$opts{'headers'} || {}
$opts{'headers'}
Truth table0%0/2
A
A= $opts{'headers'}
Aresult
11
00
47  
48 $headers->{'Authorization'} = $self->{auth_header}
49 0 ▶ if $self->{auth_header};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"auth_header"} 0 0 0
50  
51 0 my $req;
52 0 ▶ if ($opts{multipart}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'multipart'}) { } 0 0 0
53 # Multipart form upload
54 $req = POST(
55 $url,
56 Content_Type => 'form-data',
57 Content => $opts{multipart},
58 0 );
59 # Apply auth header on top of the multipart request
60 $req->header('Authorization' => $headers->{'Authorization'})
61 0 ▶ if $headers->{'Authorization'};
Branch: 1 branch
BranchTrueFalseTotal
if $headers->{"Authorization"} 0 0 0
62 # Apply any extra headers
63 0 for my $key (keys %$headers) {
64 0 ▶ next if $key eq 'Authorization';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "Authorization" 0 0 0
65 0 $req->header($key => $headers->{$key});
66 }
67 }
68 else {
69 0 $req = HTTP::Request->new(uc($method), $url);
70 0 for my $key (keys %$headers) {
71 0 $req->header($key => $headers->{$key});
72 }
73 0 ▶ if (defined $opts{body}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"body"}) 0 0 0
74 0 ▶ $req->content_type($headers->{'Content-Type'} // 'application/json');
Condition0%0/2
$headers->{"Content-Type"} // "application/json"
l!l
00
MC/DC0%0/1
$headers->{"Content-Type"} // "application/json"
$headers->{"Content-Type"}
Truth table0%0/2
A
A= $headers->{"Content-Type"}
Aresult
11
00
75 0 $req->content($opts{body});
76 }
77 }
78  
79 0 my $res;
80 0 ▶ if (defined $opts{timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'timeout'}) { } 0 0 0
81 0 my $prev = $self->{ua}->timeout;
82 0 $self->{ua}->timeout($opts{timeout});
83 0 $res = eval { $self->{ua}->request($req) };
84 0 my $err = $@;
85 0 $self->{ua}->timeout($prev);
86 0 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
87 }
88 else {
89 0 $res = $self->{ua}->request($req);
90 }
91 0 my $status = $res->code;
92  
93 0 ▶ if ($status >= 400) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status >= 400) 0 0 0
94 0 $self->_handle_error($res);
95 }
96  
97 0 ▶ if ($status == 204) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 204) 0 0 0
98 0 return undef;
99 }
100  
101 0 return decode_json($res->decoded_content);
102 }
103  
104 0 ▶sub request_raw {
Subroutine request_raw: not called CC=5 SCAR=34.0
Pod documented
105 0 my ($self, $method, $path, %opts) = @_;
106  
107 0 my $url = $self->{base_url} . $path;
108 0 ▶ my $headers = $opts{headers} || {};
Condition0%0/2
$opts{'headers'} || {}
l!l
00
MC/DC0%0/1
$opts{'headers'} || {}
$opts{'headers'}
Truth table0%0/2
A
A= $opts{'headers'}
Aresult
11
00
109  
110 $headers->{'Authorization'} = $self->{auth_header}
111 0 ▶ if $self->{auth_header};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"auth_header"} 0 0 0
112  
113 0 my $req = HTTP::Request->new(uc($method), $url);
114 0 for my $key (keys %$headers) {
115 0 $req->header($key => $headers->{$key});
116 }
117  
118 0 my $res = $self->{ua}->request($req);
119 0 my $status = $res->code;
120  
121 0 ▶ if ($status >= 400) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status >= 400) 0 0 0
122 0 $self->_handle_error($res);
123 }
124  
125 0 return $res->content;
126 }
127  
128 0 ▶sub _handle_error {
Subroutine _handle_error: not called CC=8 SCAR=42.8
129 0 my ($self, $res) = @_;
130 0 my $status = $res->code;
131  
132 0 my $body = {};
133 0 eval {
134 0 $body = decode_json($res->decoded_content);
135 };
136 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
137 0 $body = { message => "HTTP $status" };
138 }
139  
140 0 ▶ my $message = $body->{message} || "HTTP $status";
Condition0%0/2
$body->{'message'} || "HTTP $status"
l!l
00
MC/DC0%0/1
$body->{'message'} || "HTTP $status"
$body->{'message'}
Truth table0%0/2
A
A= $body->{'message'}
Aresult
11
00
141 0 my $code = $body->{code};
142  
143 0 ▶ if ($status == 401) {
Branch: 4 branches
BranchTrueFalseTotal
if ($status == 401) { } 0 0 0
elsif ($status == 404) { } 0 0 0
elsif ($status == 429) { } 0 0 0
elsif ($status == 400) { } 0 0 0
144 0 Apertur::SDK::Error::Authentication->throw(message => $message);
145 }
146 elsif ($status == 404) {
147 0 Apertur::SDK::Error::NotFound->throw(message => $message);
148 }
149 elsif ($status == 429) {
150 0 my $retry_after = $res->header('Retry-After');
151 0 ▶ $retry_after = defined $retry_after ? int($retry_after) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $retry_after ? : 0 0 0
152 0 Apertur::SDK::Error::RateLimit->throw(
153 message => $message,
154 retry_after => $retry_after,
155 );
156 }
157 elsif ($status == 400) {
158 0 Apertur::SDK::Error::Validation->throw(message => $message);
159 }
160 else {
161 0 Apertur::SDK::Error->throw(
162 status_code => $status,
163 code => $code,
164 message => $message,
165 );
166 }
167 }
168  
169 1 1;
170  
171 __END__
↑ Summary SDK/Resource/Polling.pm »