From dfc7f6c299273dfc2231336c62aa4774f406e1e1 Mon Sep 17 00:00:00 2001 From: monkumar Date: Mon, 23 Jun 2025 10:46:42 +0530 Subject: [PATCH 1/3] adding sample for pgp encrypted upload --- .../batch-upload-mtls-with-keys.py | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 samples/BatchUploadAPI/batch-upload-mtls-with-keys.py diff --git a/samples/BatchUploadAPI/batch-upload-mtls-with-keys.py b/samples/BatchUploadAPI/batch-upload-mtls-with-keys.py new file mode 100644 index 0000000..dfeffa8 --- /dev/null +++ b/samples/BatchUploadAPI/batch-upload-mtls-with-keys.py @@ -0,0 +1,76 @@ +from CyberSource import * +import os +from importlib.machinery import SourceFileLoader +from pathlib import Path + + +config_file = os.path.join(os.getcwd(), "data", "Configuration.py") +configuration = SourceFileLoader("module.name", config_file).load_module() + + +# To delete None values in Input Request Json body +def del_none(d): + for key, value in list(d.items()): + if value is None: + del d[key] + elif isinstance(value, dict): + del_none(value) + elif isinstance(value, list): + for item in value: + del_none(item) + return d + + +def batch_upload_mtls_with_keys(): + + input_file_path = os.path.join( + os.getcwd(), "data", "batchAPIMTLS", "batchapiTest.csv" + ) + public_key_file = os.path.join( + os.getcwd(), "data", "batchAPIMTLS", "bts-encryption-public.asc" + ) + client_certificate_file = os.path.join( + os.getcwd(), "data", "batchAPIMTLS", "client_cert.crt" + ) + private_key_file = os.path.join( + os.getcwd(), "data", "batchAPIMTLS", "client_private_key.key" + ) + server_certificate_file = os.path.join( + os.getcwd(), "data", "batchAPIMTLS", "server.crt.pem" + ) + env_host_name = "secure-batch-test.cybersource.com" # cas env + + try: + config_obj = configuration.Configuration() + client_config = config_obj.get_configuration() + api_instance = BatchUploadWithMTLSApi(client_config["log_config"]) + return_data, status, body = ( + api_instance.upload_batch_api_with_key_and_certs_file( + input_file_path=input_file_path, + environment_hostname=env_host_name, + pgp_encryption_public_key_path=public_key_file, + server_trust_cert_path=server_certificate_file, + client_cert_path=client_certificate_file, + client_key_path=private_key_file, + ) + ) + print("\nAPI RESPONSE CODE : ", status) + print("\nAPI RESPONSE BODY : ", body) + + write_log_audit(status) + + return return_data + except Exception as e: + write_log_audit(getattr(e, "status", "0")) + print( + "\nException when calling BatchUploadMTLS->upload_batch_api_with_key_and_certs_file: %s\n" + % e + ) + + +def write_log_audit(status): + print(f"[Sample Code Testing] [{Path(__file__).stem}] {status}") + + +if __name__ == "__main__": + batch_upload_mtls_with_keys() From 7aec19395102dbf8ba597f200ba29ecea6c84048 Mon Sep 17 00:00:00 2001 From: monkumar Date: Mon, 23 Jun 2025 11:18:49 +0530 Subject: [PATCH 2/3] minor fix --- samples/BatchUploadAPI/batch-upload-mtls-with-keys.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/BatchUploadAPI/batch-upload-mtls-with-keys.py b/samples/BatchUploadAPI/batch-upload-mtls-with-keys.py index dfeffa8..879113d 100644 --- a/samples/BatchUploadAPI/batch-upload-mtls-with-keys.py +++ b/samples/BatchUploadAPI/batch-upload-mtls-with-keys.py @@ -44,7 +44,7 @@ def batch_upload_mtls_with_keys(): config_obj = configuration.Configuration() client_config = config_obj.get_configuration() api_instance = BatchUploadWithMTLSApi(client_config["log_config"]) - return_data, status, body = ( + body, status, headers = ( api_instance.upload_batch_api_with_key_and_certs_file( input_file_path=input_file_path, environment_hostname=env_host_name, @@ -59,7 +59,7 @@ def batch_upload_mtls_with_keys(): write_log_audit(status) - return return_data + return body except Exception as e: write_log_audit(getattr(e, "status", "0")) print( From 4b3e78f89287af9a80ebae46aa8d7d8f73b04f08 Mon Sep 17 00:00:00 2001 From: monkumar Date: Mon, 23 Jun 2025 12:40:39 +0530 Subject: [PATCH 3/3] minor refactoring --- samples/BatchUploadAPI/batch-upload-mtls-with-keys.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/BatchUploadAPI/batch-upload-mtls-with-keys.py b/samples/BatchUploadAPI/batch-upload-mtls-with-keys.py index 879113d..d69a22b 100644 --- a/samples/BatchUploadAPI/batch-upload-mtls-with-keys.py +++ b/samples/BatchUploadAPI/batch-upload-mtls-with-keys.py @@ -49,9 +49,9 @@ def batch_upload_mtls_with_keys(): input_file_path=input_file_path, environment_hostname=env_host_name, pgp_encryption_public_key_path=public_key_file, - server_trust_cert_path=server_certificate_file, client_cert_path=client_certificate_file, client_key_path=private_key_file, + server_trust_cert_path=server_certificate_file, ) ) print("\nAPI RESPONSE CODE : ", status)