Skip to content

Commit

Permalink
Merge pull request #26 from bento-platform/fix/refget-accept
Browse files Browse the repository at this point in the history
fix(refget): properly handle multi-Accept headers
  • Loading branch information
davidlougheed authored Nov 20, 2024
2 parents 7d067f5 + 5896dd1 commit adcc87a
Show file tree
Hide file tree
Showing 4 changed files with 347 additions and 335 deletions.
14 changes: 12 additions & 2 deletions bento_reference_service/routers/refget.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import io
import math
import orjson
import re
import typing

from bento_lib.service_info.helpers import build_service_type, build_service_info_from_pydantic_config
Expand Down Expand Up @@ -35,6 +36,8 @@
REFGET_HEADER_JSON = f"application/vnd.ga4gh.refget.v{REFGET_VERSION}+json"
REFGET_HEADER_JSON_WITH_CHARSET = f"{REFGET_HEADER_JSON}; charset={REFGET_CHARSET}"

ACCEPT_SPLIT = re.compile(r",\s*")


class RefGetJSONResponse(Response):
media_type = REFGET_HEADER_JSON
Expand Down Expand Up @@ -66,8 +69,15 @@ def check_accept_header(accept_header: str | None, mode: Literal["text", "json"]
)
)

if accept_header and accept_header not in valid_header_values:
raise HTTPException(status_code=status.HTTP_406_NOT_ACCEPTABLE, detail="Not Acceptable")
if not accept_header: # None or blank
return None # valid - everything accepted

for accept in ACCEPT_SPLIT.split(accept_header):
if accept.split(";")[0] in valid_header_values:
return None # valid - don't raise

# If none of the accept header values matched, we need to raise Not Acceptable
raise HTTPException(status_code=status.HTTP_406_NOT_ACCEPTABLE, detail="Not Acceptable")


@refget_router.get("/service-info", dependencies=[authz_middleware.dep_public_endpoint()])
Expand Down
Loading

0 comments on commit adcc87a

Please sign in to comment.