| 1 | # vim: ts=4 sts=4 sw=4 et: syntax=perl | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # Copyright (c) 2026 Sven Kirmess | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # Permission to use, copy, modify, and distribute this software for any | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # purpose with or without fee is hereby granted, provided that the above | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # copyright notice and this permission notice appear in all copies. | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||||||||||||||||||||||||||||||||||||||||||
| 14 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||||||||||||||||||||||||||||||||||||||||||
| 15 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 6 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | package WebService::UptimeKuma::Push; | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 6 | our $VERSION = '0.002'; | |||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 6 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 6 | ▶ | use HTTP::Tiny 0.025 (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 6 | ▶ | use JSON::PP (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 6 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 6 | ▶ | use Time::HiRes (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | # WebService::UptimeKuma::Push->new( url => $url, %options ); | ||||||||||||||||||||||||||||||||||||||||||
| 32 | # WebService::UptimeKuma::Push->new( server => $server, token => $token, %options ); | ||||||||||||||||||||||||||||||||||||||||||
| 33 | # options: | ||||||||||||||||||||||||||||||||||||||||||
| 34 | # - fatal (default 0) | ||||||||||||||||||||||||||||||||||||||||||
| 35 | # - http (default HTTP::Tiny object) | ||||||||||||||||||||||||||||||||||||||||||
| 36 | # - timeout (default 10) | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 50 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=23 SCAR=31.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 50 | my ( $class, @args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 50 | ▶ | Carp::croak 'usage: new( url => URL, [ARGS] ) or new( server => SERVER, token => TOKEN, [ARGS] )' if @args % 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 49 | my %args = @args; | |||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 49 | my %args_keys = map { $_ => 1 } keys %args; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 49 | my %self = ( | |||||||||||||||||||||||||||||||||||||||||
| 45 | _fatal => !!0, | ||||||||||||||||||||||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | # url, or server and token | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 49 | ▶ | if ( exists $args{url} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 41 | ▶ | Carp::croak 'Cannot specify url together with server or token' if exists $args{server} || exists $args{token}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $args{"server"} or exists $args{"token"}
MC/DC100%2/2
exists $args{"server"} or exists $args{"token"}
exists $args{"server"}
exists $args{"token"}
Truth table100%3/3
A or B
A= exists $args{"server"}B= exists $args{"token"}
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 39 | my $url = $args{url}; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 39 | ▶ | Carp::croak 'url must be defined' if !defined $url; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | # The push URL shown by Uptime Kuma contains a query string | ||||||||||||||||||||||||||||||||||||||||||
| 56 | # (?status=up&msg=OK&ping=) which we ignore. | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 38 | ▶ | my ( $server, $token ) = "$url" =~ m{ \A ( https? :// [^?#]+? ) /api/push/ ( [^/?#]+ ) /? (?: [?#] .* )? \z }xsmi | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | or Carp::croak "Not an Uptime Kuma push URL: '$url'"; | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 33 | $self{_server} = $server; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 33 | $self{_token} = $token; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 33 | delete $args_keys{url}; | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 8 | for my $arg (qw(server token)) { | |||||||||||||||||||||||||||||||||||||||||
| 66 | 14 | ▶ | Carp::croak "Missing argument: '$arg'" if !defined $args{$arg} || $args{$arg} eq q{}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $args{$arg} or $args{$arg} eq ""
MC/DC100%2/2
not defined $args{$arg} or $args{$arg} eq ""
not defined $args{$arg}
$args{$arg} eq ""
Truth table100%3/3
not(A) or B
A= not defined $args{$arg}B= $args{$arg} eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 4 | my $server = "$args{server}"; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 4 | $server =~ s{ /+ \z }{}xsm; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 4 | ▶ | Carp::croak "server must be a http or https URL: '$args{server}'" if $server !~ m{ \A https? :// [^/?#]+ }xsmi; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 3 | $self{_server} = $server; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 3 | $self{_token} = "$args{token}"; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 3 | delete @args_keys{qw(server token)}; | |||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | # fatal | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 36 | ▶ | if ( exists $args{fatal} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 3 | $self{_fatal} = !!$args{fatal}; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 3 | delete $args_keys{fatal}; | |||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | # http and timeout | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 36 | ▶ | Carp::croak 'Cannot specify http together with timeout' if exists $args{http} && exists $args{timeout}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $args{"http"} and exists $args{"timeout"}
MC/DC100%2/2
exists $args{"http"} and exists $args{"timeout"}
exists $args{"http"}
exists $args{"timeout"}
Truth table100%3/3
A and B
A= exists $args{"http"}B= exists $args{"timeout"}
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 35 | ▶ | if ( exists $args{http} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 12 | my $http = $args{http}; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 12 | ▶ | Carp::croak 'http must be an object with a get method' if !defined Scalar::Util::blessed($http) || !$http->can('get'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined builtin::blessed($http) and $http->can("get")
MC/DC100%2/2
defined builtin::blessed($http) and $http->can("get")
defined builtin::blessed($http)
$http->can("get")
Truth table100%3/3
A and B
A= defined builtin::blessed($http)B= $http->can("get")
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 10 | $self{_http} = $http; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 10 | delete $args_keys{http}; | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 23 | my $timeout = 10; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 23 | ▶ | if ( exists $args{timeout} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 7 | $timeout = $args{timeout}; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 7 | ▶ | Carp::croak 'timeout must be a positive number' if !defined $timeout || $timeout !~ m{ \A [0-9]+ (?: [.] [0-9]+ )? \z }xsm || $timeout <= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not defined $timeout && $timeout =~ / \A [0-9]+ (?: [.] [0-9]+ )? \z /msx or $timeout <= 0
defined $timeout && $timeout =~ / \A [0-9]+ (?: [.] [0-9]+ )? \z /msx
MC/DC100%3/3
not defined $timeout && $timeout =~ / \A [0-9]+ (?: [.] [0-9]+ )? \z /msx or $timeout <= 0
defined $timeout
$timeout =~ / \A [0-9]+ (?: [.] [0-9]+ )? \z /msx
$timeout <= 0
Truth table100%4/4
not(A && B) or C
A= defined $timeoutB= $timeout =~ / \A [0-9]+ (?: [.] [0-9]+ )? \z /msxC= $timeout <= 0
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 2 | delete $args_keys{timeout}; | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 18 | $self{_http} = HTTP::Tiny->new( | |||||||||||||||||||||||||||||||||||||||||
| 103 | agent => "WebService-UptimeKuma-Push/$VERSION ", | ||||||||||||||||||||||||||||||||||||||||||
| 104 | timeout => $timeout, | ||||||||||||||||||||||||||||||||||||||||||
| 105 | verify_SSL => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 106 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | # | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 28 | my @unknown = sort keys %args_keys; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 28 | ▶ | Carp::croak 'Unknown argument' . ( @unknown > 1 ? 's' : q{} ) . q{: '} . join( q{', '}, @unknown ) . q{'} if @unknown; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 26 | return bless \%self, $class; | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 5 | ▶ | sub down { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
down: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 5 | my ( $self, @args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 5 | return $self->_push( 'down', $self->{_fatal}, @args ); | |||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 17 | ▶ | sub error { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 17 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 17 | return $self->{_error}; | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 16 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 16 | my ( $self, $code, @args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 16 | ▶ | Carp::croak 'usage: run( CODE, [ARGS] )' if ref $code ne 'CODE' || @args % 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $code ne "CODE" or @args % 2
MC/DC100%2/2
ref $code ne "CODE" or @args % 2
ref $code ne "CODE"
@args % 2
Truth table100%3/3
A or B
A= ref $code ne "CODE"B= @args % 2
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 14 | my %args = @args; | |||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 14 | my @unknown = sort grep { $_ ne 'msg' } keys %args; | |||||||||||||||||||||||||||||||||||||||||
| 135 | 14 | ▶ | Carp::croak 'Unknown argument' . ( @unknown > 1 ? 's' : q{} ) . q{: '} . join( q{', '}, @unknown ) . q{'} if @unknown; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | 13 | my $wantarray = wantarray; ## no critic (Community::Wantarray) | |||||||||||||||||||||||||||||||||||||||||
| 138 | 13 | my @result; | |||||||||||||||||||||||||||||||||||||||||
| 139 | 13 | my $start = [Time::HiRes::gettimeofday]; | |||||||||||||||||||||||||||||||||||||||||
| 140 | 13 | my $ok; | |||||||||||||||||||||||||||||||||||||||||
| 141 | my $e; | ||||||||||||||||||||||||||||||||||||||||||
| 142 | { | ||||||||||||||||||||||||||||||||||||||||||
| 143 | 13 | local $@; ## no critic (Variables::RequireInitializationForLocalVars) | |||||||||||||||||||||||||||||||||||||||||
| 144 | 13 | $ok = eval { | |||||||||||||||||||||||||||||||||||||||||
| 145 | 13 | ▶ | if ($wantarray) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 1 | @result = $code->(); | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | elsif ( defined $wantarray ) { | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 1 | $result[0] = $code->(); | |||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 11 | $code->(); | |||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 156 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 13 | $e = $@; | |||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | 13 | my $ping = sprintf '%.3f', 1000 * Time::HiRes::tv_interval($start); | |||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | 13 | ▶ | if ( !$ok ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 6 | ▶ | my $msg = defined $e && $e ne q{} ? "$e" : 'Unknown error'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $e and $e ne ""
MC/DC0%0/2
defined $e and $e ne ""
defined $e
$e ne ""
Truth table33%1/3
A and B
A= defined $eB= $e ne ""
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 6 | $msg =~ s{ \s+ \z }{}xsm; | |||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | # The exception from the code is more important than a failed | ||||||||||||||||||||||||||||||||||||||||||
| 166 | # heartbeat, even if fatal is set. | ||||||||||||||||||||||||||||||||||||||||||
| 167 | 6 | $self->_push( 'down', !!0, msg => $msg, ping => $ping ); | |||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | # rethrow the original exception | ||||||||||||||||||||||||||||||||||||||||||
| 170 | 6 | die $e; ## no critic (ErrorHandling::RequireCarping) | |||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | 7 | ▶ | $self->up( ( exists $args{msg} ? ( msg => $args{msg} ) : () ), ping => $ping ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | 6 | ▶ | return @result if $wantarray; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 5 | return $result[0]; | |||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | 5 | ▶ | sub server { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
server: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | 5 | return $self->{_server}; | |||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | 4 | ▶ | sub token { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
token: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | 4 | return $self->{_token}; | |||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | 25 | ▶ | sub up { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
up: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | 25 | my ( $self, @args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | 25 | return $self->_push( 'up', $self->{_fatal}, @args ); | |||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | 36 | ▶ | sub url { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
url: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | 36 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | 36 | return "$self->{_server}/api/push/" . _uri_escape( $self->{_token} ); | |||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | 36 | ▶ | sub _push { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_push: called CC=15 SCAR=27.1
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | 36 | my ( $self, $status, $fatal, @args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | 36 | ▶ | Carp::croak "usage: $status( [ARGS] )" if @args % 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | 35 | my %args = @args; | |||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | 35 | my %args_keys = map { $_ => 1 } keys %args; | |||||||||||||||||||||||||||||||||||||||||
| 210 | 35 | my @query = ( status => $status ); | |||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | # msg | ||||||||||||||||||||||||||||||||||||||||||
| 213 | 35 | ▶ | if ( exists $args{msg} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | 12 | ▶ | if ( defined $args{msg} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | 12 | push @query, msg => "$args{msg}"; | |||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||
| 217 | 12 | delete $args_keys{msg}; | |||||||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | # ping | ||||||||||||||||||||||||||||||||||||||||||
| 221 | 35 | ▶ | if ( exists $args{ping} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | 20 | my $ping = $args{ping}; | |||||||||||||||||||||||||||||||||||||||||
| 223 | 20 | ▶ | if ( defined $ping ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 19 | ▶ | Carp::croak "ping must be a non-negative number: '$ping'" if $ping !~ m{ \A [0-9]+ (?: [.] [0-9]+ )? \z }xsm; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | 16 | push @query, ping => $ping; | |||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | 17 | delete $args_keys{ping}; | |||||||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | # | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 32 | my @unknown = sort keys %args_keys; | |||||||||||||||||||||||||||||||||||||||||
| 232 | 32 | ▶ | Carp::croak 'Unknown argument' . ( @unknown > 1 ? 's' : q{} ) . q{: '} . join( q{', '}, @unknown ) . q{'} if @unknown; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | 30 | my @query_string; | |||||||||||||||||||||||||||||||||||||||||
| 235 | 30 | ▶ | while ( my ( $key, $value ) = splice @query, 0, 2 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | 58 | push @query_string, "$key=" . _uri_escape($value); | |||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||
| 238 | 30 | my $url = $self->url . q{?} . join q{&}, @query_string; | |||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | 30 | delete $self->{_error}; | |||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | 30 | my $error; | |||||||||||||||||||||||||||||||||||||||||
| 243 | { | ||||||||||||||||||||||||||||||||||||||||||
| 244 | 30 | local $@; ## no critic (Variables::RequireInitializationForLocalVars) | |||||||||||||||||||||||||||||||||||||||||
| 245 | 30 | my $response = eval { $self->{_http}->get($url) }; | |||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | 30 | ▶ | if ( !defined $response ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | 1 | ▶ | $error = defined $@ && $@ ne q{} ? qq{$@} : 'HTTP request failed'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $@ and $@ ne ""
MC/DC0%0/2
defined $@ and $@ ne ""
defined $@
$@ ne ""
Truth table33%1/3
A and B
A= defined $@B= $@ ne ""
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | 1 | $error =~ s{ \s+ \z }{}xsm; | |||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||
| 251 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 29 | $error = _check_response($response); | |||||||||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | 30 | ▶ | return !!1 if !defined $error; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | 10 | $self->{_error} = $error; | |||||||||||||||||||||||||||||||||||||||||
| 259 | 10 | ▶ | Carp::croak "Cannot push status '$status' to Uptime Kuma: $error" if $fatal; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | 8 | return !!0; | |||||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | 29 | ▶ | sub _check_response { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_response: called CC=17 SCAR=31.2
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | 29 | my ($response) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | 29 | ▶ | return 'Invalid response from HTTP client' if !defined Scalar::Util::reftype($response) || Scalar::Util::reftype($response) ne 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined builtin::reftype($response) or builtin::reftype($response) ne "HASH"
MC/DC0%0/2
not defined builtin::reftype($response) or builtin::reftype($response) ne "HASH"
not defined builtin::reftype($response)
builtin::reftype($response) ne "HASH"
Truth table33%1/3
not(A) or B
A= not defined builtin::reftype($response)B= builtin::reftype($response) ne "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | 29 | ▶ | my $content = defined $response->{content} ? $response->{content} : q{}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | # HTTP::Tiny uses status 599 for internal errors (e.g. connection | ||||||||||||||||||||||||||||||||||||||||||
| 272 | # failed) with the error message as content | ||||||||||||||||||||||||||||||||||||||||||
| 273 | 29 | ▶ | if ( defined $response->{status} && $response->{status} eq '599' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $response->{"status"} and $response->{"status"} eq 599
MC/DC50%1/2
defined $response->{"status"} and $response->{"status"} eq 599
defined $response->{"status"}
$response->{"status"} eq 599
Truth table66%2/3
A and B
A= defined $response->{"status"}B= $response->{"status"} eq 599
| |||||||||||||||||||||||||||||||||||||||||||
| 274 | 1 | $content =~ s{ \s+ \z }{}xsm; | |||||||||||||||||||||||||||||||||||||||||
| 275 | 1 | ▶ | return $content ne q{} ? $content : 'HTTP request failed'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | 28 | my $json = eval { JSON::PP->new->decode($content) }; | |||||||||||||||||||||||||||||||||||||||||
| 279 | 28 | ▶ | if ( defined Scalar::Util::reftype($json) && Scalar::Util::reftype($json) eq 'HASH' && exists $json->{ok} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined builtin::reftype($json) and builtin::reftype($json) eq "HASH" and exists $json->{"ok"}
defined builtin::reftype($json) and builtin::reftype($json) eq "HASH"
MC/DC33%1/3
defined builtin::reftype($json) and builtin::reftype($json) eq "HASH" and exists $json->{"ok"}
defined builtin::reftype($json)
builtin::reftype($json) eq "HASH"
exists $json->{"ok"}
Truth table50%2/4
A and B and C
A= defined builtin::reftype($json)B= builtin::reftype($json) eq "HASH"C= exists $json->{"ok"}
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | 26 | ▶ | return if $json->{ok} && $response->{success}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$json->{"ok"} and $response->{"success"}
MC/DC50%1/2
$json->{"ok"} and $response->{"success"}
$json->{"ok"}
$response->{"success"}
Truth table66%2/3
A and B
A= $json->{"ok"}B= $response->{"success"}
| |||||||||||||||||||||||||||||||||||||||||||
| 281 | 6 | ▶ | return $json->{msg} if defined $json->{msg} && $json->{msg} ne q{}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $json->{"msg"} and $json->{"msg"} ne ""
MC/DC0%0/2
defined $json->{"msg"} and $json->{"msg"} ne ""
defined $json->{"msg"}
$json->{"msg"} ne ""
Truth table33%1/3
A and B
A= defined $json->{"msg"}B= $json->{"msg"} ne ""
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | 2 | ▶ | my $status = defined $response->{status} ? $response->{status} : q{???}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 285 | 2 | ▶ | my $reason = defined $response->{reason} ? $response->{reason} : q{}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 286 | 2 | ▶ | return $response->{success} ? "Unexpected response from Uptime Kuma: $status $reason" : "$status $reason"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | 94 | ▶ | sub _uri_escape { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_uri_escape: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 290 | 94 | my ($value) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | 94 | utf8::encode($value); | |||||||||||||||||||||||||||||||||||||||||
| 293 | 94 | $value =~ s{ ( [^A-Za-z0-9\-._~] ) }{ sprintf '%%%02X', ord $1 }xsmge; | |||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | 94 | return $value; | |||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||
| 300 | __END__ | ||||||||||||||||||||||||||||||||||||||||||