blib/lib/AsposePdfCloud/ApiClient.pm

filter: Statementstmt 33.9% 58 / 171 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 57.5% 19 / 33 Podpod 0.0% 0 / 14 totaltotal 22.7% 77 / 339 SCAR 70.6
CC
48
Cov
21%
call_api
272.0
deserialize
182.0
pre_deserialize
42.0
CRAP 1170.1
« blib/lib/AsposePdfCloud/PdfApi.pm ↑ Summary blib/lib/AsposePdfCloud/Object/BaseObject.pm »
1 package AsposePdfCloud::ApiClient;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use HTTP::Request::Common qw(DELETE POST GET HEAD PUT);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use HTTP::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use URI::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Module::Runtime qw(use_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Digest::HMAC_SHA1;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use AsposePdfCloud::Configuration;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 sub new
26 {
27
28 0 ▶ if(not defined $AsposePdfCloud::Configuration::app_sid or $AsposePdfCloud::Configuration::app_sid eq ''){
Subroutine new: not called CC=5 SCAR=34.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $AsposePdfCloud::Configuration::app_sid or $AsposePdfCloud::Configuration::app_sid eq "") 0 0 0
Condition0%0/3
not defined $AsposePdfCloud::Configuration::app_sid or $AsposePdfCloud::Configuration::app_sid eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $AsposePdfCloud::Configuration::app_sid or $AsposePdfCloud::Configuration::app_sid eq ""
not defined $AsposePdfCloud::Configuration::app_sid $AsposePdfCloud::Configuration::app_sid eq ""
Truth table0%0/3
not(A) or B
A= not defined $AsposePdfCloud::Configuration::app_sidB= $AsposePdfCloud::Configuration::app_sid eq ""
ABresult
1X1
011
000
29 0 croak("Aspose Cloud App SID key is missing.");
30 }
31
32 0 ▶ if(not defined $AsposePdfCloud::Configuration::api_key or $AsposePdfCloud::Configuration::api_key eq ''){
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $AsposePdfCloud::Configuration::api_key or $AsposePdfCloud::Configuration::api_key eq "") 0 0 0
Condition0%0/3
not defined $AsposePdfCloud::Configuration::api_key or $AsposePdfCloud::Configuration::api_key eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $AsposePdfCloud::Configuration::api_key or $AsposePdfCloud::Configuration::api_key eq ""
not defined $AsposePdfCloud::Configuration::api_key $AsposePdfCloud::Configuration::api_key eq ""
Truth table0%0/3
not(A) or B
A= not defined $AsposePdfCloud::Configuration::api_keyB= $AsposePdfCloud::Configuration::api_key eq ""
ABresult
1X1
011
000
33 0 croak("Aspose Cloud API key is missing.");
34 }
35
36 0 my $class = shift;
37 0 my (%args) = (
38 'ua' => LWP::UserAgent->new,
39 'base_url' => $AsposePdfCloud::Configuration::api_server,
40 @_
41 );
42
43 0 return bless \%args, $class;
44 }
45  
46 # Set the user agent of the API client
47 #
48 # @param string $user_agent The user agent of the API client
49 #
50 sub set_user_agent {
51 0 ▶ my ($self, $user_agent) = @_;
Subroutine set_user_agent: not called CC=1 SCAR=6.9
Pod undocumented
52 0 $self->{http_user_agent}= $user_agent;
53 }
54  
55 # Set timeout
56 #
57 # @param integer $seconds Number of seconds before timing out [set to 0 for no timeout]
58 #
59 sub set_timeout {
60 0 ▶ my ($self, $seconds) = @_;
Subroutine set_timeout: not called CC=2 SCAR=17.9
Pod undocumented
61 0 ▶ if (!looks_like_number($seconds)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (looks_like_number($seconds)) 0 0 0
62 0 croak('Timeout variable must be numeric.');
63 }
64 0 $self->{http_timeout} = $seconds;
65 }
66  
67 # make the HTTP request
68 # @param string $resourcePath path to method endpoint
69 # @param string $method method to call
70 # @param array $queryParams parameters to be place in query URL
71 # @param array $postData parameters to be placed in POST body
72 # @param array $headerParams parameters to be place in request header
73 # @return mixed
74 sub call_api {
75 0 ▶ my $self = shift;
Subroutine call_api: not called CC=16 SCAR=56.1
Pod undocumented
76 0 my ($resource_path, $method, $query_params, $post_params, $header_params, $body_data, $auth_settings) = @_;
77
78 0 $resource_path =~ s/\Q{appSid}\E/$AsposePdfCloud::Configuration::app_sid/g;
79
80 0 my $_url = $self->{base_url} . $resource_path;
81
82 # update signature
83 0 my $signature = $self->sign($_url, $AsposePdfCloud::Configuration::app_sid, $AsposePdfCloud::Configuration::api_key);
84
85 0 $_url = $_url . '&signature=' . $signature;
86
87 0 ▶ if($AsposePdfCloud::Configuration::debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($AsposePdfCloud::Configuration::debug) 0 0 0
88 0 print "\nFinal URL: ".$method."::".$_url."\n";
89 }
90 # body data
91 0 ▶ $body_data = to_json($body_data->to_hash) if defined $body_data && $body_data->can('to_hash'); # model to json string
Branch: 1 branch
BranchTrueFalseTotal
if defined $body_data and $body_data->can("to_hash") 0 0 0
Condition0%0/3
defined $body_data and $body_data->can("to_hash")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $body_data and $body_data->can("to_hash")
defined $body_data $body_data->can("to_hash")
Truth table0%0/3
A and B
A= defined $body_dataB= $body_data->can("to_hash")
ABresult
0X0
100
111
92 0 ▶ my $_body_data = keys %$post_params > 1 ? $post_params : $body_data;
Branch: 1 branch
BranchTrueFalseTotal
keys %$post_params > 1 ? : 0 0 0
93
94 #my $_body_data = $body_data;
95
96 # Make the HTTP request
97 0 my $_request;
98 0 ▶ if ($method eq 'POST') {
Branch: 6 branches
BranchTrueFalseTotal
if ($method eq 'POST') { } 0 0 0
elsif ($method eq 'PUT') { } 0 0 0
elsif ($method eq 'GET') { } 0 0 0
elsif ($method eq 'HEAD') { } 0 0 0
elsif ($method eq 'DELETE') { } 0 0 0
elsif ($method eq 'PATCH') { } 0 0 0
99 # multipart
100 $header_params->{'Content-Type'} = lc $header_params->{'Content-Type'} eq 'multipart/form' ?
101 0 ▶ 'form-data' : $header_params->{'Content-Type'};
Branch: 1 branch
BranchTrueFalseTotal
lc $header_params->{'Content-Type'} eq 'multipart/form' ? : 0 0 0
102
103 0 $_request = POST($_url, %$header_params, Content => $_body_data);
104
105 }
106 elsif ($method eq 'PUT') {
107 # multipart
108 $header_params->{'Content-Type'} = lc $header_params->{'Content-Type'} eq 'multipart/form' ?
109 0 ▶ 'form-data' : $header_params->{'Content-Type'};
Branch: 1 branch
BranchTrueFalseTotal
lc $header_params->{'Content-Type'} eq 'multipart/form' ? : 0 0 0
110
111 0 $_request = PUT($_url, %$header_params, Content => $_body_data);
112
113 }
114 elsif ($method eq 'GET') {
115 0 my $headers = HTTP::Headers->new(%$header_params);
116 0 $_request = GET($_url, %$header_params);
117 }
118 elsif ($method eq 'HEAD') {
119 0 my $headers = HTTP::Headers->new(%$header_params);
120 0 $_request = HEAD($_url,%$header_params);
121 }
122 elsif ($method eq 'DELETE') { #TODO support form data
123 0 my $headers = HTTP::Headers->new(%$header_params);
124 0 $_request = DELETE($_url, %$headers, Content => $_body_data);
125 }
126 elsif ($method eq 'PATCH') { #TODO
127 }
128 else {
129 }
130
131 0 ▶ $self->{ua}->timeout($self->{http_timeout} || $AsposePdfCloud::Configuration::http_timeout);
Condition0%0/3
$self->{'http_timeout'} || $AsposePdfCloud::Configuration::http_timeout
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'http_timeout'} || $AsposePdfCloud::Configuration::http_timeout
$self->{'http_timeout'} $AsposePdfCloud::Configuration::http_timeout
Truth table0%0/3
A || B
A= $self->{'http_timeout'}B= $AsposePdfCloud::Configuration::http_timeout
ABresult
1X1
011
000
132 0 ▶ $self->{ua}->agent($self->{http_user_agent} || $AsposePdfCloud::Configuration::http_user_agent);
Condition0%0/3
$self->{'http_user_agent'} || $AsposePdfCloud::Configuration::http_user_agent
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'http_user_agent'} || $AsposePdfCloud::Configuration::http_user_agent
$self->{'http_user_agent'} $AsposePdfCloud::Configuration::http_user_agent
Truth table0%0/3
A || B
A= $self->{'http_user_agent'}B= $AsposePdfCloud::Configuration::http_user_agent
ABresult
1X1
011
000
133
134 0 my $_response = $self->{ua}->request($_request);
135
136 0 ▶ unless ($_response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_response->is_success) 0 0 0
137 0 croak("API Exception(".$_response->code."): ".$_response->message);
138 }
139
140 0 return $_response;
141
142 }
143  
144 # Take value and turn it into a string suitable for inclusion in
145 # the path, by url-encoding.
146 # @param string $value a string which will be part of the path
147 # @return string the serialized object
148 sub to_path_value {
149 0 ▶ my ($self, $value) = @_;
Subroutine to_path_value: not called CC=1 SCAR=6.9
Pod undocumented
150 0 return uri_escape($self->to_string($value));
151 }
152  
153  
154 # Take value and turn it into a string suitable for inclusion in
155 # the query, by imploding comma-separated if it's an object.
156 # If it's a string, pass through unchanged. It will be url-encoded
157 # later.
158 # @param object $object an object to be serialized to a string
159 # @return string the serialized object
160 sub to_query_value {
161 0 ▶ my ($self, $object) = @_;
Subroutine to_query_value: not called CC=2 SCAR=17.9
Pod undocumented
162 0 ▶ if (is_array($object)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_array($object)) { } 0 0 0
163 0 return implode(',', $object);
164 } else {
165 0 return $self->to_string($object);
166 }
167 }
168  
169  
170 # Take value and turn it into a string suitable for inclusion in
171 # the header. If it's a string, pass through unchanged
172 # If it's a datetime object, format it in ISO8601
173 # @param string $value a string which will be part of the header
174 # @return string the header string
175 sub to_header_value {
176 0 ▶ my ($self, $value) = @_;
Subroutine to_header_value: not called CC=1 SCAR=6.9
Pod undocumented
177 0 return $self->to_string($value);
178 }
179  
180 # Take value and turn it into a string suitable for inclusion in
181 # the http body (form parameter). If it's a string, pass through unchanged
182 # If it's a datetime object, format it in ISO8601
183 # @param string $value the value of the form parameter
184 # @return string the form string
185 sub to_form_value {
186 0 ▶ my ($self, $value) = @_;
Subroutine to_form_value: not called CC=1 SCAR=6.9
Pod undocumented
187 0 return $self->to_string($value);
188 }
189  
190 # Take value and turn it into a string suitable for inclusion in
191 # the parameter. If it's a string, pass through unchanged
192 # If it's a datetime object, format it in ISO8601
193 # @param string $value the value of the parameter
194 # @return string the header string
195 sub to_string {
196 0 ▶ my ($self, $value) = @_;
Subroutine to_string: not called CC=2 SCAR=17.9
Pod undocumented
197 0 ▶ if (ref($value) eq "DateTime") { # datetime in ISO8601 format
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'DateTime') { } 0 0 0
198 0 return $value->datetime();
199 }
200 else {
201 0 return $value;
202 }
203 }
204  
205 # Deserialize a JSON string into an object
206 #
207 # @param string $class class name is passed as a string
208 # @param string $data data of the body
209 # @return object an instance of $class
210 sub deserialize
211 {
212 0 ▶ my ($self, $class, $data) = @_;
Subroutine deserialize: not called CC=13 SCAR=52.0
Pod undocumented
213 0 $log->debugf("deserializing %s for %s", $data, $class);
214
215 0 ▶ if (not defined $data) {
Branch: 5 branches
BranchTrueFalseTotal
if (not defined $data) { } 0 0 0
elsif (substr($class, 0, 5) eq 'HASH[') { } 0 0 0
elsif (substr($class, 0, 6) eq 'ARRAY[') { } 0 0 0
elsif ($class eq 'DateTime') { } 0 0 0
elsif (grep /^$class$/, ('string', 'int', 'float', 'bool', 'object')) { } 0 0 0
216 0 return undef;
217 } elsif ( (substr($class, 0, 5)) eq 'HASH[') { #hash
218 0 ▶ if ($class =~ /^HASH\[(.*),(.*)\]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class =~ /^HASH\[(.*),(.*)\]$/) { } 0 0 0
219 0 my ($key_type, $type) = ($1, $2);
220 0 my %hash;
221 0 my $decoded_data = decode_json $data;
222 0 foreach my $key (keys %$decoded_data) {
223 0 ▶ if (ref $decoded_data->{$key} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $decoded_data->{$key} eq 'HASH') { } 0 0 0
224 0 $hash{$key} = $self->deserialize($type, encode_json $decoded_data->{$key});
225 } else {
226 0 $hash{$key} = $self->deserialize($type, $decoded_data->{$key});
227 }
228 }
229 0 return \%hash;
230 } else {
231 #TODO log error
232 }
233
234 } elsif ( (substr($class, 0, 6)) eq 'ARRAY[' ) { # array of data
235 0 ▶ return $data if $data eq '[]'; # return if empty array
Branch: 1 branch
BranchTrueFalseTotal
if $data eq "[]" 0 0 0
236
237 0 my $_sub_class = substr($class, 6, -1);
238 0 my $_json_data = decode_json $data;
239 0 my @_values = ();
240 0 foreach my $_value (@$_json_data) {
241 0 ▶ if (ref $_value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_value eq 'ARRAY') { } 0 0 0
242 0 push @_values, $self->deserialize($_sub_class, encode_json $_value);
243 } else {
244 0 push @_values, $self->deserialize($_sub_class, $_value);
245 }
246 }
247 0 return \@_values;
248 } elsif ($class eq 'DateTime') {
249 0 return DateTime->from_epoch(epoch => str2time($data));
250 } elsif (grep /^$class$/, ('string', 'int', 'float', 'bool', 'object')) {
251 0 return $data;
252 } else { # model
253 0 my $_instance = use_module("AsposePdfCloud::Object::$class")->new;
254 0 ▶ if (ref $data eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq 'HASH') { } 0 0 0
255 0 return $_instance->from_hash($data);
256 } else { # string, need to json decode first
257 0 return $_instance->from_hash(decode_json $data);
258 }
259 }
260
261 }
262  
263 # return 'Accept' based on an array of accept provided
264 # @param [Array] header_accept_array Array fo 'Accept'
265 # @return String Accept (e.g. application/json)
266 sub select_header_accept
267 {
268 0 ▶ my ($self, @header) = @_;
Subroutine select_header_accept: not called CC=5 SCAR=34.0
Pod undocumented
269
270 0 ▶ if (@header == 0 || (@header == 1 && $header[0] eq '')) {
Branch: 2 branches
BranchTrueFalseTotal
if (@header == 0 or @header == 1 and $header[0] eq '') { } 0 0 0
elsif (grep m[^application/json$]i, @header) { } 0 0 0
Condition0%0/6
@header == 0 or @header == 1 and $header[0] eq ""
l!l&&r!l&&!r
000
@header == 1 and $header[0] eq ""
!ll&&!rl&&r
000
MC/DC0%0/3
@header == 0 or @header == 1 and $header[0] eq ""
@header == 0 @header == 1 $header[0] eq ""
Truth table0%0/4
A or B and C
A= @header == 0B= @header == 1C= $header[0] eq ""
ABCresult
1XX1
0111
00X0
0100
271 0 return undef;
272 } elsif (grep(/^application\/json$/i, @header)) {
273 0 return 'application/json';
274 } else {
275 0 return join(',', @header);
276 }
277
278 }
279  
280 # return the content type based on an array of content-type provided
281 # @param [Array] content_type_array Array fo content-type
282 # @return String Content-Type (e.g. application/json)
283 sub select_header_content_type
284 {
285 0 ▶ my ($self, @header) = @_;
Subroutine select_header_content_type: not called CC=5 SCAR=34.0
Pod undocumented
286
287 0 ▶ if (@header == 0 || (@header == 1 && $header[0] eq '')) {
Branch: 2 branches
BranchTrueFalseTotal
if (@header == 0 or @header == 1 and $header[0] eq '') { } 0 0 0
elsif (grep m[^application/json$]i, @header) { } 0 0 0
Condition0%0/6
@header == 0 or @header == 1 and $header[0] eq ""
l!l&&r!l&&!r
000
@header == 1 and $header[0] eq ""
!ll&&!rl&&r
000
MC/DC0%0/3
@header == 0 or @header == 1 and $header[0] eq ""
@header == 0 @header == 1 $header[0] eq ""
Truth table0%0/4
A or B and C
A= @header == 0B= @header == 1C= $header[0] eq ""
ABCresult
1XX1
0111
00X0
0100
288 0 return 'application/json'; # default to application/json
289 } elsif (grep(/^application\/json$/i, @header)) {
290 0 return 'application/json';
291 } else {
292 0 return join(',', @header);
293 }
294
295 }
296  
297 sub pre_deserialize
298 {
299 0 ▶ my ($self, $data, $class, $content_type) = @_;
Subroutine pre_deserialize: not called CC=6 SCAR=37.4
Pod undocumented
300 0 $log->debugf("pre_deserialize %s for %s and content_type %s", $data, $class, $content_type);
301 0 ▶ if ($class eq "ResponseMessage") {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq "ResponseMessage") 0 0 0
302 0 ▶ if (defined $content_type and lc $content_type ne 'application/json' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content_type and lc $content_type ne "application/json") 0 0 0
Condition0%0/3
defined $content_type and lc $content_type ne "application/json"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $content_type and lc $content_type ne "application/json"
defined $content_type lc $content_type ne "application/json"
Truth table0%0/3
A and B
A= defined $content_typeB= lc $content_type ne "application/json"
ABresult
0X0
100
111
303 0 my $_instance = use_module("AsposePdfCloud::Object::$class")->new;
304 0 $_instance->{'Status'} = 'OK';
305 0 $_instance->{'Code'} = 200;
306 0 $_instance->{'Content'} = $data;
307 0 return $_instance;
308 }
309 }
310
311 0 ▶ if ( (defined $content_type) && ($content_type !~ m/json/) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content_type and not $content_type =~ /json/) 0 0 0
Condition0%0/3
defined $content_type and not $content_type =~ /json/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $content_type and not $content_type =~ /json/
defined $content_type not $content_type =~ /json/
Truth table0%0/3
A and not(B)
A= defined $content_typeB= not $content_type =~ /json/
ABresult
0X0
100
111
312 0 croak("API Exception(406.): Invalid contentType".$content_type);
313 }
314
315 0 return $self->deserialize($class, $data);
316 }
317  
318 # return signature for aspose cloud api
319 sub sign {
320 0 ▶ my ($self, $url_to_sign, $appSid, $appKey) = @_;
Subroutine sign: not called CC=1 SCAR=6.9
Pod undocumented
321
322 #return if (!defined($url_to_sign) || scalar(@$auth_settings) == 0);
323
324 0 my $hmac = Digest::HMAC_SHA1->new($appKey);
325 0 $hmac->add($url_to_sign);
326 0 my $signature = $hmac->digest;
327 0 $signature = encode_base64($signature, '');
328 0 $signature =~ s/=//;
329 0 $log->debugf ("signature :: ".$signature);
330 0 return $signature;
331 }
332  
333 1 1;
« blib/lib/AsposePdfCloud/PdfApi.pm ↑ Summary blib/lib/AsposePdfCloud/Object/BaseObject.pm »