Skip to content

Adding sample for PGP encrypted upload #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions samples/BatchUploadAPI/batch-upload-mtls-with-keys.py
Original file line number Diff line number Diff line change
@@ -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"])
body, status, headers = (
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,
client_cert_path=client_certificate_file,
client_key_path=private_key_file,
server_trust_cert_path=server_certificate_file,
)
)
print("\nAPI RESPONSE CODE : ", status)
print("\nAPI RESPONSE BODY : ", body)

write_log_audit(status)

return body
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()