[libflickr-api-perl] 04/40: imported 0.03

Lucas Kanashiro kanashiro-guest at moszumanska.debian.org
Sat Jul 25 21:12:16 UTC 2015


This is an automated email from the git hooks/post-receive script.

kanashiro-guest pushed a commit to tag 1.08
in repository libflickr-api-perl.

commit 210e92851e0d5d291b90b4b1992387f3ad0d3170
Author: Cal Henderson <cal at iamcal.com>
Date:   Wed Apr 23 05:44:05 2008 +0000

    imported 0.03
---
 API.pm          | 46 +++++++++++++++++++++++++++++++++-------------
 API/Request.pm  | 41 +++++++++++++++++++++++++++++++++++------
 API/Response.pm | 36 +++++++++++++++++++++++++++---------
 Makefile.PL     |  3 +++
 4 files changed, 98 insertions(+), 28 deletions(-)

diff --git a/API.pm b/API.pm
index 4665cf0..9646a7d 100644
--- a/API.pm
+++ b/API.pm
@@ -7,13 +7,16 @@ use XML::Parser::Lite::Tree;
 use Flickr::API::Request;
 use Flickr::API::Response;
 
-our $VERSION = '0.02';
+our @ISA = qw(LWP::UserAgent);
+
+our $VERSION = '0.03';
 
 sub new {
 	my $class = shift;
-	my $self = bless {}, $class;
 	my $options = shift;
-	$self->{key} = $options->{key};
+	my $self = new LWP::UserAgent;
+	$self->{api_key} = $options->{key};
+	bless $self, $class;
 	return $self;
 }
 
@@ -28,20 +31,20 @@ sub execute_method {
 sub execute_request {
 	my ($self, $request) = @_;
 
-	my $response = new Flickr::API::Response({'request' => $request});
-
-	$request->{args}->{method} = $request->{method};
-	$request->{args}->{api_key} = $self->{key};
+	$request->{api_args}->{method} = $request->{api_method};
+	$request->{api_args}->{api_key} = $self->{api_key};
+	$request->encode_args();
 
-	my $ua = LWP::UserAgent->new;
-	my $ua_resp = $ua->post('http://www.flickr.com/services/rest/', $request->{args});
+	my $response = $self->request($request);
+	bless $response, 'Flickr::API::Response';
+	$response->init_flickr();
 
-	if ($ua_resp->{_rc} != 200){
-		$response->set_fail(0, "API returned a non-200 status code ($ua_resp->{_rc})");
+	if ($response->{_rc} != 200){
+		$response->set_fail(0, "API returned a non-200 status code ($response->{_rc})");
 		return $response;
 	}
 
-	my $tree = XML::Parser::Lite::Tree::instance()->parse($ua_resp->{_content});
+	my $tree = XML::Parser::Lite::Tree::instance()->parse($response->{_content});
 
 	my $rsp_node = $self->_find_tag($tree->{children});
 
@@ -90,15 +93,32 @@ Flickr::API - Perl interface to the Flickr API
 
   my $api = new Flickr::API({'key' => 'your_api_key'});
 
-  my $rsp = $api->execute_method('flickr.test.echo', {
+  my $response = $api->execute_method('flickr.test.echo', {
 		'foo' => 'bar',
 		'baz' => 'quux',
 	});
 
+or
+
+  use Flickr::API;
+  use Flickr::API::Request;
+
+  my $api = new Flickr::API({'key' => 'your_api_key'});
+
+  my $request = new Flickr::API::Request({
+		'method' => 'flickr.test.echo',
+		'args' => {},
+	});
+
+  my $response = $api->execute_request($request);
+  
+
 =head1 DESCRIPTION
 
 A simple interface for using the Flickr API.
 
+C<Flickr::API> is a subclass of L<LWP::UserAgent>, so all of the various
+proxy, request limits, caching, etc are available.
 
 =head2 METHODS
 
diff --git a/API/Request.pm b/API/Request.pm
index 8a102b5..7fcf644 100644
--- a/API/Request.pm
+++ b/API/Request.pm
@@ -2,18 +2,40 @@ package Flickr::API::Request;
 
 use strict;
 use warnings;
+use HTTP::Request;
+use URI;
 
-our $VERSION = '0.01';
+our @ISA = qw(HTTP::Request);
+our $VERSION = '0.02';
 
 sub new {
 	my $class = shift;
-	my $self = bless {}, $class;
+	my $self = new HTTP::Request;
 	my $options = shift;
-	$self->{method} = $options->{method};
-	$self->{args} = $options->{args};
+	$self->{api_method} = $options->{method};
+	$self->{api_args} = $options->{args};
+	bless $self, $class;
+
+	$self->method('POST');
+        $self->uri('http://www.flickr.com/services/rest/');
+
 	return $self;
 }
 
+sub encode_args {
+	my ($self) = @_;
+
+	my $url = URI->new('http:');
+	$url->query_form(%{$self->{api_args}});
+	my $content = $url->query;
+
+	$self->header('Content-Type' => 'application/x-www-form-urlencoded');
+	if (defined($content)) {
+		$self->header('Content-Length' => length($content));
+		$self->content($content);
+	}
+}
+
 1;
 
 __END__
@@ -27,18 +49,25 @@ Flickr::API::Request - A request to the Flickr API
   use Flickr::API;
   use Flickr::API::Request;
 
+  my $api = new Flickr::API({'key' => 'your_api_key'});
+
   my $request = new Flickr::API::Request({
   	'method' => $method,
-  	'args' => \%args,
+  	'args' => {},
   }); 
 
-  $api->execute_request($request);
+  my $response = $api->execute_request($request);
 
 
 =head1 DESCRIPTION
 
 This object encapsulates a request to the Flickr API.
 
+C<Flickr::API::Request> is a subclass of C<HTTP::Request>, so you can access
+any of the request parameters and tweak them yourself. The content, content-type
+header and content-length header are all built from the 'args' list by the
+C<Flickr::API::execute_request()> method.
+
 
 =head1 AUTHOR
 
diff --git a/API/Response.pm b/API/Response.pm
index b65a180..e77c462 100644
--- a/API/Response.pm
+++ b/API/Response.pm
@@ -2,20 +2,26 @@ package Flickr::API::Response;
 
 use strict;
 use warnings;
+use HTTP::Response;
+
+our @ISA = qw(HTTP::Response);
 
 our $VERSION = '0.02';
 
 sub new {
 	my $class = shift;
-	my $self = bless {}, $class;
+	my $self = new HTTP::Response;
 	my $options = shift;
-	#$self->{raw} = '';
-	$self->{request} = $options->{request};
+	bless $self, $class;
+	return $self;
+}
+
+sub init_flickr {
+	my ($self, $options) = @_;
 	$self->{tree} = undef;
 	$self->{success} = 0;
 	$self->{error_code} = 0;
-	$self->{error_message} = '';
-	return $self;
+	$self->{error_message} = '';	
 }
 
 sub set_fail {
@@ -42,23 +48,35 @@ Flickr::API::Response - A response from the flickr API.
 =head1 SYNOPSIS
 
   use Flickr::API;
+  use Flickr::API::Response;
+
+  my $api = new Flickr::API({'key' => 'your_api_key'});
 
+  my $response = $api->execute_method('flickr.test.echo', {
+                'foo' => 'bar',
+                'baz' => 'quux',
+        });
+
+  print "Success: $response->{success}\n";
 
 =head1 DESCRIPTION
 
 This object encapsulates a response from the Flickr API. It's
-basically a blessed hash with the following structure:
+a subclass of C<HTTP::Response> with the following additional
+keys:
 
   {
-	'request' => Flickr::API::Request,
 	'success' => 1,
 	'tree' => XML::Parser::Lite::Tree,
 	'error_code' => 0,
 	'error_message' => '',
   }
 
-The C<request> key contains the request object that this response
-was generated from. The C<sucess> key contains 1 or 0, indicating
+The C<_request> key contains the request object that this response
+was generated from. This request will be a C<Flickr::API::Request>
+object, which is a subclass of C<HTTP:Request>.
+
+The C<sucess> key contains 1 or 0, indicating
 whether the request suceeded. If it failed, C<error_code> and
 C<error_message> explain what went wrong. If it suceeded, C<tree>
 contains an C<XML::Parser::Lite::Tree> object of the response XML.
diff --git a/Makefile.PL b/Makefile.PL
index 116466e..bb8f8a5 100644
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -5,6 +5,9 @@ WriteMakefile(
     'VERSION_FROM'	=> 'API.pm',
     'PREREQ_PM'		=> {
 		'LWP::UserAgent' => 0,
+		'HTTP::Request' => 0,
+		'HTTP::Response' => 0,
+		'URI' => 0,
 		'XML::Parser::Lite::Tree' => 0.03,
 	},
 );

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libflickr-api-perl.git



More information about the Pkg-perl-cvs-commits mailing list