line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::Azure::CognitiveServices::Face::Base; |
2
|
29
|
|
|
29
|
|
266
|
use strict; |
|
29
|
|
|
|
|
62
|
|
|
29
|
|
|
|
|
818
|
|
3
|
29
|
|
|
29
|
|
142
|
use warnings; |
|
29
|
|
|
|
|
56
|
|
|
29
|
|
|
|
|
646
|
|
4
|
29
|
|
|
29
|
|
20451
|
use HTTP::Tiny; |
|
29
|
|
|
|
|
1424975
|
|
|
29
|
|
|
|
|
1134
|
|
5
|
29
|
|
|
29
|
|
18374
|
use JSON; |
|
29
|
|
|
|
|
340977
|
|
|
29
|
|
|
|
|
185
|
|
6
|
29
|
|
|
29
|
|
4466
|
use Carp; |
|
29
|
|
|
|
|
79
|
|
|
29
|
|
|
|
|
1683
|
|
7
|
29
|
|
|
29
|
|
14623
|
use URI; |
|
29
|
|
|
|
|
120991
|
|
|
29
|
|
|
|
|
16764
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new { |
10
|
28
|
|
|
28
|
0
|
170
|
my ($class, %opts) = @_; |
11
|
28
|
|
|
|
|
361
|
return bless {%opts}, $class; |
12
|
|
|
|
|
|
|
} |
13
|
|
|
|
|
|
|
|
14
|
28
|
|
|
28
|
1
|
240
|
sub access_key {shift->{access_key}} |
15
|
28
|
|
|
28
|
1
|
213
|
sub endpoint {shift->{endpoint}} |
16
|
|
|
|
|
|
|
|
17
|
0
|
|
|
0
|
1
|
0
|
sub path {''}; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub uri { |
20
|
28
|
|
|
28
|
1
|
104
|
my ($self, $path, %query) = @_; |
21
|
28
|
|
|
|
|
170
|
my $uri = URI->new($self->endpoint); |
22
|
28
|
100
|
|
|
|
237454
|
$uri->path($path ? join('/', $self->path, $path) : $self->path); |
23
|
28
|
100
|
|
|
|
3223
|
if (keys %query) { |
24
|
4
|
|
|
|
|
36
|
$uri->query_form(%query); |
25
|
|
|
|
|
|
|
} |
26
|
28
|
|
|
|
|
599
|
$uri; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub json { |
30
|
15
|
|
|
15
|
1
|
42
|
my $self = shift; |
31
|
15
|
|
33
|
|
|
498
|
$self->{json} ||= JSON->new->utf8(1); |
32
|
15
|
|
|
|
|
294
|
$self->{json}; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub agent { |
36
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
37
|
0
|
|
0
|
|
|
0
|
$self->{agent} ||= HTTP::Tiny->new(agent => __PACKAGE__, timeout => 60); |
38
|
0
|
|
|
|
|
0
|
$self->{agent}; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub request { |
42
|
0
|
|
|
0
|
1
|
0
|
my ($self, $req) = @_; |
43
|
0
|
|
|
|
|
0
|
my $res; |
44
|
0
|
|
|
|
|
0
|
my $try = 0; |
45
|
0
|
|
|
|
|
0
|
while (1) { |
46
|
0
|
|
|
|
|
0
|
$res = $self->agent->request(@$req); |
47
|
0
|
|
|
|
|
0
|
$try++; |
48
|
0
|
0
|
0
|
|
|
0
|
if ($try > 10 || $res->{status} != 429) { |
49
|
0
|
|
|
|
|
0
|
last; |
50
|
|
|
|
|
|
|
} |
51
|
0
|
|
|
|
|
0
|
carp sprintf('Retry. Because API said %s', $res->{content}); |
52
|
|
|
|
|
|
|
} |
53
|
0
|
|
|
|
|
0
|
my $body; |
54
|
0
|
0
|
|
|
|
0
|
if ($res->{content}) { |
55
|
0
|
|
0
|
|
|
0
|
my $content_type = $res->{headers}{'Content-Type'} || $res->{headers}{'content-type'}; |
56
|
0
|
0
|
|
|
|
0
|
if ($content_type !~ /application\/json/) { |
57
|
0
|
|
|
|
|
0
|
croak($res->{content}); |
58
|
|
|
|
|
|
|
} |
59
|
0
|
|
|
|
|
0
|
$body = $self->json->decode($res->{content}); |
60
|
|
|
|
|
|
|
} |
61
|
0
|
0
|
|
|
|
0
|
if (!$res->{success}) { |
62
|
0
|
|
|
|
|
0
|
croak($body->{error}{message}); |
63
|
|
|
|
|
|
|
} |
64
|
0
|
|
|
|
|
0
|
$body; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub build_headers { |
68
|
28
|
|
|
28
|
1
|
119
|
my ($self, @headers) = @_; |
69
|
|
|
|
|
|
|
{ |
70
|
28
|
|
|
|
|
180
|
"Content-Type" => "application/json", |
71
|
|
|
|
|
|
|
"Ocp-Apim-Subscription-Key" => $self->access_key, |
72
|
|
|
|
|
|
|
@headers, |
73
|
|
|
|
|
|
|
}; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub build_request { |
77
|
28
|
|
|
28
|
1
|
131
|
my ($self, $method, $uri_param, $header, $hash) = @_; |
78
|
28
|
|
|
|
|
208
|
my $uri = $self->uri(@$uri_param); |
79
|
28
|
100
|
|
|
|
223
|
my $body = $hash ? $self->json->encode($hash) : undef; |
80
|
28
|
50
|
|
|
|
311
|
my $headers = $self->build_headers(defined $header ? @$header : ()); |
81
|
28
|
|
|
|
|
250
|
return [$method, $uri, {headers => $headers, content => $body}]; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
1; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
__END__ |