Client.pm

filter: Statementstmt 24.6% 16 / 65 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 22.2% 4 / 18 Podpod 92.8% 13 / 14 totaltotal 29.2% 33 / 113 CC 9 SCAR 16.8
CC
9
Cov
20%
update_collection
12.0
delete_collection
12.0
get_entity
6.0
CRAP 5.4
« Collection.pm ↑ Summary Entity.pm »
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15  
16 package Usergrid::Client;
17  
18 10 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 10 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 10 ▶use Usergrid::Entity;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 10 ▶use Usergrid::Collection;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 10 with (
24 'Usergrid::Request',
25 );
26  
27 10 our $VERSION = '0.22';
28  
29 =head1 NAME
30  
31 Usergrid::Client - Usergrid Perl Client
32  
33 =head1 SYNOPSIS
34  
35 use Usergrid::Client;
36  
37 my $client = Usergrid::Client->new(
38 organization => 'test-organization',
39 application => 'test-app',
40 api_url => 'http://localhost:8080'
41 );
42  
43 $client->login('johndoe', 'Johndoe123$');
44  
45 $client->add_entity("books", { name => "Ulysses" });
46 $client->add_entity("books", { name => "Neuromancer" });
47  
48 my $books = $client->get_collection("books");
49  
50 while ($books->has_next_entity()) {
51 my $book = $books->get_next_entity();
52  
53 print "Name: " . $book->get('name') . ", ";
54 print "Author: " . $book->get('author') . "\n";
55  
56 $book->set("in-stock", 0);
57 $client->update_entity($book);
58 }
59  
60 =head1 DESCRIPTION
61  
62 Usergrid::Client provides an easy to use Perl API for Apache Usergrid.
63  
64 =head1 WHAT IS APACHE USERGRID
65  
66 Usergrid is an open-source Backend-as-a-Service ("BaaS" or "mBaaS") composed of
67 an integrated distributed NoSQL database, application layer and client tier with
68 SDKs for developers looking to rapidly build web and/or mobile applications.
69 It provides elementary services (user registration & management, data storage,
70 file storage, queues) and retrieval features (full text search, geolocation
71 search, joins) to power common app features.
72  
73 It is a multi-tenant system designed for deployment to public cloud environments
74 (such as Amazon Web Services, Rackspace, etc.) or to run on traditional server
75 infrastructures so that anyone can run their own private BaaS deployment.
76  
77 For architects and back-end teams, it aims to provide a distributed, easily
78 extendable, operationally predictable and highly scalable solution. For
79 front-end developers, it aims to simplify the development process by enabling
80 them to rapidly build and operate mobile and web applications without requiring
81 backend expertise.
82  
83 Source: L<https://usergrid.incubator.apache.org/docs/>
84  
85 For more information, visit L<http://www.usergrid.org>
86  
87 =head1 ATTRIBUTES
88  
89 The following attributes are made available via the L<Usergrid::Request> role:
90  
91 =over 4
92  
93 =item organization (String)
94  
95 Organization name
96  
97 =item application (String)
98  
99 Application name
100  
101 =item api_url (String)
102  
103 URL of the Usergrid instance
104  
105 =item trace (Boolean)
106  
107 Enable/disable request and response tracing for debugging and troubleshooting
108 (Optional)
109  
110 =back
111  
112 =head1 METHODS
113  
114 The following methods are provided in this API for interacting with the Apache
115 Usergrid backend.
116  
117 =head2 Authentication
118  
119 =over 4
120  
121 =item login ( $username, $password )
122  
123 Performs application user authentication. Returns the user token for the
124 logged in user. The token is also kept in memory and used for subsequent
125 authentication of requests.
126  
127 =cut
128 0 ▶sub login {
Subroutine login: not called CC=1 SCAR=6.9
Pod documented
129 0 my ($self, $username, $password) = @_;
130  
131 0 my %request = (
132 grant_type => "password",
133 username => $username,
134 password => $password
135 );
136  
137 0 my $uri = URI::Template
138 ->new('/{organization}/{application}/token')
139 ->process(
140 organization => $self->organization,
141 application => $self->application
142 );
143  
144 0 my $token = $self->POST($uri, \%request);
145  
146 0 $self->user_token($token);
147  
148 0 return $self->user_token;
149 }
150  
151 =item admin_login ( $username, $password )
152  
153 Performs admin user authentication. Returns the user token for the
154 logged in user. The token is also kept in memory and used for subsequent
155 authentication of requests.
156  
157 =cut
158 0 ▶sub management_login {
Subroutine management_login: not called CC=1 SCAR=6.9
Pod undocumented
159 0 my ($self, $username, $password) = @_;
160  
161 0 my %request = (
162 grant_type => "password",
163 username => $username,
164 password => $password
165 );
166  
167 0 my $token = $self->POST('/management/token', \%request);
168  
169 0 $self->user_token($token);
170  
171 0 return $self->user_token;
172 }
173  
174 =back
175  
176 =head2 Entities
177  
178 This section covers some of the entity level methods available in the API.
179 Entities form one of the basic building blocks of Usergrid and is analogous to
180 a row in an RDBMS table.
181  
182 =over 4
183  
184 =item add_entity ( $collection, \%entity )
185  
186 Creates a new entity within the specified collection. Returns a L<Usergrid::Entity>
187 for the newly added entity.
188  
189 =cut
190 0 ▶sub add_entity {
Subroutine add_entity: not called CC=1 SCAR=6.9
Pod documented
191 0 my ($self, $collection, $entity) = @_;
192  
193 0 my $uri = URI::Template
194 ->new('/{organization}/{application}/{collection}')
195 ->process(
196 organization => $self->organization,
197 application => $self->application,
198 collection => $collection
199 );
200  
201 0 return Usergrid::Entity->new( object => $self->POST($uri, $entity));
202 }
203  
204 =item update_entity ( L<Usergrid::Entity> )
205  
206 Saves changes to the given entity. Returns the updated L<Usergrid::Entity>.
207  
208 =cut
209 0 ▶sub update_entity {
Subroutine update_entity: not called CC=1 SCAR=6.9
Pod documented
210 0 my ($self, $entity) = @_;
211  
212 0 my $uri = URI::Template
213 ->new('/{organization}/{application}/{collection}/{uuid}')
214 ->process(
215 organization => $self->organization,
216 application => $self->application,
217 collection => $entity->get('type'),
218 uuid => $entity->get('uuid')
219 );
220  
221 return Usergrid::Entity->new( object => $self->PUT($uri,
222 0 $entity->object->{'entities'}[0]) );
223 }
224  
225 =item get_entity ( $collection, $id )
226  
227 Returns a L<Usergrid::Entity> identified by either UUID or name.
228 If the entity does not exist, the method returns FALSE.
229  
230 =cut
231 0 ▶sub get_entity {
Subroutine get_entity: not called CC=2 SCAR=17.9
Pod documented
232 0 my ($self, $collection, $id_or_name) = @_;
233  
234 0 my $uri = URI::Template
235 ->new('/{organization}/{application}/{collection}/{id_or_name}')
236 ->process(
237 organization => $self->organization,
238 application => $self->application,
239 collection => $collection,
240 id_or_name => $id_or_name
241 );
242  
243 0 my $response = $self->GET($uri);
244  
245 0 ▶ return undef if (! $response);
Branch: 1 branch
BranchTrueFalseTotal
unless $response 0 0 0
246  
247 0 return Usergrid::Entity->new( object => $response );
248 }
249  
250 =item delete_entity_by_id ( $collection, $id )
251  
252 Deletes an entity from the collection identified by either UUID or name. Returns
253 a L<Usergrid::Entity> of the deleted entity.
254  
255 =cut
256 0 ▶sub delete_entity_by_id {
Subroutine delete_entity_by_id: not called CC=1 SCAR=6.9
Pod documented
257 0 my ($self, $collection, $id_or_name) = @_;
258  
259 0 my $uri = URI::Template
260 ->new('/{organization}/{application}/{collection}/{id}')
261 ->process(
262 organization => $self->organization,
263 application => $self->application,
264 collection => $collection,
265 id => $id_or_name
266 );
267  
268 0 return Usergrid::Entity->new( object => $self->DELETE($uri) );
269 }
270  
271 =item delete_entity ( L<Usergrid::Entity> )
272  
273 Deletes the specified L<Usergrid::Entity>. Returns an instance of the deleted
274 L<Usergrid::Entity> if successful.
275  
276 =cut
277 0 ▶sub delete_entity {
Subroutine delete_entity: not called CC=1 SCAR=6.9
Pod documented
278 0 my ($self, $entity) = @_;
279  
280 0 my $uri = URI::Template
281 ->new('/{organization}/{application}/{collection}/{uuid}')
282 ->process(
283 organization => $self->organization,
284 application => $self->application,
285 collection => $entity->get('type'),
286 uuid => $entity->get('uuid')
287 );
288  
289 0 return Usergrid::Entity->new( object => $self->DELETE($uri) );
290 }
291  
292 =item connect_entities ( L<Usergrid::Entity>, $relationship, L<Usergrid::Entity> )
293  
294 Creates a connection from Entity #1 to Entity #2 signified by the relationship.
295 The first entity in this relationship is identified as the connecting entity and
296 the second as the connected entity. The relationship is a string that signifies
297 the type of connection. This returns a L<Usergrid::Entity> of the connecting
298 entity.
299  
300 =cut
301 0 ▶sub connect_entities {
Subroutine connect_entities: not called CC=1 SCAR=6.9
Pod documented
302 0 my ($self, $connecting, $relationship, $connected) = @_;
303  
304 0 my $uri = URI::Template
305 ->new ('/{organization}/{application}/{connecting_collection}/{connecting}/{relationship}/{connected_collection}/{connected}')
306 ->process(
307 organization => $self->organization,
308 application => $self->application,
309 connecting_collection => $connecting->get('type'),
310 connecting => $connecting->get('uuid'),
311 relationship => $relationship,
312 connected_collection => $connected->get('type'),
313 connected => $connected->get('uuid')
314 );
315  
316 0 return Usergrid::Entity->new ( object => $self->POST($uri));
317 }
318  
319 =item disconnect_entities ( L<Usergrid::Entity>, $relationship, L<Usergrid::Entity> )
320  
321 Removes the connection between the two entities signified by the relationship.
322 This does not affect the entities in any other way apart from the removal of the
323 connection that is depicted by the relationship. This returns a L<Usergrid::Entity>
324 of the connecting entity with the given relationship removed.
325  
326 =cut
327 0 ▶sub disconnect_entities {
Subroutine disconnect_entities: not called CC=1 SCAR=6.9
Pod documented
328 0 my ($self, $connecting, $relationship, $connected) = @_;
329  
330 0 my $uri = URI::Template
331 ->new ('/{organization}/{application}/{connecting_collection}/{connecting}/{relationship}/{connected_collection}/{connected}')
332 ->process(
333 organization => $self->organization,
334 application => $self->application,
335 connecting_collection => $connecting->get('type'),
336 connecting => $connecting->get('uuid'),
337 relationship => $relationship,
338 connected_collection => $connected->get('type'),
339 connected => $connected->get('uuid')
340 );
341  
342 0 return Usergrid::Entity->new ( object => $self->DELETE($uri));
343 }
344  
345 =back
346  
347 =head2 Collections
348  
349 This section covers the methods related to retrieving and working with
350 collections in the Usergrid API. Collections contains groups of entities and is
351 analogous to a table in an RDBMS.
352  
353 =over 4
354  
355 =item get_collection ( $collection, [ $limit ] )
356  
357 Returns a L<Usergrid::Collection> with the list of entities up to the maximum
358 specified limit, which is 10 if not provided.
359  
360 =cut
361 0 ▶sub get_collection {
Subroutine get_collection: not called CC=2 SCAR=17.9
Pod documented
362 0 my ($self, $collection, $limit) = @_;
363  
364 0 ▶ my $uri = URI::Template
Branch: 1 branch
BranchTrueFalseTotal
defined $limit ? : 0 0 0
365 ->new('/{organization}/{application}/{collection}?limit={limit}')
366 ->process(
367 organization => $self->organization,
368 application => $self->application,
369 collection => $collection,
370 limit => ( defined $limit ) ? $limit: 10
371 );
372  
373 0 return $self->_collection($self->GET($uri), $uri);
374 }
375  
376 =item update_collection ( $collection, \%attributes, [ $query ], [ $limit ] )
377  
378 Updates all the entities in the specified collection with the provided attributes.
379 Optionally pass in the SQL-like query to narrow the scope of the objects that
380 are affected. This also supports specifying a limit to restrict the maximum number of
381 records that are updated. If not specified, the limit defaults to 10 entities.
382  
383 =cut
384 0 ▶sub update_collection {
Subroutine update_collection: not called CC=3 SCAR=24.8
Pod documented
385 0 my ($self, $collection, $properties, $query, $limit) = @_;
386  
387 0 ▶ my $uri = URI::Template
Branch: 2 branches
BranchTrueFalseTotal
defined $limit ? : 0 0 0
defined $query ? : 0 0 0
388 ->new('/{organization}/{application}/{collection}/?limit={limit}&ql={query}')
389 ->process(
390 organization => $self->organization,
391 application => $self->application,
392 collection => $collection,
393 limit => ( defined $limit ) ? $limit : 10,
394 query => ( defined $query ) ? $query : undef
395 );
396  
397 0 return $self->_collection($self->PUT($uri, $properties), $uri);
398 }
399  
400 =item delete_collection ( $collection, [ $query ], [ $limit ] )
401  
402 Batch delete entities in the specified collection. Optionally pass in a SQL-like
403 query to narrow the scope of the objects that are affected and a limit to restrict
404 the maximum number of records that are deleted. If not specified, the limit
405 defaults to 10 entities.
406  
407 =cut
408 0 ▶sub delete_collection {
Subroutine delete_collection: not called CC=3 SCAR=24.8
Pod documented
409 0 my ($self, $collection, $query, $limit) = @_;
410  
411 0 ▶ my $uri = URI::Template
Branch: 2 branches
BranchTrueFalseTotal
defined $limit ? : 0 0 0
defined $query ? : 0 0 0
412 ->new('/{organization}/{application}/{collection}/?limit={limit}&ql={query}')
413 ->process(
414 organization => $self->organization,
415 application => $self->application,
416 collection => $collection,
417 limit => ( defined $limit ) ? $limit : 10,
418 query => ( defined $query ) ? $query : undef
419 );
420  
421 0 return $self->_collection($self->DELETE($uri), $uri);
422 }
423  
424 =item query_collection ( $collection, $query, [ $limit ] )
425  
426 Queries all the entities in the specified collection using a SQL-like query string.
427 This also supports specifying a limit to restrict the maximum number of
428 records that are returned. If not specified, the limit defaults to 10 entities.
429  
430 =cut
431 0 ▶sub query_collection {
Subroutine query_collection: not called CC=2 SCAR=17.9
Pod documented
432 0 my ($self, $collection, $query, $limit) = @_;
433  
434 0 ▶ my $uri = URI::Template
Branch: 1 branch
BranchTrueFalseTotal
defined $limit ? : 0 0 0
435 ->new('/{organization}/{application}/{collection}?limit={limit}&ql={ql}')
436 ->process(
437 organization => $self->organization,
438 application => $self->application,
439 collection => $collection,
440 limit => ( defined $limit ) ? $limit : 10,
441 ql => $query
442 );
443  
444 0 return $self->_collection($self->GET($uri), $uri);
445 }
446  
447 =item query_connections ( $entity, $relationship, [ $query ], [ $limit ] )
448  
449 Returns a collection of entities for the given relationship, optionally filtered
450 by a SQL-like query and limited to the maximum number of records specified. If no limit
451 is provided, a default of 10 entities is assumed.
452  
453 =cut
454 0 ▶sub query_connections {
Subroutine query_connections: not called CC=2 SCAR=17.9
Pod documented
455 0 my ($self, $entity, $relationship, $query, $limit) = @_;
456  
457 0 ▶ my $uri = URI::Template
Branch: 1 branch
BranchTrueFalseTotal
defined $limit ? : 0 0 0
458 ->new('/{organization}/{application}/{collection}/{id}/{relationship}?limit={limit}&ql={ql}')
459 ->process(
460 organization => $self->organization,
461 application => $self->application,
462 collection => $entity->get('type'),
463 id => $entity->get('uuid'),
464 relationship => $relationship,
465 limit => ( defined $limit ) ? $limit : 10,
466 ql => $query
467 );
468  
469 0 return $self->_collection($self->GET($uri), $uri);
470 }
471  
472 10 __PACKAGE__->meta->make_immutable;
473  
474 10 1;
475  
476 __END__
« Collection.pm ↑ Summary Entity.pm »