| POE-Component-Client-HTTPDeferred documentation | Contained in the POE-Component-Client-HTTPDeferred distribution. |
POE::Component::Client::HTTPDeferred - Yet another poco http client with twist like deferred interface.
use POE qw/Component::Client::HTTPDeferred/;
use HTTP::Request::Common;
POE::Session->create(
inline_states => {
_start => sub {
my $ua = POE::Component::Client::HTTPDeferred->new;
my $d = $ua->request( GET 'http://example.com/' );
$d->addBoth(sub {
my $res = shift;
if ($res->is_success) {
print $res->as_string;
}
else {
warn $res->status_line;
}
$ua->shutdown;
});
},
},
);
POE::Kernel->run;
POE::Component::Client::HTTPDeferred is a wrapper module to add twist (or MochiKit) like callback interface to POE::Component::Client::HTTP.
To use this module, you can use code reference as response callback. So you don't have to create POE state for handling response.
Create POE::Component::Client::HTTPDeferred instance.
my $ua = POE::Component::Client::HTTPDeferred->new;
Once you call this, POE::Component::Client::HTTPDeferred will start POE::Session for own use. So you need to call ->shutdown method to stop the session.
Send HTTP request and return Deferred object (POE::Component::Client::HTTPDeferred::Deferred).
my $d = $ua->request($request);
This $request argument should be HTTP::Request object.
Shutdown POE::Component::Client::HTTPDeferred session.
Internal POE methods.
Daisuke Murase <typester@cpan.org>
Copyright (c) 2008-2009 by KAYAC Inc.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included with this module.
| POE-Component-Client-HTTPDeferred documentation | Contained in the POE-Component-Client-HTTPDeferred distribution. |
package POE::Component::Client::HTTPDeferred; use Any::Moose; our $VERSION = '0.02'; use POE qw/ Component::Client::HTTP Component::Client::HTTPDeferred::Deferred /; has client_alias => ( is => 'rw', isa => 'Str', default => sub { 'ua' }, ); has session => ( is => 'rw', isa => 'POE::Session', ); no Any::Moose;
sub BUILD { my $self = shift; $self->{session} = POE::Session->create( object_states => [ $self => { map { $_ => "poe_$_" } qw/_start request response/ }, ], ); }
sub request { my ($self, $req) = @_; my $d = $req->{_deferred} = POE::Component::Client::HTTPDeferred::Deferred->new( request => $req, client_alias => $self->client_alias, ); $poe_kernel->post( $self->session->ID => request => $req ); $d; }
sub shutdown { my $self = shift; $poe_kernel->post( $self->client_alias => 'shutdown' ); }
sub poe__start { my ($self, $kernel) = @_[OBJECT, KERNEL]; POE::Component::Client::HTTP->spawn( Alias => $self->client_alias ); }
sub poe_request { my ($self, $kernel, $req) = @_[OBJECT, KERNEL, ARG0]; $kernel->post( $self->client_alias, 'request', 'response', $req ); }
sub poe_response { my ($self, $kernel) = @_[OBJECT, KERNEL]; my ($req, $res) = ($_[ARG0]->[0], $_[ARG1]->[0]); my $d = delete $req->{_deferred} or Carp::confess 'deferred object not found'; if ($res->is_success) { $d->callback($res); } else { $d->errback($res); } }
__PACKAGE__->meta->make_immutable;