Request.pm

filter: Statementstmt 38.3% 28 / 73 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 36.8% 7 / 19 Podpod 100% 7 / 7 totaltotal 30.8% 42 / 136 CC 18 SCAR 46.2
CC
18
Cov
26%
_api_request
240.0
_is_token_required
6.0
_enable_tracing
6.0
CRAP 101.6
↑ Summary Collection.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::Request;
17  
18 10 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 10 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 10 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 10 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 10 ▶use REST::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 10 ▶use URI::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 10 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 10 my $json = JSON->new->allow_nonref;
27  
28 =head1 NAME
29  
30 Usergrid::Request - Role that provides HTTP invocation and utility methods
31  
32 =head1 DESCRIPTION
33  
34 This is a Role that is applied to L<Usergrid::Client> and L<Usergrid::Collection>
35 which provides methods that relate to HTTP invocation as well as some utility
36 functions.
37  
38 =head1 ATTRIBUTES
39  
40 =over 4
41  
42 =item organization (String)
43  
44 Organization name
45  
46 =item application (String)
47  
48 Application name
49  
50 =item api_url (String)
51  
52 URL of the Usergrid instance
53  
54 =item trace (Boolean)
55  
56 Enable/disable request and response tracing for debugging and troubleshooting
57 (Optional)
58  
59 =cut
60 10 has 'organization' => ( is => 'rw', isa => 'Str', required => 1);
61 10 has 'application' => ( is => 'rw', isa => 'Str', required => 1);
62 10 has 'api_url' => ( is => 'rw', isa => 'Str', required => 1);
63  
64 10 has 'trace' => ( is => 'rw', isa => 'Bool', trigger => \&_enable_tracing);
65  
66 10 has 'user_token' => ( is => 'rw');
67  
68 # internal method
69 0 ▶sub _is_token_required {
Subroutine _is_token_required: not called CC=2 SCAR=17.9
70 0 my ($self, $resource) = @_;
71 0 ▶ return 0 if $resource =~ m/\/management\/token/;
Branch: 1 branch
BranchTrueFalseTotal
if $resource =~ m[/management/token] 0 0 0
72 0 1;
73 }
74  
75 # internal method
76 0 ▶sub _api_request {
Subroutine _api_request: not called CC=15 SCAR=54.8
77 0 my ($self, $method, $resource, $request) = @_;
78  
79 0 $self->trace_message("$method $resource");
80 0 ▶ $self->trace_message("REQUEST: " . $self->prettify($request)) if ($request);
Branch: 1 branch
BranchTrueFalseTotal
if $request 0 0 0
81  
82 0 my $client = REST::Client->new();
83 0 $client->setHost($self->api_url);
84  
85 0 ▶ if ($self->_is_token_required($resource) == 1 && defined $self->user_token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_token_required($resource) == 1 and defined $self->user_token) 0 0 0
Condition0%0/3
$self->_is_token_required($resource) == 1 and defined $self->user_token
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_is_token_required($resource) == 1 and defined $self->user_token
$self->_is_token_required($resource) == 1 defined $self->user_token
Truth table0%0/3
A and B
A= $self->_is_token_required($resource) == 1B= defined $self->user_token
ABresult
0X0
100
111
86 $client->addHeader('Authorization',
87 0 'Bearer ' . $self->user_token->{'access_token'});
88 }
89  
90 0 ▶ my $json_req = $self->json_encode($request) if ($request);
Branch: 1 branch
BranchTrueFalseTotal
if $request 0 0 0
91  
92 0 ▶ $client->DELETE($resource) if ($method eq 'DELETE');
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DELETE" 0 0 0
93 0 ▶ $client->GET($resource) if ($method eq 'GET');
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "GET" 0 0 0
94 0 ▶ $client->POST($resource, $json_req) if ($method eq 'POST');
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "POST" 0 0 0
95 0 ▶ $client->PUT($resource, $json_req) if ($method eq 'PUT');
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "PUT" 0 0 0
96  
97 0 my $response = $client->responseContent();
98  
99 0 ▶ $self->trace_message("RESPONSE: " . $self->prettify($response)) if ($response);
Branch: 1 branch
BranchTrueFalseTotal
if $response 0 0 0
100  
101 0 ▶ return undef if ($client->responseCode() eq "404");
Branch: 1 branch
BranchTrueFalseTotal
if $client->responseCode eq 404 0 0 0
102  
103 0 ▶ confess "Bad request" if ($client->responseCode() eq "400");
Branch: 1 branch
BranchTrueFalseTotal
if $client->responseCode eq 400 0 0 0
104 0 ▶ confess "Unauthorized" if ($client->responseCode() eq "401");
Branch: 1 branch
BranchTrueFalseTotal
if $client->responseCode eq 401 0 0 0
105 0 ▶ confess "Forbidden" if ($client->responseCode() eq "403");
Branch: 1 branch
BranchTrueFalseTotal
if $client->responseCode eq 403 0 0 0
106 0 ▶ confess "Server error" if ($client->responseCode() eq "500");
Branch: 1 branch
BranchTrueFalseTotal
if $client->responseCode eq 500 0 0 0
107  
108 0 return $self->json_decode($response);
109 }
110  
111 # internal method
112 0 ▶sub _enable_tracing {
Subroutine _enable_tracing: not called CC=2 SCAR=17.9
113 0 my ($self, $state, $old_state) = @_;
114 0 ▶ if ($state) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state) 0 0 0
115 0 Log::Log4perl::easy_init($DEBUG);
116 0 our $logger = Log::Log4perl->get_logger();
117 }
118 }
119  
120 # internal method
121 0 ▶sub _collection {
Subroutine _collection: not called CC=1 SCAR=6.9
122 0 my ($self, $object, $uri) = @_;
123  
124 0 return Usergrid::Collection->new (
125 object => $object,
126 uri => $uri,
127 organization => $self->organization,
128 application => $self->application,
129 api_url => $self->api_url,
130 trace => $self->trace,
131 user_token => $self->user_token
132 );
133 }
134  
135 =back
136  
137 =head1 METHODS
138  
139 =head2 HTTP Invocation Methods
140  
141 =over 4
142  
143 =item DELETE ( $resource )
144  
145 Invokes HTTP DELETE on the specified resource.
146  
147 =cut
148 0 ▶sub DELETE {
Subroutine DELETE: not called CC=1 SCAR=6.9
149 0 my ($self, $resource) = @_;
150 0 $self->_api_request('DELETE', $resource);
151 }
152  
153 =item GET ( $resource )
154  
155 Invokes HTTP GET on the specified resource.
156  
157 =cut
158 0 ▶sub GET {
Subroutine GET: not called CC=1 SCAR=6.9
Pod documented
159 0 my ($self, $resource) = @_;
160 0 $self->_api_request('GET', $resource);
161 }
162  
163 =item POST ( $resource, \%request )
164  
165 Invokes HTTP POST on the specified resource and passes in the payload
166 for the request.
167  
168 =cut
169 0 ▶sub POST {
Subroutine POST: not called CC=1 SCAR=6.9
Pod documented
170 0 my ($self, $resource, $request) = @_;
171 0 $self->_api_request('POST', $resource, $request);
172 }
173  
174 =item PUT ( $resource, \%request )
175  
176 Invokes HTTP PUT on the specified resource and passes in the payload
177 for the request.
178  
179 =cut
180 0 ▶sub PUT {
Subroutine PUT: not called CC=1 SCAR=6.9
Pod documented
181 0 my ($self, $resource, $request) = @_;
182 0 $self->_api_request('PUT', $resource, $request);
183 }
184  
185 =back
186  
187 =head2 Utility Methods
188  
189 =over 4
190  
191 =item trace_message ( $message )
192  
193 Utility method to log a message to console if tracing is enabled.
194  
195 =cut
196 0 ▶sub trace_message {
Subroutine trace_message: not called CC=2 SCAR=17.9
Pod documented
197 0 my ($self, $message) = @_;
198 0 ▶ $Usergrid::Request::logger->debug($message) if (defined $Usergrid::Request::logger);
Branch: 1 branch
BranchTrueFalseTotal
if defined $Usergrid::Request::logger 0 0 0
199 }
200  
201 =item prettify ( $message, \%object )
202  
203 Returns a prettified string representation hash reference.
204  
205 =cut
206 0 ▶sub prettify {
Subroutine prettify: not called CC=1 SCAR=6.9
Pod documented
207 0 my ($self, $json_obj) = @_;
208 0 return $json->pretty->encode($json_obj);
209 }
210  
211 =item json_encode ( \%hashref )
212  
213 Returns a JSON object from a hash reference.
214  
215 =cut
216 0 ▶sub json_encode {
Subroutine json_encode: not called CC=1 SCAR=6.9
Pod documented
217 0 my ($self, $json_obj) = @_;
218 0 $json->encode($json_obj);
219 }
220  
221 =item json_decode ( $json_object )
222  
223 Returns a hash reference from a JSON object.
224  
225 =cut
226 0 ▶sub json_decode {
Subroutine json_decode: not called CC=1 SCAR=6.9
Pod documented
227 0 my ($self, $json_obj) = @_;
228 0 $json->decode($json_obj);
229 }
230  
231 10 1;
232  
233 __END__
↑ Summary Collection.pm »