MailboxOrg/Role/HTTP.pm

filter: Statementstmt 82.0% 41 / 50 Branchbran 58.3% 7 / 12 Conditioncond 64.2% 9 / 14 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 71.4% 10 / 14 Podpod 100% 2 / 2 totaltotal 71.0% 71 / 100 CC 13 SCAR 15.4
CC
13
Cov
75%
_build_jsonrpc_request
8.7
_set_auth_header
6.0
_parse_response
5.2
CRAP 4.7
↑ Summary MailboxOrg/JSONRPCRequest.pm »
1 package WWW::MailboxOrg::Role::HTTP;
2  
3 # ABSTRACT: JSON-RPC HTTP client role
4  
5 5 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use WWW::MailboxOrg::JSONRPCRequest;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use WWW::MailboxOrg::JSONRPCResponse;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use JSON::MaybeXS qw(decode_json encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 5 requires 'token';
14 5 requires 'base_url';
15  
16 has io => (
17 is => 'lazy',
18 0 ▶ builder => sub { require WWW::MailboxOrg::LWPIO; WWW::MailboxOrg::LWPIO->new },
Subroutine _build_io: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
19 5 );
20  
21  
22 59 ▶sub call {
Subroutine call: called CC=1 SCAR=0.0
Pod documented
23 59 my ($self, $method, @params) = @_;
24  
25 59 my $req = $self->_build_jsonrpc_request($method, @params);
26 59 my $res = $self->io->call($req);
27 59 return $self->_parse_response($res);
28 }
29  
30  
31 0 ▶sub notification {
Subroutine notification: not called CC=1 SCAR=6.9
Pod documented
32 0 my ($self, $method, @params) = @_;
33 0 my $req = $self->_build_jsonrpc_request($method, @params, 1);
34 0 return $self->io->call($req);
35 }
36  
37  
38 59 ▶sub _build_jsonrpc_request {
Subroutine _build_jsonrpc_request: called CC=8 SCAR=21.6
39 59 my ($self, $method, @params) = @_;
40 59 ▶ my $is_notification = pop @params if @params && $_[-1] == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @params and $_[-1] == 1 0 59 59
Condition66%2/3
@params and $_[-1] == 1
!ll&&!rl&&r
12470
MC/DC0%0/2
@params and $_[-1] == 1
@params $_[-1] == 1
Truth table66%2/3
A and B
A= @paramsB= $_[-1] == 1
ABresult
0X0
100
111
41 59 ▶ $is_notification //= 0;
Condition100%2/2
$is_notification //= 0
l!l
563
MC/DC100%1/1
$is_notification //= 0
$is_notification
Truth table100%2/2
A
A= $is_notification
Aresult
11
00
42  
43 59 ▶ my $params_ref = @params == 1 && ref($params[0]) eq 'HASH' ? $params[0] : \@params;
Branch: 1 branch
BranchTrueFalseTotal
@params == 1 && ref $params[0] eq 'HASH' ? : 47 12 59
Condition66%2/3
@params == 1 and ref $params[0] eq "HASH"
!ll&&!rl&&r
12047
MC/DC50%1/2
@params == 1 and ref $params[0] eq "HASH"
@params == 1 ref $params[0] eq "HASH"
Truth table66%2/3
A and B
A= @params == 1B= ref $params[0] eq "HASH"
ABresult
0X0
100
111
44  
45 59 my %headers;
46 59 ▶ $self->_set_auth_header(\%headers) if $self->can('_set_auth_header');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("_set_auth_header") 59 0 59
47  
48 59 ▶ return WWW::MailboxOrg::JSONRPCRequest->new(
Branch: 1 branch
BranchTrueFalseTotal
$is_notification ? : 0 59 59
49 method => $method,
50 params => $params_ref,
51 id => $is_notification ? undef : $self->_next_id,
52 url => $self->base_url,
53 headers => \%headers,
54 );
55 }
56  
57 0 ▶sub _set_auth_header {
Subroutine _set_auth_header: not called CC=2 SCAR=17.9
58 0 my ($self, $headers) = @_;
59 0 ▶ $headers->{'HPLS-AUTH'} = $self->token if $self->token;
Branch: 1 branch
BranchTrueFalseTotal
if $self->token 0 0 0
60 }
61  
62 59 ▶sub _parse_response {
Subroutine _parse_response: called CC=5 SCAR=16.6
63 59 my ($self, $res) = @_;
64  
65 59 ▶ if ($res->has_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->has_error) 1 58 59
66 1 my $err = $res->error;
67 1 ▶ $log->errorf("RPC error: %s (code=%s)", $err->{message} // 'unknown', $err->{code} // -1);
Condition50%2/4
$err->{"message"} // "unknown"
l!l
10
$err->{"code"} // -1
l!l
10
MC/DC0%0/1
$err->{"message"} // "unknown"
$err->{"message"}
MC/DC0%0/1
$err->{"code"} // -1
$err->{"code"}
Truth table50%1/2
A
A= $err->{"message"}
Aresult
11
00
Truth table50%1/2
A
A= $err->{"code"}
Aresult
11
00
68 1 ▶ croak "Mailbox.org API error: " . ($err->{message} // 'unknown');
Condition50%1/2
$err->{"message"} // "unknown"
l!l
10
MC/DC0%0/1
$err->{"message"} // "unknown"
$err->{"message"}
Truth table50%1/2
A
A= $err->{"message"}
Aresult
11
00
69 }
70  
71 58 return $res->result;
72 }
73  
74 5 my $_id = 0;
75 59 ▶sub _next_id { ++$_id }
Subroutine _next_id: called CC=1 SCAR=0.0
76  
77 5 1;
78  
79 __END__
↑ Summary MailboxOrg/JSONRPCRequest.pm »