import os import requests from dotenv import load_dotenv # Load environment variables load_dotenv() HOST = os.getenv("HOST") USER = os.getenv("USER") PASS = os.getenv("PASS") CSV_FILE_PATH = r'C:\Humbingo\to_humbingo\transactions.csv' def get_auth_token(): url = f"{HOST}/auth/token/" payload = {"phone_no": USER, "password": PASS} try: response = requests.post(url, data=payload) response.raise_for_status() return response.json().get('access') except requests.exceptions.RequestException as e: print(f"Error obtaining auth token: {e}") return None def send_csv_to_server(csv_file_path, token): url = f"{HOST}/api/v1/uploadTransaction/" headers = {'Authorization': f'Bearer {token}'} try: with open(csv_file_path, 'rb') as file: files = {'file': file} response = requests.post(url, files=files, headers=headers) if response.status_code == 201: print("CSV file sent successfully to the API") print("Server response:", response.json()) else: print(f"Failed to send CSV file to the API. Status code: {response.status_code}") print("Response content:", response.content.decode('utf-8')) except Exception as e: print(f"Error sending CSV file to server: {e}") def main(): token = get_auth_token() if token: send_csv_to_server(CSV_FILE_PATH, token) else: print("Failed to obtain auth token.") if __name__ == "__main__": main()