| 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.3.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 | ▶ | my $type = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ▶ | my($self, $apiEndpoint) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
setApiEndpoint: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | $self->{apiEndpoint} = $apiEndpoint if defined($apiEndpoint); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ▶ | my($self, $apiAsyncEndpoint) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
setApiAsyncEndpoint: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | $self->{apiAsyncEndpoint} = $apiAsyncEndpoint if defined($apiAsyncEndpoint); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ▶ | my($self, $apiWebElementsEndpoint) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
setApiWebElementsEndpoint: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | $self->{apiWebElementsEndpoint} = $apiWebElementsEndpoint if defined($apiWebElementsEndpoint); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | return $self->{apiWebElementsEndpoint}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | # Create a POST request. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | # @returns Response content. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | sub performPost { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
performPost: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
performPostAsMultipartFormData: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
startAsyncJob: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
startAsyncJobMultipartFormData: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
getNumberOfPages: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ▶ | my($self, $value) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
serializeBoolean: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | ▶ | if (not defined($value) or $value eq 'undef') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $value or $value eq "undef"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/9
$value eq "false" or $value eq "no" or $value eq 0 or $value eq "off"
$value eq "false" or $value eq "no" or $value eq 0
$value eq "false" or $value eq "no"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | $value = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | ▶ | return $value ? 'True' : 'False'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||