release-1.10release-1.9
Last change
on this file was
7777ac2,
checked in by Edward Z. Yang <ezyang@mit.edu>, 13 years ago
|
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>
|
-
Property mode set to
100644
|
File size:
2.2 KB
|
Line | |
---|
1 | package Facebook::Graph::Response; |
---|
2 | BEGIN { |
---|
3 | $Facebook::Graph::Response::VERSION = '1.0300'; |
---|
4 | } |
---|
5 | |
---|
6 | use Any::Moose; |
---|
7 | use JSON; |
---|
8 | use Ouch; |
---|
9 | |
---|
10 | has response => ( |
---|
11 | is => 'ro', |
---|
12 | required=> 1, |
---|
13 | ); |
---|
14 | |
---|
15 | has headers => ( |
---|
16 | is => 'ro', |
---|
17 | required=> 1, |
---|
18 | ); |
---|
19 | |
---|
20 | has uri => ( |
---|
21 | is => 'ro', |
---|
22 | required=> 1, |
---|
23 | ); |
---|
24 | |
---|
25 | has as_string => ( |
---|
26 | is => 'ro', |
---|
27 | lazy => 1, |
---|
28 | default => sub { |
---|
29 | my $self = shift; |
---|
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; |
---|
44 | }, |
---|
45 | ); |
---|
46 | |
---|
47 | has as_json => ( |
---|
48 | is => 'ro', |
---|
49 | lazy => 1, |
---|
50 | default => sub { |
---|
51 | my $self = shift; |
---|
52 | return $self->as_string; |
---|
53 | }, |
---|
54 | ); |
---|
55 | |
---|
56 | has as_hashref => ( |
---|
57 | is => 'ro', |
---|
58 | lazy => 1, |
---|
59 | default => sub { |
---|
60 | my $self = shift; |
---|
61 | return JSON->new->decode($self->as_json); |
---|
62 | }, |
---|
63 | ); |
---|
64 | |
---|
65 | no Any::Moose; |
---|
66 | __PACKAGE__->meta->make_immutable; |
---|
67 | |
---|
68 | =head1 NAME |
---|
69 | |
---|
70 | Facebook::Graph::Response - Handling of a Facebook::Graph response documents. |
---|
71 | |
---|
72 | =head1 VERSION |
---|
73 | |
---|
74 | version 1.0300 |
---|
75 | |
---|
76 | =head1 DESCRIPTION |
---|
77 | |
---|
78 | You'll be given one of these as a result of calling the C<request> method on a C<Facebook::Graph::Query> or others. |
---|
79 | |
---|
80 | |
---|
81 | =head1 METHODS |
---|
82 | |
---|
83 | Returns the response as a string. Does not throw an exception of any kind. |
---|
84 | |
---|
85 | =head2 as_json () |
---|
86 | |
---|
87 | Returns the response from Facebook as a JSON string. |
---|
88 | |
---|
89 | =head2 as_hashref () |
---|
90 | |
---|
91 | Returns the response from Facebook as a hash reference. |
---|
92 | |
---|
93 | =head2 as_string () |
---|
94 | |
---|
95 | No processing what so ever. Just returns the raw body string that was received from Facebook. |
---|
96 | |
---|
97 | =head2 response () |
---|
98 | |
---|
99 | Direct access to the L<HTTP::Response> object. |
---|
100 | |
---|
101 | =head1 LEGAL |
---|
102 | |
---|
103 | Facebook::Graph is Copyright 2010 Plain Black Corporation (L<http://www.plainblack.com>) and is licensed under the same terms as Perl itself. |
---|
104 | |
---|
105 | =cut |
---|
Note: See
TracBrowser
for help on using the repository browser.