MailboxOrg/LWPIO.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« MailboxOrg/API/Validate.pm ↑ Summary MailboxOrg/API/System.pm »
1 package WWW::MailboxOrg::LWPIO;
2  
3 # ABSTRACT: Synchronous JSON-RPC backend using Mojo::UserAgent
4  
5 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 0 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=2.6
7 use WWW::MailboxOrg::JSONRPCRequest;
8 use WWW::MailboxOrg::JSONRPCResponse;
9 use JSON::MaybeXS qw(decode_json encode_json);
10  
11 with 'WWW::MailboxOrg::Role::IO';
12  
13  
14 has timeout => (
15 is => 'ro',
16 default => 30,
17 );
18  
19  
20 has ua => (
21 is => 'lazy',
22 builder => sub {
23 my ($self) = @_;
24 Mojo::UserAgent->new(
25 timeout => $self->timeout,
26 );
27 },
28 );
29  
30  
31 sub call {
32 my ($self, $req) = @_;
33  
34 my $url = $req->url;
35  
36 my %headers = (
37 'Content-Type' => 'application/json',
38 );
39 $headers{'HPLS-AUTH'} = $req->headers->{'HPLS-AUTH'}
40 if $req->headers && $req->headers->{'HPLS-AUTH'};
41  
42 my $payload = encode_json($req->to_hash);
43  
44 my $tx = $self->ua->post($url, \%headers, json => $req->to_hash);
45  
46 if (my $err = $tx->error) {
47 return WWW::MailboxOrg::JSONRPCResponse->new(
48 error => {
49 code => -32300,
50 message => $err->{message},
51 },
52 id => $req->id,
53 );
54 }
55  
56 my $data = $tx->res->json;
57  
58 if (!$data) {
59 return WWW::MailboxOrg::JSONRPCResponse->new(
60 error => {
61 code => -32603,
62 message => 'Empty or invalid JSON response',
63 },
64 id => $req->id,
65 );
66 }
67  
68 return WWW::MailboxOrg::JSONRPCResponse->new(%$data);
69 }
70  
71  
72 1;
73  
74 __END__
« MailboxOrg/API/Validate.pm ↑ Summary MailboxOrg/API/System.pm »