Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add explanatory comments to request-status samples #72

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const pollUntilFulfilled = async (requestId) => {
};
requestStatusResponse = await axios(pollConfig);
let status = requestStatusResponse.data.status;

// This example will repeat the GET request until the BMP request is completed.
while (status === "pending") {
console.log(JSON.stringify(requestStatusResponse.data));
await sleep(5000);
Expand All @@ -34,6 +36,7 @@ const pollUntilFulfilled = async (requestId) => {

const demoApiPolling = async () => {
try {
// Send a request with the Response-Type header (using /bmp as an arbitrary example)
const bmpRequestData = new FormData();
bmpRequestData.append("file", fs.createReadStream(pathToFile));

Expand All @@ -50,6 +53,8 @@ const demoApiPolling = async () => {
};
bmpResponse = await sendBmpReq(bmpConfig);
console.log(JSON.stringify(bmpResponse.data));

// Get the request ID from the initial response.
const requestId = bmpResponse.data.requestId;
await pollUntilFulfilled(requestId);
} catch (err) {
Expand Down
6 changes: 5 additions & 1 deletion PHP/Endpoint Examples/Multipart Payload/request-status.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

$headers = [
'Api-Key' => $apiKey,
'response-type' => 'requestId'
'response-type' => 'requestId' // Use this header to obtain a request status.
];

$pngOptions = [
Expand All @@ -27,15 +27,18 @@
]
];

// Using /png as an arbitrary example, send a request with the Request-Type header.
$pngRequest = new Request('POST', 'https://api.pdfrest.com/png', $headers);

$pngResponse = $client->sendAsync($pngRequest, $pngOptions)->wait();

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

// Get the request ID from the response.
$requestId = json_decode($pngResponse->getBody())->{'requestId'};

// Get the status of the PNG request by its ID.
$request_status_endpoint_url = 'https://api.pdfrest.com/request-status/'.$requestId;

$headers = [
Expand All @@ -48,6 +51,7 @@

$status = json_decode($res->getBody())->{'status'};

// This example repeats the status request until the request is fulfilled.
while (strcmp($status, "pending") == 0):
echo $res->getBody(); // Output the response body, which contains the status information.
echo "\r\n";
Expand Down
2 changes: 2 additions & 0 deletions Python/Endpoint Examples/Multipart Payload/request-status.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
}
)

# Send a request to a pdfRest tool with the Response-Type header to get a request ID,
pdfa_headers = {
'Accept': 'application/json',
'Content-Type': mp_encoder_pdfa.content_type,
Expand Down Expand Up @@ -44,6 +45,7 @@
if response.ok:
response_json = response.json()
while response_json["status"] == "pending":
# This example will get the request status every 5 seconds until the request is completed.
print(json.dumps(response_json, indent = 2))
time.sleep(5)
response = requests.get(api_polling_endpoint_url, headers=headers)
Expand Down
6 changes: 6 additions & 0 deletions cURL/Endpoint Examples/Multipart Payload/request-status.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

API_KEY="xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

# Send a request with the response-type header.

REQUEST_ID=$(curl -X POST "https://api.pdfrest.com/pdfa" \
-H "Accept: application/json" \
-H "Content-Type: multipart/form-data" \
Expand All @@ -12,11 +14,15 @@ REQUEST_ID=$(curl -X POST "https://api.pdfrest.com/pdfa" \
-F "output=example_out" \
| jq -r '.requestId')

# Get the request status at /request-status

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')

# This example repeats the GET request until the Toolkit request is completed.

while [ $STATUS = "pending" ]
do
sleep 5
Expand Down
Loading