File Coverage

blib/lib/Pingdom/Client.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Pingdom::Client;
2             {
3             $Pingdom::Client::VERSION = '0.13';
4             }
5             # ABSTRACT: a perl implementation of a client for the Pingdom REST API.
6              
7 1     1   29478 use 5.010_000;
  1         4  
  1         60  
8 1     1   2209 use mro 'c3';
  1         1372  
  1         152  
9 1     1   222 use feature ':5.10';
  1         8  
  1         230  
10              
11 1     1   972 use Moose;
  0            
  0            
12             use namespace::autoclean;
13              
14             use Carp;
15             use Data::Dumper;
16             use Try::Tiny;
17             use LWP::UserAgent;
18             use JSON;
19             use URI::Escape ();
20              
21             # see http://www.pingdom.com/services/api-documentation-rest/
22              
23             # use autodie;
24             # use MooseX::Params::Validate;
25              
26             has '_json' => (
27             'is' => 'ro',
28             'isa' => 'JSON',
29             'lazy' => 1,
30             'builder' => '_init_json',
31             );
32              
33             has '_ua' => (
34             'is' => 'rw',
35             'isa' => 'LWP::UserAgent',
36             'lazy' => 1,
37             'builder' => '_init_ua',
38             );
39              
40             has 'username' => (
41             'is' => 'rw',
42             'isa' => 'Str',
43             'required' => 1,
44             );
45              
46             has 'password' => (
47             'is' => 'rw',
48             'isa' => 'Str',
49             'required' => 1,
50             );
51              
52             has 'apikey' => (
53             'is' => 'rw',
54             'isa' => 'Str',
55             'required' => 1,
56             );
57              
58             has 'apiurl' => (
59             'is' => 'rw',
60             'isa' => 'Str',
61             'default' => 'https://api.pingdom.com',
62             );
63              
64             has 'apiversion' => (
65             'is' => 'rw',
66             'isa' => 'Str',
67             'default' => '2.0',
68             );
69              
70             has 'lasterror' => (
71             'is' => 'rw',
72             'isa' => 'HashRef',
73             'default' => sub { {} },
74             );
75              
76             sub _init_ua {
77             my $self = shift;
78              
79             my $UA = LWP::UserAgent::->new();
80             $UA->agent('Pingdom::Client/0.01');
81              
82             return $UA;
83             }
84              
85             sub _init_json {
86             my $self = shift;
87              
88             my $JSON = JSON::->new()->utf8();
89              
90             return $JSON;
91             }
92              
93             sub _set_lasterror {
94             my $self = shift;
95             my $code = shift;
96             my $msg = shift;
97             my $longmsg = shift;
98              
99             $self->lasterror()->{'statuscode'} = $code;
100             $self->lasterror()->{'statusdesc'} = $msg;
101             $self->lasterror()->{'errormessage'} = $longmsg;
102              
103             return 1;
104             }
105              
106             sub _validate_params {
107             my $self = shift;
108             my $ref = shift;
109             my $params = shift;
110              
111             foreach my $key (keys %{$params}) {
112             # whine on superflous params
113             if(!$ref->{$key}) {
114             return; # not a valid param for this operation
115             }
116             if(ref($ref->{$key})) {
117             if(ref($ref->{$key}) eq 'Regexp') {
118             if($params->{$key} !~ m/$ref->{$key}/) {
119             # RE didn't match
120             return;
121             }
122             }
123             } else {
124             # match String, Int, Bool
125             if($ref->{$key} eq 'Str') {
126             if($params->{$key} !~ m/^.{1,4096}$/) {
127             return; # no string
128             }
129             } elsif($ref->{$key} eq 'Int') {
130             if($params->{$key} !~ m/^\d+$/) {
131             return; # no int
132             }
133             } elsif($ref->{$key} eq 'Bool') {
134             if($params->{$key} !~ m/^(?:true|false)$/) {
135             return; # no bool
136             }
137             } elsif($ref->{$key} eq 'Ids') {
138             if($params->{$key} !~ m/^(?:\d+,)*\d+$/) {
139             return; # no id list
140             }
141             } elsif($ref->{$key} eq 'Order') {
142             if($params->{$key} !~ m/^(?:ASC|DESC)$/i) {
143             return; # not valid value for order
144             }
145             } elsif($ref->{$key} eq 'Checktype') {
146             if($params->{$key} !~ m/^(?:http|httpcustom|tcp|ping|dns|udp|smtp|pop3|imap)$/) {
147             return; # not a valid type of check
148             }
149             }
150             }
151             }
152              
153             return 1;
154             }
155              
156             sub _api_call {
157             my $self = shift;
158             my $method = shift;
159             my $url = shift;
160             my $params = shift;
161              
162             $method = uc($method);
163             $url = $self->apiurl().'/api/'.$self->apiversion().'/'.$url;
164              
165             my $content = '';
166             if($params && ref($params) eq 'HASH' ) {
167             foreach my $key (keys %{$params}) {
168             my $value = $params->{$key};
169             # urlencode key and value
170             $key = URI::Escape::uri_escape($key);
171             $value = URI::Escape::uri_escape($value);
172             $content .= $key.'='.$value.'&';
173             }
174             if($method eq 'GET') {
175             $url .= '?'.$content;
176             }
177             }
178             my $req = HTTP::Request::->new( $method => $url, );
179             if($method ne 'GET') {
180             $req->content_type('application/x-www-form-urlencoded');
181             $req->content($content);
182             }
183              
184             $req->authorization_basic( $self->username(), $self->password() );
185             $req->header( 'App-Key', $self->apikey() );
186              
187             my $res = $self->_ua()->request($req);
188              
189             if ( $res->is_success() ) {
190             my $result_ref;
191             try {
192             $result_ref = $self->_json()->decode($res->content());
193             1;
194             } catch {
195             my $msg = 'Unable to decode JSON: '.$_;
196             $self->_set_lasterror(901,'JSON-Decode',$msg);
197             carp $msg;
198             };
199             return $result_ref;
200             }
201             else {
202             $self->_set_lasterror($res->code(), $res->message(), $res->content());
203             carp "Request to $url failed: ".$res->code().' '.$res->message().' '.$res->content();
204             return;
205             }
206             }
207              
208             sub actions {
209             my $self = shift;
210             my $params = shift;
211              
212             # Valid params:
213             # from - Int
214             # to - Int
215             # limit - Int (<= 300)
216             # offset - Int
217             # checkids - String
218             # contactids - String
219             # status - String (sent,delivered,error,not_delivered,no_credits)
220             # via - String (email,sms,twitter,iphone,android)
221             my $ref = {
222             'from' => 'Int',
223             'to' => 'Int',
224             'limit' => qr/^[0123]?[0-9]?[0-9]$/,
225             'offset' => 'Int',
226             'checkids' => 'Ids',
227             'contactids' => 'Ids',
228             'status' => qr/^(?:sent|delivered|error|not_delivered|no_credits)$/,
229             'via' => qr/^(?:email|sms|twitter|iphone|android)$/,
230             };
231             if(!$self->_validate_params($ref,$params)) {
232             $self->_set_lasterror(902,'Validate','Failed to validate params for method action');
233             return;
234             }
235              
236             my $method = 'GET';
237             my $url = 'actions';
238              
239             my $result = $self->_api_call($method,$url,$params);
240              
241             return $result;
242             }
243              
244             sub analysis {
245             my $self = shift;
246             my $checkid = shift;
247             my $params = shift;
248              
249             # Valid params:
250             # limit - Int
251             # offset - Int
252             # from - Int
253             # to - Int
254             my $ref = {
255             'from' => 'Int',
256             'to' => 'Int',
257             'limit' => qr/^[0123]?[0-9]?[0-9]$/,
258             'offset' => 'Int',
259             };
260             if(!$self->_validate_params($ref,$params)) {
261             $self->_set_lasterror(902,'Validate','Failed to validate params for method analysis');
262             return;
263             }
264              
265             my $method = 'GET';
266             my $url = 'analysis/'.$checkid;
267              
268             my $result = $self->_api_call($method,$url,$params);
269              
270             return $result;
271             }
272              
273             sub analysis_raw {
274             my $self = shift;
275             my $checkid = shift;
276             my $analysisid = shift;
277              
278             # Valid params:
279             # none
280              
281             my $method = 'GET';
282             my $url = 'analysis/'.$checkid.'/'.$analysisid;
283              
284             my $result = $self->_api_call($method,$url,{});
285              
286             return $result;
287             }
288              
289             sub checks {
290             my $self = shift;
291             my $params = shift;
292              
293             # Valid params:
294             # limit - Int
295             # offset - Int
296             my $ref = {
297             'limit' => qr/^[0123]?[0-9]?[0-9]$/,
298             'offset' => 'Int',
299             };
300             if(!$self->_validate_params($ref,$params)) {
301             $self->_set_lasterror(902,'Validate','Failed to validate params for method checks');
302             return;
303             }
304              
305             my $method = 'GET';
306             my $url = 'checks';
307              
308             my $result = $self->_api_call($method,$url,$params);
309              
310             return $result;
311             }
312              
313             sub check_details {
314             my $self = shift;
315             my $checkid = shift;
316              
317             # Valid params:
318             # none
319              
320             my $method = 'GET';
321             my $url = 'checks/'.$checkid;
322              
323             my $result = $self->_api_call($method,$url,{});
324              
325             return $result;
326             }
327              
328             sub check_create {
329             my $self = shift;
330             my $params = shift;
331              
332             # Valid params:
333             # name - Str
334             # host - Str
335             # type - String (http, httpcustom,tcp,ping,dns,udp,smtp,pop3,imap)
336             # paused - Bool
337             # resolution - Int (1, 5, 15, 30, 60)
338             # contactids - Ints
339             # sendtoemail - Bool
340             # sendtosms - Bool
341             # sendtotwitter - Bool
342             # sendtoiphone - Bool
343             # sendtoandroid - Bool
344             # sendnotificationwhendown - Int
345             # notifyagainevery - Int
346             # notifywhenbackup - Bool
347             # ... (many more)
348             my $ref = {
349             'name' => 'Str',
350             'host' => 'Str',
351             'type' => 'Checktype',
352             'paused' => 'Bool',
353             'resolution' => 'Int',
354             'contactids' => 'Ids',
355             'sendtoemail' => 'Bool',
356             'sendtosms' => 'Bool',
357             'sendtotwitter' => 'Bool',
358             'sendtoiphone' => 'Bool',
359             'sendtoandroid' => 'Bool',
360             'sendnotificationwhendown' => 'Int',
361             'notifyagainevery' => 'Int',
362             'notifywhenbackup' => 'Bool',
363             'url' => 'Str',
364             'encryption' => 'Bool',
365             'port' => 'Int',
366             'auth' => 'Str',
367             'shouldcontain' => 'Str',
368             'shouldnotcontain' => 'Str',
369             'postdata' => 'Str',
370             'additionalurls' => 'Str',
371             'stringtosend' => 'Str',
372             'stringtoexpect' => 'Str',
373             'expectedip' => 'Str',
374             'nameserver' => 'Str',
375             };
376             if(!$self->_validate_params($ref,$params)) {
377             $self->_set_lasterror(902,'Validate','Failed to validate params for method check_create');
378             return;
379             }
380              
381             my $method = 'POST';
382             my $url = 'checks';
383              
384             my $result = $self->_api_call($method,$url,$params);
385              
386             return $result;
387             }
388              
389             sub check_modify {
390             my $self = shift;
391             my $checkid = shift;
392             my $params = shift;
393              
394             # Valid params:
395             # ...
396             my $ref = {
397             'name' => 'Str',
398             'host' => 'Str',
399             'type' => 'Checktype',
400             'paused' => 'Bool',
401             'resolution' => 'Int',
402             'contactids' => 'Ids',
403             'sendtoemail' => 'Bool',
404             'sendtosms' => 'Bool',
405             'sendtotwitter' => 'Bool',
406             'sendtoiphone' => 'Bool',
407             'sendtoandroid' => 'Bool',
408             'sendnotificationwhendown' => 'Int',
409             'notifyagainevery' => 'Int',
410             'notifywhenbackup' => 'Bool',
411             'url' => 'Str',
412             'encryption' => 'Bool',
413             'port' => 'Int',
414             'auth' => 'Str',
415             'shouldcontain' => 'Str',
416             'shouldnotcontain' => 'Str',
417             'postdata' => 'Str',
418             'additionalurls' => 'Str',
419             'stringtosend' => 'Str',
420             'stringtoexpect' => 'Str',
421             'expectedip' => 'Str',
422             'nameserver' => 'Str',
423             };
424             if(!$self->_validate_params($ref,$params)) {
425             $self->_set_lasterror(902,'Validate','Failed to validate params for method check_modify');
426             return;
427             }
428              
429             my $method = 'PUT';
430             my $url = 'checks/'.$checkid;
431              
432             my $result = $self->_api_call($method,$url,$params);
433              
434             return $result;
435             }
436              
437             sub check_modify_bulk {
438             my $self = shift;
439             my $params = shift;
440              
441             # Valid params:
442             # paused - Bool
443             # resolution - Int (1, 5, 15, 30, 60)#
444             # checkids - Str
445             my $ref = {
446             'paused' => 'Bool',
447             'resolution' => qr/^(?:1|5|15|30|60)$/,
448             'checkids' => 'Ids',
449             };
450             if(!$self->_validate_params($ref,$params)) {
451             $self->_set_lasterror(902,'Validate','Failed to validate params for method check_modify_bulk');
452             return;
453             }
454              
455             my $method = 'PUT';
456             my $url = 'checks';
457              
458             my $result = $self->_api_call($method,$url,$params);
459              
460             return $result;
461             }
462              
463             sub check_delete {
464             my $self = shift;
465             my $checkid = shift;
466              
467             # Valid params:
468             # none
469              
470             my $method = 'DELETE';
471             my $url = 'checks/'.$checkid;
472              
473             my $result = $self->_api_call($method,$url,{});
474              
475             return $result;
476             }
477              
478             sub contacts {
479             my $self = shift;
480             my $params = shift;
481              
482             # Valid params:
483             # limit - Int
484             # offset - Int
485             my $ref = {
486             'limit' => qr/^[0123]?[0-9]?[0-9]$/,
487             'offset' => 'Int',
488             };
489             if(!$self->_validate_params($ref,$params)) {
490             $self->_set_lasterror(902,'Validate','Failed to validate params for method contacts');
491             return;
492             }
493              
494             my $method = 'GET';
495             my $url = 'contacts';
496              
497             my $result = $self->_api_call($method,$url,$params);
498              
499             return $result;
500             }
501              
502             sub contact_create {
503             my $self = shift;
504             my $params = shift;
505              
506             # Valid params:
507             # name - Str
508             # email - Str
509             # cellphone - Str
510             # countrycode - Str
511             # countryiso - Str (iso3166)
512             # defaultsmsprovider - String (clickatell,bulksms,esendex,cellsynt)
513             # directtwitter - Bool
514             # twitteruser - Str
515             my $ref = {
516             'name' => 'Str',
517             'email' => 'Str',
518             'cellphone' => 'Str',
519             'countryiso' => 'Str',
520             'countrycode' => 'Str',
521             'defaultsmsprovider' => 'Str',
522             'directtwitter' => 'Bool',
523             'twitteruser' => 'Bool',
524             };
525             if(!$self->_validate_params($ref,$params)) {
526             $self->_set_lasterror(902,'Validate','Failed to validate params for method contact_create');
527             return;
528             }
529              
530             my $method = 'POST';
531             my $url = 'contacts';
532              
533             my $result = $self->_api_call($method,$url,$params);
534              
535             return $result;
536             }
537              
538             sub contact_modify {
539             my $self = shift;
540             my $contact_id = shift;
541             my $params = shift;
542              
543             # Valid params:
544             # name - String
545             # email - String
546             # cellphone - String, excl. countrycode and leading zero
547             # countrycode - String, tel.
548             # countryiso - String, iso3166
549             # defaultsmsprovider - String (clickatell,bulksms,esendex,cellsynt)
550             # paused - Boolean
551             my $ref = {
552             'name' => 'Str',
553             'email' => 'Str',
554             'cellphone' => 'Str',
555             'countryiso' => 'Str',
556             'countrycode' => 'Str',
557             'defaultsmsprovider' => 'Str',
558             'directtwitter' => 'Bool',
559             'twitteruser' => 'Bool',
560             };
561             if(!$self->_validate_params($ref,$params)) {
562             $self->_set_lasterror(902,'Validate','Failed to validate params for method contact_modify');
563             return;
564             }
565              
566             my $method = 'PUT';
567             my $url = 'contacts/'.$contact_id;
568              
569             my $result = $self->_api_call($method,$url,$params);
570              
571             return $result;
572             }
573              
574             sub contact_delete {
575             my $self = shift;
576             my $contactid = shift;
577              
578             # Valid params:
579             # none
580              
581             my $method = 'DELETE';
582             my $url = 'contacts/'.$contactid;
583              
584             my $result = $self->_api_call($method,$url,{});
585              
586             return $result;
587             }
588              
589             sub credits {
590             my $self = shift;
591              
592             # Valid params:
593             # none
594              
595             my $method = 'GET';
596             my $url = 'credits';
597              
598             my $result = $self->_api_call($method,$url,{});
599              
600             return $result;
601             }
602              
603             sub probes {
604             my $self = shift;
605             my $params = shift;
606              
607             # Valid params:
608             # limit - Int
609             # offset - Int
610             # onlyactive - Bool
611             # includedeleted - Bool
612             my $ref = {
613             'onlyactive' => 'Bool',
614             'includedeleted' => 'Bool',
615             'limit' => qr/^[0123]?[0-9]?[0-9]$/,
616             'offset' => 'Int',
617             };
618             if(!$self->_validate_params($ref,$params)) {
619             $self->_set_lasterror(902,'Validate','Failed to validate params for method probes');
620             return;
621             }
622              
623             my $method = 'GET';
624             my $url = 'probes';
625              
626             my $result = $self->_api_call($method,$url,$params);
627              
628             return $result;
629             }
630              
631             sub reference {
632             my $self = shift;
633              
634             # Valid params:
635             # none
636              
637             my $method = 'GET';
638             my $url = 'reference';
639              
640             my $result = $self->_api_call($method,$url,{});
641              
642             return $result;
643             }
644              
645             # Method: Get Email Report Subscription List
646             # Description: Returns a list of email report subscriptions.
647             sub reports_email {
648             my $self = shift;
649              
650             # Valid params:
651             # none
652              
653             my $method = 'GET';
654             my $url = 'reports.email';
655              
656             my $result = $self->_api_call($method,$url,{});
657              
658             return $result;
659             }
660              
661             # Method: Create Email Report
662             # Description: Creates a new email report.
663             sub reports_email_create {
664             my $self = shift;
665             my $params = shift;
666              
667             # Valid params:
668             # name - Str - req!
669             # checkid - Int
670             # frequency - Str (monthly,weekly,daily)
671             # contactids - Str
672             # additionalemails - Str
673             my $ref = {
674             'name' => 'Str',
675             'checkid' => 'Int',
676             'frequency' => qr/^(?:daily|weekly|monthly)$/,
677             'contactids' => 'Ids',
678             'additionalemails' => 'Str',
679             };
680             if(!$self->_validate_params($ref,$params)) {
681             $self->_set_lasterror(902,'Validate','Failed to validate params for method reports_email_create');
682             return;
683             }
684             if(!$params->{'name'}) {
685             return; # required parameter
686             }
687              
688             my $method = 'POST';
689             my $url = 'reports.email';
690              
691             my $result = $self->_api_call($method,$url,$params);
692              
693             return $result;
694             }
695              
696             # Method: Modify Email Report
697             # Description: Modify an email report.
698             sub reports_email_modify {
699             my $self = shift;
700             my $reportid = shift;
701             my $params = shift;
702              
703             # Valid params:
704             # name - Str
705             # checkid - Str
706             # frequency - Str (monthly, weekly, daily)
707             # contactids - Str
708             # additionalemails - Str
709             my $ref = {
710             'name' => 'Str',
711             'checkid' => 'Int',
712             'frequency' => qr/^(?:daily|weekly|monthly)$/,
713             'contactids' => 'Ids',
714             'additionalemails' => 'Str',
715             };
716             if(!$self->_validate_params($ref,$params)) {
717             $self->_set_lasterror(902,'Validate','Failed to validate params for method reports_email_modify');
718             return;
719             }
720              
721             my $method = 'PUT';
722             my $url = 'reports.email/'.$reportid;
723              
724             my $result = $self->_api_call($method,$url,$params);
725              
726             return $result;
727             }
728              
729             # Method: Delete Email Report
730             # Description: Delete an email report.
731             sub reports_email_delete {
732             my $self = shift;
733             my $reportid = shift;
734              
735             # Valid params:
736             # none
737              
738             my $method = 'DELETE';
739             my $url = 'reports.email/'.$reportid;
740              
741             my $result = $self->_api_call($method,$url,{});
742              
743             return $result;
744             }
745              
746             # Method: Get Public Report List
747             # Description: Returns a list of public (web-based) reports.
748             sub reports_public {
749             my $self = shift;
750              
751             # Valid params:
752             # none
753              
754             my $method = 'GET';
755             my $url = 'reports.public';
756              
757             my $result = $self->_api_call($method,$url,{});
758              
759             return $result;
760             }
761              
762             # Method: Publish Public Report
763             # Description: Activate public report for a specified check.
764             sub reports_public_create {
765             my $self = shift;
766             my $checkid = shift;
767              
768             # Valid params:
769             # none
770              
771             my $method = 'PUT';
772             my $url = 'reports.public/'.$checkid;
773              
774             my $result = $self->_api_call($method,$url,{});
775              
776             return $result;
777             }
778              
779             # Method: Withdraw Public Report
780             # Description: Deactivate public report for a specified check.
781             sub reports_public_delete {
782             my $self = shift;
783             my $checkid = shift;
784              
785             # Valid params:
786             # none
787              
788             my $method = 'DELETE';
789             my $url = 'reports.public/'.$checkid;
790              
791             my $result = $self->_api_call($method,$url,{});
792              
793             return $result;
794             }
795              
796             # Method: Get Shared Reports (Banners) List
797             # Description: Returns a list of shared reports (banners).
798             sub reports_shared {
799             my $self = shift;
800              
801             # Valid params:
802             # none
803              
804             my $method = 'GET';
805             my $url = 'reports.shared';
806              
807             my $result = $self->_api_call($method,$url,{});
808              
809             return $result;
810             }
811              
812             # Method: Create Shared Report (Banner)
813             # Description: Create a shared report (banner).
814             sub reports_shared_create {
815             my $self = shift;
816             my $params = shift;
817              
818             # Valid params:
819             # sharedtype - Str - req!
820             # checkid - Int - req!
821             # auto - Bool
822             # fromyear - Int
823             # frommonth - Int
824             # fromday - Int
825             # toyear - Int
826             # tomonth - Int
827             # today - Int
828             # type - String (uptime, response)
829             my $ref = {
830             'sharedtype' => 'Str',
831             'checkid' => 'Int',
832             'auto' => 'Bool',
833             'fromyear' => 'Int',
834             'frommonth' => 'Int',
835             'fromday' => 'Int',
836             'toyear' => 'Int',
837             'tomonth' => 'Int',
838             'today' => 'Int',
839             'type' => qr/^(?:uptime|response)$/,
840             };
841             if(!$self->_validate_params($ref,$params)) {
842             $self->_set_lasterror(902,'Validate','Failed to validate params for method reports_shared_create');
843             return;
844             }
845             if(!$params->{'sharedtype'} || !$params->{'checkid'}) {
846             return; # missing req. params
847             }
848              
849             my $method = 'POST';
850             my $url = 'reports.shared';
851              
852             my $result = $self->_api_call($method,$url,$params);
853              
854             return $result;
855             }
856              
857             # Method: Delete Shared Report (Banner)
858             # Description: Delete a shared report (banner).
859             sub reports_shared_delete {
860             my $self = shift;
861             my $reportid = shift;
862              
863             # Valid params:
864             # none
865              
866             my $method = 'DELETE';
867             my $url = 'reports.shared/'.$reportid;
868              
869             my $result = $self->_api_call($method,$url,{});
870              
871             return $result;
872             }
873              
874             # Method: Get Raw Check Results
875             # Description: Return a list of raw test results for a specified check
876             sub results {
877             my $self = shift;
878             my $checkid = shift;
879             my $params = shift;
880              
881             # Valid params:
882             # to - Int
883             # from - Int
884             # probes - Str
885             # status - Str
886             # limit - Int
887             # offset - Int
888             # includeanalysis - Bool
889             # maxresponse - Int
890             # minresponse - Int
891             my $ref = {
892             'from' => 'Int',
893             'to' => 'Int',
894             'limit' => qr/^[0123]?[0-9]?[0-9]$/,
895             'offset' => 'Int',
896             'probes' => 'Str',
897             'status' => 'Str',
898             'includeanalysis' => 'Bool',
899             'maxresponse' => 'Int',
900             'minresponse' => 'Int',
901             };
902             if(!$self->_validate_params($ref,$params)) {
903             $self->_set_lasterror(902,'Validate','Failed to validate params for method results');
904             return;
905             }
906              
907             my $method = 'GET';
908             my $url = 'results/'.$checkid;
909              
910             my $result = $self->_api_call($method,$url,$params);
911              
912             return $result;
913             }
914              
915             # Method: Get Current Server Time
916             # Description: Get the current time of the API server.
917             sub servertime {
918             my $self = shift;
919              
920             # Valid params:
921             # none
922              
923             my $method = 'GET';
924             my $url = 'servertime';
925              
926             my $result = $self->_api_call($method,$url,{});
927              
928             return $result;
929             }
930              
931             # Method: Get Account Settings
932             # Description: Returns all account-specific settings.
933             sub settings {
934             my $self = shift;
935              
936             # Valid params:
937             # none
938              
939             my $method = 'GET';
940             my $url = 'settings';
941              
942             my $result = $self->_api_call($method,$url,{});
943              
944             return $result;
945             }
946              
947             # Method: Modify Account Settings
948             # Description: Modify account-specific settings.
949             sub settings_modify {
950             my $self = shift;
951             my $params = shift;
952              
953             # Valid params:
954             # firstname - Str
955             # lastname - Str
956             # company - Str
957             # email - Str
958             # cellphone - Str
959             # cellcountrycode - Int
960             # cellcountryiso - Str (iso3166)
961             # phone - Str
962             # phonecountrycode - Int
963             # phonecountryiso - Str (iso3166)
964             # address - Str
965             # address2 - Str
966             # zip - Str
967             # location - Str
968             # state - Str
969             # countryiso - Str (iso3166)
970             # vatcode - Str
971             # autologout - Bool
972             # regionid - Int
973             # timezoneid - Int
974             # datetimeformatid - Int
975             # numberformatid - Int
976             # pubrcustomdesign - Bool
977             # pubrtextcolor - Str
978             # pubrbackgroundcolor - Str
979             # pubrlogourl - Str
980             # pubrmonths - Str (none, all, 3)
981             # pubrshowoverview - Bool
982             # pubrcustomdomain - Bool
983             my $ref = {
984             'firstname' => 'Str',
985             'lastname' => 'Str',
986             'company' => 'Str',
987             'email' => 'Str',
988             'cellphone' => 'Str',
989             'cellcountrycode' => 'Int',
990             'cellcountryiso' => 'Str',
991             'phone' => 'Str',
992             'phonecountrycode' => 'Int',
993             'phonecountryiso' => 'Str',
994             'address' => 'Str',
995             'address2' => 'Str',
996             'zip' => 'Str',
997             'location' => 'Str',
998             'state' => 'Str',
999             'countryiso' => 'Str',
1000             'vatcode' => 'Str',
1001             'autologout' => 'Bool',
1002             'regionid' => 'Int',
1003             'timezoneid' => 'Int',
1004             'datetimeformatid' => 'Int',
1005             'numberformatid' => 'Int',
1006             'pubrcustomdesign' => 'Bool',
1007             'pubrtextcolor' => 'Str',
1008             'pubrbackgroundcolor' => 'Str',
1009             'pubrlogourl' => 'Str',
1010             'pubrmonths' => qr/^(?:none|all|3)$/,
1011             'pubrshowoverview' => 'Bool',
1012             'pubrcustomdomain' => 'Bool',
1013             };
1014             if(!$self->_validate_params($ref,$params)) {
1015             $self->_set_lasterror(902,'Validate','Failed to validate params for method settings modify');
1016             return;
1017             }
1018              
1019             my $method = 'PUT';
1020             my $url = 'settings';
1021              
1022             my $result = $self->_api_call($method,$url,$params);
1023              
1024             return $result;
1025             }
1026              
1027             # Method: Get A Response Time / Uptime Average
1028             # Description: Get a summarized response time / uptime value for a specified check and time period.
1029             sub summary_average {
1030             my $self = shift;
1031             my $checkid = shift;
1032             my $params = shift;
1033              
1034             # Valid params:
1035             # from - Int
1036             # to - Int
1037             # probes - Str
1038             # includeuptime - Bool
1039             # bycountry - Bool
1040             # byprobe - Bool
1041             my $ref = {
1042             'from' => 'Int',
1043             'to' => 'Int',
1044             'probes' => 'Str',
1045             'includeuptime' => 'Bool',
1046             'bycountry' => 'Bool',
1047             'byprobe' => 'Bool',
1048             };
1049             if(!$self->_validate_params($ref,$params)) {
1050             $self->_set_lasterror(902,'Validate','Failed to validate params for method summary_average');
1051             return;
1052             }
1053              
1054             my $method = 'GET';
1055             my $url = 'summary.average/'.$checkid;
1056              
1057             my $result = $self->_api_call($method,$url,$params);
1058              
1059             return $result;
1060             }
1061              
1062             # Method: Get Response Time Averages For Each Hour Of The Day
1063             # Description: Returns the average response time for each hour of the day (0-23) for a specific check over a selected time period. I.e. it shows you what an average day looks like during that time period.
1064             sub summary_hoursofday {
1065             my $self = shift;
1066             my $checkid = shift;
1067             my $params = shift;
1068              
1069             # Valid params:
1070             # from - Int
1071             # to - Int
1072             # probes - Str
1073             # uselocaltime - Bool
1074             my $ref = {
1075             'from' => 'Int',
1076             'to' => 'Int',
1077             'probes' => 'Str',
1078             'uselocaltime' => 'Bool',
1079             };
1080             if(!$self->_validate_params($ref,$params)) {
1081             $self->_set_lasterror(902,'Validate','Failed to validate params for method summary_hoursofday');
1082             return;
1083             }
1084              
1085             my $method = 'GET';
1086             my $url = 'summary.hoursofday/'.$checkid;
1087              
1088             my $result = $self->_api_call($method,$url,$params);
1089              
1090             return $result;
1091             }
1092              
1093             # Method: Get Outages List
1094             # Description: Get a list of status changes for a specified check and time period.
1095             sub summary_outage {
1096             my $self = shift;
1097             my $checkid = shift;
1098             my $params = shift;
1099              
1100             # Valid params:
1101             # from - Int
1102             # to - Int
1103             # order - Str (asc, desc)
1104             my $ref = {
1105             'from' => 'Int',
1106             'to' => 'Int',
1107             'order' => 'Order',
1108             };
1109             if(!$self->_validate_params($ref,$params)) {
1110             $self->_set_lasterror(902,'Validate','Failed to validate params for method summary_outage');
1111             return;
1112             }
1113              
1114             my $method = 'GET';
1115             my $url = 'summary.outage/'.$checkid;
1116              
1117             my $result = $self->_api_call($method,$url,$params);
1118              
1119             return $result;
1120             }
1121              
1122             # Method: Get Intervals Of Average Response Time And Uptime
1123             # Description: Get the average response time and uptime for a list of intervals. Useful for generating graphs.
1124             sub summary_performance {
1125             my $self = shift;
1126             my $checkid = shift;
1127             my $params = shift;
1128              
1129             # Valid params:
1130             # from - Int
1131             # to - Int
1132             # resolution - Str (hour, day, week)
1133             # includeuptime - Bool
1134             # probes - Str
1135             # order - Str (asc, desc)
1136             my $ref = {
1137             'from' => 'Int',
1138             'to' => 'Int',
1139             'resolution' => qr/^(?:hour|day|week)$/i,
1140             'includeuptime' => 'Bool',
1141             'probes' => 'Str',
1142             'order' => 'Order',
1143             };
1144             if(!$self->_validate_params($ref,$params)) {
1145             $self->_set_lasterror(902,'Validate','Failed to validate params for method summary_performance');
1146             return;
1147             }
1148              
1149             my $method = 'GET';
1150             my $url = 'summary.performance/'.$checkid;
1151              
1152             my $result = $self->_api_call($method,$url,$params);
1153              
1154             return $result;
1155             }
1156              
1157             # Method: Get Active Probes For A Period
1158             # Description: Get a list of probes that performed tests for a specified check during a specified period.
1159             sub summary_probes {
1160             my $self = shift;
1161             my $checkid = shift;
1162             my $params = shift;
1163              
1164             # Valid params:
1165             # from - Int - req!
1166             # to - Int
1167             my $ref = {
1168             'from' => 'Int',
1169             'to' => 'Int',
1170             };
1171             if(!$self->_validate_params($ref,$params)) {
1172             $self->_set_lasterror(902,'Validate','Failed to validate params for method summary_probes');
1173             return;
1174             }
1175              
1176             my $method = 'GET';
1177             my $url = 'summary.probes/'.$checkid;
1178              
1179             my $result = $self->_api_call($method,$url,$params);
1180              
1181             return $result;
1182             }
1183              
1184             # Method: Make A Single Test
1185             # Description: Performs a single test using a specified Pingdom probe against a specified target. Please note that this method is meant to be used sparingly, not to set up your own monitoring solution.
1186             sub single {
1187             my $self = shift;
1188             my $params = shift;
1189              
1190             # Valid params:
1191             # host - Str - req!
1192             # type - Str (http, httpcustom, tcp, ping, dns, udp, smtp, pop3, imap) - req!
1193             # probeid - Int
1194             # TODO handle requestheader X ...
1195             my $ref = {
1196             'host' => 'Str',
1197             'type' => 'Checktype',
1198             'probeid' => 'Int',
1199             'url' => 'Str',
1200             'encryption' => 'Bool',
1201             'port' => 'Int',
1202             'auth' => 'Str',
1203             'shouldcontain' => 'Str',
1204             'shouldnotcontain' => 'Str',
1205             'postdata' => 'Str',
1206             'additionalurls' => 'Str',
1207             'stringtosend' => 'Str',
1208             'stringtoexpect' => 'Str',
1209             'expectedip' => 'Str',
1210             'nameserver' => 'Str',
1211             };
1212             if(!$self->_validate_params($ref,$params)) {
1213             $self->_set_lasterror(902,'Validate','Failed to validate params for method single');
1214             return;
1215             }
1216             if(!$params->{'host'} || !$params->{'type'}) {
1217             return; # missing req. arg host
1218             }
1219              
1220             my $method = 'GET';
1221             my $url = 'single';
1222              
1223             my $result = $self->_api_call($method,$url,$params);
1224              
1225             return $result;
1226             }
1227              
1228             # Method: Make A Traceroute
1229             # Description: Perform a traceroute to a specified target from a specified Pingdom probe.
1230             sub traceroute {
1231             my $self = shift;
1232             my $params = shift;
1233              
1234             # Valid params:
1235             # host - Str - req!
1236             # probeid - Int
1237             my $ref = {
1238             'host' => 'Str',
1239             'probeid' => 'Int',
1240             };
1241             if(!$self->_validate_params($ref,$params)) {
1242             $self->_set_lasterror(902,'Validate','Failed to validate params for method traceroute');
1243             return;
1244             }
1245             if(!$params->{'host'}) {
1246             return;
1247             }
1248              
1249             my $method = 'GET';
1250             my $url = 'traceroute';
1251              
1252             my $result = $self->_api_call($method,$url,$params);
1253              
1254             return $result;
1255             }
1256              
1257             no Moose;
1258             __PACKAGE__->meta->make_immutable;
1259              
1260             1;
1261              
1262             __END__
1263              
1264             =pod
1265              
1266             =encoding utf-8
1267              
1268             =head1 NAME
1269              
1270             Pingdom::Client - a perl implementation of a client for the Pingdom REST API.
1271              
1272             =head1 SYNOPSIS
1273              
1274             use Pingdom::Client;
1275             my $API = Pingdom::Client::->new({
1276             'username' => 'user',
1277             'password' => 'pass',
1278             'apikey' => 'key',
1279             });
1280             print $API->contacts();
1281              
1282             =head1 METHODS
1283              
1284             =head2 actions
1285              
1286             Returns a list of actions (alerts) that have been generated for your account.
1287              
1288             =head2 analysis
1289              
1290             Returns a list of the latest error analysis results for a specified check.
1291              
1292             =head2 analysis_raw
1293              
1294             Returns the raw result for a specified error analysis. This data is primarily intended for internal use, but you might be interested in it as well. However, there is no real documentation for this data at the moment. In the future, we may add a new API method that provides a more user-friendly format.
1295              
1296             =head2 check_create
1297              
1298             Creates a new check with settings specified by provided parameters.
1299              
1300             =head2 check_delete
1301              
1302             Deletes a check. THIS METHOD IS IRREVERSIBLE! You will lose all collected data. Be careful!
1303              
1304             =head2 check_details
1305              
1306             Returns a detailed description of a specified check.
1307              
1308             =head2 check_modify
1309              
1310             Modify settings for a check. The provided settings will overwrite previous values. Settings not provided will stay the same as before the update. To clear an existing value, provide an empty value. Please note that you cannot change the type of a check once it has been created.
1311              
1312             =head2 check_modify_bulk
1313              
1314             Pause or change resolution for multiple checks in one bulk call.
1315              
1316             =head2 checks
1317              
1318             Returns a list overview of all checks.
1319              
1320             =head2 contact_create
1321              
1322             Create a new contact.
1323              
1324             =head2 contact_delete
1325              
1326             Deletes a contact.
1327              
1328             =head2 contact_modify
1329              
1330             Modify a contact.
1331              
1332             =head2 contacts
1333              
1334             Returns a list of all contacts.
1335              
1336             =head2 credits
1337              
1338             Returns information about remaining checks, SMS credits and SMS auto-refill status.
1339              
1340             =head2 probes
1341              
1342             Returns a list of all Pingdom probe servers.
1343              
1344             =head2 reference
1345              
1346             Get a reference of regions, timezones and date/time/number formats and their identifiers.
1347              
1348             =head2 reports_email
1349              
1350             Returns a list of email report subscriptions.
1351              
1352             =head2 reports_email_create
1353              
1354             Creates a new email report.
1355              
1356             =head2 reports_email_delete
1357              
1358             Delete an email report.
1359              
1360             =head2 reports_email_modify
1361              
1362             Modify an email report.
1363              
1364             =head2 reports_public
1365              
1366             Returns a list of public (web-based) reports.
1367              
1368             =head2 reports_public_create
1369              
1370             Activate public report for a specified check.
1371              
1372             =head2 reports_public_delete
1373              
1374             Deactivate public report for a specified check.
1375              
1376             =head2 reports_shared
1377              
1378             Returns a list of shared reports (banners).
1379              
1380             =head2 reports_shared_create
1381              
1382             Create a shared report (banner).
1383              
1384             =head2 reports_shared_delete
1385              
1386             Delete a shared report (banner).
1387              
1388             =head2 results
1389              
1390             Return a list of raw test results for a specified check.
1391              
1392             =head2 servertime
1393              
1394             Get the current time of the API server.
1395              
1396             =head2 settings
1397              
1398             Returns all account-specific settings.
1399              
1400             =head2 settings_modify
1401              
1402             Modify account-specific settings.
1403              
1404             =head2 single
1405              
1406             Performs a single test using a specified Pingdom probe against a specified target. Please note that this method is meant to be used sparingly, not to set up your own monitoring solution.
1407              
1408             =head2 summary_average
1409              
1410             Get a summarized response time / uptime value for a specified check and time period.
1411              
1412             =head2 summary_hoursofday
1413              
1414             Returns the average response time for each hour of the day (0-23) for a specific check over a selected time period. I.e. it shows you what an average day looks like during that time period.
1415              
1416             =head2 summary_outage
1417              
1418             Get a list of status changes for a specified check and time period.
1419              
1420             =head2 summary_performance
1421              
1422             Get the average response time and uptime for a list of intervals. Useful for generating graphs.
1423              
1424             =head2 summary_probes
1425              
1426             Get a list of probes that performed tests for a specified check during a specified period.
1427              
1428             =head2 traceroute
1429              
1430             Perform a traceroute to a specified target from a specified Pingdom probe.
1431              
1432             =head1 AUTHOR
1433              
1434             Dominik Schulz <dominik.schulz@gauner.org>
1435              
1436             =head1 COPYRIGHT AND LICENSE
1437              
1438             This software is copyright (c) 2012 by Dominik Schulz.
1439              
1440             This is free software; you can redistribute it and/or modify it under
1441             the same terms as the Perl 5 programming language system itself.
1442              
1443             =cut