Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(late bytes): add optional param of session_id so we can be explicit #90

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## v1.2.2 - 2024-08-17

### Fix

- Fix late bytes 422 error by adding optional param session_id to late bytes endpoint


## v1.2.1 - 2024-07-21

### Fixed
Expand Down
3 changes: 2 additions & 1 deletion masterbase/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ def late_bytes(request: Request, api_key: str, data: LateBytesBody) -> dict[str,
current_time = datetime.now().astimezone(timezone.utc)
steam_id = steam_id_from_api_key(engine, api_key)
converted_late_bytes = bytes.fromhex(data.late_bytes)
error = late_bytes_helper(engine, steam_id, converted_late_bytes, current_time)
session_id = data.session_id
error = late_bytes_helper(engine, steam_id, converted_late_bytes, current_time, session_id)
if error is None:
return {"late_bytes": True}
else:
Expand Down
69 changes: 43 additions & 26 deletions masterbase/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,40 +539,57 @@ def late_bytes_helper(
steam_id: str,
late_bytes: bytes,
current_time: datetime,
session_id: str | None = None,
) -> str | None:
"""Add late bytes to the database and blob storage.

No-ops and returns an error message if late bytes are found or there are no active sessions.
"""
with engine.connect() as conn:
Comment on lines 539 to 548
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function getting super ugly...

try:
session_id, old_late_bytes = conn.execute(
if session_id is not None:
conn.execute(
sa.text(
"""SELECT session_id, late_bytes FROM demo_sessions
WHERE active = True
AND steam_id = :steam_id;
""",
"""UPDATE demo_sessions
SET
late_bytes = :late_bytes,
updated_at = :updated_at
WHERE session_id = session_id;"""
),
{"steam_id": steam_id},
).one()
except NoResultFound:
return "no active session"
if session_id and old_late_bytes:
return "already submitted"
conn.execute(
sa.text(
"""UPDATE demo_sessions
SET
late_bytes = :late_bytes,
updated_at = :updated_at
WHERE session_id = session_id;"""
),
{
"session_id": session_id,
"late_bytes": late_bytes,
"updated_at": current_time.isoformat(),
},
)
{
"session_id": session_id,
"late_bytes": late_bytes,
"updated_at": current_time.isoformat(),
},
)
else:
try:
session_id, old_late_bytes = conn.execute(
sa.text(
"""SELECT session_id, late_bytes FROM demo_sessions
WHERE active = True
AND steam_id = :steam_id;
""",
),
{"steam_id": steam_id},
).one()
except NoResultFound:
return "no active session"
if session_id and old_late_bytes:
return "already submitted"
conn.execute(
sa.text(
"""UPDATE demo_sessions
SET
late_bytes = :late_bytes,
updated_at = :updated_at
WHERE session_id = session_id;"""
),
{
"session_id": session_id,
"late_bytes": late_bytes,
"updated_at": current_time.isoformat(),
},
)
conn.commit()
return None

Expand Down
2 changes: 2 additions & 0 deletions masterbase/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Module of pydantic models."""

from enum import Enum
from typing import Optional

from pydantic import BaseModel

Expand Down Expand Up @@ -31,3 +32,4 @@ class LateBytesBody(BaseModel):
"""Report model for late_bytes post request body."""

late_bytes: str
session_id: Optional[str] = None