Host.pm untested

filter: Statementstmt 0.0% 0 / 52 Branchbran 0.0% 0 / 2 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 68 CC 2 SCAR 10.3
CC
2
Cov
0%
_throw_exception
6.0
call
2.0
_do_call
2.0
CRAP 2.8
1 package WebService::CloudFlare::Host;
2 use Moose;
3 use Try::Tiny;
4 use Data::Dumper;
5 use LWP;
6  
7 our $VERSION = "000100"; # 0.1.0
8 $VERSION = eval $VERSION;
9  
10 has 'host_key' => (
11 is => 'rw',
12 isa => 'Str',
13 required => 1,
14 );
15  
16 has 'base_api' => (
17 is => 'ro',
18 isa => 'Str',
19 default => 'https://api.cloudflare.com/host-gw.html',
20 );
21  
22 has 'user_agent' => (
23 is => 'ro',
24 isa => 'Str',
25 default => 'WebService::CloudFlare::Host/1.0',
26 );
27  
28 has 'http_timeout' => (
29 is => 'ro',
30 isa => 'Int',
31 default => 60,
32 );
33  
34 has 'ua' => (
35 is => 'ro',
36 isa => 'LWP::UserAgent',
37 lazy_build => 1,
38 );
39  
40 sub call {
41 my ( $self, $class, %args ) = @_;
42  
43 # Load the request class.
44 try {
45 Class::MOP::load_class("WebService::CloudFlare::Host::Request::$class");
46 } catch {
47 $self->_throw_exception( $_, "Loading request class: $class",
48 "Class::MOP::load_class",
49 "WebService::CloudFlare::Host::Request::$class"
50 );
51 };
52  
53 # Create the request object.
54 my $req = try {
55 "WebService::CloudFlare::Host::Request::$class"->new( %args )
56 } catch {
57 $self->_throw_exception( $_, 'Creating API Request',
58 "WebService::CloudFlare::Host::Request::$class", Dumper \%args
59 );
60 };
61  
62 # Make the actual HTTP request.
63 my $http_res = try {
64 $self->_do_call( $req );
65 } catch {
66 $self->_throw_exception( $_, 'Making HTTP Call To Server API',
67 'WebService::CloudFlare::Host::do_call', Dumper $req
68 );
69 };
70  
71 # Create a response object to send back to the user.
72 my $res = try {
73 $self->_create_response( $class, $http_res );
74 } catch {
75 $self->_throw_exception( $_, 'Creating API Response From HTTP Data',
76 'WebService::CloudFlare::Host::create_response', Dumper $http_res,
77 );
78 };
79 return $res;
80 }
81  
82 sub _throw_exception {
83 my ( $self, $message, $layer, $function, $args ) = @_;
84  
85 # We installed ::Exception with the package, it's here.
86 Class::MOP::load_class('WebService::CloudFlare::Host::Exception');
87  
88 # Let's get to the bottom of the exception...
89 my $exception;
90 while ( $message->isa( 'WebService::CloudFlare::Host::Exception' ) ) {
91 $exception = $message;
92 $message = $message->message;
93 }
94 die $exception if $exception;
95  
96  
97 die WebService::CloudFlare::Host::Exception->new(
98 message => $message,
99 layer => $layer,
100 function => $function,
101 args => $args,
102 );
103 }
104  
105 sub _do_call {
106 my ( $self, $request ) = @_;
107
108 my %arguments = $request->as_post_params;
109 $arguments{host_key} = $self->host_key;
110  
111 return $self->ua->post($self->base_api, \%arguments);
112 }
113  
114 sub _create_response {
115 my ( $self, $class, $http ) = @_;
116
117 try {
118 Class::MOP::load_class("WebService::CloudFlare::Host::Response::$class");
119 } catch {
120 $self->_throw_exception( $_, "Loading request class: $class",
121 "Class::MOP::load_class",
122 "WebService::CloudFlare::Host::Reponse::$class"
123 );
124 };
125  
126 return "WebService::CloudFlare::Host::Response::$class"->new( $http );
127  
128 }
129  
130 sub _build_ua {
131 my ( $self ) = @_;
132 return LWP::UserAgent->new(
133 timeout => $self->http_timeout,
134 user_agent => $self->user_agent
135 );
136 }
137  
138 1;
139  
140 =head1 NAME
141  
142 WebService::CloudFlare::Host - A client API For Hosting Partners
143  
144 =head1 VERSION
145  
146 000100 (0.1.0)
147  
148 =head1 SYNOPSIS
149  
150 my $CloudFlare = WebService::CloudFlare::Host->new(
151 host_key => 'cloudflare hostkey',
152 timeout => 30,
153 );
154  
155 my $response = eval { $CloudFlare->call('UserCreate',
156 email => 'richard.castle@hyperionbooks.com',
157 pass => 'ttekceBetaK',
158 ) };
159  
160 if ( $@ ) {
161 die "Error: in " . $@->function . ": " . $@->message;
162 }
163  
164  
165 printf("Got API Keys: User Key: %s, User API Key: %s",
166 $response->user_key, $response->api_key
167 );
168  
169 =head1 DESCRIPTION
170  
171 WebService::CloudFlare::Host is a client side API library
172 to make using CloudFlare simple for hosting providers.
173  
174 It gives a simple interface for making API calls, getting
175 response objects, and implementing additional API calls.
176  
177 All API calls have a Request and Response object that define
178 the accepted information for that call.
179  
180 =head1 METHODS
181  
182 The only method used is C<call($api_call, %arguments)>.
183  
184 When making an API call, the first argument defines the API request
185 to load. This is loaded from Request::. Additional arguments are
186 passed as-is to the Request Object.
187  
188 Once the object has been made, an HTTP call to the CloudFlare API
189 is made. The JSON returned is used to construct a Response object
190 loaded from Response:: with the same name as the Request object.
191  
192 C<call> dies on error, giving a WebService::CloudFlare::Host::Exception
193 object and should be run in an eval or with Try::Tiny.
194  
195 =head1 STANDARD OBJECT METHODS
196  
197 =head2 Standard Request Object
198  
199 The host key is dynamically inserted into the Requests.
200  
201 =head2 Standard Response Object
202  
203 The following methods are avilable on standard Response objects.
204  
205 =over 4
206  
207 =item result
208  
209 The result sent from the API: 'success' or 'error'.
210  
211 =item message
212  
213 If the result is 'error', a message will be set with a user-readable explaination
214 of the error; otherwise, this method will not exist.
215  
216 =item code
217  
218 If the result is 'error', a code will be set. This error can be found at
219 L<http://www.cloudflare.com/docs/host-api.html>.
220  
221 =back
222  
223 =head1 API CALLS
224  
225  
226 =head2 UserCreate
227  
228 The UserCreate API call creates a user for the CloudFlare
229 service, as if they had signed up through CloudFlare's website.
230  
231 my $response = eval { $CloudFlare->call('UserCreate',
232 email => 'Casey.Klein@partydown.com',
233 pass => 'omgPassword',
234 unique_id => '506172747920446f776e203c33205521',
235 )};
236  
237 =head3 Request
238  
239 The request uses the following parameters:
240  
241 =over 4
242  
243 =item email
244  
245 The email address that the end-user can use to sign into
246 the CloudFlare service.
247  
248 =item pass
249  
250 The password the user can use to sign into the CloudFlare service.
251 This should not be recorded on the Hosting Provider's side.
252  
253 =item user
254  
255 A username for the user. This is used in saluations and emails from
256 CloudFlare. It has no bearing in the rest of the API.
257  
258 =item unique_id
259  
260 A unique id that may be used for UserLookup calls (as opposed to the
261 user's email address).
262  
263 =item clobber
264  
265 When set to 1, a user's unique_id can be replaced with a new unique_id.
266  
267 =back
268  
269 =head3 Response
270  
271 printf("Created account for %s, with Unique ID => %s, "
272 . "User Key => %s, and API Key => %s",
273 $response->unique_id, $response->user_key, $response->api_key
274 );
275  
276 The response sets the following methods:
277  
278 =over 4
279  
280 =item api_key
281  
282 This API key allows a hosting provider to act as the user. All user
283 API requests can be completed with this key.
284  
285 =item email
286  
287 This is the registered email account for the CloudFlare user.
288  
289 =item user_key
290  
291 This user_key is used to make Hosting API calls, specifically
292 the ZoneSet, ZoneDelete, and ZoneLookup API calls.
293  
294 =item unique_id
295  
296 This can be used instead of the email address to do UserLookup calls.
297  
298 =item username
299  
300 The username.
301  
302 =back
303  
304 =head2 UserAuth
305  
306 The UserAuth API call gives the hosting provider access to
307 the User's account. The call returns a user_key as well as
308 the api_key and authenticates the Hosting Provider to perform
309 actions as the user.
310  
311 =head3 Request
312  
313 my $response = eval { $CloudFlare->call('UserAuth',
314 email => 'Casey.Klein@partydown.com',
315 pass => 'omgPassword',
316 ) };
317  
318 The request uses the following parameters:
319  
320 =over 4
321  
322 =item email
323  
324 The email address that the user used to register the account
325 with CloudFlare.
326  
327 =item pass
328  
329 The password the user uses to login to CloudFlare. This should
330 not be stored on a hosting provider's side.
331  
332 =item unique_id
333  
334 A unique_id that may be used to perform UserLookup API calls.
335  
336 =item clobber
337  
338 If true, the unique_id can be clobbered.
339  
340 =back
341  
342 =head3 Response
343  
344 The response sets the following methods:
345  
346 =over 4
347  
348 =item api_key
349  
350 This API key allows a hosting provider to act as the user. All user
351 API requests can be completed with this key.
352  
353 =item email
354  
355 This is the registered email account for the CloudFlare user.
356  
357 =item user_key
358  
359 This user_key is used to make Hosting API calls, specifically
360 the ZoneSet, ZoneDelete, and ZoneLookup API calls.
361  
362 =item unique_id
363  
364 This can be used instead of the email address to do UserLookup calls.
365  
366 =back
367  
368 =head2 UserLookup
369  
370 The UserLookup API call gives a hosting provider the ablity
371 to find information on a user account that it has access to through
372 either the unique_id or the email address that was used in UserAuth
373 or UserCreate API calls.
374  
375 =head3 Request
376  
377 my $response = eval { $CloudFlare->call('UserLookup',
378 email => 'Casey.Klein@partydown.com',
379 ) };
380  
381 The request uses the following parameters:
382  
383 =over 4
384  
385 =item email
386  
387 The email address that was used in UserCreate or UserAuth
388 API call. This is required if C<unique_id> is not set.
389  
390 =item unique_id
391  
392 The unique_id that was last set in UserCreate or UserAuth
393 API call. This is required if C<email> is not set.
394  
395 =back
396  
397 =head3 Response
398  
399 The response sets the following methods:
400  
401 =over 4
402  
403 =item api_key
404  
405 This API key allows a hosting provider to act as the user. All user
406 API requests can be completed with this key.
407  
408 =item email
409  
410 This is the registered email account for the CloudFlare user.
411  
412 =item user_key
413  
414 This user_key is used to make Hosting API calls, specifically
415 the ZoneSet, ZoneDelete, and ZoneLookup API calls.
416  
417 =item unique_id
418  
419 This can be used instead of the email address to do UserLookup calls.
420  
421 =item user_authed
422  
423 True if the hosting provider has access to this user.
424  
425 =item user_exists
426  
427 True if the user exists in the CloudFlare system.
428  
429 =item zones
430  
431 A list of zones that the user has associated with his or her account.
432  
433 =back
434  
435 =head2 ZoneSet
436  
437 This associates a zone with the CloudFlare service for the
438 user whose user_key is used.
439  
440 =head3 Request
441  
442 my $response = eval { $CloudFlare->call('ZoneSet',
443 user_key => 'e7af5f120e3240e7bfba063b5f62c922',
444 resolve_to => '173.230.133.102',
445 zone_name => 'partydown.com',
446 subdomains => 'www',
447 ) };
448  
449 The request uses the following parameters:
450  
451 =over 4
452  
453 =item user_key
454  
455 The user_key provided by UserCreate, UserLookup, or UserAuth.
456  
457 The zone will be associated with the user whose user_key is used.
458  
459 =item resolve_to
460  
461 The IP address or a CNAME that resolves to the origin server that
462 hosts the content for the given website.
463  
464 =item zone_name
465  
466 The name of the domain.
467  
468 =item subdomains
469  
470 A comma-seperated list of the subdomains from the zone for which
471 CloudFlare should act as a reverse proxy.
472  
473 =back
474  
475 =head3 Response
476  
477 The response sets the following methods:
478  
479 =over 4
480  
481 =item zone_name
482  
483 The name of the domain.
484  
485 =item resolving
486  
487 The origin server that has been recorded. The same as the one
488 submitted in the Request.
489  
490 =item forwarded
491  
492 A hashref whose keys are the domain, and whose value is the CNAME that
493 should be used in the DNS system to have the requests be processed by
494 CloudFlare.
495  
496 =item hosted
497  
498 A hashref whose keys are the the domain name(s) that are hosted, and
499 whose value is the resolving address.
500  
501 =back
502  
503 =head2 ZoneDelete
504  
505 This will remove a zone from being hosted by the user whose
506 user_key is used, provided they are the ones hosting the zone.
507  
508 =head3 Request
509  
510 my $response = eval { $CloudFlare->call('ZoneDelete',
511 user_key => 'e7af5f120e3240e7bfba063b5f62c922',
512 zone_name => 'partydown.com',
513 ) };
514  
515 The request uses the following parameters:
516  
517 =over 4
518  
519 =item user_key
520  
521 The user_key of the user whose zone is being removed from CloudFlare.
522  
523 =item zone_name
524  
525 The name of the zone to be removed.
526  
527 =back
528  
529 =head3 Response
530  
531 The response sets the following methods:
532  
533 =over 4
534  
535 =item zone_name
536  
537 The name of the zone from the Request.
538  
539 =item zone_deleted
540  
541 True if the zone was deleted.
542  
543 =back
544  
545 =head2 ZoneLookup
546  
547 Find information on a zone hosted by a given user_key.
548  
549 =head3 Request
550  
551 my $response = eval { $CloudFlare->call('ZoneLookup',
552 user_key => 'e7af5f120e3240e7bfba063b5f62c922',
553 zone_name => 'partydown.com',
554 ) };
555  
556 The request uses the following parameters:
557  
558 =over 4
559  
560 =item user_key
561  
562 The user_key of the user whose zone is being removed from CloudFlare.
563  
564 =item zone_name
565  
566 The name of the zone to be removed.
567  
568 =back
569  
570 =head3 Response
571  
572 The response sets the following methods:
573  
574 =over 4
575  
576 =item zone_name
577  
578 =item resolving
579  
580 The origin server that has been recorded. The same as the one
581 submitted in the Request.
582  
583 =item forwarded
584  
585 A hashref whose keys are the domain, and whose value is the CNAME that
586 should be used in the DNS system to have the requests be processed by
587 CloudFlare.
588  
589 =item hosted
590  
591 A hashref whose keys are the the domain name(s) that are hosted, and
592 whose value is the resolving address.
593  
594 =item zone_exists
595  
596 True if the zone exists in the CloudFlare service.
597  
598 =item zone_hosted
599  
600 True if the zone is hosted by this user_key.
601  
602 =back
603  
604 =head1 CREATING API CALLS
605  
606 TBD
607  
608 =head2 Request Classes
609  
610 =head2 Response Classes
611  
612 =head1 AUTHOR
613  
614 SymKat I<E<lt>symkat@symkat.comE<gt>>
615  
616 =head1 COPYRIGHT AND LICENSE
617  
618 This is free software licensed under a I<BSD-Style> License. Please see the
619 LICENSE file included in this package for more detailed information.
620  
621 =head1 AVAILABILITY
622  
623 The latest version of this software is available through GitHub at
624 https://github.com/symkat/webservice-cloudflare-host/
625  
626 =cut