blib/lib/WWW/LogicBoxes/Role/Command/Raw.pm

filter: Statementstmt 39.3% 35 / 89 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 62.5% 10 / 16 Podpod 0.0% 0 / 2 totaltotal 37.8% 45 / 119 CC 12 SCAR 23.7
CC
12
Cov
35%
install_methods
20.0
_make_query_string
20.0
_construct_get_args
20.0
CRAP 10.7
« blib/lib/WWW/LogicBoxes/Role/Command.pm ↑ Summary blib/lib/WWW/LogicBoxes/Domain.pm »
1 package WWW::LogicBoxes::Role::Command::Raw;
2  
3 36 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 36 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 36 ▶use MooseX::Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 36 ▶use WWW::LogicBoxes::Types qw( HashRef Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 36 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 36 ▶use URI::Escape qw( uri_escape );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 36 ▶use XML::LibXML::Simple qw(XMLin);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 36 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 36 requires 'username', 'password', 'api_key', '_base_uri', 'response_type';
17  
18 36 our $VERSION = '1.1.0'; # VERSION
19 # ABSTRACT: Construct Methods For Making Raw LogicBoxes Requests
20  
21 36 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 36 Readonly our $API_METHODS => {
23 domains => {
24 GET => [
25 qw(available suggest-names v5/suggest-names validate-transfer search customer-default-ns orderid details details-by-name locks tel/cth-details)
26 ],
27 POST => [
28 qw(register transfer eu/transfer eu/trade uk/transfer renew modify-ns add-cns modify-cns-name modify-cns-ip delete-cns-ip modify-contact modify-privacy-protection modify-auth-code enable-theft-protection disable-theft-protection tel/modify-whois-pref resend-rfa uk/release cancel-transfer delete restore de/recheck-ns dotxxx/assoication-details)
29 ],
30 },
31 contacts => {
32 GET => [qw(details search sponsors dotca/registrantagreement)],
33 POST => [qw(add modify default set-details delete coop/add-sponsor)],
34 },
35 customers => {
36 GET =>
37 [qw(details details-by-id generate-token authenticate-token search)],
38 POST => [qw(signup modify change-password delete)],
39 },
40 resellers => {
41 GET => [
42 qw(details generate-token authenticate-token promo-details temp-password search)
43 ],
44 POST => [qw(signup modify-details)],
45 },
46 products => {
47 GET => [
48 qw(availability details plan-details customer-price reseller-price reseller-cost-price)
49 ],
50 POST => [qw(category-keys-mapping move)],
51 },
52 webservices => {
53 GET => [
54 qw(details active-plan-categories mock/products/reseller-price orderid search modify-pricing)
55 ],
56 POST => [qw(add renew modify enable-ssl enable-maintenance delete)],
57 },
58 multidomainhosting => {
59 GET => [qw(details orderid search modify-pricing)],
60 POST => [qw(add renew modify enable-ssl delete)],
61 },
62 'multidomainhosting/windows' => {
63 GET => [qw(details orderid search modify-pricing)],
64 POST => [qw(add renew modify enable-ssl delete)],
65 },
66 resellerhosting => {
67 GET => [qw(details orderid search modify-pricing)],
68 POST => [
69 qw(add renew modify add-dedicated-ip delete-dedicated-ip delete generate-license-key)
70 ],
71 },
72 mail => {
73 GET => [qw(user mailinglists)],
74 POST => [qw(activate)],
75 },
76 'mail/user' => {
77 GET => [qw(authenticate)],
78 POST => [
79 qw(add add-forward-only-account modify suspend unsuspend change-password reset-password update-autoresponder delete add-admin-forwards delete-admin-forwards add-user-forwards delete-user-forwards)
80 ],
81 },
82 'mail/users' => {
83 GET => [qw(search)],
84 POST => [qw(suspend unsuspend delete)],
85 },
86 'mail/domain' => {
87 GET => [qw(is-owernship-verified catchall dns-records)],
88 POST => [
89 qw(add-alias delete-alias update-notification-email active-catchall deactivate-catchall)
90 ],
91 },
92 'mail/mailinglist' => {
93 GET => [qw(subscribers)],
94 POST => [
95 qw(add update add-subscribers delete-subscribers delete add-moderators delete-moderators)
96 ],
97 },
98 dns => {
99 GET => [],
100 POST => [qw(activate)],
101 },
102 'dns/manage' => {
103 GET => [qw(search-records delete-record)],
104 POST => [
105 qw(add-ipv4-record add-ipv6-record add-cname-record add-mx-record add-ns-record add-txt-record add-srv-record update-ipv4-record update-ipv6-record update-cname-record update-mx-record update-ns-record update-txt-record update-srv-record update-soa-record delete-ipv4-record delete-ipv6-record delete-cname-record delete-mx-record delete-ns-record delete-txt-record delete-srv-record)
106 ],
107 },
108 domainforward => {
109 GET => [qw(details dns-records)],
110 POST => [qw(activate manage)],
111 },
112 digitalcertificate => {
113 GET => [qw(check-status details search orderid)],
114 POST =>
115 [qw(add cancel delete enroll-for-thawtecertificate reissue renew)],
116 },
117 billing => {
118 GET => [
119 qw(customer-transactions reseller-transactions customer-greedy-transactions reseller-greedy-transactions customer-balance customer-transactions/search reseller-transactions/search customer-archived-transactions/search customer-balanced-transactions reseller-balance)
120 ],
121 POST => [
122 qw(customer-pay execute-order-without-payment add-customer-fund add-reseller-fund add-customer-debit-note add-reseller-debit-note add-customer-misc-invoice add-reseller-misc-invoice)
123 ],
124 },
125 orders => {
126 GET => [qw()],
127 POST => [qw(suspend unsuspend)],
128 },
129 actions => {
130 GET => [qw(search-current search-archived)],
131 POST => [qw()],
132 },
133 commons => {
134 GET => [qw(legal-agreements)],
135 POST => [qw()],
136 },
137 pg => {
138 GET => [
139 qw(allowedlist-for-customer list-for-reseller customer-transactions)
140 ],
141 POST => [qw()],
142 },
143 };
144  
145 has api_methods => (
146 is => 'ro',
147 isa => HashRef,
148 0 ▶ default => sub { $API_METHODS },
Subroutine __ANON__: not called CC=1 SCAR=6.9
149 36 init_arg => undef,
150 );
151  
152 sub BUILD {
153 0 ▶ my $self = shift;
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
154  
155 0 $self->install_methods();
156  
157 0 return;
158 }
159  
160 sub install_methods {
161 0 ▶ my $self = shift;
Subroutine install_methods: not called CC=4 SCAR=30.0
Pod undocumented
162  
163 0 my $ua = HTTP::Tiny->new;
164 0 for my $api_class ( keys %{ $self->api_methods } ) {
165 0 for my $http_method ( keys %{ $self->api_methods->{ $api_class } } ) {
166 0 for my $api_method (@{ $self->api_methods->{ $api_class }{ $http_method } }) {
167 0 my $method_name = $api_class . '__' . $api_method;
168  
169 0 $method_name =~ s|-|_|g;
170 0 $method_name =~ s|/|__|g;
171  
172 $self->meta->add_method(
173 $method_name => sub {
174 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
175 0 my $args = shift;
176  
177 0 ▶ if( !grep { $_ eq $http_method } qw( GET POST ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $http_method;} "GET", "POST") 0 0 0
178 0 croak 'Unable to determine if this is a GET or POST request';
179 }
180  
181 0 my $uri = $self->_make_query_string(
182 api_class => $api_class,
183 api_method => $api_method,
184 params => $args,
185 );
186  
187 ### Method Name: ( $method_name )
188 ### HTTP Method: ( $http_method )
189 ### URI: ( $uri )
190  
191 0 my $response = $ua->request( $http_method, $uri );
192 0 ▶ if ( $self->response_type eq "xml_simple" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->response_type eq 'xml_simple') { } 0 0 0
193 0 return XMLin( $response->{content} );
194 }
195 else {
196 0 return $response->{content};
197 }
198 }
199 0 );
200 }
201 }
202 }
203  
204 0 return;
205 }
206  
207 sub _make_query_string {
208 0 ▶ my $self = shift;
Subroutine _make_query_string: not called CC=4 SCAR=30.0
209 0 my ( %args ) = validated_hash(
210 \@_,
211 api_class => { isa => Str },
212 api_method => { isa => Str },
213 params => { isa => HashRef },
214 );
215  
216 0 my $api_class = $args{api_class};
217 0 $api_class =~ s/_/-/g;
218 0 $api_class =~ s/__/\//g;
219  
220 0 my $api_method = $args{api_method};
221 0 $api_method =~ s/_/-/g;
222 0 $api_method =~ s/__/\//g;
223  
224 0 ▶ my $response_type = ( $self->response_type eq 'xml_simple' ) ? 'xml' : $self->response_type;
Branch: 1 branch
BranchTrueFalseTotal
$self->response_type eq 'xml_simple' ? : 0 0 0
225  
226 0 my $query_uri = sprintf('%s/api/%s/%s.%s?auth-userid=%s',
227 $self->_base_uri, $api_class, $api_method, $response_type, uri_escape( $self->username ) );
228  
229 0 ▶ if( $self->has_password ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->has_password) { } 0 0 0
elsif ($self->has_api_key) { } 0 0 0
230 0 $query_uri .= "&auth-password=" . uri_escape( $self->password )
231 }
232 elsif($self->has_api_key) {
233 0 $query_uri .= "&api-key=" . uri_escape( $self->apikey )
234 }
235 else {
236 0 croak 'Unable to construct query string without a password or api_key';
237 }
238  
239 0 $query_uri .= $self->_construct_get_args( $args{params} );
240  
241 0 return $query_uri;
242 }
243  
244 sub _construct_get_args {
245 0 ▶ my $self = shift;
Subroutine _construct_get_args: not called CC=4 SCAR=30.0
246 0 my ( $params ) = pos_validated_list( \@_, { isa => HashRef } );
247  
248 0 my $get_args;
249 0 for my $param_name ( keys %{ $params } ) {
250 0 ▶ if( ref $params->{ $param_name } eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params->{$param_name} eq 'ARRAY') { } 0 0 0
251 0 for my $param_value (@{ $params->{ $param_name } }) {
252 0 $get_args .= sprintf('&%s=%s', uri_escape( $param_name ), uri_escape( $param_value ) );
253 }
254 }
255 else {
256 0 $get_args .= sprintf('&%s=%s', uri_escape( $param_name ), uri_escape( $params->{ $param_name } ) );
257 }
258 }
259  
260 0 return $get_args;
261 }
262  
263 36 1;
264  
265 __END__
266 =pod
267  
268 =head1 NAME
269  
270 WWW::LogicBoxes::Role::Command::Raw - Low Level Access to LogicBoxes API
271  
272 =head1 SYNOPSIS
273  
274 use WWW::LogicBoxes;
275  
276 my $logic_boxes = WWW::LogicBoxes->new( ... );
277  
278 my $response = $logic_boxes->domains__suggest_names({
279 'keyword' => 'car',
280 'tlds' => ['com', 'net', 'org'],
281 'no-of-results' => 10,
282 'hypehn-allowed'=> 'false',
283 'add-related' => 'true',
284 });
285  
286 =head1 REQUIRES
287  
288 =over 4
289  
290 =item username
291  
292 =item password
293  
294 =item api_key
295  
296 =item _base_uri
297  
298 =item response_Type
299  
300 =back
301  
302 =head1 DESCRIPTION
303  
304 This role composes a series of methods into the consuming class (L<WWW::LogicBoxes>) that directly expose methods of the L<LogicBoxes|http://www.logicboxes.com> API. It is the lowest level of access to the LogicBoxes API and is intended only for the most advanced usages that are not covered by other commands.
305  
306 B<NOTE> You almost never want to make this low level of a call. You really should be looking at the L<commands|WWW::LogicBoxes/COMMANDS> to find the specific method to accomplish your goals.
307  
308 =head1 METHODS
309  
310 Methods are constructed by abstracting out the need to specify the HTTP method (POST or GET) and automagically building the request URI according to the documentation provided by L<LogicBoxes|http://www.logicboxes.com> (see the Logic Boxes API user guide at L<http://manage.logicboxes.com/kb/answer/744> for additional information).
311  
312 =head2 Method Naming
313  
314 To fully understand the method names it's best to take a specific example (in this case the suggestion of domain names).
315  
316 =head3 Suggest Domains (and many others)
317  
318 my $response = $logic_boxes->domains__suggest_names({
319 'keyword' => 'car',
320 'tlds' => ['com', 'net', 'org'],
321 'no-of-results' => 10,
322 'hypehn-allowed'=> 'false',
323 'add-related' => 'true',
324 });
325  
326 L<LogicBoxes|http://www.logicboxes.com>' API states that this method is part of their HTTP API, specifically the Domain Category and more specifically the Suggest Names method. The sample URI for this request would then be:
327  
328 https://test.httpapi.com/api/domains/suggest-names.json?auth-userid=0&auth-password=password&keyword=domain&tlds=com&tlds=net&no-of-results=0&hyphen-allowed=true&add-related=true
329  
330 The method name is built using the URI that the request is expected at in a logical way. Since this method is a member of the Domains Category and is specifically Suggest Names we end up:
331  
332 $logic_boxes->domains__suggest_names
333  
334 Where everything before the first "__" is the category and everything following it is the specific method (with - replaced with _ and / replaced with __).
335  
336 =head2 Arguments Passed to Methods
337  
338 The specific arguments each method requires is not enforced by this module, rather it is left to the developer to reference the L<LogicBoxes API|http://manage.logicboxes.com/kb/answer/744> and to pass the correct arguments to each method as a hash. Again, this is a module of last resort, you should really be using the exposed Commands if at all posible.
339  
340 There are two I<odd> cases that you should be aware of with respect to the way arguments must be passed.
341  
342 =head3 Repeated Elements
343  
344 For methods such as domains__check that accept the same I<key> multiple times:
345  
346 https://test.httpapi.com/api/domains/available.json?auth-userid=0&auth-password=password&domain-name=domain1&domain-name=domain2&tlds=com&tlds=net
347  
348 This module accepts a hash where the key is the name of the argument (such as domain-name) and the value is an array of values you wish to pass:
349  
350 $logic_boxes->domains__available({
351 'domain-name' => ["google", "cnn"],
352 'tlds' => ["com","net"]
353 });
354  
355 This is interpreted for you automagically into the repeating elements when the API's URI is built.
356  
357 =head3 Array of Numbered Elements
358  
359 For methods such as contacts__set_details that accept the same key multiple times except an incrementing digit is appended:
360  
361 https://test.httpapi.com/api/contacts/set-details.json?auth-userid=0&auth-password=password&contact-id=0&attr-name1=sponsor1&attr-value1=0&product-key=dotcoop
362  
363 This module still accepts a hash and leaves it to the developer to handle the appending of the incrementing digit to the keys of the hash:
364  
365 $logic_boxes->contacts__set_details({
366 'contact-id' => 1337,
367 'attr-name1' => 'sponsor',
368 'attr-value1' => '0',
369 'attr-name2' => 'CPR',
370 'attr-value2' => 'COO',
371 'product-key' => 'dotcoop'
372 });
373  
374 In this way you are able to overcome the need for unique keys and still pass the needed values onto LogicBoxes' API.
375  
376 =cut
« blib/lib/WWW/LogicBoxes/Role/Command.pm ↑ Summary blib/lib/WWW/LogicBoxes/Domain.pm »