API.pm

filter: Statementstmt 60.2% 50 / 83 Branchbran 25.0% 6 / 24 Conditioncond 18.1% 2 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 86.6% 13 / 15 Podpod 100% 5 / 5 totaltotal 52.4% 76 / 145 CC 18 SCAR 30.4
CC
18
Cov
49%
request
72.0
new
8.8
_argparse
4.1
CRAP 20.9
« Domain.pm ↑ Summary NS.pm »
1 package WWW::Namecheap::API;
2  
3 12 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Carp();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use LWP::UserAgent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # For convenience methods
12 12 ▶use WWW::Namecheap::Domain ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use WWW::Namecheap::DNS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 WWW::Namecheap::API - Perl interface to the Namecheap API
18  
19 =cut
20  
21 12 our $VERSION = '0.06';
22 12 our %APIURL = (
23 prod => 'https://api.namecheap.com/xml.response',
24 test => 'https://api.sandbox.namecheap.com/xml.response',
25 );
26  
27 =head1 SYNOPSIS
28  
29 Perl interface to the Namecheap API. API details at:
30  
31 https://www.namecheap.com/support/api/api.aspx
32  
33 Actual API calls happen in the other modules in the distribution, which
34 can be accessed via convenience methods from an API object. Brief
35 example:
36  
37 use WWW::Namecheap::API;
38  
39 my $api = WWW::Namecheap::API->new(
40 System => 'test',
41 ApiUser => 'wwwnamecheapapi',
42 ApiKey => 'keyhere',
43 DefaultIp => '1.2.3.4',
44 );
45  
46 my $result = $api->domain->check(Domains => ['example.com']);
47  
48 if ($result->{'example.com'}) {
49 $api->domain->create(
50 DomainName => 'example.com',
51 Years => 1,
52 Registrant => {
53 OrganizationName => 'Foo Bar Inc.',
54 FirstName => 'Joe',
55 LastName => 'Manager',
56 Address1 => '123 Fake Street',
57 City => 'Univille',
58 StateProvince => 'SD',
59 PostalCode => '12345',
60 Country => 'US',
61 Phone => '+1.2125551212',
62 EmailAddress => 'joe@example.com',
63 },
64 );
65 }
66  
67 =head1 GLOBAL PARAMETERS
68  
69 There are a few parameters that can be included in any of the individual
70 methods within the Namecheap API modules. These are listed below.
71  
72 =head2 ClientIp
73  
74 The client IP address for which this request is effective. If a DefaultIp
75 was not provided when setting up the parent API object, this parameter
76 is required, otherwise it is optional.
77  
78 =head2 UserName
79  
80 A sub-user (see L<WWW::Namecheap::User>) under which the command should
81 be performed. A DefaultUser may be specified at API object creation
82 time; if one is not specified there, the default is to use the ApiUser
83 unless a UserName is provided for the specific command being issued.
84  
85 =head1 SUBROUTINES/METHODS
86  
87 =head2 WWW::Namecheap::API->new(%hash)
88  
89 Instantiate a new API object. Example:
90  
91 my $api = WWW::Namecheap::API->new(
92 System => 'test', # or 'prod' for production, default test
93 ApiUser => 'username',
94 ApiKey => 'apikey',
95 DefaultIp => '1.2.3.4', # optional
96 DefaultUser => 'otheruser', #optional, default ApiUser
97 ApiUrl => 'https://foo.bar/', # overrides URL chosen by System
98 Agent => 'My API Agent/1.0', # optional, overrides default UA
99 );
100  
101 Only ApiUser and ApiKey are required, in which case System will default
102 to 'test' and Agent defaults to 'WWW::Namecheap::API/$VERSION'. This
103 API object will be passed to the constructors of the other classes in
104 the distribution (or you can use its built-in convenience methods to
105 get objects of those classes directly).
106  
107 =cut
108  
109 1 ▶sub new {
Subroutine new: called CC=7 SCAR=21.8
Pod documented
110 1 my $class = shift;
111  
112 1 my $params = _argparse(@_);
113  
114 1 for (qw(ApiUser ApiKey)) {
115 2 ▶ Carp::croak("${class}->new(): Mandatory parameter $_ not provided.") unless $params->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{$_} 0 2 2
116 }
117  
118 my $ua = LWP::UserAgent->new(
119 1 ▶ agent => $params->{'Agent'} || "WWW::Namecheap::API/$VERSION",
Condition50%1/2
$params->{'Agent'} || "WWW::Namecheap::API/$WWW::Namecheap::API::VERSION"
l!l
01
MC/DC0%0/1
$params->{'Agent'} || "WWW::Namecheap::API/$WWW::Namecheap::API::VERSION"
$params->{'Agent'}
Truth table50%1/2
A
A= $params->{'Agent'}
Aresult
11
00
120 );
121  
122 1 my $apiurl;
123 1 ▶ if ($params->{'ApiUrl'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{'ApiUrl'}) { } 0 1 1
124 0 $apiurl = $params->{'ApiUrl'}; # trust the user?!?!
125 } else {
126 1 ▶ if ($params->{'System'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{'System'}) { } 1 0 1
127 1 $apiurl = $APIURL{$params->{'System'}};
128 } else {
129 0 $apiurl = $APIURL{'test'};
130 }
131 }
132  
133 my $self = {
134 ApiUrl => $apiurl,
135 ApiUser => $params->{'ApiUser'},
136 ApiKey => $params->{'ApiKey'},
137 DefaultUser => $params->{'DefaultUser'} || $params->{'ApiUser'},
138 1 ▶ DefaultIp => $params->{'DefaultIp'},
Condition33%1/3
$params->{'DefaultUser'} || $params->{'ApiUser'}
l!l&&r!l&&!r
010
MC/DC0%0/2
$params->{'DefaultUser'} || $params->{'ApiUser'}
$params->{'DefaultUser'} $params->{'ApiUser'}
Truth table33%1/3
A || B
A= $params->{'DefaultUser'}B= $params->{'ApiUser'}
ABresult
1X1
011
000
139 _ua => $ua,
140 };
141  
142 1 return bless($self, $class);
143 }
144  
145 =head2 $api->request(%hash)
146  
147 Send a request to the Namecheap API. Returns the XML response parsed into
148 Perl form by XML::Simple. Intended for use by sub-classes, not outside
149 calls. Parameters should be of the type and quantity required by the
150 Namecheap API for the given Command. Example:
151  
152 my $xml = $api->request(
153 Command => 'namecheap.domains.check',
154 DomainList => 'example.com,example2.net',
155 ClientIp => '1.2.3.4', # required if no DefaultIp in $api
156 );
157  
158 =cut
159  
160 0 ▶sub request {
Subroutine request: not called CC=8 SCAR=42.8
Pod documented
161 0 my $self = shift;
162 0 my %reqparams = @_;
163  
164 0 ▶ unless ($reqparams{'Command'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reqparams{"Command"}) 0 0 0
165 0 Carp::carp("No command specified, bailing!");
166 0 return;
167 }
168  
169 0 ▶ my $clientip = delete($reqparams{'ClientIp'}) || $self->{'DefaultIp'};
Condition0%0/3
delete $reqparams{'ClientIp'} || $self->{'DefaultIp'}
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $reqparams{'ClientIp'} || $self->{'DefaultIp'}
delete $reqparams{'ClientIp'} $self->{'DefaultIp'}
Truth table0%0/3
A || B
A= delete $reqparams{'ClientIp'}B= $self->{'DefaultIp'}
ABresult
1X1
011
000
170 0 ▶ unless ($clientip) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($clientip) 0 0 0
171 0 Carp::carp("No Client IP or default IP specified, cannot perform request.");
172 0 return;
173 }
174 0 ▶ my $username = delete($reqparams{'UserName'}) || $self->{'DefaultUser'};
Condition0%0/3
delete $reqparams{'UserName'} || $self->{'DefaultUser'}
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $reqparams{'UserName'} || $self->{'DefaultUser'}
delete $reqparams{'UserName'} $self->{'DefaultUser'}
Truth table0%0/3
A || B
A= delete $reqparams{'UserName'}B= $self->{'DefaultUser'}
ABresult
1X1
011
000
175  
176 0 ▶ map { delete($reqparams{$_}) unless defined($reqparams{$_}) } keys %reqparams;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reqparams{$_} 0 0 0
177  
178 0 my $ua = $self->{_ua}; # convenience
179 my $url = sprintf('%s?ApiUser=%s&ApiKey=%s&UserName=%s&Command=%s&ClientIp=%s&',
180 $self->{'ApiUrl'}, $self->{'ApiUser'}, $self->{'ApiKey'},
181 0 $username, delete($reqparams{'Command'}), $clientip);
182 0 $url .= join('&', map { join('=', map { uri_escape($_) } each %reqparams) } keys %reqparams);
183 #print STDERR "Sent URL $url\n";
184 0 my $response = $ua->get($url);
185  
186 0 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
187 0 Carp::carp("Request failed: " . $response->message);
188 0 return;
189 }
190  
191 0 my $xml = XMLin($response->content);
192  
193 0 ▶ if ($xml->{Status} eq 'ERROR') {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml->{"Status"} eq "ERROR") 0 0 0
194 0 $self->{_error} = $xml;
195 0 return;
196 }
197 0 return $xml;
198 }
199  
200 =head2 $api->domain()
201  
202 Helper method to create and return a WWW::Namecheap::Domain object utilizing
203 this API object. Always returns the same object within a given session via
204 internal caching.
205  
206 =cut
207  
208 1 ▶sub domain {
Subroutine domain: called CC=2 SCAR=7.6
Pod documented
209 1 my $self = shift;
210  
211 1 ▶ if ($self->{_domain}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_domain"}) 0 1 1
212 0 return $self->{_domain};
213 }
214  
215 1 return $self->{_domain} = WWW::Namecheap::Domain->new(API => $self);
216 }
217  
218 =head2 $api->dns()
219  
220 Helper method to create and return a WWW::Namecheap::DNS object utilizing
221 this API object. Always returns the same object within a given session via
222 internal caching.
223  
224 =cut
225  
226 1 ▶sub dns {
Subroutine dns: called CC=2 SCAR=7.6
Pod documented
227 1 my $self = shift;
228  
229 1 ▶ if ($self->{_dns}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_dns"}) 0 1 1
230 0 return $self->{_dns};
231 }
232  
233 1 return $self->{_dns} = WWW::Namecheap::DNS->new(API => $self);
234 }
235  
236 =head2 $api->error()
237  
238 Returns the full XML response from the API if an error occurred during
239 the request. Most likely key of interest is $xml->{Errors} and below.
240  
241 =cut
242  
243 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
244 0 return $_[0]->{_error};
245 }
246  
247 1 ▶sub _argparse {
Subroutine _argparse: called CC=3 SCAR=14.2
248 1 my $hashref;
249 1 ▶ if (@_ % 2 == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ % 2 == 0) { } 1 0 1
elsif (ref $_[0] eq 'HASH') { } 0 0 0
250 1 $hashref = { @_ }
251 } elsif (ref($_[0]) eq 'HASH') {
252 0 $hashref = \%{$_[0]};
253 }
254 1 return $hashref;
255 }
256  
257 =head1 AUTHOR
258  
259 Tim Wilde, C<< <twilde at cpan.org> >>
260  
261 =head1 BUGS
262  
263 Please report any bugs or feature requests to C<bug-www-namecheap-api at rt.cpan.org>, or through
264 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-Namecheap-API>. I will be notified, and then you'll
265 automatically be notified of progress on your bug as I make changes.
266  
267  
268  
269 =head1 SUPPORT
270  
271 You can find documentation for this module with the perldoc command.
272  
273 perldoc WWW::Namecheap::API
274  
275  
276 You can also look for information at:
277  
278 =over 4
279  
280 =item * RT: CPAN's request tracker (report bugs here)
281  
282 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-Namecheap-API>
283  
284 =item * AnnoCPAN: Annotated CPAN documentation
285  
286 L<http://annocpan.org/dist/WWW-Namecheap-API>
287  
288 =item * CPAN Ratings
289  
290 L<http://cpanratings.perl.org/d/WWW-Namecheap-API>
291  
292 =item * Search CPAN
293  
294 L<http://search.cpan.org/dist/WWW-Namecheap-API/>
295  
296 =back
297  
298  
299 =head1 ACKNOWLEDGEMENTS
300  
301  
302 =head1 LICENSE AND COPYRIGHT
303  
304 Copyright 2011 Tim Wilde.
305  
306 This program is free software; you can redistribute it and/or modify it
307 under the terms of either: the GNU General Public License as published
308 by the Free Software Foundation; or the Artistic License.
309  
310 See http://dev.perl.org/licenses/ for more information.
311  
312  
313 =cut
314  
315 12 1; # End of WWW::Namecheap::API
« Domain.pm ↑ Summary NS.pm »