import base64 import os import rsa from datetime import date import secrets import string import requests import json def generate_token_id(length): characters = string.ascii_letters + string.digits # + string.punctuation token = ''.join(secrets.choice(characters) for _ in range(length)) return token # Examples for what will be generated # 5!bA9H2f1q^... # Xe7uM$4d9@... # &3yTb1*8Z#... # %pWqN7!6zX... # @9oV!s6Rd2... def get_today_date(): today = date.today() return str(today) # Example for what will be returned # 2023-06-29 def generate_request(data): url = 'http://ipygg-api-test-env.ap-east-1.elasticbeanstalk.com/SBT' pubkey_path = os.path.join(os.path.dirname(__file__), '..', 'pubkey.pem') with open(pubkey_path, 'rb') as f: pubKey = rsa.PublicKey.load_pkcs1(f.read()) for key, value in data.items(): value_bytes = value.encode("utf-8") encrypted_value = rsa.encrypt(value_bytes, pubKey) encoded_value = base64.b64encode(encrypted_value) data[key] = encoded_value # Write the encrypted and encoded values to a file with open("sbt_request.txt", "w") as f: for key, value in data.items(): f.write(f"{key}: {value}\n\n") # posting Json file to api r = requests.post(url, data=data) print(r.json) def split_data(data): # request_id = "request1234" # token_id = "12344321" f = open('data1.txt', 'r') with open('data1.txt') as f: data_raw = f.read() data = json.loads(data_raw) if "avg_score" not in data.keys(): data["avg_score"] = "0" elif "similarity_score" not in data.keys(): data["similarity_score"] = "0" sbt_data = { "endpoint": "SBT", "apiType": "store_img_verif", "requestId": "request_id_1234", "date": get_today_date(), # a string "docType": "HKID", "nameDoc": data["name_on_id"], # a string; lower case with space separate; e.g. san chi nan "docID": data["hkid"], # a string; with bracket (); e.g. G908833(1) "docValidity": data["validity"], # a string; "True" or "False" "dateOfIssue": data["issue_date"], # a string; month-year; e.g. 07-81 "matchingScore": str(data["similarity_score"]), # a string; e.g. "0.957" "bank":str(data["bank"]), # "nameStatement":str(data["nameStatement"]), # "address":str(data["address"]), # "asset": str(data["totalAsset"]), # a string containing only numbers "liability": str(data["totalLiability"]), # a string containing only numbers "statementDate": str(data["statementDate"]), # a string } generate_request(sbt_data)