lib/WebService/Async/Onfido.pm

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