Client.pm

filter: Statementstmt 35.3% 35 / 99 Branchbran 0.0% 0 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 9 / 18 Podpod 100% 8 / 8 totaltotal 34.4% 52 / 151 CC 14 SCAR 20.5
CC
14
Cov
28%
BUILDARGS
12.0
get_people
12.0
get_person
12.0
CRAP 7.8
« Client/HTTPRequestBuilder/ST.pm ↑ Summary Client/Resource/AppData.pm »
1 package Net::OpenSocial::Client;
2  
3 1 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Any::Moose 'X::AttributeHelpers';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Net::OpenSocial::Client::Protocol::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Net::OpenSocial::Client::Request::FetchPeople;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Net::OpenSocial::Client::Request::FetchPerson;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Net::OpenSocial::Client::Request::FetchFriends;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Net::OpenSocial::Client::Request::FetchPersonAppData;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Net::OpenSocial::Client::Request::FetchFriendsAppData;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our $VERSION = '0.01_05';
14 1 our $DEFAULT_PROTOCOL_VERSION = '0.8.1';
15  
16 1 with 'Net::OpenSocial::Client::ErrorHandler';
17  
18 1 has 'protocol' => (
19 is => 'ro',
20 isa => 'Net::OpenSocial::Client::Protocol',
21 required => 1,
22 );
23  
24 1 has 'container' => (
25 is => 'ro',
26 isa => 'Net::OpenSocial::Client::Container',
27 required => 1,
28 );
29  
30 has '_requests' => (
31 is => 'ro',
32 isa => 'ArrayRef',
33 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
34 1 metaclass => 'Collection::Array',
35 provides => { clear => 'clear_requests', },
36 );
37  
38 =head1 NAME
39  
40 Net::OpenSocial::Client - OpenSocial REST/RPC Client
41  
42 =head1 SYNOPSIS
43  
44  
45 use Net::OpenSocial::Client;
46 use Net::OpenSocial::Client::Container::OrkutSandbox;
47 use Net::OpenSocial::Client::Type::Protocol qw(REST RPC);
48 use Data::Dump qw(dump);
49  
50 my $client = Net::OpenSocial::Client->new(
51 container => Net::OpenSocial::Client::Container::OrkutSandbox->new,
52 consumer_key => q{orkut.com:623061448914},
53 consumer_secret => q{uynAeXiWTisflWX99KU1D2q5},
54 requestor => q{03067092798963641994},
55 protocol_type => RPC,
56 );
57  
58 my $friends = $client->get_friends('@me')
59 or die $client->errstr;
60 for my $person ( @{ $friends->items } ) {
61 say $person->get_field('id');
62 ...
63  
64 say dump($person->fields);
65 }
66  
67 =head1 DESCRIPTION
68  
69 OpenSocial provides API endpoints which called 'RESTful API' and 'RPC'.
70 This module allows you to handle it easily.
71 This provides you a same interface for both REST-API and RPC, so you
72 don't need to mind about the big differences between them.
73  
74 =head1 SIMPLE USAGE
75  
76 If you don't need to handle multiple requests at once for more effective performance (batch request),
77 this module provides you some simple methods that can handle resources easily.
78  
79 my $client = Net::OpenSocial::Client->new(...);
80  
81 my $user_id = '@me';
82 my $person = $client->get_person( $user_id )
83 or die $client->errstr;
84  
85 say $person->get_field('id');
86  
87 my $friends = $client->get_friends( $user_id )
88 or die $client->errstr;
89  
90 foreach my $friend ( @{ $friends->items } ) {
91 say $friend->get_field('id');
92 }
93  
94  
95 For more details, look at each methods' document.
96  
97 =over 4
98  
99 =item get_people
100 =item get_person
101 =item get_friends
102 =item get_person_appdata
103 =item get_friends_appdata
104  
105 =back
106  
107 =head1 RAW APIs AND BATCH REQUEST
108  
109 =head2 BUILD REQUEST
110  
111 You can build a request object by yourself or choose from preset.
112 See L<Net::OpenSocial::Client::Request>.
113  
114 my $request = Net::OpenSocial::Client::Request->new(
115 service => PEOPLE,
116 operation => GET,
117 user_id => '@me',
118 group_id => '@friends',
119 params => {
120 itemsPerPage => 10,
121 startIndex => 10,
122 },
123 );
124  
125 or
126  
127 my $request = Net::OpenSocial::Client::Request::FetchFriends->new( '@me',
128 { itemsPerPage => 10, startIndex => 10 } );
129  
130 =head2 EXECUTE REQUEST
131  
132 You should add request to client with request-id.
133  
134 $client->add_request( req1 => $request1 );
135 $client->add_request( req2 => $request2 );
136  
137 Only execute 'add_request', you can't obtain a result corresponding to the requests.
138 To get them, you have to call 'send'.
139  
140 my $result_set = $client->send()
141  
142 Internally, it works apropriately according to the protocol type you choose.
143 If RPC is selected, multiple requests are send as one single http-request.
144 Or with REST, it send multiple http-request for each opensocial-request.
145 And it returns L<Net::OpenSocial::Client::ResultSet> object.
146  
147 =head2 PICK UP RESULT
148  
149 Pick up L<Net::OpenSocial::Client::Result> object with
150 request-id you passed when invoking 'add_request'.
151  
152 my $result1 = $result_set->get_result('req1');
153 my $result2 = $result_set->get_result('req2');
154  
155 =head2 HANDLE DATA
156  
157 if ( $result1->is_error ) {
158 die sprintf q{%d: %s.},
159 $result->code, $result->message;
160 } else {
161 my $data = $result1->data;
162 ...
163 }
164  
165 the data may be L<Net::OpenSocial::Client::Collection> or
166 L<Net::OpenSocial::Client::Resource> subclass's object.
167 such like L<Net::OpenSocial::Client::Resource::Person>
168 ,L<Net::OpenSocial::Client::Resource::Activity>
169 ,L<Net::OpenSocial::Client::Resource::AppData>
170 , or L<Net::OpenSocial::Client::Resource::Group>.
171  
172 =head1 METHODS
173  
174 =head2 new
175  
176 use Net::OpenSocial::Client::Type::Auth qw(OAUTH ST);
177 use Net::OpenSocial::Client::Type::Format qw(JSON XML);
178 use Net::OpenSocial::Client::Type::Protocol qw(REST RPC);
179  
180 my $client = Net::OpenSocial::Client->new(
181 container => $container,
182 auty_type => OAUTH,
183 format_type => JSON,
184 protocol_type => REST,
185 );
186  
187 =head3 container
188  
189 First, you have to prepare L<Net::OpenSocial::Client::Container> object.
190 You can build it with container-information you want to access as of now,
191 or choose from preset.
192 There exists container objects which support major provider.
193  
194 L<Net::OpenSocial::Client::Container::Orkut>
195 L<Net::OpenSocial::Client::Container::MySpace>
196 L<Net::OpenSocial::Client::Container::Google>
197 and etc.
198  
199  
200 my $container = Net::OpenSocial::Client::Container->new(...);
201  
202 or
203  
204 my $container = Net::OpenSocial::Client::Container::MySpace->new;
205  
206  
207 =head3 auth_type
208  
209 Authorization type.
210 You can choose from OAUTH and ST.
211 OAUTH means 2-legged or 3-legged OAuth.
212 When you choose OAUTH type, you have to pass both 'consumer_key' and 'consumer_secret'
213 parameter which you got on provider beforehand.
214  
215 my $client = Net::OpenSocial::Client->new(
216 auth_type => OAUTH, # you can omit this. OAUTH is set by default.
217 consumer_key => q{foobarbuz},
218 consumer_secret => q{foobarbuz},
219 ...
220 );
221  
222 And if you want to use 3-legged OAuth, you need to pass authorized access-token.
223 You should already have completed OAuth authorization process.
224 See L<OAuth::Lite::Consumer>.
225  
226 my $access_token = OAuth::Lite::Token->new( token => q{aaa}, secret => {bbb} );
227  
228 my $client = Net::OpenSocial::Client->new(
229 auth_type => OAUTH, # you can omit this. OAUTH is set by default.
230 consumer_key => q{foobarbuz},
231 consumer_secret => q{foobarbuz},
232 token => $access_token,
233 ...
234 );
235  
236 Or in case you try 2-legged OAuth.
237 pass user-id of xoauth_request_id as 'requestor' parameter.
238  
239 my $client = Net::OpenSocial::Client->new(
240 auth_type => OAUTH, # you can omit this. OAUTH is set by default.
241 consumer_key => q{foobarbuz},
242 consumer_secret => q{foobarbuz},
243 requestor => q{myid},
244 ...
245 );
246  
247 ST means security-token.
248 You have pass to security-token as 'st' parameter.
249  
250 my $client = Net::OpenSocial::Client->new(
251 auth_type => ST,
252 st => q{foobarbuz},
253 ...
254 );
255  
256 =head3 format_type
257  
258 Now it only supports JSON format, so, it sets JSON by default.
259  
260 =head3 protocol_type
261  
262 REST or RPC.
263 REST is set by default.
264 Make sure that the container which you want to access supports
265 the protocol.
266  
267 If container supports RPC, and you choose it, you can execute
268 batch request.
269  
270 =head2 BUILDARGS
271  
272 See L<Moose>, L<Mouse>.
273 You don't need to call this method directly.
274  
275 =cut
276  
277 0 ▶sub BUILDARGS {
Subroutine BUILDARGS: not called CC=3 SCAR=24.8
Pod documented
278 0 my ( $self, %args ) = @_;
279 0 my $params = {};
280 0 $params->{container} = delete $args{container};
281 0 ▶ unless ( exists $args{protocol} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"protocol"}) 0 0 0
282 0 my $builder = Net::OpenSocial::Client::Protocol::Builder->new(%args);
283 0 ▶ my $protocol = $builder->build_protocol()
Branch: 1 branch
BranchTrueFalseTotal
my $protocol = $builder->build_protocol or die $builder->errstr 0 0 0
284 or die $builder->errstr;
285 0 $params->{protocol} = $protocol;
286 }
287 0 return $params;
288 }
289  
290 =head2 add_request( $request )
291  
292 Pass L<Net::OpenSocial::Client::Request> or its subclass's object.
293 At this time, noghing happens. When you execute 'send' method,
294 You will get the result of this request.
295  
296 You should make ID string for the request arbitrarily,
297 and set it with the request.
298 Remember this ID to pick up a result you want from result-set
299 that you get after executing 'send' method.
300  
301 $client->add_request( $request_id => $request );
302  
303 =cut
304  
305 0 ▶sub add_request {
Subroutine add_request: not called CC=1 SCAR=6.9
Pod documented
306 0 my ( $self, $id, $req ) = @_;
307 0 $req->id($id);
308 0 push( @{ $self->_requests }, $req );
309 }
310  
311 =head2 send()
312  
313 This method send all requests that you added by 'add_request' before you call this.
314 If it's done successfuly, it returns a L<Net::OpenSocial::Client::ResultSet> object.
315  
316 To obtain a result you want, you need to invoke 'get_result' method of result-set object
317 with request-id you set when you do 'add_request'.
318 Then it returns a L<Net::OpenSocial::Client::Result> object.
319  
320 my $result_set = $client->send()
321 or die $client->errstr;
322 my $result = $result_set->get_result($request_id);
323  
324 =cut
325  
326 0 ▶sub send {
Subroutine send: not called CC=2 SCAR=17.9
Pod documented
327 0 my $self = shift;
328 0 my @requests = @{ $self->_requests };
329 0 $self->clear_requests();
330 0 ▶ my $result_set = $self->protocol->execute( $self->container, \@requests )
Branch: 1 branch
BranchTrueFalseTotal
my $result_set = $self->protocol->execute($self->container, \@requests) or return $self->ERROR($self->protocol->errstr) 0 0 0
331 or return $self->ERROR( $self->protocol->errstr );
332 0 return $result_set;
333 }
334  
335 =head2 get_people( $user_id, $group_id, $option )
336  
337 my $people = $client->get_people( '@me', '@friends', { itemsPerPage => 10 } );
338 say $people->count;
339 for my $person ( @{ $people->items } ) {
340 say $person->get_field('id');
341 }
342  
343 =cut
344  
345 0 ▶sub get_people {
Subroutine get_people: not called CC=3 SCAR=24.8
Pod documented
346 0 my ( $self, $user_id, $group_id, $params ) = @_;
347 0 $self->clear_requests();
348 0 my $req_id = 'get_people';
349 0 my $req = Net::OpenSocial::Client::Request::FetchPeople->new( $user_id,
350 $group_id, $params );
351 0 $self->add_request( $req_id => $req );
352 0 ▶ my $result_set = $self->send() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $result_set = $self->send or return 0 0 0
353 0 my $result = $result_set->get_result($req_id);
354 0 ▶ return $self->ERROR( $result->message ) if $result->is_error;
Branch: 1 branch
BranchTrueFalseTotal
if $result->is_error 0 0 0
355 0 return $result->data;
356 }
357  
358 =head2 get_person( $user_id )
359  
360 my $person = $client->get_person('@me');
361 say $person->get_field('id');
362  
363 =cut
364  
365 0 ▶sub get_person {
Subroutine get_person: not called CC=3 SCAR=24.8
Pod documented
366 0 my ( $self, $user_id ) = @_;
367 0 $self->clear_requests();
368 0 my $req_id = 'get_person';
369 0 my $req = Net::OpenSocial::Client::Request::FetchPerson->new($user_id);
370 0 $self->add_request( $req_id => $req );
371 0 ▶ my $result_set = $self->send() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $result_set = $self->send or return 0 0 0
372 0 my $result = $result_set->get_result($req_id);
373 0 ▶ return $self->ERROR( $result->message ) if $result->is_error;
Branch: 1 branch
BranchTrueFalseTotal
if $result->is_error 0 0 0
374 0 return $result->data->first;
375 }
376  
377 =head2 get_friends( $user_id, $option )
378  
379 my $people = $client->get_people( '@me', { itemsPerPage => 10 } );
380 say $people->count;
381 for my $person ( @{ $people->items } ) {
382 say $person->get_field('id');
383 }
384  
385 =cut
386  
387 0 ▶sub get_friends {
Subroutine get_friends: not called CC=3 SCAR=24.8
Pod documented
388 0 my ( $self, $user_id, $params ) = @_;
389 0 $self->clear_requests();
390 0 my $req_id = 'get_friends';
391 0 my $req = Net::OpenSocial::Client::Request::FetchFriends->new( $user_id,
392 $params );
393 0 $self->add_request( $req_id => $req );
394 0 ▶ my $result_set = $self->send() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $result_set = $self->send or return 0 0 0
395 0 my $result = $result_set->get_result($req_id);
396 0 ▶ return $self->ERROR( $result->message ) if $result->is_error;
Branch: 1 branch
BranchTrueFalseTotal
if $result->is_error 0 0 0
397 0 return $result->data;
398 }
399  
400 =head2 get_person_appdata( $user_id, $option )
401  
402 returns L<Net::OpenSocial::Client::Resource::AppData>.
403  
404 my $appdata = $client->get_person_appdata('@me')
405 or die $client->errstr;
406 say $appdata->get_data($user_id, $key);
407  
408 =cut
409  
410 0 ▶sub get_person_appdata {
Subroutine get_person_appdata: not called CC=3 SCAR=24.8
Pod documented
411 0 my ( $self, $user_id, $params ) = @_;
412 0 $self->clear_requests();
413 0 my $req_id = 'get_person_appdata';
414 0 my $req
415 = Net::OpenSocial::Client::Request::FetchPersonAppData->new( $user_id, $params );
416 0 $self->add_request( $req_id => $req );
417 0 ▶ my $result_set = $self->send() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $result_set = $self->send or return 0 0 0
418 0 my $result = $result_set->get_result($req_id);
419 0 ▶ return $self->ERROR( $result->message ) if $result->is_error;
Branch: 1 branch
BranchTrueFalseTotal
if $result->is_error 0 0 0
420 0 return $result->data->first;
421 }
422  
423 =head2 get_friends_appdata( $user_id, $option )
424  
425 my $collection = $client->get_friends_appdata('@me')
426 or die $client->errstr;
427 for my $appdata ( @{ $collection->items } ) {
428 ...
429 }
430  
431 =cut
432  
433 0 ▶sub get_friends_appdata {
Subroutine get_friends_appdata: not called CC=3 SCAR=24.8
Pod documented
434 0 my ( $self, $user_id, $params ) = @_;
435 0 $self->clear_requests();
436 0 my $req_id = 'get_friens_appdata';
437 0 my $req = Net::OpenSocial::Client::Request::FetchFriendsAppData->new(
438 $user_id, $params );
439 0 $self->add_request( $req_id => $req );
440 0 ▶ my $result_set = $self->send() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $result_set = $self->send or return 0 0 0
441 0 my $result = $result_set->get_result($req_id);
442 0 ▶ return $self->ERROR( $result->message ) if $result->is_error;
Branch: 1 branch
BranchTrueFalseTotal
if $result->is_error 0 0 0
443 0 return $result->data;
444 }
445  
446 =head1 DEVELOPMENT
447  
448 I'm woking on github.
449 http://github.com/lyokato/p5-net-opensocial-client/tree/master
450  
451 =head1 SEE ALSO
452  
453 L<http://code.google.com/apis/opensocial/docs/>,
454 L<http://www.opensocial.org/Technical-Resources/opensocial-spec-v081/restful-protocol>,
455 L<http://www.opensocial.org/Technical-Resources/opensocial-spec-v081/rpc-protocol>,
456 L<http://code.google.com/apis/friendconnect/opensocial_rest_rpc.html>
457 L<http://blog.opensocial.org/2008/12/opensocial-now-friends-with-php-java.html>
458  
459 =head1 AUTHOR
460  
461 Lyo Kato, E<lt>lyo.kato@gmail.comE<gt>
462  
463 =head1 COPYRIGHT AND LICENSE
464  
465 Copyright (C) 2009 by Lyo Kato
466  
467 This library is free software; you can redistribute it and/or modify
468 it under the same terms as Perl itself, either Perl version 5.8.8 or,
469 at your option, any later version of Perl 5 you may have available.
470  
471 =cut
472  
473 1 ▶no Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
474 1 __PACKAGE__->meta->make_immutable;
475 1 1;
476  
« Client/HTTPRequestBuilder/ST.pm ↑ Summary Client/Resource/AppData.pm »