Skip to content

Commit 0b445b4

Browse files
Merge pull request #101 from datalogics-tsmith/batch-delete
PDFCLOUD-5122 Add batch delete samples
2 parents 9bc5abb + 8e13c18 commit 0b445b4

12 files changed

Lines changed: 283 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Newtonsoft.Json.Linq;
2+
using System.Text;
3+
4+
var client = new HttpClient();
5+
var request = new HttpRequestMessage(HttpMethod.Post, "https://api.pdfrest.com/delete");
6+
request.Headers.Add("api-key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
7+
request.Headers.TryAddWithoutValidation("Content-Type", "application/json");
8+
9+
10+
JObject parameterJson = new JObject
11+
{
12+
["ids"] = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
13+
};
14+
15+
request.Content = new StringContent(parameterJson.ToString(), Encoding.UTF8, "application/json"); ;
16+
var response = await client.SendAsync(request);
17+
response.EnsureSuccessStatusCode();
18+
Console.WriteLine(await response.Content.ReadAsStringAsync());
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var client = new HttpClient();
2+
var request = new HttpRequestMessage(HttpMethod.Post, "https://api.pdfrest.com/delete");
3+
request.Headers.Add("api-key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
4+
var content = new MultipartFormDataContent();
5+
content.Add(new StringContent("xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx,xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"), "ids");
6+
request.Content = content;
7+
var response = await client.SendAsync(request);
8+
response.EnsureSuccessStatusCode();
9+
Console.WriteLine(await response.Content.ReadAsStringAsync());
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import io.github.cdimascio.dotenv.Dotenv;
2+
import java.io.IOException;
3+
import java.util.concurrent.TimeUnit;
4+
import okhttp3.*;
5+
import org.json.JSONObject;
6+
7+
public class BatchDelete {
8+
9+
// Specify your API key here, or in the environment variable PDFREST_API_KEY.
10+
// You can also put the environment variable in a .env file.
11+
private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
12+
13+
public static void main(String[] args) {
14+
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();
15+
16+
String JSONString =
17+
String.format(
18+
"{ \"ids\":\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\"}");
19+
20+
final RequestBody requestBody =
21+
RequestBody.create(JSONString, MediaType.parse("application/json"));
22+
23+
Request request =
24+
new Request.Builder()
25+
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
26+
.url("https://api.pdfrest.com/delete")
27+
.post(requestBody)
28+
.build();
29+
try {
30+
OkHttpClient client =
31+
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();
32+
33+
Response response = client.newCall(request).execute();
34+
System.out.println("Processing Result code " + response.code());
35+
if (response.body() != null) {
36+
System.out.println(prettyJson(response.body().string()));
37+
}
38+
} catch (IOException e) {
39+
throw new RuntimeException(e);
40+
}
41+
}
42+
43+
private static String prettyJson(String json) {
44+
// https://stackoverflow.com/a/9583835/11996393
45+
return new JSONObject(json).toString(4);
46+
}
47+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import io.github.cdimascio.dotenv.Dotenv;
2+
import java.io.IOException;
3+
import java.util.concurrent.TimeUnit;
4+
import okhttp3.*;
5+
import org.json.JSONObject;
6+
7+
public class BatchDelete {
8+
9+
// Specify your API key here, or in the environment variable PDFREST_API_KEY.
10+
// You can also put the environment variable in a .env file.
11+
private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
12+
13+
public static void main(String[] args) {
14+
15+
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();
16+
17+
RequestBody requestBody =
18+
new MultipartBody.Builder()
19+
.setType(MultipartBody.FORM)
20+
.addFormDataPart(
21+
"ids", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")
22+
.build();
23+
Request request =
24+
new Request.Builder()
25+
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
26+
.url("https://api.pdfrest.com/delete")
27+
.post(requestBody)
28+
.build();
29+
try {
30+
OkHttpClient client =
31+
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();
32+
Response response = client.newCall(request).execute();
33+
System.out.println("Result code " + response.code());
34+
if (response.body() != null) {
35+
System.out.println(prettyJson(response.body().string()));
36+
}
37+
} catch (IOException e) {
38+
throw new RuntimeException(e);
39+
}
40+
}
41+
42+
private static String prettyJson(String json) {
43+
// https://stackoverflow.com/a/9583835/11996393
44+
return new JSONObject(json).toString(4);
45+
}
46+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const axios = require("axios");
2+
3+
let config = {
4+
method: "post",
5+
maxBodyLength: Infinity,
6+
url: "https://api.pdfrest.com/delete",
7+
headers: {
8+
"api-key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
9+
"Content-Type": "application/json",
10+
},
11+
data: {
12+
ids:
13+
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
14+
},
15+
};
16+
17+
axios
18+
.request(config)
19+
.then((response) => {
20+
console.log(JSON.stringify(response.data));
21+
})
22+
.catch((error) => {
23+
console.log(error);
24+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// This request demonstrates how to delete multiple files at once
2+
var axios = require("axios");
3+
var FormData = require("form-data");
4+
5+
// Create a new form data instance and append the PDF file and parameters to it
6+
var data = new FormData();
7+
data.append(
8+
"ids",
9+
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
10+
);
11+
12+
// define configuration options for axios request
13+
var config = {
14+
method: "post",
15+
maxBodyLength: Infinity, // set maximum length of the request body
16+
url: "https://api.pdfrest.com/delete",
17+
headers: {
18+
"Api-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", // Replace with your API key
19+
...data.getHeaders(), // set headers for the request
20+
},
21+
data: data, // set the data to be sent with the request
22+
};
23+
24+
// send request and handle response or error
25+
axios(config)
26+
.then(function (response) {
27+
console.log(JSON.stringify(response.data));
28+
})
29+
.catch(function (error) {
30+
console.log(error);
31+
});
32+
33+
// If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.js' sample.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client.
3+
4+
use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace.
5+
use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class.
6+
use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams.
7+
8+
9+
$delete_client = new Client(['http_errors' => false]);
10+
$delete_headers = [
11+
'api-key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
12+
'Content-Type' => 'application/json'
13+
];
14+
$delete_body = '{"ids":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}';
15+
$delete_request = new Request('POST', 'https://api.pdfrest.com/delete', $delete_headers, $delete_body);
16+
$delete_res = $delete_client->sendAsync($delete_request)->wait();
17+
echo $delete_res->getBody() . PHP_EOL;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client.
3+
4+
use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace.
5+
use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class.
6+
use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams.
7+
8+
$client = new Client(); // Create a new instance of the Guzzle HTTP client.
9+
10+
$headers = [
11+
'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' // Set the API key in the headers for authentication.
12+
];
13+
14+
$options = [
15+
'multipart' => [
16+
[
17+
'name' => 'ids',
18+
'contents' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
19+
]
20+
]
21+
];
22+
23+
$request = new Request('POST', 'https://api.pdfrest.com/delete', $headers); // Create a new HTTP POST request with the API endpoint and headers.
24+
25+
$res = $client->sendAsync($request, $options)->wait(); // Send the asynchronous request and wait for the response.
26+
27+
echo $res->getBody(); // Output the response body, which contains the result
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import requests
2+
import json
3+
4+
delete_data = { "ids" : "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" }
5+
6+
7+
print("Processing files...")
8+
delete_response = requests.post(url='https://api.pdfrest.com/delete',
9+
data=json.dumps(delete_data),
10+
headers={'Content-Type': 'application/json', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"})
11+
12+
13+
14+
print("Processing response status code: " + str(delete_response.status_code))
15+
if delete_response.ok:
16+
delete_response_json = delete_response.json()
17+
print(json.dumps(delete_response_json, indent = 2))
18+
19+
else:
20+
print(delete_response.text)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from requests_toolbelt import MultipartEncoder
2+
import requests
3+
import json
4+
5+
delete_endpoint_url = 'https://api.pdfrest.com/delete'
6+
7+
mp_encoder_delete = MultipartEncoder(
8+
fields={
9+
'ids' : 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
10+
}
11+
)
12+
13+
# Let's set the headers that the delete endpoint expects.
14+
# Since MultipartEncoder is used, the 'Content-Type' header gets set to 'multipart/form-data' via the content_type attribute below.
15+
headers = {
16+
'Accept': 'application/json',
17+
'Content-Type': mp_encoder_delete.content_type,
18+
'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
19+
}
20+
21+
print("Sending POST request to delete endpoint...")
22+
response = requests.post(delete_endpoint_url, data=mp_encoder_delete, headers=headers)
23+
24+
print("Response status code: " + str(response.status_code))
25+
26+
if response.ok:
27+
response_json = response.json()
28+
print(json.dumps(response_json, indent = 2))
29+
else:
30+
print(response.text)
31+
32+
# If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.py' sample.

0 commit comments

Comments
 (0)