import requests
import json
import os
import datetime
import logging
import traceback
import shutil
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
BACKUP_DIR = "http_request_backups" # Directory to store backups
def backup_http_requests(url, backup_dir=BACKUP_DIR):
"""
Backs up HTTP request data to a JSON file. Handles potential errors gracefully.
"""
try:
# Make the HTTP request
response = requests.get(url)
response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
# Parse the response as JSON
data = response.json()
# Create a timestamped backup filename
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"http_request_{timestamp}.json"
filepath = os.path.join(backup_dir, filename)
# Create the backup directory if it doesn't exist
os.makedirs(backup_dir, exist_ok=True)
# Write the data to the JSON file
with open(filepath, "w") as f:
json.dump(data, f, indent=4)
logging.info(f"Successfully backed up data from {url} to {filepath}")
except requests.exceptions.RequestException as e:
logging.error(f"Request error for {url}: {e}")
traceback.print_exc() # Print the full traceback for debugging
except json.JSONDecodeError as e:
logging.error(f"JSON decoding error for {url}: {e}")
traceback.print_exc()
except Exception as e:
logging.error(f"An unexpected error occurred for {url}: {e}")
traceback.print_exc()
if __name__ == "__main__":
# Example Usage
urls_to_backup = [
"https://rickandmortyapi.com/api/character",
"https://jsonplaceholder.typicode.com/todos/1"
]
for url in urls_to_backup:
backup_http_requests(url)
Add your comment