HTTPDeferred.pm

filter: Statementstmt 100% 6 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 100% 8 / 8 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« HTTPDeferred/Deferred.pm ↑ Summary
1 package POE::Component::Client::HTTPDeferred;
2 2 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 our $VERSION = '0.02';
5  
6 2 ▶use POE qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
7 Component::Client::HTTP
8 Component::Client::HTTPDeferred::Deferred
9 2 /;
10  
11 has client_alias => (
12 is => 'rw',
13 isa => 'Str',
14 default => sub { 'ua' },
15 );
16  
17 has session => (
18 is => 'rw',
19 isa => 'POE::Session',
20 );
21  
22 no Any::Moose;
23  
24 =head1 NAME
25  
26 POE::Component::Client::HTTPDeferred - Yet another poco http client with twist like deferred interface.
27  
28 =head1 SYNOPSIS
29  
30 use POE qw/Component::Client::HTTPDeferred/;
31 use HTTP::Request::Common;
32
33 POE::Session->create(
34 inline_states => {
35 _start => sub {
36 my $ua = POE::Component::Client::HTTPDeferred->new;
37 my $d = $ua->request( GET 'http://example.com/' );
38
39 $d->addBoth(sub {
40 my $res = shift;
41
42 if ($res->is_success) {
43 print $res->as_string;
44 }
45 else {
46 warn $res->status_line;
47 }
48
49 $ua->shutdown;
50 });
51 },
52 },
53 );
54 POE::Kernel->run;
55  
56 =head1 DESCRIPTION
57  
58 POE::Component::Client::HTTPDeferred is a wrapper module to add twist (or MochiKit) like callback interface to POE::Component::Client::HTTP.
59  
60 To use this module, you can use code reference as response callback. So you don't have to create POE state for handling response.
61  
62 =head1 SEE ALSO
63  
64 L<POE::Component::Client::HTTPDeferred::Deferred>
65  
66 =head1 METHODS
67  
68 =head2 new
69  
70 Create POE::Component::Client::HTTPDeferred instance.
71  
72 my $ua = POE::Component::Client::HTTPDeferred->new;
73  
74 Once you call this, POE::Component::Client::HTTPDeferred will start POE::Session for own use.
75 So you need to call ->shutdown method to stop the session.
76  
77 =cut
78  
79 sub BUILD {
80 my $self = shift;
81  
82 $self->{session} = POE::Session->create(
83 object_states => [
84 $self => {
85 map { $_ => "poe_$_" } qw/_start request response/
86 },
87 ],
88 );
89 }
90  
91 =head2 request
92  
93 Send HTTP request and return Deferred object (L<POE::Component::Client::HTTPDeferred::Deferred>).
94  
95 my $d = $ua->request($request);
96  
97 This $request argument should be HTTP::Request object.
98  
99 =cut
100  
101 sub request {
102 my ($self, $req) = @_;
103  
104 my $d = $req->{_deferred} = POE::Component::Client::HTTPDeferred::Deferred->new(
105 request => $req,
106 client_alias => $self->client_alias,
107 );
108  
109 $poe_kernel->post( $self->session->ID => request => $req );
110  
111 $d;
112 }
113  
114 =head2 shutdown
115  
116 Shutdown POE::Component::Client::HTTPDeferred session.
117  
118 =cut
119  
120 sub shutdown {
121 my $self = shift;
122 $poe_kernel->post( $self->client_alias => 'shutdown' );
123 }
124  
125 =head1 POE METHODS
126  
127 Internal POE methods.
128  
129 =head2 poe__start
130  
131 =cut
132  
133 sub poe__start {
134 my ($self, $kernel) = @_[OBJECT, KERNEL];
135 POE::Component::Client::HTTP->spawn( Alias => $self->client_alias );
136 }
137  
138 =head2 poe_request
139  
140 =cut
141  
142 sub poe_request {
143 my ($self, $kernel, $req) = @_[OBJECT, KERNEL, ARG0];
144  
145 $kernel->post( $self->client_alias, 'request', 'response', $req );
146 }
147  
148 =head2 poe_response
149  
150 =cut
151  
152 sub poe_response {
153 my ($self, $kernel) = @_[OBJECT, KERNEL];
154 my ($req, $res) = ($_[ARG0]->[0], $_[ARG1]->[0]);
155  
156 my $d = delete $req->{_deferred} or Carp::confess 'deferred object not found';
157  
158 if ($res->is_success) {
159 $d->callback($res);
160 }
161 else {
162 $d->errback($res);
163 }
164 }
165  
166 =head1 AUTHOR
167  
168 Daisuke Murase <typester@cpan.org>
169  
170 =head1 COPYRIGHT AND LICENSE
171  
172 Copyright (c) 2008-2009 by KAYAC Inc.
173  
174 This program is free software; you can redistribute
175 it and/or modify it under the same terms as Perl itself.
176  
177 The full text of the license can be found in the
178 LICENSE file included with this module.
179  
180 =cut
181  
182 __PACKAGE__->meta->make_immutable;
« HTTPDeferred/Deferred.pm ↑ Summary