From d6721e61bee1cd95955ec07e887bf56808711a99 Mon Sep 17 00:00:00 2001 From: Christopher Green Date: Thu, 6 Jun 2024 15:20:01 -0500 Subject: [PATCH] Update request-status.py sample --- .../Multipart Payload/request-status.py | 45 ++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/Python/Endpoint Examples/Multipart Payload/request-status.py b/Python/Endpoint Examples/Multipart Payload/request-status.py index 4aa28ee..588e62f 100644 --- a/Python/Endpoint Examples/Multipart Payload/request-status.py +++ b/Python/Endpoint Examples/Multipart Payload/request-status.py @@ -2,23 +2,46 @@ import requests import json -api_polling_endpoint_url = 'https://api.pdfrest.com/request-status' +api_key = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here -request_id = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place requestId to poll here +pdfa_endpoint_url = 'https://api.pdfrest.com/pdfa' -api_polling_endpoint_url = f'https://api.pdfrest.com/request-status/{request_id}' +mp_encoder_pdfa = MultipartEncoder( + fields={ + 'file': ('file_name.pdf', open('/path/to/file.pdf', 'rb'), 'application/pdf'), + 'output_type': 'PDF/A-1b', + } +) -headers = { - 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here +pdfa_headers = { + 'Accept': 'application/json', + 'Content-Type': mp_encoder_pdfa.content_type, + 'Response-Type': "requestId", + 'Api-Key': api_key } -print("Sending GET request to request-status endpoint...") -response = requests.get(api_polling_endpoint_url, headers=headers) +print("Sending POST request to pdfa endpoint...") +response = requests.post(pdfa_endpoint_url, data=mp_encoder_pdfa, headers=pdfa_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) + + response_json = response.json() + request_id = response_json["requestId"] + api_polling_endpoint_url = f'https://api.pdfrest.com/request-status/{request_id}' + + headers = { + 'Api-Key': api_key + } + + 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)