Skip to content

Commit

Permalink
Add more build endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
raimannma committed Sep 20, 2024
1 parent a09d59a commit 7e2db4d
Showing 1 changed file with 48 additions and 7 deletions.
55 changes: 48 additions & 7 deletions deadlock_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import time

from discord_webhook import DiscordWebhook
from fastapi import FastAPI
from fastapi import FastAPI, HTTPException
from pydantic import TypeAdapter
from starlette.responses import RedirectResponse, Response

Expand Down Expand Up @@ -36,35 +36,76 @@ def redirect_to_docs():
@app.get("/builds")
def get_builds(response: Response) -> dict[str, list[Build]]:
last_modified = os.path.getmtime("builds.json")
cache_time = dynamic_cache_time(last_modified)
cache_time = dynamic_cache_time(last_modified, CACHE_AGE_BUILDS)
response.headers["Cache-Control"] = f"public, max-age={cache_time}"
response.headers["Last-Updated"] = str(int(last_modified))
ta = TypeAdapter(dict[str, list[Build]])
with open("builds.json") as f:
return ta.validate_json(f.read())


@app.get("/builds/{build_id}")
def get_build(response: Response, build_id: int) -> Build:
builds = get_builds(response)
build = next(
(
b
for bs in builds.values()
for b in bs
if b.hero_build.hero_build_id == build_id
),
None,
)
if build is None:
raise HTTPException(status_code=404, detail="Build not found")
return build


@app.get("/builds/by-hero-id/{hero_id}")
def get_builds_by_hero_id(response: Response, hero_id: int) -> list[Build]:
builds = get_builds(response)
filtered = {
k: [h for h in v if h.hero_build.hero_id == hero_id] for k, v in builds.items()
}
filtered = {k: v for k, v in filtered.items() if len(v) > 0}
if len(filtered) == 0:
raise HTTPException(status_code=404, detail="Hero not found")
return next(v for k, v in builds.items())


@app.get("/builds/by-hero-name/{hero_name}")
def get_builds_by_hero_name(response: Response, hero_name: str) -> list[Build]:
builds = get_builds(response)
filtered = next(
(v for k, v in builds.items() if k.lower() == hero_name.lower()),
None,
)
if filtered is None:
raise HTTPException(status_code=404, detail="Hero not found")
return filtered


@app.get("/active-matches")
def get_active_matches(response: Response) -> list[ActiveMatch]:
last_modified = os.path.getmtime("active_matches.json")
cache_time = dynamic_cache_time(last_modified)
cache_time = dynamic_cache_time(last_modified, CACHE_AGE_ACTIVE_MATCHES)
response.headers["Cache-Control"] = f"public, max-age={cache_time}"
response.headers["Last-Updated"] = str(int(last_modified))
with open("active_matches.json") as f:
return APIActiveMatch.model_validate_json(f.read()).active_matches


def dynamic_cache_time(last_modified: float) -> int:
def dynamic_cache_time(last_modified: float, max_cache_age: int) -> int:
age = time.time() - last_modified

if age < CACHE_AGE_ACTIVE_MATCHES:
if age < max_cache_age:
if APP_STATE.is_up is False:
WEBHOOK.content = f"Data is now up to date"
WEBHOOK.execute()
APP_STATE.is_up = True
return int(CACHE_AGE_ACTIVE_MATCHES - age)
return int(max_cache_age - age)

if age > 2 * CACHE_AGE_ACTIVE_MATCHES:
if age > 2 * max_cache_age:
print("Data is stale")
if APP_STATE.is_up:
WEBHOOK.content = f"Data last updated {int(age)} seconds ago"
Expand Down

0 comments on commit 7e2db4d

Please sign in to comment.