App-MultiModule-Tasks-HTTPClient

 view release on metacpan or  search on metacpan

lib/App/MultiModule/Tasks/HTTPClient.pm  view on Meta::CPAN

package App::MultiModule::Tasks::HTTPClient;
$App::MultiModule::Tasks::HTTPClient::VERSION = '1.161230';
use 5.006;
use strict;
use warnings FATAL => 'all';
use POE qw(Component::Client::HTTP);
use HTTP::Request;
use Modern::Perl;
use Data::Dumper;
use Storable;

use parent 'App::MultiModule::Task';

=head1 NAME

App::MultiModule::Tasks::HTTPClient - Do http/httpds requests in MultiModule

=cut

=head2 message

=cut
{
sub message {
    my $self = shift;
    my $message = shift;
    my %args = @_;
    $self->debug('message', message => $message)
        if $self->{debug} > 5;
    my $url = $message->{http_url};
    my $timeout = $message->{http_timeout} || 30;
    POE::Component::Client::HTTP->spawn(
        Alias     => $url,
        Timeout   => $timeout,
    );
    my $response_handler = sub {
        my ($request_packet, $response_packet) = @_[ARG0, ARG1];
        my $request_object  = $request_packet->[0];
        my $response_object = $response_packet->[0];

        $message->{http_content} = $response_object->content;
        $message->{http_status_line} = $response_object->status_line;
        $message->{http_code} = $response_object->code;
        $message->{http_is_success} = $response_object->is_success;
        $message->{http_is_info} = $response_object->is_info;
        $message->{http_is_redirect} = $response_object->is_redirect;
        $message->{http_is_error} = $response_object->is_error;
        $message->{http_is_server_error} = $response_object->is_server_error;
        $message->{http_is_client_error} = $response_object->is_client_error;
        $message->{http_is_fresh} = $response_object->is_fresh;
        $message->{http_fresh_until} = $response_object->fresh_until;
        $self->emit($message);
    };

    POE::Session->create(
        inline_states => {
            _start => sub {
                POE::Kernel->post(
                    $url,        # posts to the 'ua' alias
                    'request',   # posts to ua's 'request' state
                    'response',  # which of our states will receive the response
                    HTTP::Request->new(GET => $message->{http_url}),# an HTTP::Request object
                );
            },
            _stop => sub {},
            response => $response_handler,
        },
    );
}
}

=head2 set_config

=cut
sub set_config {
    my $self = shift;
    my $config = shift;
    $self->{config} = $config;
}


=head1 AUTHOR

Dana M. Diederich, C<< <dana@realms.org> >>

=head1 BUGS

Please report any bugs or feature requests through L<https://github.com/dana/perl-App-MultiModule-Tasks-HTTPClient/issues>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.525 second using v1.00-cache-2.02-grep-82fe00e-cpan-2c419f77a38b )