forked from pdfrest/pdfrest-api-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete.cs
More file actions
59 lines (53 loc) · 2.2 KB
/
Copy pathdelete.cs
File metadata and controls
59 lines (53 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* What this sample does:
* - Called from Program.cs to delete multiple resources by ids.
*
* Setup (environment):
* - Copy .env.example to .env
* - Set PDFREST_API_KEY=your_api_key_here
* - Optional: set PDFREST_URL to override the API region. For EU/GDPR compliance and proximity, use:
* PDFREST_URL=https://eu-api.pdfrest.com
* For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
*
* Usage:
* dotnet run -- batch-delete <id1> [id2] [...]
*
* Output:
* - Prints JSON responses; non-2xx results exit non-zero.
*/
using Newtonsoft.Json.Linq;
using System.Text;
namespace Samples.EndpointExamples.JsonPayload
{
public static class BatchDelete
{
public static async Task Execute(string[] args)
{
if (args == null || args.Length < 1)
{
Console.Error.WriteLine("batch-delete requires <id1> [id2] [id3] ... OR a single comma-separated list");
Environment.Exit(1);
return;
}
var apiKey = Environment.GetEnvironmentVariable("PDFREST_API_KEY");
if (string.IsNullOrWhiteSpace(apiKey))
{
Console.Error.WriteLine("Missing required environment variable: PDFREST_API_KEY");
Environment.Exit(1);
return;
}
var baseUrl = Environment.GetEnvironmentVariable("PDFREST_URL") ?? "https://api.pdfrest.com";
var url = baseUrl.TrimEnd('/') + "/delete";
string ids = args.Length == 1 ? args[0] : string.Join(", ", args);
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, url);
request.Headers.TryAddWithoutValidation("Api-Key", apiKey);
request.Headers.TryAddWithoutValidation("Content-Type", "application/json");
JObject parameterJson = new JObject { ["ids"] = ids };
request.Content = new StringContent(parameterJson.ToString(), Encoding.UTF8, "application/json");
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
}
}