line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::OpenSocial::Client::Protocol::RPC; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
4
|
use Any::Moose; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
4
|
|
|
|
|
|
|
extends 'Net::OpenSocial::Client::Protocol'; |
5
|
|
|
|
|
|
|
with 'Net::OpenSocial::Client::ErrorHandler'; |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
348
|
use Net::OpenSocial::Client::ResultSet; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
8
|
1
|
|
|
1
|
|
46
|
use Net::OpenSocial::Client::Result; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
7
|
|
9
|
1
|
|
|
1
|
|
30
|
use Net::OpenSocial::Client::Collection; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
10
|
1
|
|
|
1
|
|
24
|
use Net::OpenSocial::Client::Resource::Factory; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
11
|
1
|
|
|
1
|
|
488
|
use Net::OpenSocial::Client::Type::Operation qw(CREATE UPDATE); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
799
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
override 'execute' => sub { |
14
|
|
|
|
|
|
|
my ( $self, $container, $requests ) = @_; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $url = $container->rpc_endpoint; |
17
|
|
|
|
|
|
|
return $self->ERROR(q{This container doesn't support RPC endpoint.}) |
18
|
|
|
|
|
|
|
unless $url; |
19
|
|
|
|
|
|
|
my $method = 'POST'; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
my @obj; |
22
|
|
|
|
|
|
|
my %id_req_map = (); |
23
|
|
|
|
|
|
|
for my $request (@$requests) { |
24
|
|
|
|
|
|
|
my $service = $request->rpc_service; |
25
|
|
|
|
|
|
|
my $operation = $request->operation; |
26
|
|
|
|
|
|
|
my $obj = {}; |
27
|
|
|
|
|
|
|
$obj->{method} = sprintf( q{%s.%s}, $service, $operation ); |
28
|
|
|
|
|
|
|
$obj->{id} = $request->id; |
29
|
|
|
|
|
|
|
$obj->{params} = $request->params; |
30
|
|
|
|
|
|
|
$obj->{params}{userId} = $request->user_id; |
31
|
|
|
|
|
|
|
$obj->{params}{groupId} = $request->group_id; |
32
|
|
|
|
|
|
|
if ( $operation eq CREATE |
33
|
|
|
|
|
|
|
|| $operation eq UPDATE ) |
34
|
|
|
|
|
|
|
{ |
35
|
|
|
|
|
|
|
unless ( $request->has_resource ) { |
36
|
|
|
|
|
|
|
return $self->ERROR(q{CREATE/UPDATE operations need resource.}); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
my $resource = $request->resource; |
39
|
|
|
|
|
|
|
$obj->{params}{$service} = $resource->fields; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
push( @obj, $obj ); |
42
|
|
|
|
|
|
|
$id_req_map{ $request->id } = $request; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
my $content = $self->formatter->encode( @obj > 1 ? [@obj] : $obj[0] ); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
my $http_req = $self->request_builder->build_request( |
47
|
|
|
|
|
|
|
method => $method, |
48
|
|
|
|
|
|
|
url => $url, |
49
|
|
|
|
|
|
|
container => $container, |
50
|
|
|
|
|
|
|
content_type => $self->formatter->content_type, |
51
|
|
|
|
|
|
|
content => $content, |
52
|
|
|
|
|
|
|
); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
my $http_res = $self->agent->request($http_req); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
my $result_set = Net::OpenSocial::Client::ResultSet->new; |
57
|
|
|
|
|
|
|
unless ( $http_res->is_success ) { |
58
|
|
|
|
|
|
|
for my $request (@$requests) { |
59
|
|
|
|
|
|
|
my $error = Net::OpenSocial::Client::Result->new( |
60
|
|
|
|
|
|
|
is_error => 1, |
61
|
|
|
|
|
|
|
code => $http_res->code, |
62
|
|
|
|
|
|
|
message => $http_res->message, |
63
|
|
|
|
|
|
|
); |
64
|
|
|
|
|
|
|
$result_set->set_result( $request->id => $error ); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
return $result_set; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
my $results = $self->formatter->decode( $http_res->content ); |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
unless ( ref $results eq 'ARRAY' ) { |
71
|
|
|
|
|
|
|
$results = [$results]; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
for my $result (@$results) { |
74
|
|
|
|
|
|
|
my $res_id |
75
|
|
|
|
|
|
|
= exists $result->{id} ? $result->{id} : $requests->[0]->id; |
76
|
|
|
|
|
|
|
my $origin_req = $id_req_map{$res_id}; |
77
|
|
|
|
|
|
|
$result_set->set_result( |
78
|
|
|
|
|
|
|
$res_id => $self->_build_result( $origin_req->service, $origin_req->operation, $result ) ); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
return $result_set; |
81
|
|
|
|
|
|
|
}; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub _build_result { |
84
|
0
|
|
|
0
|
|
|
my ( $self, $service, $operation, $obj ) = @_; |
85
|
0
|
|
0
|
|
|
|
$obj ||= {}; |
86
|
0
|
0
|
|
|
|
|
if ( exists $obj->{error} ) { |
87
|
0
|
|
|
|
|
|
return Net::OpenSocial::Client::Result->new( |
88
|
|
|
|
|
|
|
is_error => 1, |
89
|
0
|
|
|
|
|
|
%{ $obj->{error} } |
90
|
|
|
|
|
|
|
); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
else { |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
#my $result = exists $obj->{result} ? $obj->{result} : $obj; |
95
|
0
|
|
0
|
|
|
|
my $result = $obj->{data} || {}; |
96
|
0
|
0
|
0
|
|
|
|
if ( ( $operation eq CREATE || $operation eq UPDATE ) && scalar( keys %$result ) == 0 ) { |
|
|
|
0
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# VOID result |
99
|
0
|
|
|
|
|
|
return Net::OpenSocial::Client::Result->new(); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
else { |
102
|
0
|
|
|
|
|
|
my $coll = Net::OpenSocial::Client::Collection->new(); |
103
|
0
|
0
|
|
|
|
|
if ( exists $result->{list} ) { |
|
|
0
|
|
|
|
|
|
104
|
0
|
0
|
|
|
|
|
$coll->total_results( $result->{totalResults} ) |
105
|
|
|
|
|
|
|
if exists $result->{totalResults}; |
106
|
0
|
0
|
|
|
|
|
$coll->start_index( $result->{startIndex} ) |
107
|
|
|
|
|
|
|
if exists $result->{startIndex}; |
108
|
0
|
0
|
|
|
|
|
$coll->items_per_page( $result->{itemsPerPage} ) |
109
|
|
|
|
|
|
|
if exists $result->{itemsPerPage}; |
110
|
0
|
|
|
|
|
|
my $list = $result->{list}; |
111
|
0
|
0
|
|
|
|
|
unless ( ref $list eq 'ARRAY' ) { |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# invalid response format |
114
|
|
|
|
|
|
|
} |
115
|
0
|
|
|
|
|
|
for my $item ( @{ $result->{list} } ) { |
|
0
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
my $resource = Net::OpenSocial::Client::Resource::Factory |
117
|
|
|
|
|
|
|
->gen_resource( $service, $item ); |
118
|
0
|
|
|
|
|
|
$coll->add_item($resource); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
elsif ( keys %$result > 0 ) { |
122
|
0
|
|
|
|
|
|
my $resource = Net::OpenSocial::Client::Resource::Factory |
123
|
|
|
|
|
|
|
->gen_resource( $service, $result ); |
124
|
0
|
|
|
|
|
|
$coll->add_item($resource); |
125
|
|
|
|
|
|
|
} |
126
|
0
|
|
|
|
|
|
return Net::OpenSocial::Client::Result->new( data => $coll ); |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
1
|
|
|
1
|
|
6
|
no Any::Moose; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
7
|
|
132
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
133
|
|
|
|
|
|
|
1; |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head1 NAME |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
Net::OpenSocial::Client::Protocol::PRC - Protocol class for RPC. |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head1 SYNOPSIS |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
my $protocol = Net::OpenSocial::Client::Protocol::RPC->new( |
142
|
|
|
|
|
|
|
formatter => Net::OpenSocial::CLient::Formatter::JSON->new, |
143
|
|
|
|
|
|
|
request_builder => Net::OpenSocial::Client::HTTPRequestBuilder::OAuth->new(...), |
144
|
|
|
|
|
|
|
agent => LWP::UserAgent->new, |
145
|
|
|
|
|
|
|
); |
146
|
|
|
|
|
|
|
my $result_set = $protocol->execute($container, $requests); |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head1 DESCRIPTION |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Protocol class for RPC. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head1 METHODS |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head2 execute($container, $requests) |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head1 SEE ALSO |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
L |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head1 AUTHOR |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
Lyo Kato, Elyo.kato@gmail.comE |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
Copyright (C) 2009 by Lyo Kato |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
169
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.8.8 or, |
170
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=cut |