Skip to content

Commit

Permalink
Merge pull request #71 from datalogics-cgreen/pdfcloud-3551-poll
Browse files Browse the repository at this point in the history
Add polling retries to samples that lacked it
  • Loading branch information
datalogics-tsmith authored Jun 26, 2024
2 parents 8515f55 + 8612a15 commit 8b224e3
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 34 deletions.
79 changes: 48 additions & 31 deletions JavaScript/Endpoint Examples/Multipart Payload/request-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,59 @@ const axios = require("axios");
const FormData = require("form-data");
const fs = require("fs");


const apiKey = "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; // Replace with your API key
const pathToFile = "/path/to/file.pdf";

let bmpRequestData = new FormData();
bmpRequestData.append("file", fs.createReadStream(pathToFile));

let bmpConfig = {
method: "post",
maxBodyLength: Infinity,
url: "https://api.pdfrest.com/bmp",
headers: {
"Api-Key": apiKey,
"Response-Type": "requestId", // Use this header to get a request ID.
...bmpRequestData.getHeaders(),
},
data: bmpRequestData,
const sleep = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms));
}

const sendBmpReq = async (config) => {
return await axios(config);
};

const pollUntilFulfilled = async (requestId) => {
const pollConfig = {
method: "get",
maxBodyLength: Infinity, // set maximum length of the request body
url: `https://api.pdfrest.com/request-status/${requestId}`,
headers: { "Api-Key": apiKey }
};
requestStatusResponse = await axios(pollConfig);
let status = requestStatusResponse.data.status;
while (status === "pending") {
console.log(JSON.stringify(requestStatusResponse.data));
await sleep(5000);
requestStatusResponse = await axios(pollConfig);
status = requestStatusResponse.data.status;
}
console.log(JSON.stringify(requestStatusResponse.data));
};

axios(bmpConfig)
.then(bmpResponse => {
const demoApiPolling = async () => {
try {
const bmpRequestData = new FormData();
bmpRequestData.append("file", fs.createReadStream(pathToFile));

const bmpConfig = {
method: "post",
maxBodyLength: Infinity,
url: "https://api.pdfrest.com/bmp",
headers: {
"Api-Key": apiKey,
"Response-Type": "requestId", // Use this header to get a request ID.
...bmpRequestData.getHeaders(),
},
data: bmpRequestData,
};
bmpResponse = await sendBmpReq(bmpConfig);
console.log(JSON.stringify(bmpResponse.data));
const requestId = bmpResponse.data.requestId;
let config = {
method: "get",
maxBodyLength: Infinity, // set maximum length of the request body
url: `https://api.pdfrest.com/request-status/${requestId}`,
headers: { "Api-Key": apiKey }
};
axios.request(config)
.then((requestStatusResponse) => {
console.log(JSON.stringify(requestStatusResponse.data));
})
.catch((error) => {
console.log(error);
});
}).catch(error => {
console.log(error);
})
await pollUntilFulfilled(requestId);
} catch (err) {
console.error(err);
}
};

demoApiPolling();
13 changes: 12 additions & 1 deletion PHP/Endpoint Examples/Multipart Payload/request-status.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@

$res = $client->sendAsync($request)->wait();

echo $res->getBody(); // Output the response body, which contains the status information.
$status = json_decode($res->getBody())->{'status'};

while (strcmp($status, "pending") == 0):
echo $res->getBody(); // Output the response body, which contains the status information.
echo "\r\n";
sleep(5);
$res = $client->sendAsync($request)->wait();
$status = json_decode($res->getBody())->{'status'};
endwhile;

echo $res->getBody();
echo "\r\n";

?>
6 changes: 6 additions & 0 deletions Python/Endpoint Examples/Multipart Payload/request-status.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from requests_toolbelt import MultipartEncoder
import requests
import json
import time

api_key = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here

Expand Down Expand Up @@ -42,6 +43,11 @@

if response.ok:
response_json = response.json()
while response_json["status"] == "pending":
print(json.dumps(response_json, indent = 2))
time.sleep(5)
response = requests.get(api_polling_endpoint_url, headers=headers)
response_json = response.json()
print(json.dumps(response_json, indent = 2))
else:
print(response.text)
15 changes: 13 additions & 2 deletions cURL/Endpoint Examples/Multipart Payload/request-status.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,16 @@ REQUEST_ID=$(curl -X POST "https://api.pdfrest.com/pdfa" \
-F "output=example_out" \
| jq -r '.requestId')

curl -X GET "https://api.pdfrest.com/request-status/$REQUEST_ID" \
-H "Api-Key: $API_KEY"
RESPONSE=$(curl -X GET "https://api.pdfrest.com/request-status/$REQUEST_ID" \
-H "Api-Key: $API_KEY")
echo $RESPONSE
STATUS=$(echo $RESPONSE | jq -r '.status')

while [ $STATUS = "pending" ]
do
sleep 5
RESPONSE=$(curl -X GET "https://api.pdfrest.com/request-status/$REQUEST_ID" \
-H "Api-Key: $API_KEY")
echo $RESPONSE
STATUS=$(echo $RESPONSE | jq -r '.status')
done

0 comments on commit 8b224e3

Please sign in to comment.