Skip to content

Commit

Permalink
chore: add case insensitive get header function (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Oct 17, 2023
1 parent 8fc63f5 commit 45b1c71
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/anthropic_bedrock/_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from ._utils import is_required_type as is_required_type
from ._utils import is_annotated_type as is_annotated_type
from ._utils import maybe_coerce_float as maybe_coerce_float
from ._utils import get_required_header as get_required_header
from ._utils import maybe_coerce_boolean as maybe_coerce_boolean
from ._utils import maybe_coerce_integer as maybe_coerce_integer
from ._utils import strip_annotated_type as strip_annotated_type
Expand Down
22 changes: 21 additions & 1 deletion src/anthropic_bedrock/_utils/_utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from __future__ import annotations

import os
import re
import inspect
import functools
from typing import Any, Mapping, TypeVar, Callable, Iterable, Sequence, cast, overload
from pathlib import Path
from typing_extensions import Required, Annotated, TypeGuard, get_args, get_origin

from .._types import NotGiven, FileTypes, NotGivenOr
from .._types import Headers, NotGiven, FileTypes, NotGivenOr, HeadersLike
from .._compat import is_union as _is_union
from .._compat import parse_date as parse_date
from .._compat import parse_datetime as parse_datetime
Expand Down Expand Up @@ -351,3 +352,22 @@ def file_from_path(path: str) -> FileTypes:
contents = Path(path).read_bytes()
file_name = os.path.basename(path)
return (file_name, contents)


def get_required_header(headers: HeadersLike, header: str) -> str:
lower_header = header.lower()
if isinstance(headers, Mapping):
headers = cast(Headers, headers)
for k, v in headers.items():
if k.lower() == lower_header and isinstance(v, str):
return v

""" to deal with the case where the header looks like Finch-Event-Id """
intercaps_header = re.sub(r"([^\w])(\w)", lambda pat: pat.group(1) + pat.group(2).upper(), header.capitalize())

for normalized_header in [header, lower_header, header.upper(), intercaps_header]:
value = headers.get(normalized_header)
if value:
return value

raise ValueError(f"Could not find {header} header")

0 comments on commit 45b1c71

Please sign in to comment.