Healthchecks

 view release on metacpan or  search on metacpan

lib/Healthchecks.pm  view on Meta::CPAN

    password => 'http_password',
    proxy    => {
        http  => 'http://proxy.example.org',
        https => 'http://proxy.example.org'
    }
  );

  $hc->get_check('uuid_or_unique_key');

=head1 DESCRIPTION

Client module for L<Healthchecks|https://healthchecks.io/> L<HTTP API|https://healthchecks.io/docs/api/>.

=head1 ATTRIBUTES

L<Healthchecks> implements the following attributes.

=head2 url

  my $url = $hc->url;
  $hc     = $hc->url('http://hc.example.org');

MANDATORY. The Healthchecks URL, no default.

=head2 apikey

  my $apikey = $hc->apikey;
  $hc        = $hc->apikey('secret_etherpad_API_key');

MANDATORY. Secret API key, no default

=head2 ua

  my $ua = $hc->ua;
  $hc    = $hc->ua(Mojo::UserAgent->new);

OPTIONAL. User agent, default to a Mojo::UserAgent. Please, don't use anything other than a Mojo::Useragent.

=head2 user

  my $user = $hc->user;
  $hc      = $hc->user('bender');

OPTIONAL. HTTP user, use it if your Healthchecks is protected by a HTTP authentication, no default.

=head2 password

  my $password = $hc->password;
  $hc          = $hc->password('beer');

OPTIONAL. HTTP password, use it if your Healthchecks is protected by a HTTP authentication, no default.

=head2 proxy

  my $proxy = $hc->proxy;
  $hc       = $hc->proxy({
    http  => 'http://proxy.example.org',
    https => 'http://proxy.example.org'
  });

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.

=cut

sub _execute {
    my $c    = shift;
    my $args = shift;

    if (defined $c->proxy) {
        if ($c->proxy->{detect}) {
            $c->ua->proxy->detect;
        } else {
            $c->ua->proxy->http($c->proxy->{http})  if defined $c->proxy->{http};
            $c->ua->proxy->http($c->proxy->{https}) if defined $c->proxy->{https};
        }
    }

    my $url = Mojo::URL->new($args->{url} // $c->url);
    $url->userinfo($c->user.':'.$c->password) if defined $c->user && defined $c->password;

    my $path = $url->path;
    $path =~ s#/$##;
    $url->path($path.'/api/'.$args->{api}) unless $args->{url};

    $url->query($args->{query} // {});

    my $method = $args->{method} // 'get';

    my $res;
    if (defined $args->{data}) {
        $res = $c->ua->$method($url => { 'X-Api-Key' => $c->apikey } => json => $args->{data})->result;
    } else {
        $res = $c->ua->$method($url => { 'X-Api-Key' => $c->apikey })->result;
    }

    return $res->is_success if $args->{success};

    if ($res->is_success) {
        # Can’t use $res->json when json is too large
        my $json = decode_json($res->body);
        my $data;
        if (defined $args->{key}) {
            $data = (ref($json) eq 'HASH') ? $json->{$args->{key}} : $json;
        } else {
            $data = $json;
        }

        return (wantarray) ? @{$data}: $data if ref($data) eq 'ARRAY';
        return $data;
    } else {
        carp Dumper $res->message;
        return undef;
    }
}

=head1 METHODS

Healthchecks inherits all methods from Mojo::Base and implements the following new ones.

=cut



( run in 0.469 second using v1.01-cache-2.11-cpan-71847e10f99 )