File Coverage

blib/lib/Healthchecks.pm
Criterion Covered Total %
statement 15 95 15.7
branch 0 26 0.0
condition 0 10 0.0
subroutine 5 16 31.2
pod 10 10 100.0
total 30 157 19.1


line stmt bran cond sub pod time code
1             # vim:set sw=4 ts=4 sts=4 ft=perl expandtab:
2             package Healthchecks;
3             # ABSTRACT: Interact with Healthchecks API
4 1     1   1475 use Mojo::Base -base, -signatures;
  1         215819  
  1         8  
5              
6 1     1   4910 use Mojo::UserAgent;
  1         269525  
  1         22  
7 1     1   49 use Mojo::JSON qw(decode_json encode_json);
  1         2  
  1         53  
8 1     1   6 use Carp qw(carp);
  1         2  
  1         44  
9 1     1   5 use Data::Dumper;
  1         2  
  1         1353  
10              
11             our $VERSION = "0.02";
12              
13             has 'url';
14             has 'apikey';
15             has 'user';
16             has 'password';
17             has 'proxy';
18             has 'ua' => sub { Mojo::UserAgent->new; };
19              
20             =encoding utf-8
21              
22             =head1 SYNOPSIS
23              
24             use Healthchecks;
25             my $hc = Healthchecks->new(
26             url => 'http://hc.example.org',
27             apikey => 'secret_healthchecks_API_key',
28             user => 'http_user',
29             password => 'http_password',
30             proxy => {
31             http => 'http://proxy.example.org',
32             https => 'http://proxy.example.org'
33             }
34             );
35              
36             $hc->get_check('uuid_or_unique_key');
37              
38             =head1 DESCRIPTION
39              
40             Client module for L L.
41              
42             =head1 ATTRIBUTES
43              
44             L implements the following attributes.
45              
46             =head2 url
47              
48             my $url = $hc->url;
49             $hc = $hc->url('http://hc.example.org');
50              
51             MANDATORY. The Healthchecks URL, no default.
52              
53             =head2 apikey
54              
55             my $apikey = $hc->apikey;
56             $hc = $hc->apikey('secret_etherpad_API_key');
57              
58             MANDATORY. Secret API key, no default
59              
60             =head2 ua
61              
62             my $ua = $hc->ua;
63             $hc = $hc->ua(Mojo::UserAgent->new);
64              
65             OPTIONAL. User agent, default to a Mojo::UserAgent. Please, don't use anything other than a Mojo::Useragent.
66              
67             =head2 user
68              
69             my $user = $hc->user;
70             $hc = $hc->user('bender');
71              
72             OPTIONAL. HTTP user, use it if your Healthchecks is protected by a HTTP authentication, no default.
73              
74             =head2 password
75              
76             my $password = $hc->password;
77             $hc = $hc->password('beer');
78              
79             OPTIONAL. HTTP password, use it if your Healthchecks is protected by a HTTP authentication, no default.
80              
81             =head2 proxy
82              
83             my $proxy = $hc->proxy;
84             $hc = $hc->proxy({
85             http => 'http://proxy.example.org',
86             https => 'http://proxy.example.org'
87             });
88              
89             OPTIONAL. Proxy settings. If set to { detect => 1 }, Healthchecks will check environment variables HTTP_PROXY, http_proxy, HTTPS_PROXY, https_proxy, NO_PROXY and no_proxy for proxy information. No default.
90              
91             =cut
92              
93             sub _execute {
94 0     0     my $c = shift;
95 0           my $args = shift;
96              
97 0 0         if (defined $c->proxy) {
98 0 0         if ($c->proxy->{detect}) {
99 0           $c->ua->proxy->detect;
100             } else {
101 0 0         $c->ua->proxy->http($c->proxy->{http}) if defined $c->proxy->{http};
102 0 0         $c->ua->proxy->http($c->proxy->{https}) if defined $c->proxy->{https};
103             }
104             }
105              
106 0   0       my $url = Mojo::URL->new($args->{url} // $c->url);
107 0 0 0       $url->userinfo($c->user.':'.$c->password) if defined $c->user && defined $c->password;
108              
109 0           my $path = $url->path;
110 0           $path =~ s#/$##;
111 0 0         $url->path($path.'/api/'.$args->{api}) unless $args->{url};
112              
113 0   0       $url->query($args->{query} // {});
114              
115 0   0       my $method = $args->{method} // 'get';
116              
117 0           my $res;
118 0 0         if (defined $args->{data}) {
119 0           $res = $c->ua->$method($url => { 'X-Api-Key' => $c->apikey } => json => $args->{data})->result;
120             } else {
121 0           $res = $c->ua->$method($url => { 'X-Api-Key' => $c->apikey })->result;
122             }
123              
124 0 0         return $res->is_success if $args->{success};
125              
126 0 0         if ($res->is_success) {
127             # Can’t use $res->json when json is too large
128 0           my $json = decode_json($res->body);
129 0           my $data;
130 0 0         if (defined $args->{key}) {
131 0 0         $data = (ref($json) eq 'HASH') ? $json->{$args->{key}} : $json;
132             } else {
133 0           $data = $json;
134             }
135              
136 0 0         return (wantarray) ? @{$data}: $data if ref($data) eq 'ARRAY';
  0 0          
137 0           return $data;
138             } else {
139 0           carp Dumper $res->message;
140 0           return undef;
141             }
142             }
143              
144             =head1 METHODS
145              
146             Healthchecks inherits all methods from Mojo::Base and implements the following new ones.
147              
148             =cut
149              
150             #################### subroutine header begin ####################
151              
152             =head3 get_all_checks
153              
154             Usage : $hc->get_all_checks();
155             Purpose : Get all checks
156             Returns : An array of checks belonging to the user, optionally filtered by one or more tags.
157             Argument : None
158             See : https://healthchecks.io/docs/api/#list-checks
159              
160             =cut
161              
162             #################### subroutine header end ####################
163              
164 0     0 1   sub get_all_checks($c) {
  0            
  0            
165 0           return $c->_execute({
166             api => 'v1/checks/',
167             key => 'checks'
168             });
169             }
170              
171             #################### subroutine header begin ####################
172              
173             =head3 get_check
174              
175             Usage : $hc->get_check('uuid_or_unique_key');
176             Purpose : Get details of a check
177             Returns : A hash, representation of a single check.
178             Argument : Accepts either check's UUID or the unique_key (a field derived from UUID and returned by API responses when using the read-only API key) as argument.
179             MANDATORY
180             See : https://healthchecks.io/docs/api/#get-check
181              
182             =cut
183              
184             #################### subroutine header end ####################
185              
186 0     0 1   sub get_check($c, $uuid) {
  0            
  0            
  0            
187 0           return $c->_execute({
188             api => 'v1/checks/'.$uuid
189             });
190             }
191              
192             #################### subroutine header begin ####################
193              
194             =head3 create_check
195              
196             Usage : $hc->({name => 'foobarbaz'});
197             Purpose : Create a check
198             Returns : A hash, representation of a single check.
199             Argument : A hash of the check’s options (see API documentation)
200             OPTIONAL
201             See : https://healthchecks.io/docs/api/#create-check
202              
203             =cut
204              
205             #################### subroutine header end ####################
206              
207 0     0 1   sub create_check($c, $data){
  0            
  0            
  0            
208 0           return $c->_execute({
209             api => 'v1/checks/',
210             method => 'post',
211             data => $data
212             });
213             }
214              
215             #################### subroutine header begin ####################
216              
217             =head3 update_check
218              
219             Usage : $hc->update_check('uuid', { name => 'quux' });
220             Purpose : Update the configuration of a check
221             Returns : A hash, representation of a single check.
222             Argument : The check's UUID (MANDATORY) and a hash of the check’s options (see API documentation)
223             OPTIONAL
224             See : https://healthchecks.io/docs/api/#update-check
225              
226             =cut
227              
228             #################### subroutine header end ####################
229              
230 0     0 1   sub update_check($c, $uuid, $data){
  0            
  0            
  0            
  0            
231 0           return $c->_execute({
232             api => 'v1/checks/'.$uuid,
233             method => 'post',
234             data => $data
235             });
236             }
237              
238             #################### subroutine header begin ####################
239              
240             =head3 pause_check
241              
242             Usage : $hc->pause_check('uuid');
243             Purpose : Disables monitoring for a check without removing it. The check goes into a "paused" state. You can resume monitoring of the check by pinging it.
244             Returns : A boolean : true if the check is paused, false otherwise.
245             Argument : The check's UUID
246             MANDATORY
247             See : https://healthchecks.io/docs/api/#pause-check
248              
249             =cut
250              
251             #################### subroutine header end ####################
252              
253 0     0 1   sub pause_check($c, $uuid){
  0            
  0            
  0            
254 0           return $c->_execute({
255             api => 'v1/checks/'.$uuid.'/pause',
256             method => 'post',
257             key => 'status'
258             }) eq 'paused';
259             }
260              
261             #################### subroutine header begin ####################
262              
263             =head3 delete_check
264              
265             Usage : $hc->delete_check('uuid');
266             Purpose : Permanently deletes the check from the user's account.
267             Returns : A boolean : true if the check has been successfully deleted, false otherwise.
268             Argument : The check's UUID
269             MANDATORY
270             See : https://healthchecks.io/docs/api/#delete-check
271              
272             =cut
273              
274             #################### subroutine header end ####################
275              
276 0     0 1   sub delete_check($c, $uuid){
  0            
  0            
  0            
277 0           return $c->_execute({
278             api => 'v1/checks/'.$uuid,
279             method => 'delete',
280             success => 1
281             });
282             }
283              
284             #################### subroutine header begin ####################
285              
286             =head3 get_check_pings
287              
288             Usage : $hc->get_check_pings('uuid');
289             Purpose : Get the pings of a check.
290             Returns : An array of pings this check has received.
291             Argument : The check's UUID
292             MANDATORY
293             See : https://healthchecks.io/docs/api/#list-pings
294              
295             =cut
296              
297             #################### subroutine header end ####################
298              
299 0     0 1   sub get_check_pings($c, $uuid){
  0            
  0            
  0            
300 0           return $c->_execute({
301             api => 'v1/checks/'.$uuid.'/pings/',
302             key => 'pings'
303             });
304             }
305              
306             #################### subroutine header begin ####################
307              
308             =head3 get_check_flips
309              
310             Usage : $hc->get_check_flips('uuid_or_unique_key', { seconds => 3, start => 1592214380, end => 1592217980});
311             Purpose : Get the "flips" of a check has experienced.
312             Returns : An array of the "flips" the check has experienced. A flip is a change of status (from "down" to "up," or from "up" to "down").
313             Argument : Accepts either check's UUID or the unique_key (a field derived from UUID and returned by API responses when using the read-only API key) as argument.
314             MANDATORY
315             You can specify an optional hash table to add parameters as query string (see API documentation)
316             See : https://healthchecks.io/docs/api/#list-flips
317              
318             =cut
319              
320             #################### subroutine header end ####################
321              
322 0     0 1   sub get_check_flips($c, $uuid, $query){
  0            
  0            
  0            
  0            
323 0           return $c->_execute({
324             api => 'v1/checks/'.$uuid.'/flips/',
325             key => 'flips',
326             query => $query
327             });
328             }
329              
330             #################### subroutine header begin ####################
331              
332             =head3 get_integrations
333              
334             Usage : $hc->get_all_checks();
335             Purpose : Get a list of existing integrations
336             Returns : An array of integrations belonging to the project.
337             Argument : None
338             See : https://healthchecks.io/docs/api/#list-channels
339              
340             =cut
341              
342             #################### subroutine header end ####################
343              
344 0     0 1   sub get_integrations($c){
  0            
  0            
345 0           return $c->_execute({
346             api => 'api/v1/channels/',
347             key => 'channels'
348             });
349             }
350              
351             #################### subroutine header begin ####################
352              
353             =head3 ping_check
354              
355             Usage : $hc->ping_check('uuid');
356             Purpose : Ping a check
357             Returns : A boolean : true if the check has been successfully pinged, false otherwise.
358             Argument : The check's UUID
359             MANDATORY
360             See : This is not part of the Healthchecks API but a facility offered by this module
361              
362             =cut
363              
364             #################### subroutine header end ####################
365              
366 0     0 1   sub ping_check($c, $uuid){
  0            
  0            
  0            
367 0           return $c->_execute({
368             url => $c->_execute({
369             api => 'v1/checks/'.$uuid,
370             key => 'ping_url'
371             }),
372             success => 1
373             });
374             }
375              
376             1;
377              
378             __END__