Ignore:
Timestamp:
Sep 20, 2011, 11:15:32 PM (11 years ago)
Author:
Edward Z. Yang <ezyang@mit.edu>
Branches:
master, release-1.9
Children:
b7fa912
Parents:
2a42248
git-author:
Edward Z. Yang <ezyang@mit.edu> (07/12/11 09:17:04)
git-committer:
Edward Z. Yang <ezyang@mit.edu> (09/20/11 23:15:32)
Message:
Convert to async (both Facebook::Graph and us.)

Work items:
    - Documentation is all out of date
    - Think more carefully about error handling
      (right now it's delayed into the response object)
    - Really bad HTTP POST hack in Publish.pm.

Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
Location:
perl/modules/Facebook/lib/Facebook
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • perl/modules/Facebook/lib/Facebook/Graph.pm

    r2a42248 r7777ac2  
    6262
    6363sub request_access_token {
    64     my ($self, $code) = @_;
    65     my $token = Facebook::Graph::AccessToken->new(
     64    my ($self, $code, $cb) = @_;
     65    Facebook::Graph::AccessToken->new(
    6666        code            => $code,
    6767        postback        => $self->postback,
    6868        secret          => $self->secret,
    6969        app_id          => $self->app_id,
    70     )->request;
    71     $self->access_token($token->token);
    72     return $token;
     70    )->request(sub {
     71        my ($response) = @_;
     72        $self->access_token($response->token);
     73        $cb->($response);
     74    });
    7375}
    7476
    7577sub convert_sessions {
    76     my ($self, $sessions) = @_;
    77     return Facebook::Graph::Session->new(
     78    my ($self, $sessions, $cb) = @_;
     79    Facebook::Graph::Session->new(
    7880        secret          => $self->secret,
    7981        app_id          => $self->app_id,
    8082        sessions        => $sessions,
    81         )
    82         ->request
    83         ->as_hashref;
     83    )->request($cb); # API change
    8484}
    8585
     
    9292}
    9393
    94 sub fetch {
    95     my ($self, $object_name) = @_;
    96     return $self->query->find($object_name)->request->as_hashref;
    97 }
     94# XXX error handling
     95#sub fetch {
     96#    my ($self, $object_name, $cb) = @_;
     97#    $self->query->find($object_name)->request(sub {
     98#        my ($result) = @_;
     99#        $cb->($result->as_hashref);
     100#    });
     101#}
    98102
    99103sub query {
     
    540544L<Any::Moose>
    541545L<JSON>
    542 L<LWP>
    543 L<LWP::Protocol::https>
     546L<AnyEvent::HTTP>
    544547L<Mozilla::CA>
    545548L<URI>
  • perl/modules/Facebook/lib/Facebook/Graph/AccessToken.pm

    r2a42248 r7777ac2  
    77use Facebook::Graph::AccessToken::Response;
    88with 'Facebook::Graph::Role::Uri';
    9 use LWP::UserAgent;
     9use AnyEvent::HTTP;
    1010
    1111has app_id => (
     
    4343
    4444sub request {
    45     my ($self) = @_;
    46     my $response = LWP::UserAgent->new->get($self->uri_as_string);
    47     return Facebook::Graph::AccessToken::Response->new(response => $response);
     45    my ($self, $cb) = @_;
     46    http_get $self->uri_as_string, sub {
     47        my ($response, $headers) = @_;
     48        $cb->(Facebook::Graph::AccessToken::Response->new(
     49            response => $response,
     50            headers => $headers,
     51            uri => $self->uri_as_string
     52        ));
     53    };
    4854}
    4955
  • perl/modules/Facebook/lib/Facebook/Graph/AccessToken/Response.pm

    r2a42248 r7777ac2  
    1818    lazy    => 1,
    1919    default => sub {
     20        # LOL error handling
    2021        my $self = shift;
    21         my $response = $self->response;
    22         if ($response->is_success) {
    23             return URI->new('?'.$response->content)->query_param('access_token');
    24         }
    25         else {
    26             ouch $response->code, 'Could not fetch access token: '.$response->message, $response->request->uri->as_string;
    27         }
     22        return URI->new('?'.$self->response)->query_param('access_token');
     23        #else {
     24        #    ouch $response->code, 'Could not fetch access token: '.$response->message, $response->request->uri->as_string;
     25        #}
    2826    }
    2927);
     
    3331    lazy    => 1,
    3432    default => sub {
     33        # LOL error handling
    3534        my $self = shift;
    36         my $response = $self->response;
    37         if ($response->is_success) {
    38             return URI->new('?'.$response->content)->query_param('expires');
    39         }
    40         else {
    41             ouch $response->code, 'Could not fetch access token: '.$response->message, $response->request->uri->as_string;
    42         }
     35        return URI->new('?'.$self->response)->query_param('expires');
     36        #else {
     37        #    ouch $response->code, 'Could not fetch access token: '.$response->message, $response->request->uri->as_string;
     38        #}
    4339    }
    4440);
  • perl/modules/Facebook/lib/Facebook/Graph/Publish.pm

    r2a42248 r7777ac2  
    77use Facebook::Graph::Response;
    88with 'Facebook::Graph::Role::Uri';
    9 use LWP::UserAgent;
     9use AnyEvent::HTTP;
     10use LWP::UserAgent; # XXX blegh
     11use HTTP::Request::Common;
    1012use URI::Encode qw(uri_decode);
    1113
     
    4244
    4345sub publish {
    44     my ($self) = @_;
     46    my ($self, $cb) = @_;
    4547    my $uri = $self->uri;
    4648    $uri->path($self->object_name.$self->object_path);
    47     my $response = LWP::UserAgent->new->post($uri, $self->get_post_params);
    48     my %params = (response => $response);
    49     if ($self->has_secret) {
    50         $params{secret} = $self->secret;
     49    # XXX blegh
     50    my $request = LWP::UserAgent->new->request(POST $uri->as_string, $self->get_post_params);
     51    http_post $uri->as_string, $request->content, sub {
     52        warn "whooo";
     53        my ($response, $headers) = @_;
     54        my %params = (
     55            response => $response,
     56            headers  => $headers,
     57            uri      => $uri->as_string
     58        );
     59        if ($self->has_secret) {
     60            $params{secret} = $self->secret;
     61        }
     62        $cb->(Facebook::Graph::Response->new(%params));
    5163    }
    52     return Facebook::Graph::Response->new(%params);
    5364}
    5465
  • perl/modules/Facebook/lib/Facebook/Graph/Query.pm

    r2a42248 r7777ac2  
    77use Facebook::Graph::Response;
    88with 'Facebook::Graph::Role::Uri';
    9 use LWP::UserAgent;
     9use AnyEvent::HTTP;
    1010use URI::Encode qw(uri_decode);
    1111
     
    183183
    184184sub request {
    185     my ($self, $uri) = @_;
    186     $uri ||= $self->uri_as_string;
    187     my $response = LWP::UserAgent->new->get($uri);
    188     my %params = (response => $response);
    189     if ($self->has_secret) {
    190         $params{secret} = $self->secret;
    191     }
    192     return Facebook::Graph::Response->new(%params);
     185    my ($self, $cb) = @_;
     186    my $uri = $self->uri_as_string;
     187    http_get $uri, sub {
     188        my ($response, $headers) = @_;
     189        my %params = (
     190            response => $response,
     191            headers => $headers,
     192            uri => $uri
     193        );
     194        if ($self->has_secret) {
     195            $params{secret} = $self->secret;
     196        }
     197        $cb->(Facebook::Graph::Response->new(%params));
     198    };
    193199}
    194200
  • perl/modules/Facebook/lib/Facebook/Graph/Response.pm

    r2a42248 r7777ac2  
    1313);
    1414
     15has headers => (
     16    is      => 'ro',
     17    required=> 1,
     18);
     19
     20has uri => (
     21    is      => 'ro',
     22    required=> 1,
     23);
     24
    1525has as_string => (
    1626    is      => 'ro',
     
    1828    default => sub {
    1929        my $self = shift;
    20         return $self->response->content;
     30        if (!defined $self->response) {
     31            ouch $self->headers->{Status}, $self->headers->{Reason}, $self->uri;
     32        }
     33        if ($self->headers->{Status} < 200 || $self->headers->{Status} >= 300) {
     34            my $type = $self->headers->{Status};
     35            my $message = $self->response;
     36            my $error = eval { JSON->new->decode($self->response) };
     37            unless ($@) {
     38                $type = $error->{error}{type};
     39                $message = $error->{error}{message};
     40            }
     41            ouch $type, 'Could not execute request ('.$self->uri.'): '.$message, $self->uri;
     42        }
     43        return $self->response;
    2144    },
    2245);
     
    2750    default => sub {
    2851        my $self = shift;
    29         my $response = $self->response;
    30         if ($response->is_success) {
    31             return $response->content;
    32         }
    33         else {
    34             my $message = $response->message;
    35             my $error = eval { JSON->new->decode($response->content) };
    36             unless ($@) {
    37                 $message = $error->{error}{type} . ' - ' . $error->{error}{message};
    38             }
    39             ouch $response->code, 'Could not execute request ('.$response->request->uri->as_string.'): '.$message, $response->request->uri->as_string;
    40         }
     52        return $self->as_string;
    4153    },
    4254);
  • perl/modules/Facebook/lib/Facebook/Graph/Session.pm

    r2a42248 r7777ac2  
    77use Facebook::Graph::Response;
    88with 'Facebook::Graph::Role::Uri';
    9 use LWP::UserAgent;
     9use AnyEvent::HTTP;
    1010
    1111has app_id => (
     
    3838
    3939sub request {
    40     my ($self) = @_;
    41     my $response = LWP::UserAgent->new->get($self->uri_as_string);
    42     return Facebook::Graph::Response->new(response => $response);
     40    my ($self, $cb) = @_;
     41    http_get $self->uri_as_string, sub {
     42        my ($response, $headers) = @_;
     43        $cb->(Facebook::Graph::Response->new(
     44            response => $response,
     45            headers  => $headers,
     46            uri      => $self->uri_as_string
     47        ));
     48    };
    4349}
    4450
Note: See TracChangeset for help on using the changeset viewer.