| 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
Condition66%2/3
@params and $_[-1] == 1
MC/DC0%0/2
@params and $_[-1] == 1
@params
$_[-1] == 1
Truth table66%2/3
A and B
A= @paramsB= $_[-1] == 1
| |||||||||||||||||||||||||||||
| 41 | 59 | ▶ | $is_notification //= 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$is_notification //= 0
MC/DC100%1/1
$is_notification //= 0
$is_notification
Truth table100%2/2
A
A= $is_notification
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 59 | ▶ | my $params_ref = @params == 1 && ref($params[0]) eq 'HASH' ? $params[0] : \@params; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@params == 1 and ref $params[0] eq "HASH"
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"
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 59 | my %headers; | |||||||||||||||||||||||||||
| 46 | 59 | ▶ | $self->_set_auth_header(\%headers) if $self->can('_set_auth_header'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 59 | ▶ | return WWW::MailboxOrg::JSONRPCRequest->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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"
$err->{"code"} // -1
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"}
Truth table50%1/2
A
A= $err->{"code"}
| |||||||||||||||||||||||||||||
| 68 | 1 | ▶ | croak "Mailbox.org API error: " . ($err->{message} // 'unknown'); | ||||||||||||||||||||||||||
|
Condition50%1/2
$err->{"message"} // "unknown"
MC/DC0%0/1
$err->{"message"} // "unknown"
$err->{"message"}
Truth table50%1/2
A
A= $err->{"message"}
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||