import time
import requests
def request_with_retries(method, url, headers=None, **kwargs):
"""Basic retry policy with 429 and transient 5xx handling."""
backoff = 1.0
for attempt in range(5):
try:
r = requests.request(method, url, headers=headers, timeout=60, **kwargs)
if r.status_code == 429:
# Rate limit
time.sleep(backoff)
backoff = min(backoff * 2, 30)
continue
if 500 <= r.status_code < 600:
# Transient server error
time.sleep(backoff)
backoff = min(backoff * 2, 30)
continue
# Non-retry path
r.raise_for_status()
return r
except requests.exceptions.RequestException as e:
if attempt == 4:
raise
time.sleep(backoff)
backoff = min(backoff * 2, 30)
raise RuntimeError("Unreachable")
# Example usage
API_KEY = "sk-your-api-key-here"
headers = {"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"}
url = "https://tryvinci.com/api/v1/generate/text-to-video"
data = {"prompt": "Calm ocean at sunrise", "duration_seconds": 5}
resp = request_with_retries("POST", url, headers=headers, json=data)
print(resp.json())