Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

batch requests #19

Merged
merged 3 commits into from

2 participants

@fayland

not sure how to ask for pull request as one commit, but here is the changes:

fayland@513f5d8

let me know if any issue.

Thanks

@rizen rizen merged commit 4f69dde into rizen:master
@rizen
Owner

thanks for the patch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 12, 2011
  1. @fayland

    'properties' in Post graph

    fayland authored
Commits on May 10, 2012
  1. @fayland

    merge

    fayland authored
  2. @fayland

    batch requests

    fayland authored
This page is out of date. Refresh to see the latest.
View
13 Changes
@@ -1,3 +1,4 @@
+
1.0401 2012-05-07
- Added impersonation recipe.
@@ -5,7 +6,7 @@
- Added a date format option to find. Thanks to jwieland.
- Added better FQL handling. Thanks to vielmath.
- Added photo publishing. Thanks to c0decafe.
- - Documented the "ua" parameter on Facebook::Graph->new().
+ - Documented the "ua" parameter on Facebook::Graph->new().
- Replaced URI::Encode with URI::Escape.
- Fixed a failing test due to changes in the Facebook API.
@@ -23,13 +24,13 @@
- Documented an easy way to take a relationship URL passed back from Facebook and turn it into a Facebook::Graph::Response object.
1.0100 2011-01-04
- - Added Facebook::Graph::Query::from. Inspired by a pull request from Dan
+ - Added Facebook::Graph::Query::from. Inspired by a pull request from Dan
Magnuszewski.
1.0000 2010-12-09
- Updated recipe2 example to use more modern Dancer features.
- Merged POD patch from fayland.
- - Can now post posts and events directly to other pages you control without
+ - Can now post posts and events directly to other pages you control without
switching access tokens.
- Added set_source(), set_privacy(), set_actions(), add_actions(),
set_target_countries(), set_target_cities(), set_target_regions(), and
@@ -37,7 +38,7 @@
- Changed get_post_params() in Facebook::Graph::Publish to return an array ref
instead of a hash ref.
- Now requires Exception::Class.
-
+
NOTE: This version is not backward compatible with previous versions because
the get_post_params() method in Facebook::Graph::Publish now returns an array
ref rather than a hash ref. However, if you didn't extend this module, then you
@@ -46,14 +47,14 @@
NOTE2: This version is not backward compatible with previous versions because
we are no longer throwing array based exceptions. Instead the exceptions are
- Exception::Class based. If you don't care about exception formatting then this
+ Exception::Class based. If you don't care about exception formatting then this
module will still be backward compatible for you.
0.0705 2010-09-26
- Missed a spot in 0.0704.
0.0704 2010-09-26
- - Fixed a problem with access token encoding.
+ - Fixed a problem with access token encoding.
0.0703 2010-09-26
- Documented Response's as_string method.
View
23 lib/Facebook/Graph.pm
@@ -18,6 +18,7 @@ use Facebook::Graph::Publish::Event;
use Facebook::Graph::Publish::RSVPMaybe;
use Facebook::Graph::Publish::RSVPAttending;
use Facebook::Graph::Publish::RSVPDeclined;
+use Facebook::Graph::BatchRequests;
use Ouch;
use LWP::UserAgent;
@@ -91,7 +92,7 @@ sub convert_sessions {
->as_hashref;
}
-sub authorize {
+sub authorize {
my ($self) = @_;
return Facebook::Graph::Authorize->new(
app_id => $self->app_id,
@@ -121,6 +122,12 @@ sub query {
return Facebook::Graph::Query->new(%params);
}
+sub batch_requests {
+ my ($self) = @_;
+ my %params = ( ua => $self->ua, access_token => $self->access_token );
+ return Facebook::Graph::BatchRequests->new(%params);
+}
+
sub picture {
my ($self, $object_name) = @_;
return Facebook::Graph::Picture->new( object_name => $object_name );
@@ -298,7 +305,7 @@ Facebook::Graph - A fast and easy way to integrate your apps with Facebook.
my $fb = Facebook::Graph->new;
my $sarah_bownds = $fb->fetch('sarahbownds');
my $perl_page = $fb->fetch('16665510298');
-
+
Or better yet:
my $sarah_bownds = $fb->query
@@ -307,7 +314,7 @@ Or better yet:
->select_fields(qw( id name picture ))
->request
->as_hashref;
-
+
my $sarahs_picture_uri = $fb->picture('sarahbownds')->get_large->uri_as_string;
Or fetching a response from a URI you already have:
@@ -315,8 +322,8 @@ Or fetching a response from a URI you already have:
my $response = $fb->query
->request('https://graph.facebook.com/btaylor')
->as_hashref;
-
-
+
+
=head2 Building A Privileged App
my $fb = Facebook::Graph->new(
@@ -338,11 +345,11 @@ Handle the Facebook authorization code postback:
my $q = Plack::Request->new($env);
$fb->request_access_token($q->query_param('code'));
-
+
Or if you already had the access token:
$fb->access_token($token);
-
+
Get some info:
my $user = $fb->fetch('me');
@@ -419,7 +426,7 @@ Creates a L<Facebook::Graph::Query> object, which can be used to fetch and searc
Returns a hash reference of an object from facebook. A quick way to grab an object from Facebook. These two statements are identical:
my $sarah = $fb->fetch('sarahbownds');
-
+
my $sarah = $fb->query->find('sarahbownds')->request->as_hashref;
=head3 id
View
107 lib/Facebook/Graph/BatchRequests.pm
@@ -0,0 +1,107 @@
+package Facebook::Graph::BatchRequests;
+
+use Any::Moose;
+use LWP::UserAgent;
+use JSON;
+use Ouch;
+
+has access_token => (
+ is => 'ro',
+ required => 1,
+);
+
+has requests => (
+ is => 'rw',
+ isa => 'ArrayRef',
+ default => sub { [] },
+);
+
+has ua => (
+ is => 'rw',
+);
+
+sub add_request {
+ my ($self, $ele) = @_;
+
+ unless (ref $ele eq 'HASH') {
+ $ele = { method => 'GET', relative_url => $ele };
+ }
+ $self->requests( [ @{$self->requests}, $ele ] );
+
+ return $self; # chained
+}
+
+sub request {
+ my ($self, @reqs) = @_;
+
+ $self->add_request($_) foreach @reqs;
+
+ my $json = JSON->new;
+ my $post = {
+ access_token => $self->access_token, # required
+ batch => $json->encode($self->requests),
+ };
+
+ $self->requests([]); # reset
+
+ my $uri = "https://graph.facebook.com";
+ my $resp = ($self->ua || LWP::UserAgent->new)->post($uri, $post);
+ unless ($resp->is_success) {
+ my $message = $resp->message;
+ my $error = eval { $json->decode($resp->content) };
+ unless ($@) {
+ $message = $error->{error}{type} . ' - ' . $error->{error}{message};
+ }
+ ouch $resp->code, "Could not execute batch requests: $message";
+ }
+
+ my $data = $json->decode($resp->decoded_content);
+ map { $_->{data} = $json->decode($_->{body}) } @$data;
+ return wantarray ? @$data : $data;
+}
+
+no Any::Moose;
+__PACKAGE__->meta->make_immutable;
+
+=head1 NAME
+
+Facebook::Graph::BatchRequests - Batch Requests
+
+=head1 SYNOPSIS
+
+ # set access_token, required
+ my $fb = Facebook::Graph->new(access_token => $access_token);
+ my @batches = $fb->batch_requests
+ ->add_request('sarahbownds')
+ ->add_request({"method" => "POST", "relative_url" => 'me/feed', body => "message=Test update"})
+ ->request;
+
+ foreach my $batch (@batches) {
+ print $batch->{code} . $batch->{body} . Dumper(\$batch->{data}, \$batch->{headers}) . "\n";
+ }
+
+=head1 DESCRIPTION
+
+send batch requests to save time: L<http://developers.facebook.com/docs/reference/api/batch/>
+
+=head1 METHODS
+
+=head2 add_request
+
+add request, if not HASHREF, will default method as GET and arg as relative_url
+
+ $batch_requests->add_request('sarahbownds'); # as { method => 'GET', relative_url => 'sarahbownds' }
+ $batch_requests->add_request({"method" => "POST", "relative_url" => 'me/feed', body => "message=Test update"})
+
+=head2 request
+
+ $batch_requests->request;
+ $batch_requests->request(@requests);
+
+Fire the request and return decoded @batches data
+
+=head1 LEGAL
+
+Facebook::Graph is Copyright 2010 - 2012 Plain Black Corporation (L<http://www.plainblack.com>) and is licensed under the same terms as Perl itself.
+
+=cut
View
17 t/05_batchrequests.t
@@ -0,0 +1,17 @@
+use Test::More tests => 3;
+use lib '../lib';
+use Ouch;
+use Facebook::Graph;
+
+my $fb = Facebook::Graph->new(access_token => $ENV{FB_ACCESS_TOKEN});
+
+my @batches = $fb->batch_requests
+ ->add_request({"method" => "GET", "relative_url" => 'sarahbownds'})
+ ->add_request({"method" => "GET", "relative_url" => '113515098748988'})
+ ->request;
+
+is($batches[0]->{data}->{id}, '767598108', 'got sarah');
+is($batches[0]->{data}->{name}, 'Sarah Bownds', 'know her name');
+
+# 'location' => 'San Francisco Design Center',
+is($batches[1]->{data}->{location}, 'San Francisco Design Center', 'know event location');
Something went wrong with that request. Please try again.