1. import requests
  2. import json
  3. import os
  4. import datetime
  5. import logging
  6. import traceback
  7. import shutil
  8. # Configure logging
  9. logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
  10. BACKUP_DIR = "http_request_backups" # Directory to store backups
  11. def backup_http_requests(url, backup_dir=BACKUP_DIR):
  12. """
  13. Backs up HTTP request data to a JSON file. Handles potential errors gracefully.
  14. """
  15. try:
  16. # Make the HTTP request
  17. response = requests.get(url)
  18. response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
  19. # Parse the response as JSON
  20. data = response.json()
  21. # Create a timestamped backup filename
  22. timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
  23. filename = f"http_request_{timestamp}.json"
  24. filepath = os.path.join(backup_dir, filename)
  25. # Create the backup directory if it doesn't exist
  26. os.makedirs(backup_dir, exist_ok=True)
  27. # Write the data to the JSON file
  28. with open(filepath, "w") as f:
  29. json.dump(data, f, indent=4)
  30. logging.info(f"Successfully backed up data from {url} to {filepath}")
  31. except requests.exceptions.RequestException as e:
  32. logging.error(f"Request error for {url}: {e}")
  33. traceback.print_exc() # Print the full traceback for debugging
  34. except json.JSONDecodeError as e:
  35. logging.error(f"JSON decoding error for {url}: {e}")
  36. traceback.print_exc()
  37. except Exception as e:
  38. logging.error(f"An unexpected error occurred for {url}: {e}")
  39. traceback.print_exc()
  40. if __name__ == "__main__":
  41. # Example Usage
  42. urls_to_backup = [
  43. "https://rickandmortyapi.com/api/character",
  44. "https://jsonplaceholder.typicode.com/todos/1"
  45. ]
  46. for url in urls_to_backup:
  47. backup_http_requests(url)

Add your comment