| 1 | package Net::Azure::CognitiveServices::Face::Base; | ||||||||||||||||||||||||||||
| 2 | 29 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 29 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 29 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 29 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 29 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 29 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 28 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 10 | 28 | my ($class, %opts) = @_; | |||||||||||||||||||||||||||
| 11 | 28 | return bless {%opts}, $class; | |||||||||||||||||||||||||||
| 12 | } | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 28 | ▶ | sub access_key {shift->{access_key}} | ||||||||||||||||||||||||||
|
Subroutine
access_key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 28 | ▶ | sub endpoint {shift->{endpoint}} | ||||||||||||||||||||||||||
|
Subroutine
endpoint: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 0 | ▶ | sub path {''}; | ||||||||||||||||||||||||||
|
Subroutine
path: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 28 | ▶ | sub uri { | ||||||||||||||||||||||||||
|
Subroutine
uri: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 28 | my ($self, $path, %query) = @_; | |||||||||||||||||||||||||||
| 21 | 28 | my $uri = URI->new($self->endpoint); | |||||||||||||||||||||||||||
| 22 | 28 | ▶ | $uri->path($path ? join('/', $self->path, $path) : $self->path); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 28 | ▶ | if (keys %query) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 4 | $uri->query_form(%query); | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | 28 | $uri; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 15 | ▶ | sub json { | ||||||||||||||||||||||||||
|
Subroutine
json: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 15 | my $self = shift; | |||||||||||||||||||||||||||
| 31 | 15 | ▶ | $self->{json} ||= JSON->new->utf8(1); | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"json"} ||= "JSON"->new->utf8(1)
MC/DC0%0/2
$self->{"json"} ||= "JSON"->new->utf8(1)
$self->{"json"}
"JSON"->new->utf8(1)
Truth table33%1/3
A ||= B
A= $self->{"json"}B= "JSON"->new->utf8(1)
| |||||||||||||||||||||||||||||
| 32 | 15 | $self->{json}; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub agent { | ||||||||||||||||||||||||||
|
Subroutine
agent: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 37 | 0 | ▶ | $self->{agent} ||= HTTP::Tiny->new(agent => __PACKAGE__, timeout => 60); | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"agent"} ||= "HTTP::Tiny"->new("agent", __PACKAGE__, "timeout", 60)
MC/DC0%0/2
$self->{"agent"} ||= "HTTP::Tiny"->new("agent", __PACKAGE__, "timeout", 60)
$self->{"agent"}
"HTTP::Tiny"->new("agent", __PACKAGE__, "timeout", 60)
Truth table0%0/3
A ||= B
A= $self->{"agent"}B= "HTTP::Tiny"->new("agent", __PACKAGE__, "timeout", 60)
| |||||||||||||||||||||||||||||
| 38 | 0 | $self->{agent}; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | sub request { | ||||||||||||||||||||||||||
|
Subroutine
request: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 42 | 0 | my ($self, $req) = @_; | |||||||||||||||||||||||||||
| 43 | 0 | my $res; | |||||||||||||||||||||||||||
| 44 | 0 | my $try = 0; | |||||||||||||||||||||||||||
| 45 | 0 | while (1) { | |||||||||||||||||||||||||||
| 46 | 0 | $res = $self->agent->request(@$req); | |||||||||||||||||||||||||||
| 47 | 0 | $try++; | |||||||||||||||||||||||||||
| 48 | 0 | ▶ | if ($try > 10 || $res->{status} != 429) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$try > 10 or $res->{"status"} != 429
MC/DC0%0/2
$try > 10 or $res->{"status"} != 429
$try > 10
$res->{"status"} != 429
Truth table0%0/3
A or B
A= $try > 10B= $res->{"status"} != 429
| |||||||||||||||||||||||||||||
| 49 | 0 | last; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 0 | carp sprintf('Retry. Because API said %s', $res->{content}); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 0 | my $body; | |||||||||||||||||||||||||||
| 54 | 0 | ▶ | if ($res->{content}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | my $content_type = $res->{headers}{'Content-Type'} || $res->{headers}{'content-type'}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$res->{'headers'}{'Content-Type'} || $res->{'headers'}{'content-type'}
MC/DC0%0/2
$res->{'headers'}{'Content-Type'} || $res->{'headers'}{'content-type'}
$res->{'headers'}{'Content-Type'}
$res->{'headers'}{'content-type'}
Truth table0%0/3
A || B
A= $res->{'headers'}{'Content-Type'}B= $res->{'headers'}{'content-type'}
| |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if ($content_type !~ /application\/json/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | croak($res->{content}); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 0 | $body = $self->json->decode($res->{content}); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | if (!$res->{success}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | croak($body->{error}{message}); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 0 | $body; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 28 | ▶ | sub build_headers { | ||||||||||||||||||||||||||
|
Subroutine
build_headers: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 28 | my ($self, @headers) = @_; | |||||||||||||||||||||||||||
| 69 | { | ||||||||||||||||||||||||||||
| 70 | 28 | "Content-Type" => "application/json", | |||||||||||||||||||||||||||
| 71 | "Ocp-Apim-Subscription-Key" => $self->access_key, | ||||||||||||||||||||||||||||
| 72 | @headers, | ||||||||||||||||||||||||||||
| 73 | }; | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 28 | ▶ | sub build_request { | ||||||||||||||||||||||||||
|
Subroutine
build_request: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 28 | my ($self, $method, $uri_param, $header, $hash) = @_; | |||||||||||||||||||||||||||
| 78 | 28 | my $uri = $self->uri(@$uri_param); | |||||||||||||||||||||||||||
| 79 | 28 | ▶ | my $body = $hash ? $self->json->encode($hash) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 28 | ▶ | my $headers = $self->build_headers(defined $header ? @$header : ()); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 28 | return [$method, $uri, {headers => $headers, content => $body}]; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 29 | 1; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | __END__ | ||||||||||||||||||||||||||||