[libflickr-api-perl] 02/40: imported 0.01
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 6d2371ee6d35fb58f08bb8f9675964c59198f162
Author: Cal Henderson <cal at iamcal.com>
Date: Wed Apr 23 05:41:41 2008 +0000
imported 0.01
---
API.pm | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
API/Request.pm | 52 +++++++++++++++++++++++
API/Response.pm | 78 ++++++++++++++++++++++++++++++++++
Changes | 6 +++
MANIFEST | 8 ++++
Makefile.PL | 10 +++++
README | 29 +++++++++++++
test.pl | 22 ++++++++++
8 files changed, 333 insertions(+)
diff --git a/API.pm b/API.pm
new file mode 100644
index 0000000..9452006
--- /dev/null
+++ b/API.pm
@@ -0,0 +1,128 @@
+package Flickr::API;
+
+use strict;
+use warnings;
+use LWP::UserAgent;
+use XML::Parser::Lite::Tree;
+use Flickr::API::Request;
+use Flickr::API::Response;
+
+our $VERSION = '0.01';
+
+sub new {
+ my $class = shift;
+ my $self = bless {}, $class;
+ my $options = shift;
+ $self->{key} = $options->{key};
+ return $self;
+}
+
+sub execute_method {
+ my ($self, $method, $args) = @_;
+
+ my $request = new Flickr::API::Request({'method' => $method, 'args' => $args});
+
+ $self->execute_request($request);
+}
+
+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};
+
+ my $ua = LWP::UserAgent->new;
+ my $ua_resp = $ua->post('http://www.flickr.com/services/rest/', $request->{args});
+
+ if ($ua_resp->{_rc} != 200){
+ $response->set_fail(0, "API returned a non-200 status code ($ua_resp->{_rc})");
+ return $response;
+ }
+
+ my $tree = XML::Parser::Lite::Tree::instance()->parse($ua_resp->{_content});
+
+ my $rsp_node = $self->_find_tag($tree->{children});
+
+ if ($rsp_node->{name} ne 'rsp'){
+ $response->set_fail(0, "API returned an invalid response");
+ return $response;
+ }
+
+ if ($rsp_node->{attributes}->{stat} eq 'fail'){
+ my $fail_node = $self->_find_tag($rsp_node->{children});
+ if ($fail_node->{name} eq 'err'){
+ $response->set_fail($fail_node->{attributes}->{code}, $fail_node->{attributes}->{msg});
+ }else{
+ $response->set_fail(0, "Method failed but returned no error code");
+ }
+ return $response;
+ }
+
+ if ($rsp_node->{attributes}->{stat} eq 'ok'){
+ $response->set_ok($rsp_node);
+ return $response;
+ }
+
+ $response->set_fail(0, "API returned an invalid status code");
+ return $response;
+}
+
+sub _find_tag {
+ my ($self, $children) = @_;
+ for my $child(@{$children}){
+ return $child if $child->{type} eq 'tag';
+ }
+ return {};
+}
+
+1;
+__END__
+
+=head1 NAME
+
+Flickr::API - Perl interface to the Flickr API
+
+=head1 SYNOPSIS
+
+ use Flickr::API;
+
+ my $api = new Flickr::API({'key' => 'your_api_key'});
+
+ my $rsp = $api->execute_method('flickr.test.echo', {
+ 'foo' => 'bar',
+ 'baz' => 'quux',
+ });
+
+=head1 DESCRIPTION
+
+A simple interface for using the Flickr API.
+
+
+=head2 METHODS
+
+=over 4
+
+=item C<execute_method($method, $args)>
+
+Constructs a C<Flickr::API::Request> object and executes it, returning a C<Flickr::API::Response> object.
+
+=item C<execute_request($request)>
+
+Executes a C<Flickr::API::Request> object, returning a C<Flickr::API::Response> object.
+
+=back
+
+
+=head1 AUTHOR
+
+Copyright (C) 2004, Cal Henderson, E<lt>cal at iamcal.comE<gt>
+
+=head1 SEE ALSO
+
+L<Flickr::API::Request>
+L<Flickr::API::Response>
+L<XML::Parser::Lite>
+
+=cut
diff --git a/API/Request.pm b/API/Request.pm
new file mode 100644
index 0000000..8a102b5
--- /dev/null
+++ b/API/Request.pm
@@ -0,0 +1,52 @@
+package Flickr::API::Request;
+
+use strict;
+use warnings;
+
+our $VERSION = '0.01';
+
+sub new {
+ my $class = shift;
+ my $self = bless {}, $class;
+ my $options = shift;
+ $self->{method} = $options->{method};
+ $self->{args} = $options->{args};
+ return $self;
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+Flickr::API::Request - A request to the Flickr API
+
+=head1 SYNOPSIS
+
+ use Flickr::API;
+ use Flickr::API::Request;
+
+ my $request = new Flickr::API::Request({
+ 'method' => $method,
+ 'args' => \%args,
+ });
+
+ $api->execute_request($request);
+
+
+=head1 DESCRIPTION
+
+This object encapsulates a request to the Flickr API.
+
+
+=head1 AUTHOR
+
+Copyright (C) 2004, Cal Henderson, E<lt>cal at iamcal.comE<gt>
+
+
+=head1 SEE ALSO
+
+L<Flickr::API>.
+
+=cut
diff --git a/API/Response.pm b/API/Response.pm
new file mode 100644
index 0000000..55b6482
--- /dev/null
+++ b/API/Response.pm
@@ -0,0 +1,78 @@
+package Flickr::API::Response;
+
+use strict;
+use warnings;
+
+our $VERSION = '0.01';
+
+sub new {
+ my $class = shift;
+ my $self = bless {}, $class;
+ my $options = shift;
+ $self->{raw} = '';
+ $self->{request} = $options->{request};
+ $self->{tree} = undef;
+ $self->{success} = 0;
+ $self->{error_code} = 0;
+ $self->{error_message} = '';
+ return $self;
+}
+
+sub set_fail {
+ my ($self, $code, $message) = @_;
+ $self->{success} = 0;
+ $self->{error_code} = $code;
+ $self->{error_message} = $message;
+}
+
+sub set_ok {
+ my ($self, $tree) = @_;
+ $self->{success} = 1;
+ $self->{tree} = $tree;
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+Flickr::API::Response - A response from the flickr API.
+
+=head1 SYNOPSIS
+
+ use Flickr::API;
+
+
+=head1 DESCRIPTION
+
+This object encapsulates a response from the Flickr API. It's
+basically a blessed hash with the following structure:
+
+ {
+ '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
+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.
+
+
+=head1 AUTHOR
+
+Copyright (C) 2004, Cal Henderson, E<lt>cal at iamcal.comE<gt>
+
+
+=head1 SEE ALSO
+
+L<Flickr::API>
+L<XML::Parser::Lite>
+
+=cut
+
diff --git a/Changes b/Changes
new file mode 100644
index 0000000..a44e796
--- /dev/null
+++ b/Changes
@@ -0,0 +1,6 @@
+Revision history for Perl extension Flickr::API.
+
+0.01 Thu Aug 19 19:21:00 2004
+ - original version; created by h2xs 1.21 with options
+ -AXc -n Flickr::API
+
diff --git a/MANIFEST b/MANIFEST
new file mode 100644
index 0000000..533d093
--- /dev/null
+++ b/MANIFEST
@@ -0,0 +1,8 @@
+API.pm
+API/Request.pm
+API/Response.pm
+Changes
+Makefile.PL
+MANIFEST
+README
+test.pl
diff --git a/Makefile.PL b/Makefile.PL
new file mode 100644
index 0000000..116466e
--- /dev/null
+++ b/Makefile.PL
@@ -0,0 +1,10 @@
+use ExtUtils::MakeMaker;
+
+WriteMakefile(
+ 'NAME' => 'Flickr::API',
+ 'VERSION_FROM' => 'API.pm',
+ 'PREREQ_PM' => {
+ 'LWP::UserAgent' => 0,
+ 'XML::Parser::Lite::Tree' => 0.03,
+ },
+);
diff --git a/README b/README
new file mode 100644
index 0000000..dfde2b9
--- /dev/null
+++ b/README
@@ -0,0 +1,29 @@
+Flickr::API
+===========
+
+Simple interface to the Flickr API.
+
+
+INSTALLATION
+
+To install this module type the following:
+
+ perl Makefile.PL
+ make
+ make test
+ make install
+
+
+DEPENDENCIES
+
+This module requires these other modules and libraries:
+
+ XML::Parser::Lite::Tree
+ LWP::UserAgent
+
+
+COPYRIGHT AND LICENCE
+
+Copyright (C) 2004 Cal Henderson <cal at iamcal.com>
+License: Perl Artistic License
+
diff --git a/test.pl b/test.pl
new file mode 100644
index 0000000..035b6da
--- /dev/null
+++ b/test.pl
@@ -0,0 +1,22 @@
+# Before `make install' is performed this script should be runnable with
+# `make test'. After `make install' it should work as `perl test.pl'
+
+#########################
+
+# change 'tests => 1' to 'tests => last_test_to_print';
+
+use Test;
+BEGIN { plan tests => 2 };
+use Flickr::API;
+ok(1); # If we made it this far, we're ok.
+
+#########################
+
+# Insert your test code below, the Test module is use()ed here so read
+# its man page ( perldoc Test ) for help writing this test script.
+
+my $api = new Flickr::API({'key' => 'made_up_key'});
+my $rsp = $api->execute_method('fake.method', {});
+
+ok($rsp->{error_code} == 100); # error code for invalid key
+
--
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