def sort_requests(requests):
"""Sorts a list of HTTP request records by scheduled run time."""
# Check if the input is valid
if not isinstance(requests, list):
raise TypeError("Input must be a list.")
if not all(isinstance(req, dict) for req in requests):
raise TypeError("Each element in the list must be a dictionary.")
if not all('scheduled_run' in req for req in requests):
raise ValueError("Each request dictionary must have a 'scheduled_run' key.")
# Sort the requests based on the 'scheduled_run' value.
# We assume 'scheduled_run' is a comparable value (e.g., timestamp).
sorted_requests = sorted(requests, key=lambda req: req['scheduled_run'])
return sorted_requests
if __name__ == '__main__':
# Example Usage
requests = [
{'url': 'https://example.com/api/data', 'scheduled_run': 1678886400}, # Example timestamp
{'url': 'https://example.com/api/users', 'scheduled_run': 1678972800},
{'url': 'https://example.com/api/products', 'scheduled_run': 1678800000}
]
sorted_requests = sort_requests(requests)
# Print the sorted requests
for req in sorted_requests:
print(req)
Add your comment