App-Sysadmin-Log-Simple

 view release on metacpan or  search on metacpan

lib/App/Sysadmin/Log/Simple/HTTP.pm  view on Meta::CPAN

package App::Sysadmin::Log::Simple::HTTP;
use strict;
use warnings;
# ABSTRACT: a HTTP (maybe RESTful?) logger for App::Sysadmin::Log::Simple
our $VERSION = '0.009'; # VERSION

use Carp;
use HTTP::Tiny;
use URI::Escape qw(uri_escape);

our $HTTP_TIMEOUT = 10;


sub new {
    my $class = shift;
    my %opts  = @_;
    my $app   = $opts{app};

    $app->{http}->{uri} ||= 'http://localhost';
    $app->{http}->{method} ||= 'post';
    $app->{http}->{method} = uc $app->{http}->{method};

    return bless {
        do_http => $app->{do_http},
        http    => $app->{http},
        user    => $app->{user},
    }, $class;
}


sub log {
    my $self     = shift;
    my $logentry = shift;

    return unless $self->{do_http};

    my $ua = HTTP::Tiny->new(
        timeout => $HTTP_TIMEOUT,
        agent   => __PACKAGE__ . '/' . (__PACKAGE__->VERSION ? __PACKAGE__->VERSION : 'dev'),
    );
    my $res = sub {
        if ( $self->{http}->{method} eq 'GET' ) {
            my $params = $ua->www_form_urlencode({
                user => $self->{user},
                log  => $logentry,
            });
            my $uri = $self->{http}->{uri} . "?$params";

            return $ua->get($uri);
        }
        elsif ( $self->{http}->{method} eq 'POST' ) {
            return $ua->post_form($self->{http}->{uri}, {
                user => $self->{user},
                log  => $logentry,
            });
        }
        elsif ( $self->{http}->{method} eq 'PUT' ) {
            return $ua->put($self->{http}->{uri}, {
                user => $self->{user},
                log  => $logentry,
            });
        }
        else {
           croak 'This shouldnt happen, as the method is populated internally. Something bad has happened'
        }
    }->();

    carp sprintf('Failed to http log via %s to %s with code %d and error %s',
		$self->{http}->{method},



( run in 0.744 second using v1.01-cache-2.11-cpan-39bf76dae61 )