Permalink
Browse files

Refactor the HTTP client example so that the client can be reused.

  • Loading branch information...
1 parent 5090f76 commit 8a4f21a9ecb9b939244f1c84f32da852b7b3b7cd @rcaputo committed Sep 27, 2012
Showing with 102 additions and 103 deletions.
  1. +79 −0 eg/HttpClient.pm
  2. +21 −0 eg/HttpResponseEvent.pm
  3. +2 −103 eg/eg-21-poco-client-http.pl
View
79 eg/HttpClient.pm
@@ -0,0 +1,79 @@
+package HttpClient;
+
+use Moose;
+extends 'Reflex::Base';
+
+use POE::Component::Client::HTTP;
+use Reflex::POE::Event;
+use HttpResponseEvent;
+
+has alias => (
+ is => 'ro',
+ isa => 'Str',
+ default => 'user-agent',
+);
+
+sub BUILD {
+ my ($self) = @_;
+
+ # Start an HTTP user-agent when the object is created.
+ #
+ # A more complete example would expose all of PoCo::Client::HTTP's
+ # configuration options as attributes.
+
+ POE::Component::Client::HTTP->spawn(Alias => $self->alias());
+}
+
+sub DESTRUCT {
+ my ($self) = @_;
+
+ # Shut down POE::Component::Client::HTTP when this object is
+ # destroyed.
+
+ POE::Kernel->post(ua => $self->alias());
+}
+
+sub request {
+ # Make a request.
+
+ my ($self, $http_request) = @_;
+
+ # There is no guarantee that the caller of request() is running in
+ # the same POE session as this HttpClient object.
+ #
+ # Reflex::Base's run_within_session() method makes sure that the
+ # right session is active when interacting with POE code. This
+ # ensures that POE-based responses are properly routed.
+
+ # The Reflex::POE::Event object created here is an event for POE's
+ # purpose, but it includes Reflex magic to route responses back to
+ # the correct Reflex object.
+
+ $self->run_within_session(
+ sub {
+ POE::Kernel->post(
+ $self->alias(),
+ 'request',
+ Reflex::POE::Event->new(
+ object => $self,
+ method => 'internal_http_response',
+ ),
+ $http_request,
+ );
+ }
+ );
+}
+
+sub internal_http_response {
+ my ($self, $args) = @_;
+
+ my ($request, $response) = @{ $args->{response} };
+ $self->emit(
+ -type => 'HttpResponseEvent',
+ -name => 'response',
+ request => $request->[0],
+ response => $response->[0],
+ );
+}
+
+1;
View
21 eg/HttpResponseEvent.pm
@@ -0,0 +1,21 @@
+package HttpResponseEvent;
+
+use Moose;
+extends 'Reflex::Event';
+
+use HTTP::Request;
+use HTTP::Response;
+
+has request => (
+ is => 'ro',
+ isa => 'HTTP::Request',
+ required => 1,
+);
+
+has response => (
+ is => 'ro',
+ isa => 'HTTP::Response',
+ required => 1,
+);
+
+1;
View
105 eg/eg-21-poco-client-http.pl
@@ -5,109 +5,8 @@
use strict;
use lib qw(../lib);
-# Define an HTTP response event.
-
-{
- package Reflex::Event::HttpResponse;
- use Moose;
- extends 'Reflex::Event';
-
- use HTTP::Request;
- use HTTP::Response;
-
- has request => (
- is => 'ro',
- isa => 'HTTP::Request',
- required => 1,
- );
-
- has response => (
- is => 'ro',
- isa => 'HTTP::Response',
- required => 1,
- );
-}
-
-# Wrap POE::Component::Client::HTTP in a Reflex object.
-
-{
- package HttpClient;
-
- use Moose;
- extends 'Reflex::Base';
-
- use POE::Component::Client::HTTP;
- use Reflex::POE::Event;
-
- has alias => (
- is => 'ro',
- isa => 'Str',
- default => 'user-agent',
- );
-
- sub BUILD {
- my ($self) = @_;
-
- # Start an HTTP user-agent when the object is created.
- #
- # A more complete example would expose all of PoCo::Client::HTTP's
- # configuration options as attributes.
-
- POE::Component::Client::HTTP->spawn(Alias => $self->alias());
- }
-
- sub DESTRUCT {
- my ($self) = @_;
-
- # Shut down POE::Component::Client::HTTP when this object is
- # destroyed.
-
- POE::Kernel->post(ua => $self->alias());
- }
-
- sub request {
- # Make a request.
-
- my ($self, $http_request) = @_;
-
- # There is no guarantee that the caller of request() is running in
- # the same POE session as this HttpClient object.
- #
- # Reflex::Base's run_within_session() method makes sure that the
- # right session is active when interacting with POE code. This
- # ensures that POE-based responses are properly routed.
-
- # The Reflex::POE::Event object created here is an event for POE's
- # purpose, but it includes Reflex magic to route responses back to
- # the correct Reflex object.
-
- $self->run_within_session(
- sub {
- POE::Kernel->post(
- $self->alias(),
- 'request',
- Reflex::POE::Event->new(
- object => $self,
- method => 'internal_http_response',
- ),
- $http_request,
- );
- }
- );
- }
-
- sub internal_http_response {
- my ($self, $args) = @_;
-
- my ($request, $response) = @{ $args->{response} };
- $self->emit(
- -type => 'Reflex::Event::HttpResponse',
- -name => 'response',
- request => $request->[0],
- response => $response->[0],
- );
- }
-}
+# HttpClient is a wrapper for POE::Component::Client::HTTP.
+use HttpClient;
### Main usage.

0 comments on commit 8a4f21a

Please sign in to comment.