APIClient.pm

filter: Statementstmt 89.4% 51 / 57 Branchbran 59.0% 13 / 22 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 76.0% 73 / 96 CC 15 SCAR 17.1
CC
15
Cov
78%
_parse_creds
8.5
_parse_transport
4.8
create
4.2
CRAP 5.5
« APIClient/X/CommandFailed.pm ↑ Summary APIClient/Utils/HTTPResponse.pm »
1 package cPanel::APIClient;
2  
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 14 our $VERSION = '0.10';
7  
8 =encoding utf-8
9  
10 =head1 NAME
11  
12 cPanel::APIClient - L<cPanel|http://cpanel.com> APIs, à la TIMTOWTDI!
13  
14 =head1 SYNOPSIS
15  
16 Create a L<cPanel::APIClient::Service::cpanel> object
17 to call cPanel APIs:
18  
19 my $cpanel = cPanel::APIClient->create(
20 service => 'cpanel',
21 transport => [ 'CLISync' ],
22 );
23  
24 my $resp = $cpanel->call_uapi( 'Email', 'list_pops' );
25  
26 my $pops_ar = $resp->get_data();
27  
28 Create a L<cPanel::APIClient::Service::whm> object
29 to call WHM APIs:
30  
31 my $whm = cPanel::APIClient->create(
32 service => 'whm',
33 transport => [ 'CLISync' ],
34 );
35  
36 my $resp = $whm->call_api1( 'listaccts' );
37  
38 my $accts_ar = $resp->get_data();
39  
40 =head1 DESCRIPTION
41  
42 cPanel & WHM exposes a number of ways to access its APIs: different transport
43 mechanisms, different authentication schemes, etc. This library provides
44 client logic with sufficient abstractions to accommodate most supported
45 access mechanisms via a unified interface.
46  
47 This library intends to supersede L<cPanel::PublicAPI> as the preferred way
48 to access cPanel & WHM’s APIs from Perl. It can also serve as a model for
49 similar client libraries in other languages.
50  
51 =head1 FEATURES
52  
53 =over
54  
55 =item * Fully object-oriented.
56  
57 =item * Can use blocking or non-blocking I/O. Non-blocking I/O implementation
58 works with almost any modern Perl event loop interface.
59  
60 =item * Uses minimal dependencies: no L<Moose> &c.
61  
62 =item * Extensively tested.
63  
64 =item * Can run in pure Perl.
65  
66 =back
67  
68 =head1 CHARACTER ENCODING
69  
70 cPanel & WHM’s API is character-set-agnostic. All text that you give to this
71 library should thus be encoded to binary, and all strings that you’ll receive
72 back will be binary.
73  
74 This means that if you character-decode your inputs—as L<perlunitut>
75 recommends—then you’ll need to encode your strings back to bytes before
76 giving them to this module.
77  
78 Use of UTF-8 encoding is B<strongly> recommended!
79  
80 =head1 FUNCTIONS
81  
82 =head2 $client = cPanel::APIClient->create( %OPTS )
83  
84 A factory function that creates a “client” object that your code can
85 use to call the APIs.
86  
87 %OPTS are:
88  
89 =over
90  
91 =item * C<service> - Required. The service that exposes the API(s) to call.
92 This controls the class of the returned object. Recognized values are:
93  
94 =over
95  
96 =item * C<cpanel> - Function will return a L<cPanel::APIClient::Service::cpanel>
97 instance.
98  
99 =item * C<whm> - Function will return a L<cPanel::APIClient::Service::whm>
100 instance.
101  
102 =back
103  
104 =item * C<transport> - Required. An array reference that describes the
105 transport mechanism to use. The first member of this array names the mechanism;
106 remaining arguments are key-value pairs of attributes to give to the
107 mechanism class’s constructor.
108  
109 Currently supported mechanisms are:
110  
111 =over
112  
113 =item * L<cPanel::APIClient::Transport::HTTPSync> (C<HTTPSync>) -
114 Synchronous HTTP requests.
115  
116 =item * L<cPanel::APIClient::Transport::CLISync> (C<CLISync>) -
117 Synchronous local requests via cPanel & WHM’s command-line API tools.
118  
119 =item * L<cPanel::APIClient::Transport::NetCurlPromiser> (C<NetCurlPromiser>) -
120 Asynchronous HTTP requests via
121 L<Net::Curl::Promiser>, which can use any event loop interface.
122 As of this writing it supports L<IO::Async>, L<AnyEvent>, and L<Mojolicious>
123 out-of-the-box.
124  
125 =item * L<cPanel::APIClient::Transport::MojoUserAgent> (C<MojoUserAgent>) -
126 Asynchronous HTTP requests via L<Mojo::UserAgent> (pure Perl).
127  
128 =back
129  
130 Which of the above to use will depend on your needs. If your application
131 is local to the cPanel & WHM server you might find it easiest to use
132 C<CLISync>. For HTTP C<NetCurlPromiser> offers the best flexibility
133 and (probably) speed, whereas C<MojoUserAgent> and C<HTTPSync> can run in
134 pure Perl (assuming you have L<Net::SSLeay>).
135  
136 There currently is no documentation for how to create a 3rd-party transport
137 mechanism (e.g., if you want to use a different HTTP library). Submissions
138 via pull request will be evaluated on a case-by-case basis.
139  
140 =item * C<credentials> - Some transports require this; others don’t.
141 The recognized schemes are:
142  
143 =over
144  
145 =item * C<username> & C<api_token> - Authenticate with an API token
146  
147 =item * C<username> & C<password> - Authenticate with a password
148  
149 =item * C<username>, C<password>, & C<tfa_token> - Authenticate with a
150 password and two-factor authentication (2FA) token.
151  
152 =item * C<username> only - Implicit authentication, only usable for local
153 transports.
154  
155 =back
156  
157 =back
158  
159 Depending on the C<service> given, this function returns an instance of
160 either L<cPanel::APIClient::Service::cpanel> or
161 L<cPanel::APIClient::Service::whm>.
162  
163 =cut
164  
165 14 my @_REQUIRED = ( 'service', 'transport' );
166  
167 8 ▶sub create {
Subroutine create: called CC=4 SCAR=14.4
Pod documented
168  
169 # We don’t need the class, but we mandate arrow syntax rather
170 # than static because it seems more consistent with Perl programmers’
171 # expectations of what it looks like to call a function whose purpose
172 # is to create an object.
173 8 shift;
174  
175 8 my (%opts) = @_;
176  
177 8 my @missing = grep { !defined $opts{$_} } @_REQUIRED;
178 8 ▶ die "Missing: @missing" if @missing;
Branch: 1 branch
BranchTrueFalseTotal
if @missing 0 8 8
179  
180 8 my $creds = delete $opts{'credentials'};
181  
182 8 my ( $svc, $transport ) = delete @opts{@_REQUIRED};
183  
184 8 ▶ if ( my @extra = sort keys %opts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@extra) = sort(keys %opts)) 0 8 8
185 0 die "Extra: @extra";
186 }
187  
188 8 my $full_ns = "cPanel::APIClient::Service::$svc";
189 8 _require($full_ns);
190  
191 8 ▶ my $authn = $creds && _parse_creds($creds);
Condition33%1/3
$creds && _parse_creds($creds)
!ll&&!rl&&r
008
MC/DC0%0/2
$creds && _parse_creds($creds)
$creds _parse_creds($creds)
Truth table33%1/3
A && B
A= $credsB= _parse_creds($creds)
ABresult
0X0
100
111
192  
193 8 $transport = _parse_transport( $transport, $authn, $svc );
194  
195 8 return $full_ns->new( $transport, $authn );
196 }
197  
198 8 ▶sub _parse_transport {
Subroutine _parse_transport: called CC=4 SCAR=15.8
199 8 my ( $transport, $authn, $service_name ) = @_;
200  
201 8 ▶ if ( 'ARRAY' ne ref $transport ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("ARRAY" ne ref $transport) 0 8 8
202 0 die "“transport” should be an ARRAY reference, not $transport!";
203 }
204  
205 8 my ( $module, @args ) = (
206 @$transport,
207 service_name => $service_name,
208 );
209  
210 8 $module = "cPanel::APIClient::Transport::$module";
211 8 _require($module);
212  
213 8 ▶ if ( $module->NEEDS_CREDENTIALS() && !$authn ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module->NEEDS_CREDENTIALS and not $authn) 0 8 8
Condition33%1/3
$module->NEEDS_CREDENTIALS and not $authn
!ll&&!rl&&r
080
MC/DC0%0/2
$module->NEEDS_CREDENTIALS and not $authn
$module->NEEDS_CREDENTIALS not $authn
Truth table33%1/3
A and not(B)
A= $module->NEEDS_CREDENTIALSB= not $authn
ABresult
0X0
100
111
214 0 die "Transporter “$transport” requires credentials!";
215 }
216  
217 8 return $module->new( $authn, @args );
218 }
219  
220 24 ▶sub _require {
Subroutine _require: called CC=2 SCAR=7.2
221 24 my ($full_ns) = @_;
222  
223 24 ▶ die if !eval "require $full_ns; 1";
Branch: 1 branch
BranchTrueFalseTotal
if not eval "require $full_ns; 1" 0 24 24
224 }
225  
226 8 ▶sub _parse_creds {
Subroutine _parse_creds: called CC=8 SCAR=21.4
227 8 my ($creds_hr) = @_;
228  
229 8 my %creds_copy = %$creds_hr;
230  
231 8 my $username = delete $creds_copy{'username'};
232  
233 8 ▶ if ( !defined $username ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $username) 0 8 8
234 0 die "Credentials need “username”!";
235 }
236  
237 8 my ( @extras, $class );
238  
239 8 ▶ if ( exists $creds_copy{'api_token'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $creds_copy{'api_token'}) { } 6 2 8
elsif (exists $creds_copy{'password'}) { } 2 0 2
240 6 @extras = ('api_token');
241 6 $class = 'cPanel::APIClient::Authn::Token';
242 }
243 elsif ( exists $creds_copy{'password'} ) {
244 2 @extras = ('password');
245  
246 2 ▶ if ( exists $creds_copy{'tfa_token'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $creds_copy{'tfa_token'}) { } 1 1 2
247 1 push @extras, 'tfa_token';
248 1 $class = 'cPanel::APIClient::Authn::Password2FA';
249 }
250 else {
251 1 $class = 'cPanel::APIClient::Authn::Password';
252 }
253 }
254 else {
255 0 $class = 'cPanel::APIClient::Authn::Username';
256 }
257  
258 8 for my $key (@extras) {
259 9 my $val = delete $creds_copy{$key};
260 9 ▶ if ( !defined $val ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 9 9
261 0 die "Undefined “$key” is invalid!";
262 }
263 }
264  
265 8 ▶ die "Bad “credentials”!" if %creds_copy;
Branch: 1 branch
BranchTrueFalseTotal
if %creds_copy 0 8 8
266  
267 8 _require($class);
268  
269 8 return $class->new( $username, @{$creds_hr}{@extras} );
270 }
271  
272 =head1 LICENSE
273  
274 Copyright 2020 cPanel, L. L. C. All rights reserved. L<http://cpanel.net>
275  
276 This is free software; you can redistribute it and/or modify it under the
277 same terms as Perl itself. See L<perlartistic>.
278  
279 =cut
280  
281 14 1;
« APIClient/X/CommandFailed.pm ↑ Summary APIClient/Utils/HTTPResponse.pm »