Simplify/SimplifyApi.pm untested

filter: Statementstmt 0.0% 0 / 186 Branchbran 0.0% 0 / 88 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 335 CC 45 SCAR 50.5
CC
45
Cov
0%
send_api_request
342.0
send_auth_request
210.0
_build_url
110.0
CRAP 156.2
1  
2 package Net::Simplify::SimplifyApi;
3  
4 =head1 NAME
5  
6 Net::Simplify::SimplifyApi - Simplify Commerce module for API requests
7  
8 =head1 SEE ALSO
9  
10 L<Net::Simplify>,
11 L<http://www.simplify.com>
12  
13 =head1 VERSION
14  
15 1.6.0
16  
17 =head1 LICENSE
18  
19 Copyright (c) 2013 - 2022 MasterCard International Incorporated
20 All rights reserved.
21  
22 Redistribution and use in source and binary forms, with or without modification, are
23 permitted provided that the following conditions are met:
24  
25 Redistributions of source code must retain the above copyright notice, this list of
26 conditions and the following disclaimer.
27 Redistributions in binary form must reproduce the above copyright notice, this list of
28 conditions and the following disclaimer in the documentation and/or other materials
29 provided with the distribution.
30 Neither the name of the MasterCard International Incorporated nor the names of its
31 contributors may be used to endorse or promote products derived from this software
32 without specific prior written permission.
33 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
34 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
36 SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
37 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
38 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
39 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
40 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 SUCH DAMAGE.
43  
44 =cut
45  
46 use 5.006;
47 use strict;
48 use warnings FATAL => 'all';
49  
50 use Net::Simplify;
51 use Net::Simplify::Constants;
52 use Net::Simplify::Jws;
53  
54 use JSON;
55 use URI::Encode qw(uri_encode);
56 use REST::Client;
57 use Mozilla::CA;
58 use MIME::Base64 qw(encode_base64url);
59 use Carp;
60  
61 sub send_api_request {
62  
63 my ($class, $domain, $op, $params, $auth) = @_;
64  
65 _check_auth($auth);
66  
67 my $url = _build_url($domain, $op, $params, $auth);
68  
69 my $jws_payload;
70 eval {
71 $jws_payload = encode_json($params) if defined $params;
72 };
73 if ($@) {
74 croak(Net::Simplify::BadRequestException->new("Error encoding parameters as JSON: " . $@));
75 }
76
77 my $jws_message = Net::Simplify::Jws->encode($url, $jws_payload, $auth);
78  
79 my $user_agent = "Perl-SDK/${Net::Simplify::Constants::VERSION}";
80 if (defined $Net::Simplify::user_agent) {
81 $user_agent .= " ${Net::Simplify::user_agent}";
82 }
83  
84 my $headers = {
85 'Content-Type' => 'application/json',
86 'Accept' => 'application/json',
87 'User-Agent' => $user_agent
88 };
89  
90 my $method;
91 my $payload = undef;
92 if ($op eq 'create') {
93 $method = 'POST';
94 $payload = $jws_message;
95 } elsif ($op eq 'update') {
96 $method = 'PUT';
97 $payload = $jws_message;
98 } elsif ($op eq 'find') {
99 $method = 'GET';
100 $headers->{Authorization} = "JWS ${jws_message}";
101 } elsif ($op eq 'list') {
102 $method = 'GET';
103 $headers->{Authorization} = "JWS ${jws_message}";
104 } elsif ($op eq 'delete') {
105 $method = 'DELETE';
106 $headers->{Authorization} = "JWS ${jws_message}";
107 }
108  
109 my $client = REST::Client->new();
110 $client->request($method, $url, $payload, $headers);
111  
112 my $code = $client->responseCode();
113  
114 if ($code == 200) {
115 my $response = $client->responseContent();
116  
117 my $result;
118 eval {
119 $result = decode_json $response;
120 };
121 if ($@) {
122 croak(Net::Simplify::SystemException->new("Error decoding JSON response data: " . $@. "Response message: " . $response));
123 }
124  
125 return $result;
126  
127 } else {
128 my $response = $client->responseContent();
129  
130 my $error;
131 eval {
132 $error = decode_json $response;
133 };
134 if ($@) {
135 croak(Net::Simplify::SystemException->new("Error decoding JSON error data: " . $@ . "Response message: " . $response));
136 }
137 if ($code >= 300 && $code < 400) {
138 croak(Net::Simplify::BadRequestException->new("Unexpected response code returned from API, have you got the correct URL?", $code));
139 } elsif ($code == 400) {
140 croak(Net::Simplify::BadRequestException->new("Bad request", $code, $error));
141 } elsif ($code == 401) {
142 croak(Net::Simplify::AuthenticationException->new("You are not authorized to make this request, are you using the correct keys?", $code, $error));
143 } elsif ($code == 404) {
144 croak(Net::Simplify::ObjectNotFoundException->new("Object not found", $code, $error));
145 } elsif ($code == 405) {
146 croak(Net::Simplify::NotAllowedException->new("Operation not allowed", $code, $error));
147 } elsif ($code < 500) {
148 croak(Net::Simplify::BadRequestException->new("Bad request", $code));
149 } else {
150 croak(Net::Simplify::SystemException->new("An unexpected error has been rased for an API request", $code, $error));
151 }
152 }
153 }
154  
155 sub send_auth_request {
156 my ($class, $params, $context, $auth) = @_;
157  
158 _check_auth($auth);
159  
160 if (defined $auth->access_token) {
161 croak(Net::Simplify::IllegalArgumentException->new("authentication object should not contain access token for Oauth requests"));
162 }
163  
164 my $url = $Net::Simplify::oauth_base_url . "/" . $context;
165  
166 my $payload = join('&', map { $_ . '=' . uri_encode($params->{$_})} keys %$params);
167  
168 my $jws_message = Net::Simplify::Jws->encode($url, $payload, $auth);
169  
170 my $client = REST::Client->new();
171  
172 my $user_agent = "Perl-SDK/${Net::Simplify::Constants::VERSION}";
173 if (defined $Net::Simplify::user_agent) {
174 $user_agent .= " ${Net::Simplify::user_agent}";
175 }
176  
177 my $headers = {
178 'Content-Type' => 'application/json',
179 'Accept' => 'application/json',
180 'User-Agent' => $user_agent
181 };
182  
183 $client->request('POST', $url, $jws_message, $headers);
184  
185 my $code = $client->responseCode();
186  
187 if ($code == 200) {
188 my $response = $client->responseContent();
189  
190 my $result;
191 eval {
192 $result = decode_json $response;
193 };
194 if ($@) {
195 croak(Net::Simplify::SystemException->new("Error decoding JSON response data: " . $@ . "Response Message: " . $response));
196 }
197  
198 return $result;
199  
200 } else {
201  
202 if ($code >= 300 && $code < 400) {
203 croak(Net::Simplify::BadRequestException->new("Unexpected response code returned from OAuth API, have you got the correct URL?", $code));
204 } elsif ($code >= 400 && $code < 500) {
205
206 my $error;
207 eval {
208 $error = decode_json $client->responseContent();
209 };
210 if ($@) {
211 croak(Net::Simplify::SystemException->new("Error decoding JSON error data: " . $@ . "Response: Message " . $client->responseContent()));
212 }
213 my $error_code = $error->{error};
214 my $error_desc = $error->{error_description};
215  
216 if ($error_code eq 'invalid_request') {
217 croak(Net::Simplify::BadRequestException->new('', $code, _get_oauth_error("Error during OAuth request", $error_code, $error_desc)));
218 } elsif ($error_code eq 'access_denied') {
219 croak(Net::Simplify::AuthenticationException->new('', $code, _get_oauth_error("Access denied for OAuth request", $error_code, $error_desc)));
220 } elsif ($error_code eq 'invalid_client') {
221 croak(Net::Simplify::AuthenticationException->new('', $code, _get_oauth_error("Invalid client ID in OAuth request", $error_code, $error_desc)));
222  
223 } elsif ($error_code eq 'unauthorized_client') {
224 croak(Net::Simplify::AuthenticationException->new('', $code, _get_oauth_error("Unauthorized client in OAuth request", $error_code, $error_desc)));
225  
226 } elsif ($error_code eq 'unsupported_grant_type') {
227 croak(Net::Simplify::BadRequestException->new('', $code, _get_oauth_error("Unsupported grant type in OAuth request", $error_code, $error_desc)));
228 } elsif ($error_code eq 'invalid_scope') {
229 croak(Net::Simplify::BadRequestException->new('', $code, _get_oauth_error("Invalid scope in OAuth request", $error_code, $error_desc)));
230 } else {
231 croak(Net::Simplify::BadRequestException->new('', $code, _get_oauth_error("Unknown OAuth error", $error_code, $error_desc)));
232 }
233 } else {
234 croak(Net::Simplify::SystemException->new("An unexpected error has been raised for an OAuth request", $code));
235 }
236 }
237 }
238  
239 sub _get_oauth_error {
240 my ($msg, $error_code, $error_desc) = @_;
241  
242 my $error = {
243 error => {
244 code => 'oauth_error',
245 message => "${msg}, error code '${error_code}', description '${error_desc}'"
246 }
247 };
248
249 $error;
250 }
251
252  
253 sub get_authentication {
254 my ($class, $auth) = @_;
255  
256 $auth = Net::Simplify::Authentication->create() unless defined $auth;
257  
258 if (! $auth->isa('Net::Simplify::Authentication')) {
259 croak(Net::Simplify::IllegalArgumentException->new("authentication object is not a Net::Simplify::Authentication object"));
260 }
261
262 $auth;
263 }
264  
265  
266 sub decode_event {
267 my ($class, $params, $auth) = @_;
268  
269 _check_auth($auth);
270 _check_param($params, 'payload');
271  
272 Net::Simplify::Jws->decode($params->{payload}, $params->{url}, $auth);
273 }
274  
275  
276 sub is_live_key {
277 my ($class, $public_key) = @_;
278  
279 _is_live_key($public_key);
280 }
281  
282  
283 sub _check_auth {
284 my ($auth) = @_;
285  
286 if (!defined $auth->{public_key}) {
287 croak(Net::Simplify::IllegalArgumentException->new("No public key"));
288 }
289  
290 if (!defined $auth->{private_key}) {
291 croak(Net::Simplify::IllegalArgumentException->new("No private key"));
292 }
293 }
294  
295  
296 sub _check_param {
297 my ($params, $name) = @_;
298  
299 if (!defined($params->{$name})) {
300 croak(Net::Simplify::IllegalArgumentException->new("Missing paramater '${name}'"));
301 }
302 }
303
304  
305 sub _build_url {
306  
307 my ($domain, $op, $params, $auth) = @_;
308 my $url;
309 eval{
310 $url = $Net::Simplify::api_base_sandbox_url;
311 if (_is_live_key($auth->public_key)) {
312 $url = $Net::Simplify::api_base_live_url;
313 }
314 if ($op eq 'find' || $op eq 'update' || $op eq 'delete') {
315 my $id = $params->{id};
316 $url .= "/${domain}/${id}"
317 } else {
318 $url .= "/${domain}"
319 }
320  
321 if ($op eq 'list' && defined $params) {
322 my @P = ();
323  
324 if (defined $params->{offset}) {
325 my $v = $params->{offset};
326 push(@P, uri_encode("offset=${v}"));
327 }
328 if (defined $params->{max}) {
329 my $v = $params->{max};
330 push(@P, uri_encode("max=${v}"));
331 }
332 if (defined $params->{sorting}) {
333 while (my ($k, $v) = each(%{$params->{sorting}})) {
334 push(@P, uri_encode("sorting[${k}]=${v}"));
335 }
336 }
337 if (defined $params->{filter}) {
338 while (my ($k, $v) = each(%{$params->{filter}})) {
339 push(@P, uri_encode("filter[${k}]=${v}"));
340 }
341 }
342 $url .= "?" . join('&', @P) if @P;
343 }
344 };
345 if ($@) {
346 croak(Net::Simplify::BadRequestException->new("Error building requests URL from parameters: " . $@));
347 }
348  
349 $url;
350 }
351  
352  
353 sub _is_live_key {
354 my ($public_key) = @_;
355  
356 $public_key =~ /^lvpb_/;
357 }
358  
359  
360 1;