Skip to content

Commit

Permalink
Merge pull request #67 from datalogics/datalogics-erics-api-polling-e…
Browse files Browse the repository at this point in the history
…xamples

Add Examples for API Polling
  • Loading branch information
datalogics-kam authored Apr 25, 2024
2 parents dd1867e + 4c53375 commit 302a1c5
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 0 deletions.
17 changes: 17 additions & 0 deletions DotNET/Endpoint Examples/Multipart Payload/request-status.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Text;

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
{
string requestId = "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; // requestId to poll
using (var request = new HttpRequestMessage(HttpMethod.Get, "request-status" + requestId))
{
request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");

var response = await httpClient.SendAsync(request);

var apiResult = await response.Content.ReadAsStringAsync();

Console.WriteLine("API response received.");
Console.WriteLine(apiResult);
}
}
50 changes: 50 additions & 0 deletions Java/Endpoint Examples/Multipart Payload/RequestStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import io.github.cdimascio.dotenv.Dotenv;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONObject;

public class RequestStatus {

// Request UUIDs can be found in the JSON response of POST requests as "requestId" when API
// Polling is enabled.
// Resource UUIDs usually look like this: '0950b9bdf-0465-4d3f-8ea3-d2894f1ae839'.
private static final String REQUEST_ID =
"2e3c603d1-30b2-4c16-8c11-911a51bb2ba9"; // place requestID here

// Specify your API key here, or in the environment variable PDFREST_API_KEY.
// You can also put the environment variable in a .env file.
private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

public static void main(String[] args) {

final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();

String urlString = String.format("https://api.pdfrest.com/request-status/%s", REQUEST_ID);

Request request =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url(urlString)
.get()
.build();
try {
OkHttpClient client =
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();
Response response = client.newCall(request).execute();
System.out.println("Result code " + response.code());
if (response.body() != null) {
System.out.println(prettyJson(response.body().string()));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static String prettyJson(String json) {
// https://stackoverflow.com/a/9583835/11996393
return new JSONObject(json).toString(4);
}
}
18 changes: 18 additions & 0 deletions JavaScript/Endpoint Examples/Multipart Payload/request-status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const axios = require("axios");

let config = {
method: "get",
maxBodyLength: Infinity, // set maximum length of the request body
url: "https://api.pdfrest.com/request-status/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
headers: { "Api-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" }
};

// define configuration options for axios request
axios
.request(config)
.then((response) => {
console.log(JSON.stringify(response.data));
})
.catch((error) => {
console.log(error);
});
23 changes: 23 additions & 0 deletions PHP/Endpoint Examples/Multipart Payload/request-status.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client.

use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace.
use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class.
use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams.

$client = new Client(); // Create a new instance of the Guzzle HTTP client.

$requestId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'; // place the requestId returned from a previous POST request here

$request_status_endpoint_url = 'https://api.pdfrest.com/request-status/'.$requestId;

$headers = [
'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' // Set the API key in the headers for authentication.
];

$request = new Request('GET', $request_status_endpoint_url, $headers); // Create a new HTTP GET request with the API endpoint and headers.

$res = $client->sendAsync($request)->wait(); // Send the asynchronous request and wait for the response.

echo $res->getBody(); // Output the response body, which contains the status information.
?>
24 changes: 24 additions & 0 deletions Python/Endpoint Examples/Multipart Payload/request-status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from requests_toolbelt import MultipartEncoder
import requests
import json

api_polling_endpoint_url = 'https://api.pdfrest.com/request-status'

request_id = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place requestId to poll here

api_polling_endpoint_url = f'https://api.pdfrest.com/request-status/{request_id}'

headers = {
'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

print("Sending GET request to request-status endpoint...")
response = requests.get(api_polling_endpoint_url, headers=headers)

print("Response status code: " + str(response.status_code))

if response.ok:
response_json = response.json()
print(json.dumps(response_json, indent = 2))
else:
print(response.text)
2 changes: 2 additions & 0 deletions cURL/Endpoint Examples/Multipart Payload/request-status.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
curl -X GET "https://api.pdfrest.com/request-status/xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" \
-H "Api-Key: xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

0 comments on commit 302a1c5

Please sign in to comment.