Repository navigation
Expand file tree
/
Copy pathquickstart.py
More file actions
89 lines (69 loc) · 2.92 KB
/
Copy pathquickstart.py
File metadata and controls
89 lines (69 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
Quickstart: authenticate, fetch live football matches, parse the Event model,
and print the scoreboard — with production-grade error handling.
What this demonstrates:
* Reading the API key from the environment (never hard-code it).
* The two required headers (X-RapidAPI-Key / X-RapidAPI-Host).
* Handling 200 (data), 204 (route OK but no data), 429 (rate limit) and
5xx (server error) with exponential backoff.
* Safely reading optional/nested fields from an Event.
Run:
export RAPIDAPI_KEY="your_key_here"
pip install requests
python quickstart.py
Switch sports by setting RAPIDAPI_HOST (e.g. basketapi1.p.rapidapi.com) and the
path (e.g. /api/basketball/matches/live). See ../README.md for the host table.
"""
import os
import sys
import time
import requests
HOST = os.environ.get("RAPIDAPI_HOST", "footapi7.p.rapidapi.com")
BASE_URL = f"https://{HOST}"
API_KEY = os.environ.get("RAPIDAPI_KEY")
def get(path, *, max_retries=4, timeout=15):
"""GET an API path and return parsed JSON (or None for 204 / no data).
Retries on 429 and 5xx with exponential backoff, honouring Retry-After.
Raises for non-retryable client errors (other 4xx).
"""
if not API_KEY:
sys.exit("Set RAPIDAPI_KEY in your environment first.")
url = f"{BASE_URL}{path}"
headers = {"X-RapidAPI-Key": API_KEY, "X-RapidAPI-Host": HOST}
backoff = 1.0
for attempt in range(1, max_retries + 1):
resp = requests.get(url, headers=headers, timeout=timeout)
if resp.status_code == 200:
return resp.json()
if resp.status_code == 204:
return None # the route is valid but there's no data for these params
if resp.status_code == 429 or resp.status_code >= 500:
if attempt == max_retries:
resp.raise_for_status()
wait = float(resp.headers.get("Retry-After", backoff))
print(
f" {resp.status_code} — retrying in {wait:.0f}s "
f"(attempt {attempt}/{max_retries})",
file=sys.stderr,
)
time.sleep(wait)
backoff *= 2
continue
# Other 4xx (e.g. 401/403/404) are not worth retrying.
resp.raise_for_status()
return None
def main():
data = get("/api/matches/live")
events = (data or {}).get("events", [])
print(f"{len(events)} live football matches\n")
for ev in events[:20]:
# Required fields are safe; optional ones are null-checked.
home = ev["homeTeam"]["name"]
away = ev["awayTeam"]["name"]
home_score = ev.get("homeScore", {}).get("current", "-")
away_score = ev.get("awayScore", {}).get("current", "-")
status = ev.get("status", {}).get("description", "")
tournament = ev.get("tournament", {}).get("name", "")
print(f"{home} {home_score} - {away_score} {away} ({status}) · {tournament}")
if __name__ == "__main__":
main()