SelectPdf/ApiClient.pm

filter: Statementstmt 17.5% 20 / 114 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 37.5% 6 / 16 Podpod 50.0% 5 / 10 totaltotal 17.0% 31 / 182 CC 20 SCAR 33.8
CC
20
Cov
13%
serializeBoolean
72.0
performPostAsMultipartFormData
42.0
performPost
30.0
CRAP 29.4
↑ Summary SelectPdf/HtmlToPdfClient.pm »
1 package SelectPdf::ApiClient;
2
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4
5 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use HTTP::Status qw(:constants :is status_message);
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 1 ▶use constant MULTIPART_FORM_DATA_BOUNDARY => "------------SelectPdf_Api_Boundry_\$";
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use constant NEW_LINE => "\r\n";
Subroutine BEGIN: called CC=1 SCAR=0.0
11
12 1 our $VERSION = '1.4.0';
13
14 =head1 NAME
15
16 SelectPdf::ApiClient - Base class for API clients. Do not use this directly.
17
18 =head1 METHODS
19
20 =head2 new
21
22 ApiClient Constructor. Do not use this directly.
23 =cut
24 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
25 0 my $type = shift;
26 0 my $self = {};
27
28 # API endpoint
29 0 $self->{apiEndpoint} = "https://selectpdf.com/api2/convert/";
30
31 # API async jobs endpoint
32 0 $self->{apiAsyncEndpoint} = "https://selectpdf.com/api2/asyncjob/";
33
34 # API web elements endpoint
35 0 $self->{apiWebElementsEndpoint} = "https://selectpdf.com/api2/webelements/";
36
37 # Parameters that will be sent to the API.
38 0 $self->{parameters} = {};
39
40 # HTTP Headers that will be sent to the API.
41 0 $self->{headers} = {};
42
43 # Files that will be sent to the API.
44 0 $self->{files} = {};
45
46 # Binary data that will be sent to the API.
47 0 $self->{binaryData} = {};
48
49 # Number of pages of the pdf document resulted from the conversion.
50 0 $self->{numberOfPages} = 0;
51
52 # Job ID for asynchronous calls or for calls that require a second request.
53 0 $self->{jobId} = "";
54
55 # Last HTTP Code
56 0 $self->{lastHTTPCode} = "";
57
58 # Ping interval in seconds for asynchronous calls. Default value is 3 seconds.
59 0 $self->{AsyncCallsPingInterval} = 3;
60
61 # Maximum number of pings for asynchronous calls. Default value is 1,000 pings.
62 0 $self->{AsyncCallsMaxPings} = 1000;
63
64 0 bless $self, $type;
65 0 return $self;
66 }
67
68 =head2 setApiEndpoint( $apiEndpoint )
69
70 Set a custom SelectPdf API endpoint. Do not use this method unless advised by SelectPdf.
71
72 $client->setApiEndpoint($apiEndpoint);
73
74 Parameters:
75
76 - $apiEndpoint API endpoint.
77 =cut
78 0 ▶sub setApiEndpoint {
Subroutine setApiEndpoint: not called CC=2 SCAR=17.9
Pod documented
79 0 my($self, $apiEndpoint) = @_;
80 0 ▶ $self->{apiEndpoint} = $apiEndpoint if defined($apiEndpoint);
Branch: 1 branch
BranchTrueFalseTotal
if defined $apiEndpoint 0 0 0
81 0 return $self->{apiEndpoint};
82 }
83
84 =head2 setApiAsyncEndpoint( $apiAsyncEndpoint )
85
86 Set a custom SelectPdf API endpoint for async jobs. Do not use this method unless advised by SelectPdf.
87
88 $client->setApiAsyncEndpoint($apiAsyncEndpoint);
89
90 Parameters:
91
92 - $apiAsyncEndpoint API async jobs endpoint.
93 =cut
94 0 ▶sub setApiAsyncEndpoint {
Subroutine setApiAsyncEndpoint: not called CC=2 SCAR=17.9
Pod documented
95 0 my($self, $apiAsyncEndpoint) = @_;
96 0 ▶ $self->{apiAsyncEndpoint} = $apiAsyncEndpoint if defined($apiAsyncEndpoint);
Branch: 1 branch
BranchTrueFalseTotal
if defined $apiAsyncEndpoint 0 0 0
97 0 return $self->{apiAsyncEndpoint};
98 }
99
100 =head2 setApiWebElementsEndpoint( $apiWebElementsEndpoint )
101
102 Set a custom SelectPdf API endpoint for web elements. Do not use this method unless advised by SelectPdf.
103
104 $client->setApiWebElementsEndpoint($apiWebElementsEndpoint);
105
106 Parameters:
107
108 - $apiWebElementsEndpoint API web elements endpoint.
109 =cut
110 0 ▶sub setApiWebElementsEndpoint {
Subroutine setApiWebElementsEndpoint: not called CC=2 SCAR=17.9
Pod documented
111 0 my($self, $apiWebElementsEndpoint) = @_;
112 0 ▶ $self->{apiWebElementsEndpoint} = $apiWebElementsEndpoint if defined($apiWebElementsEndpoint);
Branch: 1 branch
BranchTrueFalseTotal
if defined $apiWebElementsEndpoint 0 0 0
113 0 return $self->{apiWebElementsEndpoint};
114 }
115
116 # Create a POST request.
117 #
118 # @returns Response content.
119 0 ▶sub performPost {
Subroutine performPost: not called CC=5 SCAR=34.0
Pod undocumented
120 0 my($self) = @_;
121
122 # reset results
123 0 $self->{numberOfPages} = 0;
124 0 $self->{jobId} = "";
125 0 $self->{lastHTTPCode} = "";
126
127 # print "\nParameters (to endpoint $self->{apiEndpoint}):\n";
128 # foreach my $k (keys(%{ $self->{parameters} })) {
129 # print "$k => $self->{parameters}{$k}\n";
130 # }
131 # print "\n";
132
133 # prepare request
134 0 my $ua = LWP::UserAgent->new;
135 0 $ua->timeout(6000); # 6,000 seconds = 100 min
136
137 # set headers
138 0 $self->{headers}{"Content-type"} = "application/x-www-form-urlencoded";
139 0 $self->{headers}{"selectpdf-api-client"} = "perl-$]-$VERSION";
140
141 0 foreach my $k (keys(%{ $self->{headers} })) {
142 0 $ua->default_header($k => $self->{headers}{$k});
143 }
144
145 # call the API
146 0 my $response = $ua->request(POST $self->{apiEndpoint}, $self->{parameters});
147
148 # get response
149 0 my $code = $response->code;
150 0 $self->{lastHTTPCode} = $code;
151 # print ("HTTP Code: $self->{lastHTTPCode}.\n");
152
153 0 ▶ if ($response->code == HTTP_OK) {
Branch: 2 branches
BranchTrueFalseTotal
if ($response->code == 200) { } 0 0 0
elsif ($response->code == 202) { } 0 0 0
154 0 $self->{numberOfPages} = int($response->header("selectpdf-api-pages"));
155 0 $self->{jobId} = $response->header("selectpdf-api-jobid");
156
157 0 return $response->decoded_content;
158 }
159 elsif ($response->code == HTTP_ACCEPTED) {
160 0 $self->{jobId} = $response->header("selectpdf-api-jobid");
161 0 return undef;
162 }
163 else {
164 0 my $message = $response->message;
165 0 ▶ if ($response->decoded_content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->decoded_content) 0 0 0
166 0 $message = $response->decoded_content;
167 }
168
169 0 die "($code) $message";
170 }
171
172 }
173
174 # Create a POST request.
175 #
176 # @returns Response content.
177 0 ▶sub performPostAsMultipartFormData {
Subroutine performPostAsMultipartFormData: not called CC=6 SCAR=37.4
Pod undocumented
178 0 my($self) = @_;
179
180 # reset results
181 0 $self->{numberOfPages} = 0;
182 0 $self->{jobId} = "";
183 0 $self->{lastHTTPCode} = "";
184
185 # print "\nParameters (to endpoint $self->{apiEndpoint}):\n";
186 # foreach $k (keys(%{ $self->{parameters} })) {
187 # print "$k => $self->{parameters}{$k}\n";
188 # }
189 # print "\n";
190
191 # prepare request
192 0 my $ua = LWP::UserAgent->new;
193 0 $ua->timeout(6000); # 6,000 seconds = 100 min
194
195 # set headers
196 0 $self->{headers}{"selectpdf-api-client"} = "perl-$]-$VERSION";
197
198 0 foreach my $k (keys(%{ $self->{headers} })) {
199 0 $ua->default_header($k => $self->{headers}{$k});
200 }
201
202 # merge parameters and files
203 0 my $alldata = $self->{parameters};
204 0 foreach my $k (keys(%{ $self->{files} })) {
205 0 $alldata->{$k} = [$self->{files}{$k}];
206 }
207
208 # print "\nAll data (to endpoint $self->{apiEndpoint}):\n";
209 # foreach my $k (keys %{$alldata}) {
210 # print "$k => $alldata->{$k}\n";
211 # }
212 # print "\n";
213
214 # call the API
215 0 my $response = $ua->request(POST $self->{apiEndpoint}, Content_Type => 'form-data', Content => $alldata);
216
217 # get response
218 0 my $code = $response->code;
219 0 $self->{lastHTTPCode} = $code;
220 # print ("HTTP Code: $self->{lastHTTPCode}.\n");
221
222 0 ▶ if ($response->code == HTTP_OK) {
Branch: 2 branches
BranchTrueFalseTotal
if ($response->code == 200) { } 0 0 0
elsif ($response->code == 202) { } 0 0 0
223 0 $self->{numberOfPages} = int($response->header("selectpdf-api-pages"));
224 0 $self->{jobId} = $response->header("selectpdf-api-jobid");
225
226 0 return $response->decoded_content;
227 }
228 elsif ($response->code == HTTP_ACCEPTED) {
229 0 $self->{jobId} = $response->header("selectpdf-api-jobid");
230 0 return undef;
231 }
232 else {
233 0 my $message = $response->message;
234 0 ▶ if ($response->decoded_content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->decoded_content) 0 0 0
235 0 $message = $response->decoded_content;
236 }
237
238 0 die "($code) $message";
239 }
240
241 }
242
243 # Start an asynchronous job.
244 #
245 # @returns Asynchronous job ID.
246 0 ▶sub startAsyncJob {
Subroutine startAsyncJob: not called CC=1 SCAR=6.9
Pod undocumented
247 0 my($self) = @_;
248
249 0 $self->{parameters}{"async"} = "True";
250 0 $self->performPost();
251
252 0 return $self->{jobId};
253 }
254
255 # Start an asynchronous job that requires multipart form data.
256 #
257 # @returns Asynchronous job ID.
258 0 ▶sub startAsyncJobMultipartFormData {
Subroutine startAsyncJobMultipartFormData: not called CC=1 SCAR=6.9
Pod undocumented
259 0 my($self) = @_;
260
261 0 $self->{parameters}{"async"} = "True";
262 0 $self->performPostAsMultipartFormData();
263
264 0 return $self->{jobId};
265 }
266
267 =head2 getNumberOfPages
268
269 Get the number of pages of the PDF document resulted from the API call.
270
271 $pages = $client->getNumberOfPages();
272
273 Returns:
274
275 - Number of pages of the PDF document.
276 =cut
277 0 ▶sub getNumberOfPages {
Subroutine getNumberOfPages: not called CC=1 SCAR=6.9
Pod documented
278 0 my($self) = @_;
279 0 return $self->{numberOfPages};
280 }
281
282 # Serialize boolean values as "True" or "False" for the API.
283 #
284 # @returns Serialized value.
285 0 ▶sub serializeBoolean {
Subroutine serializeBoolean: not called CC=8 SCAR=42.8
Pod undocumented
286 0 my($self, $value) = @_;
287
288 0 ▶ if (not defined($value) or $value eq 'undef') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value or $value eq 'undef') { } 0 0 0
Condition0%0/3
not defined $value or $value eq "undef"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $value or $value eq "undef"
not defined $value $value eq "undef"
Truth table0%0/3
not(A) or B
A= not defined $valueB= $value eq "undef"
ABresult
1X1
011
000
289 0 $value = 0;
290 }
291 else {
292 0 $value =~ s/^\s+|\s+$//g;
293 0 $value = lc $value;
294
295 0 ▶ if ($value eq 'false' or $value eq 'no' or $value eq '0' or $value eq 'off') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq "false" or $value eq "no" or $value eq 0 or $value eq "off") 0 0 0
Condition0%0/9
$value eq "false" or $value eq "no" or $value eq 0 or $value eq "off"
l!l&&r!l&&!r
000
$value eq "false" or $value eq "no" or $value eq 0
l!l&&r!l&&!r
000
$value eq "false" or $value eq "no"
l!l&&r!l&&!r
000
MC/DC0%0/4
$value eq "false" or $value eq "no" or $value eq 0 or $value eq "off"
$value eq "false" $value eq "no" $value eq 0 $value eq "off"
Truth table0%0/5
A or B or C or D
A= $value eq "false"B= $value eq "no"C= $value eq 0D= $value eq "off"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
296 0 $value = 0;
297 }
298 }
299 0 ▶ return $value ? 'True' : 'False';
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 0 0
300 }
301
302 1 1;
↑ Summary SelectPdf/HtmlToPdfClient.pm »