From 98e1836da5ebcd33f6a6f63f366393a74be3b784 Mon Sep 17 00:00:00 2001 From: Cristiano Sticca Date: Fri, 15 Nov 2024 12:52:47 +0100 Subject: [PATCH] add python script for IBAN and CBILL massive upload --- .gitignore | 2 + .../cbill-iban-upload.py | 92 +++++++++++++++++++ python/smo-support-cbill-iban/cbill-test.csv | 2 + python/smo-support-cbill-iban/iban-test.csv | 2 + 4 files changed, 98 insertions(+) create mode 100644 python/smo-support-cbill-iban/cbill-iban-upload.py create mode 100644 python/smo-support-cbill-iban/cbill-test.csv create mode 100644 python/smo-support-cbill-iban/iban-test.csv diff --git a/.gitignore b/.gitignore index 79d7c43..9754d5d 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,5 @@ hs_err_pid* # Helm /helm/charts/* **/.terraform/ + +.DS_Store \ No newline at end of file diff --git a/python/smo-support-cbill-iban/cbill-iban-upload.py b/python/smo-support-cbill-iban/cbill-iban-upload.py new file mode 100644 index 0000000..dbcdd15 --- /dev/null +++ b/python/smo-support-cbill-iban/cbill-iban-upload.py @@ -0,0 +1,92 @@ +import sys +import requests + +# Configurazione globale +CONFIG = { + "uat": { + "base_url": "https://api.uat.platform.pagopa.it", + "subscription_key": "909d3363590745908189968449a41977" + }, + "prod": { + "base_url": "https://api.platform.pagopa.it", + "subscription_key": "d4d8a9a924044e3f89dfe4ca0af05eeb" + }, + "endpoints": { + "cbill": "/apiconfig/auth/api/v1/creditorinstitutions/cbill?incremental=true", + "iban": "/apiconfig/auth/api/v1/creditorinstitutions/ibans/csv" + } +} + +def upload_file(api_url, file_path, subscription_key): + """ + Carica un file su un endpoint API e gestisce la risposta. + + Args: + api_url (str): URL dell'API da chiamare. + file_path (str): Percorso del file da caricare. + subscription_key (str): Chiave di sottoscrizione per l'API. + + Returns: + bool: True se l'upload ha avuto successo, False altrimenti. + """ + try: + # Legge il file in memoria + with open(file_path, 'rb') as file_data: + files = {'file': file_data} + headers = { + 'Ocp-Apim-Subscription-Key': subscription_key + } + + # Esegue la richiesta POST + response = requests.post(api_url, headers=headers, files=files) + + # Controlla lo status code + if response.status_code == 200: + print("Upload riuscito!") + return True + else: + print(f"Errore nella chiamata API. Status code: {response.status_code}") + print("Messaggio di errore:", response.text) + return False + except FileNotFoundError: + print("Errore: file non trovato.") + return False + except requests.exceptions.RequestException as e: + print(f"Errore durante la chiamata API: {e}") + return False + + +def main(): + if len(sys.argv) != 4: + print("Uso corretto: python script.py ") + print(" : 'uat' o 'prod'") + print(" : 'cbill' o 'iban'") + sys.exit(1) + + ambiente = sys.argv[1].lower() + tipo = sys.argv[2].lower() + file_path = sys.argv[3] + + # Verifica ambiente + if ambiente not in CONFIG: + print("Errore: ambiente sconosciuto. Usa 'uat' o 'prod'.") + sys.exit(1) + + # Verifica tipo + if tipo not in CONFIG["endpoints"]: + print("Errore: tipo sconosciuto. Usa 'cbill' o 'iban'.") + sys.exit(1) + + # Costruisce l'URL dell'API + base_url = CONFIG[ambiente]["base_url"] + endpoint = CONFIG["endpoints"][tipo] + api_url = f"{base_url}{endpoint}" + + # Recupera la subscription key + subscription_key = CONFIG[ambiente]["subscription_key"] + + # Carica il file + upload_file(api_url, file_path, subscription_key) + +if __name__ == "__main__": + main() diff --git a/python/smo-support-cbill-iban/cbill-test.csv b/python/smo-support-cbill-iban/cbill-test.csv new file mode 100644 index 0000000..7f4c30e --- /dev/null +++ b/python/smo-support-cbill-iban/cbill-test.csv @@ -0,0 +1,2 @@ +CF;SIA;RAGIONESOCIALE +99999000013;3333;EC DEMO \ No newline at end of file diff --git a/python/smo-support-cbill-iban/iban-test.csv b/python/smo-support-cbill-iban/iban-test.csv new file mode 100644 index 0000000..b1ba1ac --- /dev/null +++ b/python/smo-support-cbill-iban/iban-test.csv @@ -0,0 +1,2 @@ +iddominio,codiceipa,ragionesociale,descrizione,iban,dataattivazioneiban,operazione +99999000013,tbd,EC DEMO,,IT55K0300203280852494789649,2024-11-30,I \ No newline at end of file