File Coverage

blib/lib/SelectPdf/ApiClient.pm
Criterion Covered Total %
statement 18 112 16.0
branch 0 24 0.0
condition 0 12 0.0
subroutine 6 16 37.5
pod 5 10 50.0
total 29 174 16.6


line stmt bran cond sub pod time code
1             package SelectPdf::ApiClient;
2            
3 1     1   7 use strict;
  1         2  
  1         32  
4            
5 1     1   692 use LWP::UserAgent;
  1         48214  
  1         45  
6 1     1   589 use HTTP::Request::Common;
  1         2374  
  1         82  
7 1     1   7 use HTTP::Status qw(:constants :is status_message);
  1         2  
  1         553  
8            
9 1     1   8 use constant MULTIPART_FORM_DATA_BOUNDARY => "------------SelectPdf_Api_Boundry_\$";
  1         3  
  1         96  
10 1     1   7 use constant NEW_LINE => "\r\n";
  1         2  
  1         1260  
11            
12             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             sub new {
25 0     0 1   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             sub setApiEndpoint {
79 0     0 1   my($self, $apiEndpoint) = @_;
80 0 0         $self->{apiEndpoint} = $apiEndpoint if defined($apiEndpoint);
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             sub setApiAsyncEndpoint {
95 0     0 1   my($self, $apiAsyncEndpoint) = @_;
96 0 0         $self->{apiAsyncEndpoint} = $apiAsyncEndpoint if defined($apiAsyncEndpoint);
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             sub setApiWebElementsEndpoint {
111 0     0 1   my($self, $apiWebElementsEndpoint) = @_;
112 0 0         $self->{apiWebElementsEndpoint} = $apiWebElementsEndpoint if defined($apiWebElementsEndpoint);
113 0           return $self->{apiWebElementsEndpoint};
114             }
115            
116             # Create a POST request.
117             #
118             # @returns Response content.
119             sub performPost {
120 0     0 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} })) {
  0            
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 0         if ($response->code == HTTP_OK) {
    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 0         if ($response->decoded_content) {
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             sub performPostAsMultipartFormData {
178 0     0 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} })) {
  0            
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} })) {
  0            
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 0         if ($response->code == HTTP_OK) {
    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 0         if ($response->decoded_content) {
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             sub startAsyncJob {
247 0     0 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             sub startAsyncJobMultipartFormData {
259 0     0 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             sub getNumberOfPages {
278 0     0 1   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             sub serializeBoolean {
286 0     0 0   my($self, $value) = @_;
287            
288 0 0 0       if (not defined($value) or $value eq 'undef') {
289 0           $value = 0;
290             }
291             else {
292 0           $value =~ s/^\s+|\s+$//g;
293 0           $value = lc $value;
294            
295 0 0 0       if ($value eq 'false' or $value eq 'no' or $value eq '0' or $value eq 'off') {
      0        
      0        
296 0           $value = 0;
297             }
298             }
299 0 0         return $value ? 'True' : 'False';
300             }
301            
302             1;