TrueLayer/Request.pm

filter: Statementstmt 95.6% 111 / 116 Branchbran 87.5% 28 / 32 Conditioncond 75.0% 6 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 92.3% 24 / 26 Podpod 0.0% 0 / 2 totaltotal 90.4% 171 / 189 CC 27 SCAR 24.3
CC
27
Cov
93%
_ua_request
23.6
api_post
4.0
__ANON__
2.0
CRAP 11.3
« TrueLayer/Payment.pm ↑ Summary TrueLayer/Authenticator.pm »
1 package Business::TrueLayer::Request;
2  
3 =head1 NAME
4  
5 Business::TrueLayer::Request - abstract class to handle low level request
6 traffic to TrueLayer, you probably don't need to use this and should use
7 the main L<Business::TrueLayer> module instead.
8  
9 =cut
10  
11 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use feature qw/ signatures postderef /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 8 ▶no warnings qw/ experimental::signatures experimental::postderef /;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 8 ▶use Business::TrueLayer::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 ▶use Business::TrueLayer::Authenticator;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 8 ▶use Business::TrueLayer::Signer;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 8 ▶use Try::Tiny::SmartCatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 8 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 8 ▶use Carp qw/ croak /;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 8 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 8 ▶use Data::GUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 my $MAX_REDIRECTS = 5;
30  
31 1 has [ qw/ client_id client_secret kid / ] => (
32 is => 'ro',
33 isa => 'Str',
34 required => 0,
35 );
36  
37 has 'host' => (
38 is => 'ro',
39 isa => 'Str',
40 required => 0,
41 13 ▶ default => sub ( $self ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
42 13 'truelayer.com',
43 }
44 1 );
45  
46 has api_host => (
47 is => 'ro',
48 isa => 'Str',
49 required => 0,
50 lazy => 1,
51 1 ▶ default => sub ( $self ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
52 1 return join( '.','api',$self->host );
53 }
54 1 );
55  
56 has auth_host => (
57 is => 'ro',
58 isa => 'Str',
59 required => 0,
60 lazy => 1,
61 1 ▶ default => sub ( $self ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
62 1 return join( '.','auth',$self->host );
63 }
64 1 );
65  
66 has payment_host => (
67 is => 'ro',
68 isa => 'Str',
69 required => 0,
70 lazy => 1,
71 0 ▶ default => sub ( $self ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 0 return join( '.','payment',$self->host );
73 }
74 1 );
75  
76 1 has 'private_key' => (
77 is => 'ro',
78 isa => 'EC512:PrivateKey',
79 coerce => 1,
80 required => 0,
81 );
82  
83 has '_ua' => (
84 is => 'ro',
85 isa => 'UserAgent',
86 required => 0,
87 19 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
88 19 return Mojo::UserAgent->new
89 ->max_redirects( $MAX_REDIRECTS )
90 ->connect_timeout( 5 )
91 ->inactivity_timeout( 5 )
92 ->request_timeout( 30 )
93 ;
94 },
95 1 );
96  
97 has 'authenticator' => (
98 is => 'ro',
99 isa => 'Authenticator',
100 lazy => 1,
101 1 ▶ default => sub ( $self ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
102  
103 1 Business::TrueLayer::Authenticator->new(
104 client_id => $self->client_id,
105 client_secret => $self->client_secret,
106 host => $self->auth_host,
107 _ua => $self->_ua,
108 );
109 },
110 1 );
111  
112 has 'signer' => (
113 is => 'ro',
114 isa => 'Signer',
115 lazy => 1,
116 0 ▶ default => sub ( $self ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
117  
118 0 Business::TrueLayer::Signer->new(
119 kid => $self->kid,
120 private_key => $self->private_key,
121 );
122 },
123 1 );
124  
125 15 ▶sub idempotency_key ( $self ) {
Subroutine idempotency_key: called CC=1 SCAR=0.0
Pod undocumented
126 15 return Data::GUID->new->as_string;
127 }
128  
129 5 ▶sub api_post (
Subroutine api_post: called CC=4 SCAR=13.9
130 $self,
131 $absolute_path,
132 5 $http_request_body = undef,
133 5 $expect_json = 1,
134 ) {
135 # sign the request
136 5 my $idempotency_key = $self->idempotency_key;
137  
138 5 ▶ my $json = $http_request_body
Branch: 1 branch
BranchTrueFalseTotal
$http_request_body ? : 5 0 5
139 ? JSON->new->utf8->canonical->encode( $http_request_body )
140 : undef;
141  
142 5 my ( $jws ) = $self->signer->sign_request(
143 'POST',
144 $absolute_path,
145 $idempotency_key,
146 $json,
147 );
148  
149 5 return $self->_ua_request(
150 "https://" . $self->api_host . $absolute_path,
151 $json,
152 [
153 'Authorization' => "Bearer " . $self->authenticator->access_token,
154 'Tl-Signature' => $jws,
155 'Idempotency-Key' => $idempotency_key,
156 ],
157 'POST',
158 $expect_json
159 );
160 }
161  
162 23 ▶sub _ua_request (
Subroutine _ua_request: called CC=23 SCAR=31.6
163 $self,
164 $url,
165 $body,
166 23 $headers = undef,
167 23 $method = 'POST',
168 23 $expect_json = 1
169 ) {
170  
171 23 my $ua = $self->_ua;
172 my $res = $ua->start($ua->build_tx(
173 $method,
174 $url,
175 {
176 'Accept' => 'application/json; charset=UTF-8',
177 'Content-Type' => 'application/json; charset=UTF-8',
178 23 ▶ @{ $headers // [] },
Branch: 1 branch
BranchTrueFalseTotal
defined $body ? : 20 3 23
Condition100%2/2
$headers // []
l!l
815
MC/DC100%1/1
$headers // []
$headers
Truth table100%2/2
A
A= $headers
Aresult
11
00
179 },
180 # Mojo::UserAgent::Transactor::tx calls $self->generators and then the
181 # callbacks based on the count of @_, and does not expect undef here
182 (defined $body ? ($body) : ()),
183 ))->result;
184  
185 # Easiest to deal with this first, even though it should be very rare:
186 23 ▶ if ( $res->code == 301 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 301) 1 22 23
187 # possibly a redirect loop
188 1 croak( "TrueLayer $method $url failed > $MAX_REDIRECTS levels of redirect" );
189 }
190  
191 22 ▶ if ( !$expect_json && !$res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($expect_json or $res->is_success) 0 22 22
Condition66%2/3
$expect_json or $res->is_success
l!l&&r!l&&!r
2110
MC/DC0%0/2
$expect_json or $res->is_success
$expect_json $res->is_success
Truth table66%2/3
A or B
A= $expect_jsonB= $res->is_success
ABresult
1X1
011
000
192 # All error responses are documented as returning JSON
193 0 $expect_json = 1;
194 }
195  
196 22 my $code = $res->code;
197  
198 # no content
199 22 ▶ return if $code == '204';
Branch: 1 branch
BranchTrueFalseTotal
if $code == 204 1 21 22
200  
201 21 my $type = $res->headers->content_type;
202 21 ▶ croak( "TrueLayer $method $url returned $code with no MIME type" )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 1 20 21
203 unless defined $type;
204  
205 20 $body = $res->body;
206  
207 20 ▶ return $body
Branch: 1 branch
BranchTrueFalseTotal
if not $expect_json and $res->is_success 1 19 20
Condition66%2/3
not $expect_json and $res->is_success
!ll&&!rl&&r
1901
MC/DC50%1/2
not $expect_json and $res->is_success
not $expect_json $res->is_success
Truth table66%2/3
not(A) and B
A= not $expect_jsonB= $res->is_success
ABresult
0X0
100
111
208 if !$expect_json && $res->is_success;
209  
210 # Either 2xx and expecting JSON, or an error response
211  
212 19 ▶ croak( "TrueLayer $method $url returned $code $type not JSON, status line: "
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ m[\Aapplication/(?:problem\+)?JSON\b]i 3 16 19
213 . $res->message)
214 unless $type =~ m!\Aapplication/(?:problem\+)?JSON\b!i;
215  
216 16 ▶ croak( "TrueLayer $method $url returned $code with an empty body" )
Branch: 1 branch
BranchTrueFalseTotal
unless length $body 2 14 16
217 unless length $body;
218  
219 14 ▶ my $res_content = try sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
220 14 JSON->new->canonical->decode( $body );
221 },
222 2 ▶ catch_default sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
223 2 croak( "TrueLayer $method $url returned $code with malformed JSON length @{[ length $body ]}: $_" );
224 14 };
225 12 ▶ croak( "TrueLayer $method $url returned $code JSON $res_content" )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $res_content eq "HASH" 1 11 12
226 unless ref $res_content eq 'HASH';
227  
228 11 ▶ return $res_content
Branch: 1 branch
BranchTrueFalseTotal
if $res->is_success 3 8 11
229 if $res->is_success;
230  
231 # From here onward, it's all error handling, as best we can:
232 8 my $title = $res_content->{title};
233 8 ▶ if ( length $title ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $title) 2 6 8
234 # This is looking like an error format we expect:
235 # https://docs.truelayer.com/docs/payments-api-errors
236 2 my $detail = $res_content->{detail};
237 2 ▶ my $message = defined $detail ? "$title - $detail" : $title;
Branch: 1 branch
BranchTrueFalseTotal
defined $detail ? : 2 0 2
238  
239 2 ▶ if ( $res_content->{errors} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res_content->{"errors"}) 0 2 2
240 0 $message .= ' ';
241 $message .= join( "; ",$_->@* )
242 0 for values $res_content->{errors}->%*
243 }
244  
245 2 croak( "TrueLayer $method $url returned $code: $message" );
246 }
247  
248 6 my $error = $res_content->{error};
249 6 ▶ if ( length $error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $error) 3 3 6
250 # This is looking like the error format for the Access tokens
251 # and the Data API
252 # https://docs.truelayer.com/reference/generateaccesstoken
253 3 my $detail = $res_content->{error_description};
254 3 ▶ my $message = defined $detail ? "'$error' - $detail" : "'$error'";
Branch: 1 branch
BranchTrueFalseTotal
defined $detail ? : 1 2 3
255 # There's no : in this message so that we distinguish it from the
256 # message generated for the croak above.
257 # (ie we can tell which format the API is actually responding
258 # with, whatever the docs might claim)
259 3 croak( "TrueLayer $method $url returned $code $message" );
260 }
261  
262 # This is not in spec:
263 croak( "TrueLayer $method $url returned $code with JSON keys "
264 3 . join( ', ', map { "'$_'" } sort keys %$res_content )
265 . ' and status line: ' . $res->message);
266 }
267  
268 3 ▶sub api_get (
Subroutine api_get: called CC=2 SCAR=6.9
Pod undocumented
269 $self,
270 $absolute_path,
271 3 $expect_json = 1
272 ) {
273 # GET requests don't need to be signed or require an Idempotency-Key
274 3 return $self->_ua_request(
275 "https://" . $self->api_host . $absolute_path,
276 undef,
277 [
278 'Authorization' => "Bearer " . $self->authenticator->access_token,
279 ],
280 'GET',
281 $expect_json
282 );
283 }
284  
285 1 1;
286  
287 # vim: ts=4:sw=4:et
« TrueLayer/Payment.pm ↑ Summary TrueLayer/Authenticator.pm »