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/Entity/Account.pm ↑ Summary MailboxOrg/Types.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 1 ▶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 our $VERSION = '0.001';
14  
15  
16 has timeout => (
17 is => 'ro',
18 default => 30,
19 );
20  
21  
22 has ua => (
23 is => 'lazy',
24 builder => sub {
25 my ($self) = @_;
26 Mojo::UserAgent->new(
27 timeout => $self->timeout,
28 );
29 },
30 );
31  
32  
33 sub call {
34 my ($self, $req) = @_;
35  
36 my $url = $req->url;
37  
38 my %headers = (
39 'Content-Type' => 'application/json',
40 );
41 $headers{'HPLS-AUTH'} = $req->headers->{'HPLS-AUTH'}
42 if $req->headers && $req->headers->{'HPLS-AUTH'};
43  
44 my $payload = encode_json($req->to_hash);
45  
46 my $tx = $self->ua->post($url, \%headers, json => $req->to_hash);
47  
48 if (my $err = $tx->error) {
49 return WWW::MailboxOrg::JSONRPCResponse->new(
50 error => {
51 code => -32300,
52 message => $err->{message},
53 },
54 id => $req->id,
55 );
56 }
57  
58 my $data = $tx->res->json;
59  
60 if (!$data) {
61 return WWW::MailboxOrg::JSONRPCResponse->new(
62 error => {
63 code => -32603,
64 message => 'Empty or invalid JSON response',
65 },
66 id => $req->id,
67 );
68 }
69  
70 return WWW::MailboxOrg::JSONRPCResponse->new(%$data);
71 }
72  
73  
74 1;
75  
76 __END__
77  
78 =pod
79  
80 =encoding UTF-8
81  
82 =head1 NAME
83  
84 WWW::MailboxOrg::LWPIO - Synchronous JSON-RPC backend using Mojo::UserAgent
85  
86 =head1 VERSION
87  
88 version 0.001
89  
90 =head1 SYNOPSIS
91  
92 use WWW::MailboxOrg::LWPIO;
93  
94 my $io = WWW::MailboxOrg::LWPIO->new(timeout => 60);
95  
96 =head1 DESCRIPTION
97  
98 Default synchronous JSON-RPC backend using L<Mojo::UserAgent>. Implements
99 L<WWW::MailboxOrg::Role::IO>.
100  
101 =head2 timeout
102  
103 Timeout in seconds for HTTP requests. Defaults to 30.
104  
105 =head2 ua
106  
107 L<Mojo::UserAgent> instance. Built lazily.
108  
109 =head2 call($req)
110  
111 Execute a L<WWW::MailboxOrg::JSONRPCRequest> via Mojo::UserAgent and return a
112 L<WWW::MailboxOrg::JSONRPCResponse>.
113  
114 =head1 SEE ALSO
115  
116 L<WWW::MailboxOrg::Role::IO>, L<WWW::MailboxOrg::Role::HTTP>,
117 L<Mojo::UserAgent>
118  
119 =cut
120  
121 =head1 SUPPORT
122  
123 =head2 Issues
124  
125 Please report bugs and feature requests on GitHub at
126 L<https://github.com/getty/p5-www-mailboxorg/issues>.
127  
128 =head2 IRC
129  
130 Join C<#perl-help> on C<irc.perl.org> or message Getty directly.
131  
132 =head1 CONTRIBUTING
133  
134 Contributions are welcome! Please fork the repository and submit a pull request.
135  
136 =head1 AUTHOR
137  
138 Torsten Raudssus <getty@cpan.org>
139  
140 =head1 COPYRIGHT AND LICENSE
141  
142 This software is copyright (c) 2026 by Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>.
143  
144 This is free software; you can redistribute it and/or modify it under
145 the same terms as the Perl 5 programming language system itself.
146  
147 =cut
« MailboxOrg/Entity/Account.pm ↑ Summary MailboxOrg/Types.pm »