Skip to content
Merged
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
10 changes: 5 additions & 5 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "credal"

[tool.poetry]
name = "credal"
version = "0.0.28"
version = "0.0.29"
description = ""
readme = "README.md"
authors = []
Expand Down Expand Up @@ -38,7 +38,7 @@ python = "^3.8"
httpx = ">=0.21.2"
httpx-sse = "0.4.0"
pydantic = ">= 1.9.2"
pydantic-core = "^2.18.2"
pydantic-core = ">=2.18.2"
typing_extensions = ">= 4.0.0"

[tool.poetry.group.dev.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
httpx>=0.21.2
httpx-sse==0.4.0
pydantic>= 1.9.2
pydantic-core==2.18.2
pydantic-core>=2.18.2
typing_extensions>= 4.0.0
10 changes: 10 additions & 0 deletions src/credal/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class CredalApi:


api_key : typing.Optional[typing.Union[str, typing.Callable[[], str]]]
headers : typing.Optional[typing.Dict[str, str]]
Additional headers to send with every request.

timeout : typing.Optional[float]
The timeout to be used, in seconds, for requests. By default the timeout is 60 seconds, unless a custom httpx client is used, in which case this default is not enforced.

Expand All @@ -58,6 +61,7 @@ def __init__(
base_url: typing.Optional[str] = None,
environment: CredalApiEnvironment = CredalApiEnvironment.PRODUCTION,
api_key: typing.Optional[typing.Union[str, typing.Callable[[], str]]] = os.getenv("CREDAL_API_KEY"),
headers: typing.Optional[typing.Dict[str, str]] = None,
timeout: typing.Optional[float] = None,
follow_redirects: typing.Optional[bool] = True,
httpx_client: typing.Optional[httpx.Client] = None,
Expand All @@ -72,6 +76,7 @@ def __init__(
self._client_wrapper = SyncClientWrapper(
base_url=_get_base_url(base_url=base_url, environment=environment),
api_key=api_key,
headers=headers,
httpx_client=httpx_client
if httpx_client is not None
else httpx.Client(timeout=_defaulted_timeout, follow_redirects=follow_redirects)
Expand Down Expand Up @@ -106,6 +111,9 @@ class AsyncCredalApi:


api_key : typing.Optional[typing.Union[str, typing.Callable[[], str]]]
headers : typing.Optional[typing.Dict[str, str]]
Additional headers to send with every request.

timeout : typing.Optional[float]
The timeout to be used, in seconds, for requests. By default the timeout is 60 seconds, unless a custom httpx client is used, in which case this default is not enforced.

Expand All @@ -130,6 +138,7 @@ def __init__(
base_url: typing.Optional[str] = None,
environment: CredalApiEnvironment = CredalApiEnvironment.PRODUCTION,
api_key: typing.Optional[typing.Union[str, typing.Callable[[], str]]] = os.getenv("CREDAL_API_KEY"),
headers: typing.Optional[typing.Dict[str, str]] = None,
timeout: typing.Optional[float] = None,
follow_redirects: typing.Optional[bool] = True,
httpx_client: typing.Optional[httpx.AsyncClient] = None,
Expand All @@ -144,6 +153,7 @@ def __init__(
self._client_wrapper = AsyncClientWrapper(
base_url=_get_base_url(base_url=base_url, environment=environment),
api_key=api_key,
headers=headers,
httpx_client=httpx_client
if httpx_client is not None
else httpx.AsyncClient(timeout=_defaulted_timeout, follow_redirects=follow_redirects)
Expand Down
16 changes: 12 additions & 4 deletions src/credal/core/client_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@ def __init__(
self,
*,
api_key: typing.Union[str, typing.Callable[[], str]],
headers: typing.Optional[typing.Dict[str, str]] = None,
base_url: str,
timeout: typing.Optional[float] = None,
):
self._api_key = api_key
self._headers = headers
self._base_url = base_url
self._timeout = timeout

def get_headers(self) -> typing.Dict[str, str]:
headers: typing.Dict[str, str] = {
"User-Agent": "credal/0.0.28",
"User-Agent": "credal/0.0.29",
"X-Fern-Language": "Python",
"X-Fern-SDK-Name": "credal",
"X-Fern-SDK-Version": "0.0.28",
"X-Fern-SDK-Version": "0.0.29",
**(self.get_custom_headers() or {}),
}
headers["Authorization"] = f"Bearer {self._get_api_key()}"
return headers
Expand All @@ -34,6 +37,9 @@ def _get_api_key(self) -> str:
else:
return self._api_key()

def get_custom_headers(self) -> typing.Optional[typing.Dict[str, str]]:
return self._headers

def get_base_url(self) -> str:
return self._base_url

Expand All @@ -46,11 +52,12 @@ def __init__(
self,
*,
api_key: typing.Union[str, typing.Callable[[], str]],
headers: typing.Optional[typing.Dict[str, str]] = None,
base_url: str,
timeout: typing.Optional[float] = None,
httpx_client: httpx.Client,
):
super().__init__(api_key=api_key, base_url=base_url, timeout=timeout)
super().__init__(api_key=api_key, headers=headers, base_url=base_url, timeout=timeout)
self.httpx_client = HttpClient(
httpx_client=httpx_client,
base_headers=self.get_headers,
Expand All @@ -64,11 +71,12 @@ def __init__(
self,
*,
api_key: typing.Union[str, typing.Callable[[], str]],
headers: typing.Optional[typing.Dict[str, str]] = None,
base_url: str,
timeout: typing.Optional[float] = None,
httpx_client: httpx.AsyncClient,
):
super().__init__(api_key=api_key, base_url=base_url, timeout=timeout)
super().__init__(api_key=api_key, headers=headers, base_url=base_url, timeout=timeout)
self.httpx_client = AsyncHttpClient(
httpx_client=httpx_client,
base_headers=self.get_headers,
Expand Down
6 changes: 3 additions & 3 deletions src/credal/core/pydantic_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ class UniversalBaseModel(pydantic.BaseModel):
protected_namespaces=(),
)

@pydantic.model_serializer(mode="wrap", when_used="json") # type: ignore[attr-defined]
def serialize_model(self, handler: pydantic.SerializerFunctionWrapHandler) -> Any: # type: ignore[name-defined]
serialized = handler(self)
@pydantic.model_serializer(mode="plain", when_used="json") # type: ignore[attr-defined]
def serialize_model(self) -> Any: # type: ignore[name-defined]
serialized = self.model_dump()
data = {k: serialize_datetime(v) if isinstance(v, dt.datetime) else v for k, v in serialized.items()}
return data

Expand Down