-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from datalogics-cgreen/pdfcloud-3551-poll
PDFCLOUD-3551 Update API polling samples to get their own request IDs
- Loading branch information
Showing
8 changed files
with
281 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1287,3 +1287,5 @@ FodyWeavers.xsd | |
|
||
Node/node_modules/ | ||
Node/package-lock.json | ||
PHP/**/vendor/** | ||
PHP/**/composer.* |
92 changes: 84 additions & 8 deletions
92
DotNET/Endpoint Examples/Multipart Payload/request-status.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,93 @@ | ||
using System.Net.Http; | ||
using System.Text; | ||
using Newtonsoft.Json.Linq; | ||
|
||
using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") }) | ||
/* | ||
* This sample demonstrates how to send a polling API request in order to obtain a request | ||
* status. | ||
*/ | ||
public class PollableApiExample | ||
{ | ||
string requestId = "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; // requestId to poll | ||
using (var request = new HttpRequestMessage(HttpMethod.Get, "request-status" + requestId)) | ||
private const string ApiKey = "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; // Your API key here | ||
private const string BaseUri = "https://api.pdfrest.com"; | ||
|
||
public static async Task Main(String[] args) | ||
{ | ||
request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"); | ||
const string pathToFile = "/path/to/file.pdf"; // Path to the input file here | ||
const string fileName = "file.pdf"; // Name of the file here | ||
|
||
var response = await httpClient.SendAsync(request); | ||
// Send a request with the 'response-type' header. | ||
string bmpResponse = await GetBmpResponseAsync(pathToFile, fileName); | ||
dynamic bmpResponseJson = JObject.Parse(bmpResponse); | ||
if (bmpResponseJson.ContainsKey("error")) | ||
{ | ||
Console.WriteLine($"Error from initial request: {bmpResponseJson.error}"); | ||
} else | ||
{ | ||
// Get the request ID from the response. | ||
string requestId = bmpResponseJson.requestId; | ||
Console.WriteLine($"Received request ID: {requestId}"); | ||
|
||
// Use the request ID to send a polling request. | ||
string requestStatusResponse = await GetRequestStatusAsync(requestId); | ||
dynamic requestStatusResponseJson = JObject.Parse(requestStatusResponse); | ||
|
||
// If the request is still pending, send another call momentarily. | ||
while (requestStatusResponseJson.status == "pending") | ||
{ | ||
const int delay = 5; // (seconds) | ||
Console.WriteLine($"Response from /request-status for request {requestId}: {requestStatusResponseJson}"); | ||
Console.WriteLine($"Request status was \"pending\". Checking again in {delay} seconds..."); | ||
await Task.Delay(TimeSpan.FromSeconds(delay)); | ||
requestStatusResponse = await GetRequestStatusAsync(requestId); | ||
requestStatusResponseJson = JObject.Parse(requestStatusResponse); | ||
} | ||
// The request status is no longer "pending". | ||
Console.WriteLine($"Response from /request-status: {requestStatusResponseJson}"); | ||
Console.WriteLine("Done!"); | ||
} | ||
} | ||
|
||
/* | ||
* Send a request with the 'response-type' header to get a request ID. /bmp is an arbitrary example. | ||
*/ | ||
public static async Task<string> GetBmpResponseAsync(string pathToFile, string fileName) | ||
{ | ||
using var httpClient = new HttpClient { BaseAddress = new Uri(BaseUri) }; | ||
|
||
using var bmpRequest = new HttpRequestMessage(HttpMethod.Post, "bmp"); | ||
|
||
bmpRequest.Headers.TryAddWithoutValidation("Api-Key", ApiKey); | ||
|
||
var apiResult = await response.Content.ReadAsStringAsync(); | ||
bmpRequest.Headers.Accept.Add(new("application/json")); | ||
var multipartContent = new MultipartFormDataContent(); | ||
|
||
var byteArray = File.ReadAllBytes(pathToFile); | ||
var byteAryContent = new ByteArrayContent(byteArray); | ||
multipartContent.Add(byteAryContent, "file", fileName); | ||
byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf"); | ||
|
||
// By adding the 'response-type' header to the request, the response will include a 'status' key. | ||
bmpRequest.Headers.Add("response-type", "requestId"); | ||
|
||
bmpRequest.Content = multipartContent; | ||
Console.WriteLine("Sending request to /bmp..."); | ||
var bmpResponse = await httpClient.SendAsync(bmpRequest); | ||
|
||
return await bmpResponse.Content.ReadAsStringAsync(); | ||
} | ||
|
||
/* | ||
* Get the request status from /request-status using the given request ID. | ||
*/ | ||
public static async Task<string> GetRequestStatusAsync(string requestId) | ||
{ | ||
using var httpClient = new HttpClient { BaseAddress = new Uri(BaseUri) }; | ||
using var request = new HttpRequestMessage(HttpMethod.Get, $"request-status/{requestId}"); | ||
request.Headers.TryAddWithoutValidation("Api-Key", ApiKey); | ||
|
||
var response = await httpClient.SendAsync(request); | ||
|
||
Console.WriteLine("API response received."); | ||
Console.WriteLine(apiResult); | ||
return await response.Content.ReadAsStringAsync(); | ||
} | ||
} |
89 changes: 73 additions & 16 deletions
89
Java/Endpoint Examples/Multipart Payload/RequestStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 38 additions & 13 deletions
51
JavaScript/Endpoint Examples/Multipart Payload/request-status.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,43 @@ | ||
const axios = require("axios"); | ||
const FormData = require("form-data"); | ||
const fs = require("fs"); | ||
|
||
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" } | ||
|
||
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, | ||
}; | ||
|
||
// define configuration options for axios request | ||
axios | ||
.request(config) | ||
.then((response) => { | ||
console.log(JSON.stringify(response.data)); | ||
}) | ||
.catch((error) => { | ||
axios(bmpConfig) | ||
.then(bmpResponse => { | ||
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); | ||
}); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.