Skip to content

Commit

Permalink
tagged 0.02
Browse files Browse the repository at this point in the history
  • Loading branch information
cal committed Apr 23, 2008
1 parent 4e30542 commit b3146af
Show file tree
Hide file tree
Showing 8 changed files with 335 additions and 0 deletions.
130 changes: 130 additions & 0 deletions API.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
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.02';

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@iamcal.comE<gt>
=head1 SEE ALSO
L<Flickr::API::Request>,
L<Flickr::API::Response>,
L<XML::Parser::Lite>,
L<http://www.flickr.com/>,
L<http://www.flickr.com/services/api/>
=cut
52 changes: 52 additions & 0 deletions API/Request.pm
Original file line number Diff line number Diff line change
@@ -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@iamcal.comE<gt>
=head1 SEE ALSO
L<Flickr::API>.
=cut
78 changes: 78 additions & 0 deletions API/Response.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package Flickr::API::Response;

use strict;
use warnings;

our $VERSION = '0.02';

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@iamcal.comE<gt>
=head1 SEE ALSO
L<Flickr::API>,
L<XML::Parser::Lite>
=cut
6 changes: 6 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -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

8 changes: 8 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
API.pm
API/Request.pm
API/Response.pm
Changes
Makefile.PL
MANIFEST
README
test.pl
10 changes: 10 additions & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
@@ -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,
},
);
29 changes: 29 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -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@iamcal.com>
License: Perl Artistic License

22 changes: 22 additions & 0 deletions test.pl
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b3146af

Please sign in to comment.