| 1 | package App::Sysadmin::Log::Simple::HTTP; | ||||||||||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | # ABSTRACT: a HTTP (maybe RESTful?) logger for App::Sysadmin::Log::Simple | ||||||||||||||||||
| 5 | 4 | our $VERSION = '0.009'; # VERSION | |||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | 4 | ▶ | use Carp; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 4 | ▶ | use HTTP::Tiny; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | 4 | ▶ | use URI::Escape qw(uri_escape); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | 4 | our $HTTP_TIMEOUT = 10; | |||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | |||||||||||||||||||
| 14 | 3 | ▶ | sub new { | ||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||
| 15 | 3 | my $class = shift; | |||||||||||||||||
| 16 | 3 | my %opts = @_; | |||||||||||||||||
| 17 | 3 | my $app = $opts{app}; | |||||||||||||||||
| 18 | |||||||||||||||||||
| 19 | 3 | ▶ | $app->{http}->{uri} ||= 'http://localhost'; | ||||||||||||||||
|
Condition50%1/2
$app->{"http"}{"uri"} ||= "http://localhost"
MC/DC0%0/1
$app->{"http"}{"uri"} ||= "http://localhost"
$app->{"http"}{"uri"}
Truth table50%1/2
A
A= $app->{"http"}{"uri"}
| |||||||||||||||||||
| 20 | 3 | ▶ | $app->{http}->{method} ||= 'post'; | ||||||||||||||||
|
Condition50%1/2
$app->{"http"}{"method"} ||= "post"
MC/DC0%0/1
$app->{"http"}{"method"} ||= "post"
$app->{"http"}{"method"}
Truth table50%1/2
A
A= $app->{"http"}{"method"}
| |||||||||||||||||||
| 21 | 3 | $app->{http}->{method} = uc $app->{http}->{method}; | |||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | return bless { | ||||||||||||||||||
| 24 | do_http => $app->{do_http}, | ||||||||||||||||||
| 25 | http => $app->{http}, | ||||||||||||||||||
| 26 | user => $app->{user}, | ||||||||||||||||||
| 27 | 3 | }, $class; | |||||||||||||||||
| 28 | } | ||||||||||||||||||
| 29 | |||||||||||||||||||
| 30 | |||||||||||||||||||
| 31 | 0 | ▶ | sub log { | ||||||||||||||||
|
Subroutine
log: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||
| 32 | 0 | my $self = shift; | |||||||||||||||||
| 33 | 0 | my $logentry = shift; | |||||||||||||||||
| 34 | |||||||||||||||||||
| 35 | 0 | ▶ | return unless $self->{do_http}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 36 | |||||||||||||||||||
| 37 | 0 | ▶ | my $ua = HTTP::Tiny->new( | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 38 | timeout => $HTTP_TIMEOUT, | ||||||||||||||||||
| 39 | agent => __PACKAGE__ . '/' . (__PACKAGE__->VERSION ? __PACKAGE__->VERSION : 'dev'), | ||||||||||||||||||
| 40 | ); | ||||||||||||||||||
| 41 | 0 | ▶ | my $res = sub { | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||
| 42 | 0 | ▶ | if ( $self->{http}->{method} eq 'GET' ) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 43 | my $params = $ua->www_form_urlencode({ | ||||||||||||||||||
| 44 | user => $self->{user}, | ||||||||||||||||||
| 45 | 0 | log => $logentry, | |||||||||||||||||
| 46 | }); | ||||||||||||||||||
| 47 | 0 | my $uri = $self->{http}->{uri} . "?$params"; | |||||||||||||||||
| 48 | |||||||||||||||||||
| 49 | 0 | return $ua->get($uri); | |||||||||||||||||
| 50 | } | ||||||||||||||||||
| 51 | elsif ( $self->{http}->{method} eq 'POST' ) { | ||||||||||||||||||
| 52 | return $ua->post_form($self->{http}->{uri}, { | ||||||||||||||||||
| 53 | user => $self->{user}, | ||||||||||||||||||
| 54 | 0 | log => $logentry, | |||||||||||||||||
| 55 | }); | ||||||||||||||||||
| 56 | } | ||||||||||||||||||
| 57 | elsif ( $self->{http}->{method} eq 'PUT' ) { | ||||||||||||||||||
| 58 | return $ua->put($self->{http}->{uri}, { | ||||||||||||||||||
| 59 | user => $self->{user}, | ||||||||||||||||||
| 60 | 0 | log => $logentry, | |||||||||||||||||
| 61 | }); | ||||||||||||||||||
| 62 | } | ||||||||||||||||||
| 63 | else { | ||||||||||||||||||
| 64 | 0 | croak 'This shouldnt happen, as the method is populated internally. Something bad has happened' | |||||||||||||||||
| 65 | } | ||||||||||||||||||
| 66 | 0 | }->(); | |||||||||||||||||
| 67 | |||||||||||||||||||
| 68 | carp sprintf('Failed to http log via %s to %s with code %d and error %s', | ||||||||||||||||||
| 69 | $self->{http}->{method}, | ||||||||||||||||||
| 70 | $self->{http}->{uri}, | ||||||||||||||||||
| 71 | $res->{status}, | ||||||||||||||||||
| 72 | $res->{reason}, | ||||||||||||||||||
| 73 | 0 | ▶ | ) unless $res->{success}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 74 | |||||||||||||||||||
| 75 | 0 | return "Logged to $self->{http}->{uri} via $self->{http}->{method}" | |||||||||||||||||
| 76 | } | ||||||||||||||||||
| 77 | |||||||||||||||||||
| 78 | 4 | 1; | |||||||||||||||||
| 79 | |||||||||||||||||||
| 80 | __END__ | ||||||||||||||||||