MailboxOrg/Role/HTTP.pm

filter: Statementstmt 48.9% 24 / 49 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 46.1% 6 / 13 Podpod 100% 2 / 2 totaltotal 32.6% 32 / 98 CC 13 SCAR 34.0
CC
13
Cov
32%
_build_jsonrpc_request
72.0
_parse_response
30.0
_set_auth_header
6.0
CRAP 30.1
↑ Summary MailboxOrg/API/Mailinglist.pm »
1 package WWW::MailboxOrg::Role::HTTP;
2  
3 # ABSTRACT: JSON-RPC HTTP client role
4  
5 1 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use WWW::MailboxOrg::JSONRPCRequest;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use WWW::MailboxOrg::JSONRPCResponse;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use JSON::MaybeXS qw(decode_json encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = '0.001';
13  
14  
15 1 requires 'token';
16 1 requires 'base_url';
17  
18 has io => (
19 is => 'lazy',
20 0 ▶ builder => sub { require WWW::MailboxOrg::LWPIO; WWW::MailboxOrg::LWPIO->new },
Subroutine _build_io: not called CC=1 SCAR=6.9
21 1 );
22  
23  
24 sub call {
25 0 ▶ my ($self, $method, @params) = @_;
Subroutine call: not called CC=1 SCAR=6.9
Pod documented
26  
27 0 my $req = $self->_build_jsonrpc_request($method, @params);
28 0 my $res = $self->io->call($req);
29 0 return $self->_parse_response($res);
30 }
31  
32  
33 sub notification {
34 0 ▶ my ($self, $method, @params) = @_;
Subroutine notification: not called CC=1 SCAR=6.9
Pod documented
35 0 my $req = $self->_build_jsonrpc_request($method, @params, 1);
36 0 return $self->io->call($req);
37 }
38  
39  
40 sub _build_jsonrpc_request {
41 0 ▶ my ($self, $method, @params) = @_;
Subroutine _build_jsonrpc_request: not called CC=8 SCAR=42.8
42 0 ▶ my $is_notification = pop @params if @params && $_[-1] == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @params and $_[-1] == 1 0 0 0
Condition0%0/3
@params and $_[-1] == 1
!ll&&!rl&&r
000
MC/DC0%0/2
@params and $_[-1] == 1
@params $_[-1] == 1
Truth table0%0/3
A and B
A= @paramsB= $_[-1] == 1
ABresult
0X0
100
111
43 0 ▶ $is_notification //= 0;
Condition0%0/2
$is_notification //= 0
l!l
00
MC/DC0%0/1
$is_notification //= 0
$is_notification
Truth table0%0/2
A
A= $is_notification
Aresult
11
00
44  
45 0 ▶ my $params_ref = @params == 1 && ref($params[0]) eq 'HASH' ? $params[0] : \@params;
Branch: 1 branch
BranchTrueFalseTotal
@params == 1 && ref $params[0] eq 'HASH' ? : 0 0 0
Condition0%0/3
@params == 1 and ref $params[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@params == 1 and ref $params[0] eq "HASH"
@params == 1 ref $params[0] eq "HASH"
Truth table0%0/3
A and B
A= @params == 1B= ref $params[0] eq "HASH"
ABresult
0X0
100
111
46  
47 0 my %headers;
48 0 ▶ $self->_set_auth_header(\%headers) if $self->can('_set_auth_header');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("_set_auth_header") 0 0 0
49  
50 0 ▶ return WWW::MailboxOrg::JSONRPCRequest->new(
Branch: 1 branch
BranchTrueFalseTotal
$is_notification ? : 0 0 0
51 method => $method,
52 params => $params_ref,
53 id => $is_notification ? undef : $self->_next_id,
54 url => $self->base_url,
55 headers => \%headers,
56 );
57 }
58  
59 sub _set_auth_header {
60 0 ▶ my ($self, $headers) = @_;
Subroutine _set_auth_header: not called CC=2 SCAR=17.9
61 0 ▶ $headers->{'HPLS-AUTH'} = $self->token if $self->token;
Branch: 1 branch
BranchTrueFalseTotal
if $self->token 0 0 0
62 }
63  
64 sub _parse_response {
65 0 ▶ my ($self, $res) = @_;
Subroutine _parse_response: not called CC=5 SCAR=34.0
66  
67 0 ▶ if ($res->has_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->has_error) 0 0 0
68 0 my $err = $res->error;
69 0 ▶ $log->errorf("RPC error: %s (code=%s)", $err->{message} // 'unknown', $err->{code} // -1);
Condition0%0/4
$err->{"message"} // "unknown"
l!l
00
$err->{"code"} // -1
l!l
00
MC/DC0%0/1
$err->{"message"} // "unknown"
$err->{"message"}
MC/DC0%0/1
$err->{"code"} // -1
$err->{"code"}
Truth table0%0/2
A
A= $err->{"message"}
Aresult
11
00
Truth table0%0/2
A
A= $err->{"code"}
Aresult
11
00
70 0 ▶ croak "Mailbox.org API error: " . ($err->{message} // 'unknown');
Condition0%0/2
$err->{"message"} // "unknown"
l!l
00
MC/DC0%0/1
$err->{"message"} // "unknown"
$err->{"message"}
Truth table0%0/2
A
A= $err->{"message"}
Aresult
11
00
71 }
72  
73 0 return $res->result;
74 }
75  
76 1 my $_id = 0;
77 0 ▶sub _next_id { ++$_id }
Subroutine _next_id: not called CC=1 SCAR=6.9
78  
79 1 1;
80  
81 __END__
82  
83 =pod
84  
85 =encoding UTF-8
86  
87 =head1 NAME
88  
89 WWW::MailboxOrg::Role::HTTP - JSON-RPC HTTP client role
90  
91 =head1 VERSION
92  
93 version 0.001
94  
95 =head1 SYNOPSIS
96  
97 package WWW::MailboxOrg;
98 use Moo;
99  
100 has token => ( is => 'ro' );
101 has base_url => ( is => 'ro', default => 'https://api.mailbox.org/v1' );
102  
103 with 'WWW::MailboxOrg::Role::HTTP';
104  
105 =head1 DESCRIPTION
106  
107 This role provides JSON-RPC 2.0 call and notification methods. It handles
108 JSON encoding/decoding, authentication, and error handling.
109  
110 HTTP transport is delegated to a pluggable L<WWW::MailboxOrg::Role::IO> backend
111 (default: L<WWW::MailboxOrg::LWPIO>), making it possible to use async HTTP
112 clients.
113  
114 Uses L<Log::Any> for logging.
115  
116 =head1 REQUIRED ATTRIBUTES
117  
118 Classes consuming this role must provide:
119  
120 =over 4
121  
122 =item * C<token> - Session ID for HPLS-AUTH header
123  
124 =item * C<base_url> - Base URL for the API
125  
126 =back
127  
128 =head2 io
129  
130 Pluggable JSON-RPC backend implementing L<WWW::MailboxOrg::Role::IO>.
131 Defaults to L<WWW::MailboxOrg::LWPIO>.
132  
133 =head2 call
134  
135 my $result = $self->call('account.get', account => 'test@example.tld');
136  
137 Execute a JSON-RPC call and return the result. Croaks on error.
138  
139 =head2 notification
140  
141 $self->notification('mail.send', %params);
142  
143 Send a fire-and-forget notification (no response ID expected).
144  
145 =head1 SEE ALSO
146  
147 L<WWW::MailboxOrg>, L<WWW::MailboxOrg::Role::IO>, L<WWW::MailboxOrg::LWPIO>,
148 L<Log::Any>
149  
150 =cut
151  
152 =head1 SUPPORT
153  
154 =head2 Issues
155  
156 Please report bugs and feature requests on GitHub at
157 L<https://github.com/getty/p5-www-mailboxorg/issues>.
158  
159 =head2 IRC
160  
161 Join C<#perl-help> on C<irc.perl.org> or message Getty directly.
162  
163 =head1 CONTRIBUTING
164  
165 Contributions are welcome! Please fork the repository and submit a pull request.
166  
167 =head1 AUTHOR
168  
169 Torsten Raudssus <getty@cpan.org>
170  
171 =head1 COPYRIGHT AND LICENSE
172  
173 This software is copyright (c) 2026 by Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>.
174  
175 This is free software; you can redistribute it and/or modify it under
176 the same terms as the Perl 5 programming language system itself.
177  
178 =cut
↑ Summary MailboxOrg/API/Mailinglist.pm »