lib/WebService/Async/Onfido.pm

filter: Statementstmt 75.4% 421 / 558 Branchbran 42.0% 21 / 50 Conditioncond 56.6% 17 / 30 MC/DCmcdc 16.6% 3 / 18 Subroutinesub 88.0% 110 / 125 Podpod 74.3% 29 / 39 totaltotal 73.2% 601 / 820 CC 46 SCAR 8.3
CC
46
Cov
72%
__ANON__
6.0
paging
6.0
__ANON__
6.0
CRAP 2.3
« lib/WebService/Async/Onfido/Base/Address.pm ↑ Summary lib/WebService/Async/Onfido/Check.pm »
1 package WebService::Async::Onfido;
2  
3 # ABSTRACT: Webservice to connect to Onfido API
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = '0.007';
9  
10 3 ▶use parent qw(IO::Async::Notifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 WebService::Async::Onfido - unofficial support for the Onfido identity verification service
15  
16 =head1 SYNOPSIS
17  
18  
19 =head1 DESCRIPTION
20  
21 =cut
22  
23 3 ▶use mro;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶no indirect;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 3 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use Dir::Self;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 3 ▶use URI::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 3 ▶use Ryu::Async;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 3 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 3 ▶use Future::Utils qw(repeat);
Subroutine BEGIN: called CC=1 SCAR=0.0
35 3 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 3 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 3 ▶use Net::Async::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 3 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 3 ▶use JSON::MaybeUTF8 qw(:v1);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 3 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 3 ▶use File::ShareDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 3 ▶use URI::Escape qw(uri_escape_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 3 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 3 ▶use WebService::Async::Onfido::Applicant;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 3 ▶use WebService::Async::Onfido::Address;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 3 ▶use WebService::Async::Onfido::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 3 ▶use WebService::Async::Onfido::Photo;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 3 ▶use WebService::Async::Onfido::Video;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 3 ▶use WebService::Async::Onfido::Check;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 3 ▶use WebService::Async::Onfido::Report;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 3 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
54 3 ▶use constant SUPPORTED_COUNTRIES_URL => 'https://documentation.onfido.com/identityISOsupported.json';
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 # Mapping file extension to mime type for currently
57 # supported document types
58 3 my %FILE_MIME_TYPE_MAPPING = (
59 jpg => 'image/jpeg',
60 jpeg => 'image/jpeg',
61 png => 'image/png',
62 pdf => 'application/pdf',
63 );
64  
65 5 ▶sub configure {
Subroutine configure: called CC=4 SCAR=13.9
Pod documented
66 5 my ($self, %args) = @_;
67 5 for my $k (qw(token requests_per_minute base_uri on_api_hit on_rate_limit rate_limit_delay)) {
68 30 ▶ $self->{$k} = delete $args{$k} if exists $args{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$k} 15 15 30
69 }
70  
71 5 ▶ $self->{rate_limit_delay} //= 60;
Condition100%2/2
$self->{"rate_limit_delay"} //= 60
l!l
14
MC/DC100%1/1
$self->{"rate_limit_delay"} //= 60
$self->{"rate_limit_delay"}
Truth table100%2/2
A
A= $self->{"rate_limit_delay"}
Aresult
11
00
72 5 return $self->next::method(%args);
73 }
74  
75 =head2 hook
76  
77 Executes a hook, if specified at configure time.
78  
79 Takes the following:
80  
81 =over 4
82  
83 =item * C<$hook> - the hook to execute
84  
85 =item * C<$data> - data to pass to the sub
86  
87 =back
88  
89 It returns C<undef>
90  
91 =cut
92  
93 28 ▶sub hook {
Subroutine hook: called CC=3 SCAR=11.0
Pod documented
94 28 my ($self, $hook, $data) = @_;
95  
96 28 ▶ return undef unless $self->{$hook};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$hook} 3 25 28
97  
98 25 ▶ return undef unless ref($self->{$hook}) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{$hook} eq "CODE" 1 24 25
99  
100 24 $self->{$hook}->($data);
101  
102 24 return undef;
103 }
104  
105 =head2 applicant_list
106  
107 Retrieves a list of all known applicants.
108  
109 Returns a L<Ryu::Source> which will emit one L<WebService::Async::Onfido::Applicant> for
110 each applicant found.
111  
112 =cut
113  
114 1 ▶sub applicant_list {
Subroutine applicant_list: called CC=1 SCAR=0.3
Pod documented
115 1 my ($self) = @_;
116 1 my $src = $self->source;
117 1 my $f = $src->completed;
118 1 my $uri = $self->endpoint('applicants');
119 (
120 1 ▶ repeat {
Subroutine __ANON__: called CC=1 SCAR=0.5
121 1 $log->tracef('GET %s', "$uri");
122 $self->rate_limiting->then(
123 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
124 1 $self->hook('on_api_hit', {GET => $uri});
125 1 $self->ua->GET($uri, $self->auth_headers,);
126 }
127 )->then(
128 1 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.5
129 try {
130 1 my ($res) = @_;
131 1 my $data = decode_json_utf8($res->content);
132 1 $log->tracef('Have response %s', $data);
133 1 my ($total) = $res->header('X-Total-Count');
134 1 $log->tracef('Expected total count %d', $total);
135 1 for (@{$data->{applicants}}) {
136 1 ▶ return $f if $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_ready 0 1 1
137 1 $src->emit(WebService::Async::Onfido::Applicant->new(%$_, onfido => $self));
138 }
139 1 $log->tracef('Links are %s', [$res->header('Link')]);
140 1 my %links = $self->extract_links($res->header('Link'));
141 1 ▶ if (my $next = $links{next}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = $links{'next'}) { } 0 1 1
142 0 ($uri) = $next;
143 } else {
144 1 $src->finish;
145 }
146 1 return Future->done;
147 1 } catch {
148 0 my ($err) = $@;
149 0 $log->errorf('Failed - %s', $err);
150 0 return Future->fail($err);
151 }
152 },
153 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
154 0 my ($err, @details) = @_;
155 0 $log->errorf('Failed to request document_list: %s', $err);
156 0 ▶ $src->fail($err, @details) unless $src->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $src->is_ready 0 0 0
157 0 Future->fail($err, @details);
158 })
159 1 }
160 1 ▶ until => sub { $f->is_ready })->retain;
Subroutine __ANON__: called CC=1 SCAR=0.0
161 1 return $src;
162 }
163  
164 =head2 paging
165  
166 Supports paging through HTTP GET requests.
167  
168 =over 4
169  
170 =item * C<$starting_uri> - the initial L<URI> to request
171  
172 =item * C<$factory> - a C<sub> that we will call with a L<Ryu::Source> and expect to return
173 a second response-processing C<sub>.
174  
175 =back
176  
177 Returns a L<Ryu::Source>.
178  
179 =cut
180  
181 0 ▶sub paging {
Subroutine paging: not called CC=2 SCAR=17.9
Pod documented
182 0 my ($self, $starting_uri, $factory) = @_;
183 0 ▶ my $uri =
Branch: 1 branch
BranchTrueFalseTotal
ref $starting_uri ? : 0 0 0
184 ref($starting_uri)
185 ? $starting_uri->clone
186 : URI->new($starting_uri);
187  
188 0 my $src = $self->source;
189 0 my $f = $src->completed;
190 0 my $code = $factory->($src);
191 (
192 0 ▶ repeat {
Subroutine __ANON__: not called CC=1 SCAR=6.9
193 0 $log->tracef('GET %s', "$uri");
194 $self->rate_limiting->then(
195 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
196 0 $self->hook('on_api_hit', {GET => $uri});
197 0 $self->ua->GET($uri, $self->auth_headers,);
198 }
199 )->then(
200 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
201 try {
202 0 my ($res) = @_;
203 0 my $data = decode_json_utf8($res->content);
204 0 $log->tracef('Have response %s', $data);
205 0 my ($total) = $res->header('X-Total-Count');
206 0 $log->tracef('Expected total count %d', $total);
207 0 $code->($data);
208 0 $log->tracef('Links are %s', [$res->header('Link')]);
209 0 my %links = $self->extract_links($res->header('Link'));
210  
211 0 ▶ if (my $next = $links{next}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = $links{'next'}) { } 0 0 0
212 0 ($uri) = $next;
213 } else {
214 0 $src->finish;
215 }
216 0 return Future->done;
217 0 } catch {
218 0 my ($err) = $@;
219 0 $log->errorf('Failed - %s', $err);
220 0 return Future->fail($err);
221 }
222 },
223 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
224 0 my ($err, @details) = @_;
225 0 $log->errorf('Failed to request %s: %s', $uri, $err);
226 0 ▶ $src->fail($err, @details) unless $src->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $src->is_ready 0 0 0
227 0 Future->fail($err, @details);
228 })
229 0 }
230 0 ▶ until => sub { $f->is_ready })->retain;
Subroutine __ANON__: not called CC=1 SCAR=6.9
231 0 return $src;
232 }
233  
234 =head2 extract_links
235  
236 Given a set of strings representing the C<Link> headers in an HTTP response,
237 extracts the URIs based on the C<rel> attribute as described in
238 L<RFC5988|http://tools.ietf.org/html/rfc5988>.
239  
240 Returns a list of key, value pairs where the key contains the lowercase C<rel> value
241 and the value is a L<URI> instance.
242  
243 my %links = $self->extract_links($res->header('Link'))
244 print "Last page would be $links{last}"
245  
246 =cut
247  
248 1 ▶sub extract_links {
Subroutine extract_links: called CC=3 SCAR=15.1
Pod documented
249 1 my ($self, @links) = @_;
250 1 my %links;
251 0 for (map { split /\h*,\h*/ } @links) {
252  
253 # Format is like:
254 # <https://api.eu.onfido.com/v3.4/applicants?page=2>; rel="next"
255 0 ▶ if (my ($url, $rel) = m{<(http[^>]+)>;\h*rel="([^"]+)"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = /<(http[^>]+)>;\h*rel="([^"]+)"/) 0 0 0
256 0 $links{lc $rel} = URI->new($url);
257 }
258 }
259 1 return %links;
260 }
261  
262 =head2 applicant_create
263  
264 Creates a new applicant record.
265  
266 See accessors in L<WebService::Async::Onfido::Applicant> for a full list of supported attributes.
267 These can be passed as named parameters to this method.
268  
269 Returns a L<Future> which resolves to a L<WebService::Async::Onfido::Applicant>
270 instance on successful completion.
271  
272 =cut
273  
274 1 ▶sub applicant_create {
Subroutine applicant_create: called CC=1 SCAR=0.1
Pod documented
275 1 my ($self, %args) = @_;
276 return $self->rate_limiting->then(
277 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
278 1 my $uri = $self->endpoint('applicants');
279 1 $self->hook(
280 'on_api_hit',
281 {
282 POST => $uri,
283 body => \%args,
284 });
285 1 $self->ua->POST(
286 $uri,
287 encode_json_utf8(\%args),
288 content_type => 'application/json',
289 $self->auth_headers,
290 );
291 }
292 )->then(
293 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.5
294 try {
295 1 my ($res) = @_;
296 1 my $data = decode_json_utf8($res->content);
297 1 $log->tracef('Have response %s', $data);
298 1 return Future->done(WebService::Async::Onfido::Applicant->new(%$data, onfido => $self));
299 1 } catch {
300 0 my ($err) = $@;
301 0 $log->errorf('Applicant creation failed - %s', $err);
302 0 return Future->fail($err);
303 }
304 1 });
305 }
306  
307 =head2 applicant_update
308  
309 Updates a single applicant.
310  
311 Returns a L<Future> which resolves to empty on success.
312  
313 =cut
314  
315 1 ▶sub applicant_update {
Subroutine applicant_update: called CC=1 SCAR=0.1
Pod documented
316 1 my ($self, %args) = @_;
317 return $self->rate_limiting->then(
318 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
319 1 my $uri = $self->endpoint('applicant', %args);
320 1 $self->hook(
321 'on_api_hit',
322 {
323 PUT => $uri,
324 body => \%args,
325 });
326 1 $self->ua->PUT(
327 $uri,
328 encode_json_utf8(\%args),
329 content_type => 'application/json',
330 $self->auth_headers,
331 );
332 }
333 )->then(
334 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.5
335 try {
336 1 my ($res) = @_;
337 1 my $data = decode_json_utf8($res->content);
338 1 $log->tracef('Have response %s', $data);
339 1 return Future->done();
340 1 } catch {
341 0 my ($err) = $@;
342 0 $log->errorf('Applicant update failed - %s', $err);
343 0 return Future->fail($err);
344 }
345 1 });
346 }
347  
348 =head2 applicant_delete
349  
350 Deletes a single applicant.
351  
352 Returns a L<Future> which resolves to empty on success.
353  
354 =cut
355  
356 1 ▶sub applicant_delete {
Subroutine applicant_delete: called CC=1 SCAR=0.8
Pod documented
357 1 my ($self, %args) = @_;
358 return $self->rate_limiting->then(
359 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
360 1 my $uri = $self->endpoint('applicant', %args);
361 1 $self->hook(
362 'on_api_hit',
363 {
364 DELETE => $uri,
365 });
366 1 $self->ua->do_request(
367 uri => $uri,
368 method => 'DELETE',
369 $self->auth_headers,
370 );
371 }
372 )->then(
373 1 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=11.1
374 try {
375 1 my ($res) = @_;
376 1 ▶ return Future->done if $res->code == 204;
Branch: 1 branch
BranchTrueFalseTotal
if $res->code == 204 1 0 1
377 0 my $data = decode_json_utf8($res->content);
378 0 $log->tracef('Have response %s', $data);
379 0 return Future->fail($data);
380 1 } catch {
381 0 my ($err) = $@;
382 0 $log->errorf('Applicant delete failed - %s', $err);
383 0 return Future->fail($err);
384 }
385 1 });
386 }
387  
388 =head2 applicant_get
389  
390 Retrieve a single applicant.
391  
392 Returns a L<Future> which resolves to a L<WebService::Async::Onfido::Applicant>
393  
394 =cut
395  
396 2 ▶sub applicant_get {
Subroutine applicant_get: called CC=1 SCAR=0.1
Pod documented
397 2 my ($self, %args) = @_;
398 return $self->rate_limiting->then(
399 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
400 2 my $uri = $self->endpoint('applicant', %args);
401 2 $self->hook(
402 'on_api_hit',
403 {
404 GET => $uri,
405 });
406 2 $self->ua->do_request(
407 uri => $uri,
408 method => 'GET',
409 $self->auth_headers,
410 );
411 }
412 )->then(
413 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.5
414 try {
415 2 my ($res) = @_;
416 2 my $data = decode_json_utf8($res->content);
417 2 $log->tracef('Have response %s', $data);
418 2 return Future->done(WebService::Async::Onfido::Applicant->new(%$data, onfido => $self));
419 2 } catch {
420 0 my ($err) = $@;
421 0 $log->errorf('Failed - %s', $err);
422 0 return Future->fail($err);
423 }
424 2 });
425 }
426  
427 1 ▶sub check_get {
Subroutine check_get: called CC=1 SCAR=0.1
Pod undocumented
428 1 my ($self, %args) = @_;
429 return $self->rate_limiting->then(
430 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
431 1 my $uri = $self->endpoint('check', %args);
432 1 $self->hook(
433 'on_api_hit',
434 {
435 GET => $uri,
436 });
437 1 $self->ua->do_request(
438 uri => $uri,
439 method => 'GET',
440 $self->auth_headers,
441 );
442 }
443 )->then(
444 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.5
445 try {
446 1 my ($res) = @_;
447 1 my $data = decode_json_utf8($res->content);
448 1 $log->tracef('Have response %s', $data);
449 1 return Future->done(WebService::Async::Onfido::Check->new(%$data, onfido => $self));
450 1 } catch {
451 0 my ($err) = $@;
452 0 $log->errorf('Failed - %s', $err);
453 0 return Future->fail($err);
454 }
455 1 });
456 }
457  
458 =head2 document_list
459  
460 List all documents for a given L<WebService::Async::Onfido::Applicant>.
461  
462 Takes the following named parameters:
463  
464 =over 4
465  
466 =item * C<applicant_id> - the L<WebService::Async::Onfido::Applicant/id> for the applicant to query
467  
468 =back
469  
470 Returns a L<Ryu::Source> which will emit one L<WebService::Async::Onfido::Document> for
471 each document found.
472  
473 =cut
474  
475 1 ▶sub document_list {
Subroutine document_list: called CC=1 SCAR=0.2
Pod documented
476 1 my ($self, %args) = @_;
477 1 my $src = $self->source;
478 1 my $uri = $self->endpoint('documents');
479 1 $uri->query('applicant_id=' . uri_escape_utf8($args{applicant_id}));
480  
481 $self->rate_limiting->then(
482 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
483 1 $self->hook(
484 'on_api_hit',
485 {
486 GET => $uri,
487 });
488 1 $self->ua->GET($uri, $self->auth_headers,);
489 }
490 )->then(
491 1 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=15.4
492 try {
493 1 my ($res) = @_;
494 1 $log->tracef("GET %s => %s", $uri, $res->decoded_content);
495 1 my $data = decode_json_utf8($res->content);
496 1 my $f = $src->completed;
497 1 $log->tracef('Have response %s', $data);
498 1 for (@{$data->{documents}}) {
499 1 ▶ return $f if $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_ready 0 1 1
500 1 $src->emit(WebService::Async::Onfido::Document->new(%$_, onfido => $self));
501 }
502 1 $src->finish;
503 1 return Future->done;
504 1 } catch {
505 0 my ($err) = $@;
506 0 $log->errorf('Failed - %s', $err);
507 0 ▶ $src->fail('Failed to get document list.')
Branch: 1 branch
BranchTrueFalseTotal
unless $src->is_ready 0 0 0
508 unless $src->is_ready;
509 0 return Future->fail($err);
510 }
511 1 })->retain;
512 1 return $src;
513 }
514  
515 =head2 get_document_details
516  
517 Gets a document object for a given L<WebService::Async::Onfido::Applicant>.
518  
519 Takes the following named parameters:
520  
521 =over 4
522  
523 =item * C<applicant_id> - the L<WebService::Async::Onfido::Applicant/id> for the applicant to query
524  
525 =item * C<document_id> - the L<WebService::Async::Onfido::Document/id> for the document to query
526  
527 =back
528  
529 Returns a Future object which consists of a L<WebService::Async::Onfido::Document>
530  
531 =cut
532  
533 1 ▶sub get_document_details {
Subroutine get_document_details: called CC=1 SCAR=0.1
Pod documented
534 1 my ($self, %args) = @_;
535 1 my $uri = $self->endpoint('document', %args);
536 return $self->rate_limiting->then(
537 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
538 1 $self->hook(
539 'on_api_hit',
540 {
541 GET => $uri,
542 });
543 1 $self->ua->GET($uri, $self->auth_headers,);
544 }
545 )->then(
546 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.4
547 try {
548 1 my ($res) = @_;
549 1 $log->tracef("GET %s => %s", $uri, $res->decoded_content);
550 1 my $data = decode_json_utf8($res->content);
551 1 $log->tracef('Have response %s', $data);
552 1 return Future->done(WebService::Async::Onfido::Document->new(%$data, onfido => $self));
553 1 } catch {
554 0 my ($err) = $@;
555 0 $log->errorf('Failed - %s', $err);
556 0 return Future->fail($err);
557 }
558 1 });
559 }
560  
561 =head2 photo_list
562  
563 List all photos for a given L<WebService::Async::Onfido::Applicant>.
564  
565 Takes the following named parameters:
566  
567 =over 4
568  
569 =item * C<applicant_id> - the L<WebService::Async::Onfido::Applicant/id> for the applicant to query
570  
571 =back
572  
573 Returns a L<Ryu::Source> which will emit one L<WebService::Async::Onfido::Photo> for
574 each photo found.
575  
576 =cut
577  
578 1 ▶sub photo_list {
Subroutine photo_list: called CC=1 SCAR=0.2
Pod documented
579 1 my ($self, %args) = @_;
580 1 my $src = $self->source;
581 1 my $uri = $self->endpoint('photos', %args);
582 $self->rate_limiting->then(
583 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
584 1 $self->hook(
585 'on_api_hit',
586 {
587 GET => $uri,
588 });
589 1 $self->ua->GET($uri, $self->auth_headers,);
590 }
591 )->then(
592 1 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=15.4
593 try {
594 1 my ($res) = @_;
595 1 $log->tracef("GET %s => %s", $uri, $res->decoded_content);
596 1 my $data = decode_json_utf8($res->content);
597 1 my $f = $src->completed;
598 1 $log->tracef('Have response %s', $data);
599 1 for (@{$data->{live_photos}}) {
600 1 ▶ return $f if $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_ready 0 1 1
601 1 $src->emit(WebService::Async::Onfido::Photo->new(%$_, onfido => $self));
602 }
603 1 $src->finish;
604 1 return Future->done;
605 1 } catch {
606 0 my ($err) = $@;
607 0 $log->errorf('Failed - %s', $err);
608 0 ▶ $src->fail('Failed to get photo list.') unless $src->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $src->is_ready 0 0 0
609 0 return Future->fail($err);
610 }
611 1 })->retain;
612 1 return $src;
613 }
614  
615 =head2 get_photo_details
616  
617 Gets a live_photo object for a given L<WebService::Async::Onfido::Applicant>.
618  
619 Takes the following named parameters:
620  
621 =over 4
622  
623 =item * C<live_photo_id> - the L<WebService::Async::Onfido::Photo/id> for the document to query
624  
625 =back
626  
627 Returns a Future object which consists of a L<WebService::Async::Onfido::Photo>
628  
629 =cut
630  
631 1 ▶sub get_photo_details {
Subroutine get_photo_details: called CC=1 SCAR=0.1
Pod documented
632 1 my ($self, %args) = @_;
633 1 my $uri = $self->endpoint('photo', %args);
634 return $self->rate_limiting->then(
635 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
636 1 $self->hook(
637 'on_api_hit',
638 {
639 GET => $uri,
640 });
641 1 $self->ua->GET($uri, $self->auth_headers,);
642 }
643 )->then(
644 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.4
645 try {
646 1 my ($res) = @_;
647 1 $log->tracef("GET %s => %s", $uri, $res->decoded_content);
648 1 my $data = decode_json_utf8($res->content);
649 1 $log->tracef('Have response %s', $data);
650 1 return Future->done(WebService::Async::Onfido::Photo->new(%$data, onfido => $self));
651 1 } catch {
652 0 my ($err) = $@;
653 0 $log->errorf('Failed - %s', $err);
654 0 return Future->fail($err);
655 }
656 1 });
657 }
658  
659 =head2 document_upload
660  
661 Uploads a single document for a given applicant.
662  
663 Takes the following named parameters:
664  
665 =over 4
666  
667 =item * C<type> - can be C<passport>, C<photo>, C<poa>
668  
669 =item * C<side> - which side, either C<front> or C<back>
670  
671 =item * C<issuing_country> - which country this document is for
672  
673 =item * C<filename> - the file name to use for this item
674  
675 =item * C<data> - the bytes for this image file (must be in JPEG format)
676  
677 =back
678  
679 =cut
680  
681 1 ▶sub document_upload {
Subroutine document_upload: called CC=1 SCAR=0.3
Pod documented
682 1 my ($self, %args) = @_;
683 1 my $uri = $self->endpoint('documents');
684  
685 my $req = HTTP::Request::Common::POST(
686 $uri,
687 content_type => 'form-data',
688 content => [
689 4 %args{grep { exists $args{$_} } qw(type side issuing_country applicant_id)},
690 file => [
691 undef, $args{filename},
692 'Content-Type' => _get_mime_type($args{filename}),
693 Content => $args{data}
694 ],
695 ],
696 1 %{$self->auth_headers},
697 );
698 return $self->rate_limiting->then(
699 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
700 1 delete $args{data};
701 1 $self->hook(
702 'on_api_hit',
703 {
704 POST => $uri,
705 body => \%args
706 });
707 1 $self->ua->do_request(
708 request => $req,
709 );
710 }
711 )->catch(
712 0 ▶ http => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
713 0 my ($message, undef, $response, $request) = @_;
714 0 $log->errorf('Request %s received %s with full response as %s', $request->uri, $message, $response->content,);
715  
716 # Just pass it on
717 0 Future->fail(
718 $message,
719 http => $response,
720 $request
721 );
722 }
723 )->then(
724 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.5
725 try {
726 1 my ($res) = @_;
727 1 my $data = decode_json_utf8($res->content);
728 1 $log->tracef('Have response %s', $data);
729 1 return Future->done(WebService::Async::Onfido::Document->new(%$data, onfido => $self));
730 1 } catch {
731 0 my ($err) = $@;
732 0 $log->errorf('Failed - %s', $err);
733 0 return Future->fail($err);
734 }
735 1 });
736 }
737  
738 =head2 live_photo_upload
739  
740 Uploads a single "live photo" for a given applicant.
741  
742 Takes the following named parameters:
743  
744 =over 4
745  
746 =item * C<applicant_id> - ID for the person this photo relates to
747  
748 =item * C<advanced_validation> - perform additional validation (ensure we only have a single face)
749  
750 =item * C<filename> - the file name to use for this item
751  
752 =item * C<data> - the bytes for this image file (must be in JPEG format)
753  
754 =back
755  
756 =cut
757  
758 1 ▶sub live_photo_upload {
Subroutine live_photo_upload: called CC=2 SCAR=7.4
Pod documented
759 1 my ($self, %args) = @_;
760 1 my $uri = $self->endpoint('photo_upload');
761 1 ▶ $args{advanced_validation} = $args{advanced_validation} ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$args{'advanced_validation'} ? : 0 1 1
762 my $req = HTTP::Request::Common::POST(
763 $uri,
764 content_type => 'form-data',
765 content => [
766 2 %args{grep { exists $args{$_} } qw(advanced_validation applicant_id)},
767 file => [
768 undef, $args{filename},
769 'Content-Type' => _get_mime_type($args{filename}),
770 Content => $args{data}
771 ],
772 ],
773 1 %{$self->auth_headers},
774 );
775 1 $log->tracef('Photo upload: %s', $req->as_string("\n"));
776 return $self->rate_limiting->then(
777 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
778 1 delete $args{data};
779 1 $self->hook(
780 'on_api_hit',
781 {
782 POST => $uri,
783 body => \%args
784 });
785 1 $self->ua->do_request(
786 request => $req,
787 );
788 }
789 )->catch(
790 0 ▶ http => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
791 0 my ($message, undef, $response, $request) = @_;
792 0 $log->errorf('Request %s received %s with full response as %s', $request->uri, $message, $response->content,);
793  
794 # Just pass it on
795 0 Future->fail(
796 $message,
797 http => $response,
798 $request
799 );
800 }
801 )->then(
802 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.5
803 try {
804 1 my ($res) = @_;
805 1 my $data = decode_json_utf8($res->content);
806 1 $log->tracef('Have response %s', $data);
807 1 return Future->done(WebService::Async::Onfido::Photo->new(%$data, onfido => $self));
808 1 } catch {
809 0 my ($err) = $@;
810 0 $log->errorf('Failed - %s', $err);
811 0 return Future->fail($err);
812 }
813 1 });
814 }
815  
816 =head2 applicant_check
817  
818 Perform an identity check on an applicant.
819  
820 This is the main method for dealing with verification - once you have created
821 the applicant and uploaded some documents, call this to start the process of
822 checking the documents and details, and generating the reports.
823  
824 L<https://documentation.onfido.com/#check-object>
825  
826 Takes the following named parameters:
827  
828 =over 4
829  
830 =item * C<applicant_id> - the applicant requesting the check
831  
832 =item * C<document_ids> - arrayref of documents ids to be analyzed on this check
833  
834 =item * C<report_names> - arrayref of the reports to be made (e.g: document, facial_similarity_photo)
835  
836 =item * C<tags> - custom tags to apply to these reports
837  
838 =item * C<suppress_form_emails> - if true, do B<not> send out the email to
839 the applicant
840  
841 =item * C<asynchronous> - return immediately and perform check in the background (default true since v3)
842  
843 =item * C<charge_applicant_for_check> - the applicant must enter payment
844 details for this check, and it will not count towards the quota for this
845 service account
846  
847 =item * C<consider> - used for sandbox API testing only
848  
849 =back
850  
851 Returns a L<Future> which will resolve with the result.
852  
853 =cut
854  
855 1 ▶sub applicant_check {
Subroutine applicant_check: called CC=1 SCAR=0.3
Pod documented
856 1 my ($self, %args) = @_;
857 3 ▶ use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
858 return $self->rate_limiting->then(
859 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
860 1 my $uri = $self->endpoint('checks');
861 1 $self->hook(
862 'on_api_hit',
863 {
864 POST => $uri,
865 body => \%args
866 });
867 1 $self->ua->POST(
868 $uri,
869 encode_json_utf8(\%args),
870 content_type => 'application/json',
871 $self->auth_headers,
872 );
873 }
874 )->catch(
875 0 ▶ http => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
876 0 my ($message, undef, $response, $request) = @_;
877  
878 0 $log->errorf('Request %s received %s with full response as %s', $request->uri, $message, $response->content,);
879  
880 # Just pass it on
881 0 Future->fail(
882 $message,
883 http => $response,
884 $request
885 );
886 }
887 )->then(
888 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.5
889 try {
890 1 my ($res) = @_;
891 1 my $data = decode_json_utf8($res->content);
892 1 $log->tracef('Have response %s', $data);
893 1 return Future->done(WebService::Async::Onfido::Check->new(%$data, onfido => $self));
894 1 } catch {
895 0 my ($err) = $@;
896 0 $log->errorf('Failed - %s', $err);
897 0 return Future->fail($err);
898 }
899 1 });
900 }
901  
902 5 ▶sub check_list {
Subroutine check_list: called CC=2 SCAR=7.0
Pod undocumented
903 5 my ($self, %args) = @_;
904 5 ▶ my $applicant_id = delete $args{applicant_id} or die 'Need an applicant ID';
Branch: 1 branch
BranchTrueFalseTotal
my $applicant_id = delete $args{"applicant_id"} or die "Need an applicant ID" 5 0 5
905 5 my $src = $self->source;
906 5 my $f = $src->completed;
907 5 my $uri = $self->endpoint('checks');
908 5 $uri->query('applicant_id=' . uri_escape_utf8($applicant_id));
909  
910 5 $log->tracef('GET %s', "$uri");
911 $self->rate_limiting->then(
912 5 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
913 5 $self->hook(
914 'on_api_hit',
915 {
916 GET => $uri,
917 });
918 5 $self->ua->do_request(
919 uri => $uri,
920 method => 'GET',
921 $self->auth_headers,
922 );
923 }
924 )->then(
925 5 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
926 try {
927 5 my ($res) = @_;
928 5 my $data = decode_json_utf8($res->content);
929 5 $log->tracef('Have response %s', $data);
930 5 my ($total) = $res->header('X-Total-Count');
931 5 $log->tracef('Expected total count %d', $total);
932 5 for (@{$data->{checks}}) {
933 5 ▶ return $f if $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_ready 0 5 5
934 5 $src->emit(WebService::Async::Onfido::Check->new(%$_, onfido => $self));
935 }
936 5 $src->finish;
937 5 Future->done;
938 5 } catch {
939 0 my ($err) = $@;
940 0 $log->errorf('Failed - %s', $err);
941 0 return Future->fail($err);
942 }
943 5 })->retain;
944 5 return $src;
945 }
946  
947 1 ▶sub report_get {
Subroutine report_get: called CC=1 SCAR=0.1
Pod undocumented
948 1 my ($self, %args) = @_;
949 return $self->rate_limiting->then(
950 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
951 1 my $uri = $self->endpoint('report', %args);
952 1 $self->hook(
953 'on_api_hit',
954 {
955 GET => $uri,
956 });
957 1 $self->ua->do_request(
958 uri => $uri,
959 method => 'GET',
960 $self->auth_headers,
961 );
962 }
963 )->then(
964 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.5
965 try {
966 1 my ($res) = @_;
967 1 my $data = decode_json_utf8($res->content);
968 1 $log->tracef('Have response %s', $data);
969 1 return Future->done(WebService::Async::Onfido::Report->new(%$data, onfido => $self));
970 1 } catch {
971 0 my ($err) = $@;
972 0 $log->errorf('Failed - %s', $err);
973 0 return Future->fail($err);
974 }
975 1 });
976 }
977  
978 1 ▶sub report_list {
Subroutine report_list: called CC=2 SCAR=7.1
Pod undocumented
979 1 my ($self, %args) = @_;
980  
981 1 ▶ my $check_id = delete $args{check_id} or die 'Need a check ID';
Branch: 1 branch
BranchTrueFalseTotal
my $check_id = delete $args{"check_id"} or die "Need a check ID" 1 0 1
982  
983 1 my $src = $self->source;
984 1 my $f = $src->completed;
985  
986 1 my $uri = $self->endpoint('reports', check_id => $check_id);
987 1 $uri->query('check_id=' . uri_escape_utf8($check_id));
988 1 $log->tracef('GET %s', "$uri");
989  
990 $self->rate_limiting->then(
991 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
992 1 $self->hook(
993 'on_api_hit',
994 {
995 GET => $uri,
996 });
997 1 $self->ua->do_request(
998 uri => $uri,
999 method => 'GET',
1000 $self->auth_headers,
1001 );
1002 }
1003 )->then(
1004 1 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.7
1005 try {
1006 1 my ($res) = @_;
1007  
1008 1 my $data = decode_json_utf8($res->content);
1009 1 for (@{$data->{reports}}) {
1010 2 ▶ return $f if $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_ready 0 2 2
1011 2 $src->emit(WebService::Async::Onfido::Report->new(%$_, onfido => $self));
1012 }
1013 1 $src->finish;
1014 1 Future->done;
1015 1 } catch {
1016 0 my ($err) = $@;
1017 0 $log->errorf('Failed - %s', $err);
1018 0 return Future->fail($err);
1019 }
1020 1 })->retain;
1021 1 return $src;
1022 }
1023  
1024 =head2 download_check
1025  
1026 Gets the PDF report for a given L<WebService::Async::Onfido::Check>.
1027  
1028 Takes the following named parameters:
1029  
1030 =over 4
1031  
1032 =item * C<check_id> - the L<WebService::Async::Onfido::Check/id> for the check to query
1033  
1034 =back
1035  
1036 Returns a PDF file blob
1037  
1038 =cut
1039  
1040 2 ▶sub download_check {
Subroutine download_check: called CC=1 SCAR=0.2
Pod documented
1041 2 my ($self, %args) = @_;
1042 return $self->rate_limiting->then(
1043 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1044 2 my $uri = $self->endpoint('check_download', %args);
1045 2 $self->hook(
1046 'on_api_hit',
1047 {
1048 GET => $uri,
1049 });
1050 2 $self->ua->do_request(
1051 uri => $uri,
1052 method => 'GET',
1053 $self->auth_headers,
1054 );
1055 }
1056 )->then(
1057 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.8
1058 try {
1059 2 my ($res) = @_;
1060 2 my $data = $res->content;
1061 2 return Future->done($data);
1062 2 } catch {
1063 0 my ($err) = $@;
1064 0 $log->errorf('Failed - %s', $err);
1065 0 return Future->fail($err);
1066 }
1067 2 });
1068 }
1069  
1070 =head2 download_photo
1071  
1072 Gets a live_photo in a form of binary data for a given L<WebService::Async::Onfido::Photo>.
1073  
1074 Takes the following named parameters:
1075  
1076 =over 4
1077  
1078 =item * C<live_photo_id> - the L<WebService::Async::Onfido::Photo/id> for the document to query
1079  
1080 =back
1081  
1082 Returns a photo file blob
1083  
1084 =cut
1085  
1086 1 ▶sub download_photo {
Subroutine download_photo: called CC=1 SCAR=0.2
Pod documented
1087 1 my ($self, %args) = @_;
1088 return $self->rate_limiting->then(
1089 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1090 1 my $uri = $self->endpoint('photo_download', %args);
1091 1 $self->hook(
1092 'on_api_hit',
1093 {
1094 GET => $uri,
1095 });
1096 1 $self->ua->do_request(
1097 uri => $uri,
1098 method => 'GET',
1099 $self->auth_headers,
1100 );
1101 }
1102 )->then(
1103 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.8
1104 try {
1105 1 my ($res) = @_;
1106 1 my $data = $res->content;
1107 1 return Future->done($data);
1108 1 } catch {
1109 0 my ($err) = $@;
1110 0 $log->errorf('Failed - %s', $err);
1111 0 return Future->fail($err);
1112 }
1113 1 });
1114 }
1115  
1116 =head2 download_document
1117  
1118 Gets a document in a form of binary data for a given L<WebService::Async::Onfido::Document>.
1119  
1120 Takes the following named parameters:
1121  
1122 =over 4
1123  
1124 =item * C<applicant_id> - the L<WebService::Async::Onfido::Applicant/id> for the applicant to query
1125  
1126 =item * C<document_id> - the L<WebService::Async::Onfido::Document/id> for the document to query
1127  
1128 =back
1129  
1130 Returns a document file blob
1131  
1132 =cut
1133  
1134 1 ▶sub download_document {
Subroutine download_document: called CC=1 SCAR=0.2
Pod documented
1135 1 my ($self, %args) = @_;
1136 return $self->rate_limiting->then(
1137 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1138 1 my $uri = $self->endpoint('document_download', %args);
1139 1 $self->hook(
1140 'on_api_hit',
1141 {
1142 GET => $uri,
1143 });
1144 1 $self->ua->do_request(
1145 uri => $uri,
1146 method => 'GET',
1147 $self->auth_headers,
1148 );
1149 }
1150 )->then(
1151 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.8
1152 try {
1153 1 my ($res) = @_;
1154 1 my $data = $res->content;
1155 1 return Future->done($data);
1156 1 } catch {
1157 0 my ($err) = $@;
1158 0 $log->errorf('Failed - %s', $err);
1159 0 return Future->fail($err);
1160 }
1161 1 });
1162 }
1163  
1164 =head2 countries_list
1165  
1166 Returns a hashref containing 3-letter country codes as keys and supporting status
1167 as their value.
1168  
1169 =cut
1170  
1171 0 ▶sub countries_list {
Subroutine countries_list: not called CC=1 SCAR=6.9
Pod documented
1172 0 my ($self) = @_;
1173  
1174 return $self->ua->GET(SUPPORTED_COUNTRIES_URL)->then(
1175 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1176 try {
1177 0 my ($res) = @_;
1178 0 my $onfido_countries = decode_json_utf8($res->content);
1179  
1180 my %countries_list =
1181 0 map { $_->{alpha3} => $_->{supported_identity_report} + 0 } @$onfido_countries;
1182 0 return Future->done(\%countries_list);
1183 0 } catch {
1184 0 my ($err) = $@;
1185 0 $log->errorf('Failed - %s', $err);
1186 0 return Future->fail($err);
1187 }
1188 0 });
1189 }
1190  
1191 =head2 supported_documents_list
1192  
1193 Returns an array of hashes of supported_documents for each country
1194  
1195 =cut
1196  
1197 2 ▶sub supported_documents_list {
Subroutine supported_documents_list: called CC=2 SCAR=7.0
Pod documented
1198 2 my $path = Path::Tiny::path(__DIR__)->parent(3)->child('share/supported_documents.json');
1199 2 ▶ $path = Path::Tiny::path(File::ShareDir::dist_file('WebService-Async-Onfido', 'supported_documents.json')) unless $path->exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $path->exists 2 0 2
1200 2 my $supported_documents = decode_json_text($path->slurp_utf8);
1201 2 return $supported_documents;
1202 }
1203  
1204 =head2 supported_documents_for_country
1205  
1206 Returns the supported_documents_list for the country
1207  
1208 =cut
1209  
1210 0 ▶sub supported_documents_for_country {
Subroutine supported_documents_for_country: not called CC=2 SCAR=17.9
Pod documented
1211 0 my ($self, $country_code) = @_;
1212  
1213 my %country_details =
1214 0 map { $_->{country_code} => $_ } @{supported_documents_list()};
1215  
1216 0 ▶ return $country_details{$country_code}->{doc_types_list} // [];
Condition0%0/2
$country_details{$country_code}{"doc_types_list"} // []
l!l
00
MC/DC0%0/1
$country_details{$country_code}{"doc_types_list"} // []
$country_details{$country_code}{"doc_types_list"}
Truth table0%0/2
A
A= $country_details{$country_code}{"doc_types_list"}
Aresult
11
00
1217 }
1218  
1219 =head2 is_country_supported
1220  
1221 Returns 1 if country supported and 0 for unsupported
1222  
1223 =cut
1224  
1225 0 ▶sub is_country_supported {
Subroutine is_country_supported: not called CC=2 SCAR=17.9
Pod documented
1226 0 my ($self, $country_code) = @_;
1227  
1228 my %country_details =
1229 0 map { $_->{country_code} => $_ } @{supported_documents_list()};
1230  
1231 0 ▶ return $country_details{$country_code} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$country_details{$country_code} ? : 0 0 0
1232 }
1233  
1234 =head2 sdk_token
1235  
1236 Returns the generated Onfido Web SDK token for the applicant.
1237  
1238 L<https://documentation.onfido.com/#web-sdk-tokens>
1239  
1240 Takes the following named parameters:
1241  
1242 =over 4
1243  
1244 =item * C<applicant_id> - ID of the applicant to request the token for
1245  
1246 =item * C<referrer> - the URL of the web page where the Web SDK will be used
1247  
1248 =back
1249  
1250 =cut
1251  
1252 1 ▶sub sdk_token {
Subroutine sdk_token: called CC=1 SCAR=0.1
Pod documented
1253 1 my ($self, %args) = @_;
1254 return $self->rate_limiting->then(
1255 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1256 1 my $uri = $self->endpoint('sdk_token');
1257 1 $self->hook(
1258 'on_api_hit',
1259 {
1260 POST => $uri,
1261 body => \%args,
1262 });
1263 1 $self->ua->POST(
1264 $uri,
1265 encode_json_utf8(\%args),
1266 content_type => 'application/json',
1267 $self->auth_headers,
1268 );
1269 }
1270 )->then(
1271 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.5
1272 try {
1273 1 my ($res) = @_;
1274 1 my $data = decode_json_utf8($res->content);
1275 1 $log->tracef('Have response %s', $data);
1276 1 return Future->done($data);
1277 1 } catch {
1278 0 my ($err) = $@;
1279 0 $log->errorf('Token generation failed - %s', $err);
1280 0 return Future->fail($err);
1281 }
1282 1 });
1283 }
1284  
1285 =head2 endpoints
1286  
1287 Returns an accessor for the endpoints data. This is a hashref containing URI
1288 templates, used by L</endpoint>.
1289  
1290 =cut
1291  
1292 50 ▶sub endpoints {
Subroutine endpoints: called CC=4 SCAR=14.0
Pod documented
1293 50 my ($self) = @_;
1294 50 ▶ return $self->{endpoints} ||= do {
Condition66%2/3
$self->{"endpoints"} ||= do { my $path = Path::Tiny::path(&__DIR__())->parent(3)->child("share/endpoints.json"); $path = Path::Tiny::path(File::ShareDir::dist_file("WebService-Async-Onfido", "endpoints.json")) unless $path->exists; my $endpoints = decode_json_text($path->slurp_utf8); my $base_uri = $self->base_uri; $_ = $base_uri . $_ foreach (values %$endpoints); $endpoints }
l!l&&r!l&&!r
4640
MC/DC0%0/2
$self->{"endpoints"} ||= do { my $path = Path::Tiny::path(&__DIR__())->parent(3)->child("share/endpoints.json"); $path = Path::Tiny::path(File::ShareDir::dist_file("WebService-Async-Onfido", "endpoints.json")) unless $path->exists; my $endpoints = decode_json_text($path->slurp_utf8); my $base_uri = $self->base_uri; $_ = $base_uri . $_ foreach (values %$endpoints); $endpoints }
$self->{"endpoints"} do { my $path = Path::Tiny::path(&__DIR__())->parent(3)->child("share/endpoints.json"); $path = Path::Tiny::path(File::ShareDir::dist_file("WebService-Async-Onfido", "endpoints.json")) unless $path->exists; my $endpoints = decode_json_text($path->slurp_utf8); my $base_uri = $self->base_uri; $_ = $base_uri . $_ foreach (values %$endpoints); $endpoints }
Truth table66%2/3
A ||= B
A= $self->{"endpoints"}B= do { my $path = Path::Tiny::path(&__DIR__())->parent(3)->child("share/endpoints.json"); $path = Path::Tiny::path(File::ShareDir::dist_file("WebService-Async-Onfido", "endpoints.json")) unless $path->exists; my $endpoints = decode_json_text($path->slurp_utf8); my $base_uri = $self->base_uri; $_ = $base_uri . $_ foreach (values %$endpoints); $endpoints }
ABresult
1X1
011
000
1295 4 my $path =
1296 Path::Tiny::path(__DIR__)->parent(3)->child('share/endpoints.json');
1297 4 ▶ $path = Path::Tiny::path(File::ShareDir::dist_file('WebService-Async-Onfido', 'endpoints.json')) unless $path->exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $path->exists 4 0 4
1298 4 my $endpoints = decode_json_text($path->slurp_utf8);
1299 4 my $base_uri = $self->base_uri;
1300 4 $_ = $base_uri . $_ for values %$endpoints;
1301 4 $endpoints;
1302 };
1303 }
1304  
1305 =head2 endpoint
1306  
1307 Expands the selected URI via L<URI::Template>. Each item is defined in our C<endpoints.json>
1308 file.
1309  
1310 Returns a L<URI> instance.
1311  
1312 =cut
1313  
1314 50 ▶sub endpoint {
Subroutine endpoint: called CC=1 SCAR=0.0
Pod documented
1315 50 my ($self, $endpoint, %args) = @_;
1316 50 return URI::Template->new($self->endpoints->{$endpoint})->process(%args);
1317 }
1318  
1319 4 ▶sub base_uri {
Subroutine base_uri: called CC=3 SCAR=11.4
Pod undocumented
1320 4 my $self = shift;
1321 4 ▶ return $self->{base_uri} if blessed($self->{base_uri});
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($self->{"base_uri"}) 0 4 4
1322 $self->{base_uri} =
1323 4 ▶ URI->new($self->{base_uri} // 'https://api.eu.onfido.com');
Condition50%1/2
$self->{"base_uri"} // "https://api.eu.onfido.com"
l!l
40
MC/DC0%0/1
$self->{"base_uri"} // "https://api.eu.onfido.com"
$self->{"base_uri"}
Truth table50%1/2
A
A= $self->{"base_uri"}
Aresult
11
00
1324 4 return $self->{base_uri};
1325 }
1326  
1327 26 ▶sub token { return shift->{token} }
Subroutine token: called CC=1 SCAR=0.0
Pod documented
1328  
1329 26 ▶sub ua {
Subroutine ua: called CC=2 SCAR=7.0
Pod undocumented
1330 26 my ($self) = @_;
1331 26 ▶ return $self->{ua} //= do {
Condition66%2/3
$self->{"ua"} //= do { $self->add_child(my $ua = "Net::Async::HTTP"->new("fail_on_error", 1, "decode_content", 1, "pipeline", 0, "stall_timeout", 60, "max_connections_per_host", 2, "user_agent", "Mozilla/4.0 (WebService::Async::Onfido; DERIV\@cpan.org; https://metacpan.org/pod/WebService::Async::Onfido)")); $ua }
l!l&&r!l&&!r
2240
MC/DC0%0/2
$self->{"ua"} //= do { $self->add_child(my $ua = "Net::Async::HTTP"->new("fail_on_error", 1, "decode_content", 1, "pipeline", 0, "stall_timeout", 60, "max_connections_per_host", 2, "user_agent", "Mozilla/4.0 (WebService::Async::Onfido; DERIV\@cpan.org; https://metacpan.org/pod/WebService::Async::Onfido)")); $ua }
$self->{"ua"} do { $self->add_child(my $ua = "Net::Async::HTTP"->new("fail_on_error", 1, "decode_content", 1, "pipeline", 0, "stall_timeout", 60, "max_connections_per_host", 2, "user_agent", "Mozilla/4.0 (WebService::Async::Onfido; DERIV\@cpan.org; https://metacpan.org/pod/WebService::Async::Onfido)")); $ua }
Truth table66%2/3
A //= B
A= $self->{"ua"}B= do { $self->add_child(my $ua = "Net::Async::HTTP"->new("fail_on_error", 1, "decode_content", 1, "pipeline", 0, "stall_timeout", 60, "max_connections_per_host", 2, "user_agent", "Mozilla/4.0 (WebService::Async::Onfido; DERIV\@cpan.org; https://metacpan.org/pod/WebService::Async::Onfido)")); $ua }
ABresult
1X1
011
000
1332 4 $self->add_child(
1333 my $ua = Net::Async::HTTP->new(
1334 fail_on_error => 1,
1335 decode_content => 1,
1336 pipeline => 0,
1337 stall_timeout => 60,
1338 max_connections_per_host => 2,
1339 user_agent => 'Mozilla/4.0 (WebService::Async::Onfido; DERIV@cpan.org; https://metacpan.org/pod/WebService::Async::Onfido)',
1340 ));
1341 4 $ua;
1342 }
1343 }
1344  
1345 26 ▶sub auth_headers {
Subroutine auth_headers: called CC=1 SCAR=0.0
Pod undocumented
1346 26 my ($self) = @_;
1347 26 return headers => {'Authorization' => 'Token token=' . $self->token};
1348 }
1349  
1350 9 ▶sub ryu {
Subroutine ryu: called CC=2 SCAR=7.0
Pod undocumented
1351 9 my ($self) = @_;
1352 9 ▶ return $self->{ryu} //= do {
Condition66%2/3
$self->{"ryu"} //= do { $self->add_child(my $ryu = "Ryu::Async"->new); $ryu }
l!l&&r!l&&!r
540
MC/DC0%0/2
$self->{"ryu"} //= do { $self->add_child(my $ryu = "Ryu::Async"->new); $ryu }
$self->{"ryu"} do { $self->add_child(my $ryu = "Ryu::Async"->new); $ryu }
Truth table66%2/3
A //= B
A= $self->{"ryu"}B= do { $self->add_child(my $ryu = "Ryu::Async"->new); $ryu }
ABresult
1X1
011
000
1353 4 $self->add_child(my $ryu = Ryu::Async->new);
1354 4 $ryu;
1355 }
1356 }
1357  
1358 =head2 is_rate_limited
1359  
1360 Returns true if we are currently rate limited, false otherwise.
1361  
1362 May eventually be updated to return number of seconds that you need to wait.
1363  
1364 =cut
1365  
1366 0 ▶sub is_rate_limited {
Subroutine is_rate_limited: not called CC=2 SCAR=17.9
Pod documented
1367 0 my ($self) = @_;
1368 return $self->{rate_limit}
1369 0 ▶ && $self->{request_count} >= $self->requests_per_minute;
Condition0%0/3
$self->{'rate_limit'} && $self->{'request_count'} >= $self->requests_per_minute
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{'rate_limit'} && $self->{'request_count'} >= $self->requests_per_minute
$self->{'rate_limit'} $self->{'request_count'} >= $self->requests_per_minute
Truth table0%0/3
A && B
A= $self->{'rate_limit'}B= $self->{'request_count'} >= $self->requests_per_minute
ABresult
0X0
100
111
1370 }
1371  
1372 =head2 rate_limiting
1373  
1374 Applies rate limiting check.
1375  
1376 Returns a L<Future> which will resolve once it's safe to send further requests.
1377  
1378 =cut
1379  
1380 26 ▶sub rate_limiting {
Subroutine rate_limiting: called CC=4 SCAR=13.9
Pod documented
1381 26 my ($self) = @_;
1382 26 ▶ $self->{rate_limit} //= do {
Condition66%2/3
$self->{"rate_limit"} //= do { $self->loop->delay_future("after", $self->{"rate_limit_delay"})->on_ready(sub { $self->{"request_count"} = 0; delete $self->{"rate_limit"}; } ) }
l!l&&r!l&&!r
2150
MC/DC0%0/2
$self->{"rate_limit"} //= do { $self->loop->delay_future("after", $self->{"rate_limit_delay"})->on_ready(sub { $self->{"request_count"} = 0; delete $self->{"rate_limit"}; } ) }
$self->{"rate_limit"} do { $self->loop->delay_future("after", $self->{"rate_limit_delay"})->on_ready(sub { $self->{"request_count"} = 0; delete $self->{"rate_limit"}; } ) }
Truth table66%2/3
A //= B
A= $self->{"rate_limit"}B= do { $self->loop->delay_future("after", $self->{"rate_limit_delay"})->on_ready(sub { $self->{"request_count"} = 0; delete $self->{"rate_limit"}; } ) }
ABresult
1X1
011
000
1383 $self->loop->delay_future(after => $self->{rate_limit_delay})->on_ready(
1384 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1385 2 $self->{request_count} = 0;
1386 2 delete $self->{rate_limit};
1387 5 });
1388 };
1389  
1390 return Future->done
1391 unless $self->requests_per_minute
1392 26 ▶ and ++$self->{request_count} >= $self->requests_per_minute;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->requests_per_minute and ++$self->{"request_count"} >= $self->requests_per_minute 24 2 26
Condition66%2/3
$self->requests_per_minute and ++$self->{"request_count"} >= $self->requests_per_minute
!ll&&!rl&&r
0242
MC/DC50%1/2
$self->requests_per_minute and ++$self->{"request_count"} >= $self->requests_per_minute
$self->requests_per_minute ++$self->{"request_count"} >= $self->requests_per_minute
Truth table66%2/3
A and B
A= $self->requests_per_minuteB= ++$self->{"request_count"} >= $self->requests_per_minute
ABresult
0X0
100
111
1393 $self->hook(
1394 'on_rate_limit',
1395 {
1396 requests_count => $self->{request_count},
1397 2 requests_per_minute => $self->requests_per_minute,
1398 });
1399 2 return $self->{rate_limit};
1400 }
1401  
1402 54 ▶sub requests_per_minute { return shift->{requests_per_minute} //= 300 }
Subroutine requests_per_minute: called CC=2 SCAR=6.9
Pod undocumented
Condition100%2/2
+(shift())->{"requests_per_minute"} //= 300
l!l
513
MC/DC100%1/1
+(shift())->{"requests_per_minute"} //= 300
+(shift())->{"requests_per_minute"}
Truth table100%2/2
A
A= +(shift())->{"requests_per_minute"}
Aresult
11
00
1403  
1404 9 ▶sub source {
Subroutine source: called CC=1 SCAR=0.0
Pod undocumented
1405 9 my ($self) = shift;
1406 9 return $self->ryu->source(@_);
1407 }
1408  
1409 2 ▶sub _get_mime_type {
Subroutine _get_mime_type: called CC=3 SCAR=11.7
1410 2 my $filename = shift;
1411  
1412 2 my $ext = (fileparse($filename, "[^.]+"))[2];
1413  
1414 2 ▶ return $FILE_MIME_TYPE_MAPPING{lc($ext // '')} // 'application/octet-stream';
Condition50%2/4
$FILE_MIME_TYPE_MAPPING{lc($ext // "")} // "application/octet-stream"
l!l
20
$ext // ""
l!l
20
MC/DC0%0/1
$FILE_MIME_TYPE_MAPPING{lc($ext // "")} // "application/octet-stream"
$FILE_MIME_TYPE_MAPPING{lc($ext // "")}
MC/DC0%0/1
$ext // ""
$ext
Truth table50%1/2
A
A= $FILE_MIME_TYPE_MAPPING{lc($ext // "")}
Aresult
11
00
Truth table50%1/2
A
A= $ext
Aresult
11
00
1415 }
1416  
1417 3 1;
1418  
1419 __END__
« lib/WebService/Async/Onfido/Base/Address.pm ↑ Summary lib/WebService/Async/Onfido/Check.pm »