Skip to content

Commit

Permalink
started working on asyncing Facebook::Graph
Browse files Browse the repository at this point in the history
  • Loading branch information
rizen committed Nov 15, 2012
1 parent dc5a6f7 commit 6d3cfa6
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 88 deletions.
4 changes: 3 additions & 1 deletion Changes
Expand Up @@ -2,7 +2,9 @@
- Added batch requests. Thanks to fayland.
- Fix: Facebook no longer allows public queries. Thanks to spazm.
- Added authentication test.
- Fixed some other tests.
- Fixed some other failing tests due to Facebook changes.
- Started making Facebook::Graph async. Still working deciding on callback API, so left out true async in this release.
- WARNING: "ua" method no longer available from Facebook::Graph.

1.0401 2012-05-07
- Added impersonation recipe.
Expand Down
6 changes: 2 additions & 4 deletions dist.ini
Expand Up @@ -3,7 +3,7 @@ author = JT Smith <jt@plainblack.com>
license = Perl_5
copyright_holder = Plain Black Corporation
copyright_year = 2012
version = 1.0401
version = 1.0500

[@Classic]

Expand All @@ -18,9 +18,7 @@ Any::Moose = 0.13
JSON = 2.16
Test::More = 0
URI = 1.54
LWP = 6.02
LWP::Protocol::https = 6.02
Mozilla::CA = 20110409
AnyEvent::HTTP::LWP::UserAgent = 0.8
DateTime = 0.61
DateTime::Format::Strptime = 1.4000
MIME::Base64::URLSafe = 0.01
Expand Down
49 changes: 18 additions & 31 deletions lib/Facebook/Graph.pm
Expand Up @@ -20,7 +20,6 @@ use Facebook::Graph::Publish::RSVPAttending;
use Facebook::Graph::Publish::RSVPDeclined;
use Facebook::Graph::BatchRequests;
use Ouch;
use LWP::UserAgent;

has app_id => (
is => 'ro',
Expand All @@ -40,14 +39,6 @@ has access_token => (
predicate => 'has_access_token',
);

has ua => (
is => 'rw',
lazy => 1,
default => sub {
LWP::UserAgent->new;
},
);

sub parse_signed_request {
my ($self, $signed_request) = @_;
require Digest::SHA;
Expand All @@ -74,7 +65,6 @@ sub request_access_token {
postback => $self->postback,
secret => $self->secret,
app_id => $self->app_id,
ua => $self->ua,
)->request;
$self->access_token($token->token);
return $token;
Expand All @@ -86,7 +76,6 @@ sub convert_sessions {
secret => $self->secret,
app_id => $self->app_id,
sessions => $sessions,
ua => $self->ua,
)
->request
->as_hashref;
Expand All @@ -112,7 +101,7 @@ sub fql {

sub query {
my ($self) = @_;
my %params = ( ua => $self->ua );
my %params = ( );
if ($self->has_access_token) {
$params{access_token} = $self->access_token;
}
Expand All @@ -124,7 +113,7 @@ sub query {

sub batch_requests {
my ($self) = @_;
my %params = ( ua => $self->ua, access_token => $self->access_token );
my %params = ( access_token => $self->access_token );
return Facebook::Graph::BatchRequests->new(%params);
}

Expand All @@ -135,7 +124,7 @@ sub picture {

sub add_post {
my ($self, $object_name) = @_;
my %params = ( ua => $self->ua );
my %params = ( );
if ($object_name) {
$params{object_name} = $object_name;
}
Expand All @@ -150,7 +139,7 @@ sub add_post {

sub add_photo {
my ($self, $object_name) = @_;
my %params = ( ua => $self->ua );
my %params = ( );
if ($object_name) {
$params{object_name} = $object_name;
}
Expand All @@ -165,7 +154,7 @@ sub add_photo {

sub add_checkin {
my ($self, $object_name) = @_;
my %params = ( ua => $self->ua );
my %params = ( );
if ($object_name) {
$params{object_name} = $object_name;
}
Expand All @@ -182,7 +171,6 @@ sub add_like {
my ($self, $object_name) = @_;
my %params = (
object_name => $object_name,
ua => $self->ua,
);
if ($self->has_access_token) {
$params{access_token} = $self->access_token;
Expand All @@ -209,7 +197,7 @@ sub add_comment {

sub add_note {
my ($self) = @_;
my %params = ( ua => $self->ua );
my %params = ( );
if ($self->has_access_token) {
$params{access_token} = $self->access_token;
}
Expand All @@ -221,7 +209,7 @@ sub add_note {

sub add_link {
my ($self) = @_;
my %params = ( ua => $self->ua );
my %params = ( );
if ($self->has_access_token) {
$params{access_token} = $self->access_token;
}
Expand All @@ -233,7 +221,7 @@ sub add_link {

sub add_event {
my ($self, $object_name) = @_;
my %params = ( ua => $self->ua );
my %params = ( );
if ($object_name) {
$params{object_name} = $object_name;
}
Expand All @@ -250,7 +238,6 @@ sub rsvp_maybe {
my ($self, $object_name) = @_;
my %params = (
object_name => $object_name,
ua => $self->ua,
);
if ($self->has_access_token) {
$params{access_token} = $self->access_token;
Expand All @@ -265,7 +252,6 @@ sub rsvp_attending {
my ($self, $object_name) = @_;
my %params = (
object_name => $object_name,
ua => $self->ua,
);
if ($self->has_access_token) {
$params{access_token} = $self->access_token;
Expand All @@ -280,7 +266,6 @@ sub rsvp_declined {
my ($self, $object_name) = @_;
my %params = (
object_name => $object_name,
ua => $self->ua,
);
if ($self->has_access_token) {
$params{access_token} = $self->access_token;
Expand Down Expand Up @@ -317,11 +302,19 @@ Or better yet:
my $sarahs_picture_uri = $fb->picture('sarahbownds')->get_large->uri_as_string;
You can also do asynchronous calls like this:
my $sarah = $fb->query->find('sarahbownds'); # making request in background here
# ... do stuff here ...
my $hashref = $sarah->as_hashref; # handling the response here
Or fetching a response from a URI you already have:
my $response = $fb->query
my $hashref = $fb->query
->request('https://graph.facebook.com/btaylor')
->as_hashref;
=head2 Building A Privileged App
Expand Down Expand Up @@ -395,10 +388,6 @@ The URI that Facebook should post your authorization code back to. Required if y
B<NOTE:> It must be a sub URI of the URI that you put in the Application Settings > Connect > Connect URL field of your application's profile on Facebook.
=item ua
This allows you to pass in your own L<LWP::UserAgent> object. By default Facebook::Graph will just create one on the fly.
=back
Expand Down Expand Up @@ -578,9 +567,7 @@ I still need to add publishing albums/photos, deleting of content, impersonation
L<Any::Moose>
L<JSON>
L<LWP>
L<LWP::Protocol::https>
L<Mozilla::CA>
L<AnyEvent::HTTP::LWP::UserAgent>
L<URI>
L<DateTime>
L<DateTime::Format::Strptime>
Expand Down
11 changes: 4 additions & 7 deletions lib/Facebook/Graph/AccessToken.pm
Expand Up @@ -2,8 +2,8 @@ package Facebook::Graph::AccessToken;

use Any::Moose;
use Facebook::Graph::AccessToken::Response;
use Facebook::Graph::Request;
with 'Facebook::Graph::Role::Uri';
use LWP::UserAgent;

has app_id => (
is => 'ro',
Expand All @@ -25,10 +25,6 @@ has code => (
required=> 1,
);

has ua => (
is => 'rw',
);

sub uri_as_string {
my ($self) = @_;
my $uri = $self->uri;
Expand All @@ -44,8 +40,9 @@ sub uri_as_string {

sub request {
my ($self) = @_;
my $response = ($self->ua || LWP::UserAgent->new)->get($self->uri_as_string);
return Facebook::Graph::AccessToken::Response->new(response => $response);
return Facebook::Graph::AccessToken::Response->new(
response => Facebook::Graph::Request->new->get($self->uri_as_string)->recv->response
);
}

no Any::Moose;
Expand Down
10 changes: 3 additions & 7 deletions lib/Facebook/Graph/BatchRequests.pm
@@ -1,9 +1,8 @@
package Facebook::Graph::BatchRequests;

use Any::Moose;
use LWP::UserAgent;
use JSON;
use Ouch;
use Facebook::Graph::Request;

has access_token => (
is => 'ro',
Expand All @@ -16,10 +15,6 @@ has requests => (
default => sub { [] },
);

has ua => (
is => 'rw',
);

sub add_request {
my ($self, $ele) = @_;

Expand All @@ -45,7 +40,8 @@ sub request {
$self->requests([]); # reset

my $uri = "https://graph.facebook.com";
my $resp = ($self->ua || LWP::UserAgent->new)->post($uri, $post);
my $resp = Facebook::Graph::Request->new->post($uri, $post)->recv->response;

unless ($resp->is_success) {
my $message = $resp->message;
my $error = eval { $json->decode($resp->content) };
Expand Down
2 changes: 1 addition & 1 deletion lib/Facebook/Graph/Cookbook.pod
Expand Up @@ -20,7 +20,7 @@ A fully functional Facebook::Graph app that publishes data to Facebook and reads

=item L<Facebook::Graph::Cookbook::Recipe3> - Impersonation

Shows you how to post as all the different pages under your control.
Shows you how to post as all the different pages under your control.

=back

Expand Down
16 changes: 4 additions & 12 deletions lib/Facebook/Graph/Publish.pm
@@ -1,9 +1,9 @@
package Facebook::Graph::Publish;

use Any::Moose;
use Facebook::Graph::Response;
use Facebook::Graph::Request;
with 'Facebook::Graph::Role::Uri';
use LWP::UserAgent;
use AnyEvent::HTTP::LWP::UserAgent;
use URI::Escape;

has secret => (
Expand All @@ -22,10 +22,6 @@ has object_name => (
default => 'me',
);

has ua => (
is => 'rw',
);

sub to {
my ($self, $object_name) = @_;
$self->object_name($object_name);
Expand All @@ -45,14 +41,10 @@ sub publish {
my ($self) = @_;
my $uri = $self->uri;
$uri->path($self->object_name.$self->object_path);
my $response = ($self->ua || LWP::UserAgent->new)->post($uri, $self->get_post_params);
my %params = (response => $response);
if ($self->has_secret) {
$params{secret} = $self->secret;
}
return Facebook::Graph::Response->new(%params);
return Facebook::Graph::Request->new->post($uri, $self->get_post_params)->recv;
}


no Any::Moose;
__PACKAGE__->meta->make_immutable;

Expand Down
17 changes: 3 additions & 14 deletions lib/Facebook/Graph/Query.pm
@@ -1,9 +1,8 @@
package Facebook::Graph::Query;

use Any::Moose;
use Facebook::Graph::Response;
use Facebook::Graph::Request;
with 'Facebook::Graph::Role::Uri';
use LWP::UserAgent;
use URI::Escape;

has secret => (
Expand Down Expand Up @@ -77,10 +76,6 @@ has since => (
predicate => 'has_since',
);

has ua => (
is => 'rw',
);

sub limit_results {
my ($self, $limit) = @_;
$self->limit($limit);
Expand Down Expand Up @@ -197,14 +192,8 @@ sub uri_as_string {
}

sub request {
my ($self, $uri) = @_;
$uri ||= $self->uri_as_string;
my $response = ($self->ua || LWP::UserAgent->new)->get($uri);
my %params = (response => $response);
if ($self->has_secret) {
$params{secret} = $self->secret;
}
return Facebook::Graph::Response->new(%params);
my ($self) = @_;
return Facebook::Graph::Request->new->get($self->uri_as_string)->recv;
}

no Any::Moose;
Expand Down

0 comments on commit 6d3cfa6

Please sign in to comment.