APIClient/Transport/MojoUserAgent.pm untested

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 104 CC 4 SCAR 12.8
CC
4
Cov
0%
new
6.0
_get_session_promise
6.0
request
6.0
CRAP 3.6
1 package cPanel::APIClient::Transport::MojoUserAgent;
2  
3 use strict;
4 use warnings;
5  
6 =encoding utf-8
7  
8 =head1 NAME
9  
10 =head1 SYNOPSIS
11  
12 my $cp = cPanel::APIClient->create(
13 service => 'cpanel',
14 transport => [
15 'MojoUserAgent',
16 hostname => 'greathosting.net',
17  
18 # For testing only:
19 # tls_verification => 'off',
20 ],
21  
22 credentials => {
23 username => 'hugh',
24 api_token -> 'MYTOKEN',
25 },
26 );
27  
28 =head1 DESCRIPTION
29  
30 This module allows L<Mojo::UserAgent> to serve as transport
31 for asynchronous cPanel API calls.
32  
33 It supports C<username>/C<password> or C<username>/C<api_token>
34 C<credentials> schemes. See L<cPanel::APIClient> for more details.
35  
36 It expects these parameters:
37  
38 =over
39  
40 =item * C<hostname> - Required. The remote hostname that will serve
41 the API calls.
42  
43 =item * C<tls_verification> - Optional. Either C<on> (default) or C<off>.
44  
45 =back
46  
47 =head1 SEE ALSO
48  
49 L<cPanel::APIClient::Transport::NetCurlPromiser> can also integrate
50 with L<Mojolicious> but requires L<Net::Curl>, which you’ll need C compiler
51 access to install.
52  
53 =cut
54  
55 #----------------------------------------------------------------------
56  
57 use parent qw(
58 cPanel::APIClient::TransportBase::HTTPBase
59 cPanel::APIClient::TransportBase::TLSVerificationBase
60 );
61  
62 use Mojo::UserAgent ();
63  
64 use cPanel::APIClient::Pending ();
65 use cPanel::APIClient::Utils::HTTPResponse ();
66 use cPanel::APIClient::X ();
67  
68 sub new {
69 my ( $class, $authn, %opts ) = @_;
70  
71 my $self = $class->SUPER::new( $authn, %opts );
72  
73 $self->{'ua'} = Mojo::UserAgent->new();
74  
75 if ( 'off' eq $self->_parse_tls_verification( \%opts ) ) {
76 $self->{'ua'}->insecure(1);
77 }
78  
79 return $self;
80 }
81  
82 sub _get_session_promise {
83 my ( $self, $service_obj ) = @_;
84  
85 return $self->{'_session_promise'} ||= $self->_needs_session() && do {
86 my ( $method, $url, $payload ) = $self->{'authn'}->get_login_request_pieces();
87 substr( $url, 0, 0, $self->_get_url_base($service_obj) );
88  
89 die "Bad method: $method" if 'POST' ne $method;
90  
91 $self->{'ua'}->post_p(
92 $url,
93 {},
94 $payload,
95 )->then(
96 sub {
97 my ($tx) = @_;
98  
99 my $resp_obj = _tx2response($tx);
100  
101 $self->{'authn'}->consume_session_response($resp_obj);
102 }
103 );
104 };
105 }
106  
107 sub request {
108 my ( $self, $service_obj, $request_obj ) = @_;
109  
110 my $make_promise_cr = sub {
111 my ( $method, $url, $headers_ar, $payload ) = $self->_assemble_request_pieces( $service_obj, $request_obj );
112  
113 die "Bad method: $method" if 'POST' ne $method;
114  
115 my $promise = $self->{'ua'}->post_p(
116 $url,
117 { map { @$_ } @$headers_ar },
118 $payload,
119 )->then(
120 sub {
121 my ($tx) = @_;
122  
123 return _xform_tx( $request_obj, $tx );
124 },
125 sub {
126 my ($why) = @_;
127  
128 die cPanel::APIClient::X->create( 'SubTransport', $why );
129 },
130 );
131 };
132  
133 # XXX dedupe
134 my $promise = $self->_get_session_promise($service_obj);
135 $promise &&= $promise->then($make_promise_cr);
136 $promise ||= $make_promise_cr->();
137  
138 # XXX How to cancel this?
139 return cPanel::APIClient::Pending->new($promise);
140 }
141  
142 sub _tx2response {
143 my ($tx) = @_;
144  
145 my $code = $tx->res()->code();
146  
147 # This appears to be the easiest way to get this from Mojo …
148 my $headers_str = $tx->res()->get_start_line_chunk(0) . $tx->res()->get_header_chunk(0);
149  
150 my $resp_obj = cPanel::APIClient::Utils::HTTPResponse->new( $code, $headers_str, $tx->res()->body() );
151  
152 return $resp_obj;
153 }
154  
155 sub _xform_tx {
156 my ( $request_obj, $tx ) = @_;
157  
158 my $resp_obj = _tx2response($tx);
159  
160 my $body = $tx->res()->body();
161  
162 return $request_obj->parse_http_response( $resp_obj, $body );
163 }
164  
165 =head1 LICENSE
166  
167 Copyright 2020 cPanel, L. L. C. All rights reserved. L<http://cpanel.net>
168  
169 This is free software; you can redistribute it and/or modify it under the
170 same terms as Perl itself. See L<perlartistic>.
171  
172 =cut
173  
174 1;