| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Net::OpenSocial::Client::Protocol::REST; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
5
|
use Any::Moose; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
|
|
extends 'Net::OpenSocial::Client::Protocol'; |
|
5
|
|
|
|
|
|
|
with 'Net::OpenSocial::Client::ErrorHandler'; |
|
6
|
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
1357
|
use Net::OpenSocial::Client::ResultSet; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
8
|
|
|
8
|
1
|
|
|
1
|
|
656
|
use Net::OpenSocial::Client::Result; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
8
|
|
|
9
|
1
|
|
|
1
|
|
557
|
use Net::OpenSocial::Client::Resource::Factory; |
|
|
1
|
|
|
|
|
4
|
|
|
|
1
|
|
|
|
|
10
|
|
|
10
|
1
|
|
|
1
|
|
669
|
use Net::OpenSocial::Client::Collection; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
8
|
|
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
override 'execute' => sub { |
|
13
|
|
|
|
|
|
|
my ( $self, $container, $requests ) = @_; |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
return $self->ERROR(q{This container doesn't support rest endpoint.}) |
|
16
|
|
|
|
|
|
|
unless $container->rest_endpoint; |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my $result = Net::OpenSocial::Client::ResultSet->new; |
|
19
|
|
|
|
|
|
|
for my $request (@$requests) { |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
my $method = $request->http_method_for_operation(); |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my %params = %{ $request->params }; |
|
24
|
|
|
|
|
|
|
my $app_id = delete $params{appId}; |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
my $url = join( |
|
27
|
|
|
|
|
|
|
'/', |
|
28
|
|
|
|
|
|
|
grep { defined($_) && $_ ne '' } ( |
|
29
|
|
|
|
|
|
|
$container->rest_endpoint, $request->service, |
|
30
|
|
|
|
|
|
|
$request->user_id, $request->group_id, |
|
31
|
|
|
|
|
|
|
$app_id, |
|
32
|
|
|
|
|
|
|
) |
|
33
|
|
|
|
|
|
|
); |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
$params{format} = $self->formatter->name; |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
my %build_args = ( |
|
38
|
|
|
|
|
|
|
method => $method, |
|
39
|
|
|
|
|
|
|
url => $url, |
|
40
|
|
|
|
|
|
|
params => {%params}, |
|
41
|
|
|
|
|
|
|
container => $container, |
|
42
|
|
|
|
|
|
|
); |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
if ( $method eq 'POST' || $method eq 'PUT' ) { |
|
45
|
|
|
|
|
|
|
unless ( $request->has_resource ) { |
|
46
|
|
|
|
|
|
|
return $self->ERROR(q{No resource data found.}); |
|
47
|
|
|
|
|
|
|
} |
|
48
|
|
|
|
|
|
|
my $resource = $request->resource; |
|
49
|
|
|
|
|
|
|
$build_args{content_type} = $self->formatter->content_type; |
|
50
|
|
|
|
|
|
|
$build_args{content} |
|
51
|
|
|
|
|
|
|
= $self->formatter->encode( $resource->fields ); |
|
52
|
|
|
|
|
|
|
} |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
my $http_req = $self->request_builder->build_request(%build_args); |
|
55
|
|
|
|
|
|
|
my $http_res = $self->agent->request($http_req); |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
unless ( $http_res->is_success ) { |
|
58
|
|
|
|
|
|
|
my $error = Net::OpenSocial::Client::Result->new( |
|
59
|
|
|
|
|
|
|
is_error => 1, |
|
60
|
|
|
|
|
|
|
code => $http_res->code, |
|
61
|
|
|
|
|
|
|
message => $http_res->message, |
|
62
|
|
|
|
|
|
|
); |
|
63
|
|
|
|
|
|
|
$result->set_result( $request->id => $error ); |
|
64
|
|
|
|
|
|
|
next; |
|
65
|
|
|
|
|
|
|
} |
|
66
|
|
|
|
|
|
|
my $res = $self->formatter->decode( $http_res->content ); |
|
67
|
|
|
|
|
|
|
$result->set_result( |
|
68
|
|
|
|
|
|
|
$request->id => $self->_build_result( $request->service, $res ) ); |
|
69
|
|
|
|
|
|
|
} |
|
70
|
|
|
|
|
|
|
return $result; |
|
71
|
|
|
|
|
|
|
}; |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub _build_result { |
|
74
|
0
|
|
|
0
|
|
|
my ( $self, $service, $obj ) = @_; |
|
75
|
0
|
|
0
|
|
|
|
$obj ||= {}; |
|
76
|
0
|
0
|
|
|
|
|
my $result = exists $obj->{response} ? $obj->{response} : $obj; |
|
77
|
0
|
0
|
|
|
|
|
if ( exists $result->{entry} ) { |
|
78
|
0
|
|
|
|
|
|
my $entries = $result->{entry}; |
|
79
|
0
|
0
|
|
|
|
|
$entries = [$entries] unless ref $entries eq 'ARRAY'; |
|
80
|
0
|
|
|
|
|
|
my $coll = Net::OpenSocial::Client::Collection->new; |
|
81
|
0
|
0
|
|
|
|
|
$coll->total_results( $result->{totalResults} ) |
|
82
|
|
|
|
|
|
|
if exists $result->{totalResults}; |
|
83
|
0
|
0
|
|
|
|
|
$coll->start_index( $result->{startIndex} ) |
|
84
|
|
|
|
|
|
|
if exists $result->{startIndex}; |
|
85
|
0
|
0
|
|
|
|
|
$coll->items_per_page( $result->{itemsPerPage} ) |
|
86
|
|
|
|
|
|
|
if exists $result->{itemsPerPage}; |
|
87
|
0
|
|
|
|
|
|
for my $entry (@$entries) { |
|
88
|
0
|
0
|
|
|
|
|
next unless ( keys %$entry > 0 ); |
|
89
|
0
|
|
|
|
|
|
my $resource |
|
90
|
|
|
|
|
|
|
= Net::OpenSocial::Client::Resource::Factory->gen_resource( |
|
91
|
|
|
|
|
|
|
$service, $entry ); |
|
92
|
0
|
|
|
|
|
|
$coll->add_item($resource); |
|
93
|
|
|
|
|
|
|
} |
|
94
|
0
|
|
|
|
|
|
return Net::OpenSocial::Client::Result->new( data => $coll ); |
|
95
|
|
|
|
|
|
|
} |
|
96
|
|
|
|
|
|
|
else { |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# VOID result |
|
99
|
0
|
|
|
|
|
|
return Net::OpenSocial::Client::Result->new; |
|
100
|
|
|
|
|
|
|
} |
|
101
|
|
|
|
|
|
|
} |
|
102
|
|
|
|
|
|
|
|
|
103
|
1
|
|
|
1
|
|
594
|
no Any::Moose; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
6
|
|
|
104
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
|
105
|
|
|
|
|
|
|
1; |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head1 NAME |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
Net::OpenSocial::Client::Protocol::REST - Protocol class for RESTful API. |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
my $protocol = Net::OpenSocial::Client::Protocol::REST->new( |
|
114
|
|
|
|
|
|
|
formatter => Net::OpenSocial::CLient::Formatter::JSON->new, |
|
115
|
|
|
|
|
|
|
request_builder => Net::OpenSocial::Client::HTTPRequestBuilder::OAuth->new(...), |
|
116
|
|
|
|
|
|
|
agent => LWP::UserAgent->new, |
|
117
|
|
|
|
|
|
|
); |
|
118
|
|
|
|
|
|
|
my $result_set = $protocol->execute($container, $requests); |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Protocol class for RESTful API. |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 METHODS |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head2 execute($container, $requests) |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
L |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 AUTHOR |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Lyo Kato, Elyo.kato@gmail.comE |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Copyright (C) 2009 by Lyo Kato |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
|
141
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.8.8 or, |
|
142
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=cut |