Skip to content

Commit

Permalink
extract osu file usecases and add s3 writes on new .osu file discovered
Browse files Browse the repository at this point in the history
  • Loading branch information
cmyui committed Jun 30, 2024
1 parent d62e15e commit 101e6b8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
14 changes: 3 additions & 11 deletions app/api/internal/v1/osu_api_v1.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
from fastapi import APIRouter
from fastapi import Response

from app.adapters import aws_s3
from app.adapters import osu_api_v1
from app.usecases import osu_files

router = APIRouter(tags=["osu Files"])


@router.get("/api/osu-api/v1/osu-files/{beatmap_id}")
async def download_beatmap_osu_file(beatmap_id: int) -> Response:
beatmap_osu_file_data = await aws_s3.get_object_data(f"/beatmaps/{beatmap_id}.osu")
beatmap_osu_file_data = await osu_files.fetch_beatmap_osu_file_data(beatmap_id)
if beatmap_osu_file_data is None:
beatmap_osu_file_data = await osu_api_v1.fetch_beatmap_osu_file_data(beatmap_id)
# TODO: consider at which points in beatmaps-service we should update
# the .osu file that is currently saved in wasabi s3 storage.
if beatmap_osu_file_data is None:
return Response(status_code=404)
else:
# TODO: consider cache expiry
...
return Response(status_code=404)

return Response(
beatmap_osu_file_data,
Expand Down
31 changes: 31 additions & 0 deletions app/usecases/osu_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import logging

from app.adapters import aws_s3
from app.adapters import osu_api_v1


async def fetch_beatmap_osu_file_data(beatmap_id: int) -> bytes | None:
beatmap_osu_file_data = await aws_s3.get_object_data(f"/beatmaps/{beatmap_id}.osu")
if beatmap_osu_file_data is None:
beatmap_osu_file_data = await osu_api_v1.fetch_beatmap_osu_file_data(beatmap_id)
if beatmap_osu_file_data is None:
return None

await aws_s3.save_object_data(
f"/beatmaps/{beatmap_id}.osu",
beatmap_osu_file_data,
)
logging.info(
"Saved beatmap osu file to s3",
extra={"beatmap_id": beatmap_id},
)
# NOTE: this is a place where .osu files could become desynced
# with akatsuki beatmaps in the mysql database, however we've
# decided to not worry about this for now, as most use cases
# which rely on .osu files will also fetch the beatmap metadata,
# forcing an update to occur.
else:
# TODO: consider cache expiry
...

return beatmap_osu_file_data

0 comments on commit 101e6b8

Please sign in to comment.