blib/lib/Net/Azure/CognitiveServices/Face/Base.pm

filter: Statementstmt 66.1% 41 / 62 Branchbran 43.7% 7 / 16 Conditioncond 11.1% 1 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 80.0% 12 / 15 Podpod 100% 7 / 7 totaltotal 59.1% 68 / 115 SCAR 31.8
CC
12
Cov
56%
request
42.0
agent
6.0
build_request
3.0
CRAP 24.0
↑ Summary blib/lib/Net/Azure/CognitiveServices/Face/Face.pm »
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 use Class::Accessor::Lite (
5 29 new => 1,
6 ro => [qw[access_key endpoint]],
7 29 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 29 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 29 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 29 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 29 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 29 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 0 ▶sub path {''};
Subroutine path: not called CC=1 SCAR=6.9
Pod documented
15  
16 sub uri {
17 28 ▶ my ($self, $path, %query) = @_;
Subroutine uri: called CC=3 SCAR=11.0
Pod documented
18 28 my $uri = URI->new($self->endpoint);
19 28 ▶ $uri->path($path ? join('/', $self->path, $path) : $self->path);
Branch: 1 branch
BranchTrueFalseTotal
$path ? : 26 2 28
20 28 ▶ if (keys %query) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %query) 4 24 28
21 4 $uri->query_form(%query);
22 }
23 28 $uri;
24 }
25  
26 sub json {
27 15 ▶ my $self = shift;
Subroutine json: called CC=2 SCAR=7.6
Pod documented
28 15 ▶ $self->{json} ||= JSON->new->utf8(1);
Condition33%1/3
$self->{"json"} ||= "JSON"->new->utf8(1)
l!l&&r!l&&!r
0150
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)
ABresult
1X1
011
000
29 15 $self->{json};
30 }
31  
32 sub agent {
33 0 ▶ my $self = shift;
Subroutine agent: not called CC=2 SCAR=17.9
Pod documented
34 0 ▶ $self->{agent} ||= LWP::UserAgent->new(agent => __PACKAGE__, timeout => 60);
Condition0%0/3
$self->{"agent"} ||= "LWP::UserAgent"->new("agent", __PACKAGE__, "timeout", 60)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"agent"} ||= "LWP::UserAgent"->new("agent", __PACKAGE__, "timeout", 60)
$self->{"agent"} "LWP::UserAgent"->new("agent", __PACKAGE__, "timeout", 60)
Truth table0%0/3
A ||= B
A= $self->{"agent"}B= "LWP::UserAgent"->new("agent", __PACKAGE__, "timeout", 60)
ABresult
1X1
011
000
35 0 $self->{agent};
36 }
37  
38 sub request {
39 0 ▶ my ($self, $req) = @_;
Subroutine request: not called CC=6 SCAR=37.4
Pod documented
40 0 my $res;
41 0 my $try = 0;
42 0 while (1) {
43 0 $res = $self->agent->request($req);
44 0 $try++;
45 0 ▶ if ($try > 10 || $res->code != 429) {
Branch: 1 branch
BranchTrueFalseTotal
if ($try > 10 or $res->code != 429) 0 0 0
Condition0%0/3
$try > 10 or $res->code != 429
l!l&&r!l&&!r
000
MC/DC0%0/2
$try > 10 or $res->code != 429
$try > 10 $res->code != 429
Truth table0%0/3
A or B
A= $try > 10B= $res->code != 429
ABresult
1X1
011
000
46 0 last;
47 }
48 0 carp sprintf('Retry. Because API said %s', $res->content);
49 }
50 0 my $body;
51 0 ▶ if ($res->content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->content) 0 0 0
52 0 ▶ if ($res->content_type !~ /application\/json/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->content_type =~ m[application/json]) 0 0 0
53 0 croak($res->content);
54 }
55 0 $body = $self->json->decode($res->content);
56 }
57 0 ▶ if (!$res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
58 0 croak($body->{error}{message});
59 }
60 0 $body;
61 }
62  
63 sub build_headers {
64 28 ▶ my ($self, @headers) = @_;
Subroutine build_headers: called CC=1 SCAR=0.0
Pod documented
65 (
66 28 "Content-Type" => "application/json",
67 "Ocp-Apim-Subscription-Key" => $self->access_key,
68 @headers,
69 );
70 }
71  
72 sub build_request {
73 28 ▶ my ($self, $method, $uri_param, $header, $hash) = @_;
Subroutine build_request: called CC=3 SCAR=11.0
Pod documented
74 28 my $uri = $self->uri(@$uri_param);
75 28 ▶ my $body = $hash ? $self->json->encode($hash) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$hash ? : 15 13 28
76 28 ▶ my @headers = $self->build_headers(defined $header ? @$header : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $header ? : 0 28 28
77 28 HTTP::Request->new($method => $uri, [@headers], $body);
78 }
79  
80 29 1;
81  
82 __END__
↑ Summary blib/lib/Net/Azure/CognitiveServices/Face/Face.pm »