Bot-Cobalt
view release on metacpan or search on metacpan
lib/Bot/Cobalt/Plugin/WWW.pm view on Meta::CPAN
my $response = $resp_pk->[0];
my $tag = $req_pk->[1];
my $this_req = delete $self->Requests->{$tag};
return unless $this_req;
my $event = $this_req->{Event};
my $args = $this_req->{Args};
core->log->debug("ht_response dispatch: $event ($tag)");
my $content = $response->is_success ?
$response->decoded_content
: $response->message;
broadcast($event, $content, $response, $args);
}
sub _start {
my ($self, $kernel) = @_[OBJECT, KERNEL];
my $sess_alias = 'www_'. core()->get_plugin_alias($self);
$kernel->alias_set( $sess_alias );
my %opts;
$opts{BindAddr} = $self->bindaddr if $self->bindaddr;
$opts{Proxy} = $self->proxy if $self->proxy;
$opts{Timeout} = $self->timeout;
## Create "ht_${plugin_alias}" session
POE::Component::Client::HTTP->spawn(
FollowRedirects => 5,
Agent => __PACKAGE__,
Alias => 'ht_'. core()->get_plugin_alias($self),
ConnectionManager => POE::Component::Client::Keepalive->new(
keep_alive => 1,
max_per_host => $self->max_per_host,
max_open => $self->max_workers,
timeout => $self->timeout,
),
%opts,
);
core()->Provided->{www_request} = __PACKAGE__ ;
}
1;
__END__
=pod
=head1 NAME
Bot::Cobalt::Plugin::WWW - Asynchronous HTTP requests from Cobalt plugins
=head1 SYNOPSIS
## Send your request, specify an event to handle response:
use HTTP::Request;
my $request = HTTP::Request->new(
'GET',
'http://www.cobaltirc.org'
);
broadcast( 'www_request',
$request,
'myplugin_resp_recv',
[ $some, $args ]
);
## Handle the response:
sub Bot_myplugin_resp_recv {
my ($self, $core) = splice @_, 0, 2;
## Content:
my $content = ${ $_[0] };
## HTTP::Response object:
my $response = ${ $_[1] };
## Attached arguments array reference:
my $args_arr = ${ $_[2] };
return PLUGIN_EAT_ALL
}
=head1 DESCRIPTION
This plugin provides an easy interface to asynchronous HTTP requests; it
bridges Cobalt's plugin pipeline and L<POE::Component::Client::HTTP> to
provide responses to B<Bot_www_request> events.
The request should be a L<HTTP::Request> object.
Inside the response handler, $_[1] will contain the L<HTTP::Response>
object; $_[0] is the undecoded content if the request was successful or
some error from L<HTTP::Status> if not.
Arguments can be attached to the request event and retrieved in the
handler via $_[2] -- this is usually an array reference, but anything
that fits in a scalar will do.
Plugin authors should check for the boolean value of B<<
$core->Provided->{www_request} >> and possibly fall back to using LWP
with a short timeout if they'd like to continue to function if this
plugin is B<not> loaded.
=head1 SEE ALSO
L<POE::Component::Client::HTTP>
L<HTTP::Request>
L<HTTP::Response>
=head1 AUTHOR
Jon Portnoy <avenj@cobaltirc.org>
L<http://www.cobaltirc.org>
=cut
( run in 0.692 second using v1.01-cache-2.11-cpan-39bf76dae61 )