[text] K

Viewer

  1. import requests
  2. import json
  3. import os
  4.  
  5. # Tesco Portal API endpoints
  6. auth_url = "https://toolkit.tesco.com/partner/reports/api/v1/authenticate"
  7. download_url = "https://toolkit.tesco.com/partner/reports/api/v1/download-report"
  8.  
  9. # Username and password for authentication
  10. username = "your_username"
  11. password = "your_password"
  12.  
  13. # Generate UUID for authentication
  14. auth_token = str(uuid.uuid4())
  15.  
  16. # Authentication request data
  17. data = {
  18.     "username": username,
  19.     "password": password
  20. }
  21.  
  22. # Headers for authentication request with UUID token
  23. headers = {
  24.     "Content-Type": "application/json",
  25.     "Authorization": f"Bearer {auth_token}"
  26. }
  27.  
  28. # Authenticate and get the session token
  29. response = requests.post(auth_url, json=data, headers=headers)
  30.  
  31. # Check if authentication was successful
  32. if response.status_code == 200:
  33.     print("Authentication successful!")
  34.     session_token = response.json().get("sessionToken")
  35.  
  36.     # Set up headers for downloading files
  37.     headers_download = {
  38.         "Authorization": f"Bearer {session_token}"
  39.     }
  40.  
  41.     # Get the latest report from the JSON response
  42.     latest_report = max(response.json()["reports"], key=lambda x: x["createdOn"])
  43.  
  44.     # Extract filename and ID of the latest report
  45.     report_name = latest_report["reportName"]
  46.     report_id = latest_report["id"]
  47.  
  48.     # Download the latest report
  49.     download_response = requests.get(f"{download_url}?reportId={report_id}", headers=headers_download)
  50.  
  51.     # Check if download was successful
  52.     if download_response.status_code == 200:
  53.         # Save the file to DBFS with the same name
  54.         with open(f"/dbfs/{report_name}", "wb") as file:
  55.             file.write(download_response.content)
  56.         print(f"File '{report_name}' downloaded and saved in DBFS.")
  57.     else:
  58.         print("Failed to download the file.")
  59. else:
  60.     print("Authentication failed. Please check your credentials.")

Editor

You can edit this paste and save as new:


File Description
  • K
  • Paste Code
  • 29 Apr-2024
  • 1.89 Kb
You can Share it: