Skip to content

Commit

Permalink
python: Switch high-level API client to use pydantic
Browse files Browse the repository at this point in the history
  • Loading branch information
svix-mman committed Jan 24, 2025
1 parent 9fea676 commit e03896a
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 240 deletions.
6 changes: 6 additions & 0 deletions python/svix/api/.codegen.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"file-generated-at": "2025-01-24T21:11:04.775124283+00:00",
"git-rev": "50ccb34db423dd808c94acef683f7225228dfbc5",
"openapi-codegen-version": "0.1.0",
"openapi.json-sha256": "9fa16f8eb28f617b8495cafc9bd529626145df7af01e5c09acdc2b98e39a4725"
}
51 changes: 25 additions & 26 deletions python/svix/api/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import typing as t
from dataclasses import dataclass

from ..internal.openapi_client import models
from ..internal.openapi_client.models.application_in import ApplicationIn
from ..internal.openapi_client.models.application_out import ApplicationOut
from ..internal.openapi_client.models.application_patch import ApplicationPatch
from ..internal.openapi_client.models.list_response_application_out import (
from .. import models
from ..models import (
ApplicationIn,
ApplicationOut,
ApplicationPatch,
ListResponseApplicationOut,
)
from .common import ApiBase, BaseOptions, serialize_params
Expand Down Expand Up @@ -55,7 +55,7 @@ async def list(
query_params=options._query_params(),
header_params=options._header_params(),
)
return ListResponseApplicationOut.from_dict(response.json())
return ListResponseApplicationOut.model_validate(response.json())

async def create(
self,
Expand All @@ -69,9 +69,9 @@ async def create(
path_params={},
query_params=options._query_params(),
header_params=options._header_params(),
json_body=application_in.to_dict(),
json_body=application_in.model_dump_json(exclude_unset=True),
)
return ApplicationOut.from_dict(response.json())
return ApplicationOut.model_validate(response.json())

async def get_or_create(
self,
Expand All @@ -84,9 +84,9 @@ async def get_or_create(
path_params={},
query_params={"get_if_exists": "true"},
header_params=options._header_params(),
json_body=application_in.to_dict(),
json_body=application_in.model_dump_json(exclude_unset=True),
)
return ApplicationOut.from_dict(response.json())
return ApplicationOut.model_validate(response.json())

async def get(self, app_id: str) -> ApplicationOut:
"""Get an application."""
Expand All @@ -97,7 +97,7 @@ async def get(self, app_id: str) -> ApplicationOut:
"app_id": app_id,
},
)
return ApplicationOut.from_dict(response.json())
return ApplicationOut.model_validate(response.json())

async def update(
self, app_id: str, application_in: ApplicationIn
Expand All @@ -109,9 +109,9 @@ async def update(
path_params={
"app_id": app_id,
},
json_body=application_in.to_dict(),
json_body=application_in.model_dump_json(exclude_unset=True),
)
return ApplicationOut.from_dict(response.json())
return ApplicationOut.model_validate(response.json())

async def delete(self, app_id: str) -> None:
"""Delete an application."""
Expand All @@ -133,9 +133,9 @@ async def patch(
path_params={
"app_id": app_id,
},
json_body=application_patch.to_dict(),
json_body=application_patch.model_dump_json(exclude_unset=True),
)
return ApplicationOut.from_dict(response.json())
return ApplicationOut.model_validate(response.json())


class Application(ApiBase):
Expand All @@ -150,7 +150,7 @@ def list(
query_params=options._query_params(),
header_params=options._header_params(),
)
return ListResponseApplicationOut.from_dict(response.json())
return ListResponseApplicationOut.model_validate(response.json())

def create(
self,
Expand All @@ -164,25 +164,24 @@ def create(
path_params={},
query_params=options._query_params(),
header_params=options._header_params(),
json_body=application_in.to_dict(),
json_body=application_in.model_dump_json(exclude_unset=True),
)
return ApplicationOut.from_dict(response.json())
return ApplicationOut.model_validate(response.json())

def get_or_create(
self,
application_in: ApplicationIn,
options: ApplicationCreateOptions = ApplicationCreateOptions(),
) -> ApplicationOut:
# ruff: noqa: F841
response = self._request_sync(
method="post",
path="/api/v1/app",
path_params={},
query_params={"get_if_exists": "true"},
header_params=options._header_params(),
json_body=application_in.to_dict(),
json_body=application_in.model_dump_json(exclude_unset=True),
)
return ApplicationOut.from_dict(response.json())
return ApplicationOut.model_validate(response.json())

def get(self, app_id: str) -> ApplicationOut:
"""Get an application."""
Expand All @@ -193,7 +192,7 @@ def get(self, app_id: str) -> ApplicationOut:
"app_id": app_id,
},
)
return ApplicationOut.from_dict(response.json())
return ApplicationOut.model_validate(response.json())

def update(self, app_id: str, application_in: ApplicationIn) -> ApplicationOut:
"""Update an application."""
Expand All @@ -203,9 +202,9 @@ def update(self, app_id: str, application_in: ApplicationIn) -> ApplicationOut:
path_params={
"app_id": app_id,
},
json_body=application_in.to_dict(),
json_body=application_in.model_dump_json(exclude_unset=True),
)
return ApplicationOut.from_dict(response.json())
return ApplicationOut.model_validate(response.json())

def delete(self, app_id: str) -> None:
"""Delete an application."""
Expand All @@ -225,6 +224,6 @@ def patch(self, app_id: str, application_patch: ApplicationPatch) -> Application
path_params={
"app_id": app_id,
},
json_body=application_patch.to_dict(),
json_body=application_patch.model_dump_json(exclude_unset=True),
)
return ApplicationOut.from_dict(response.json())
return ApplicationOut.model_validate(response.json())
24 changes: 12 additions & 12 deletions python/svix/api/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

from deprecated import deprecated

from ..internal.openapi_client.models.app_portal_access_in import AppPortalAccessIn
from ..internal.openapi_client.models.app_portal_access_out import AppPortalAccessOut
from ..internal.openapi_client.models.application_token_expire_in import (
from ..models import (
ApplicationTokenExpireIn,
AppPortalAccessIn,
AppPortalAccessOut,
DashboardAccessOut,
)
from ..internal.openapi_client.models.dashboard_access_out import DashboardAccessOut
from .common import ApiBase, BaseOptions, serialize_params


Expand Down Expand Up @@ -77,9 +77,9 @@ async def app_portal_access(
},
query_params=options._query_params(),
header_params=options._header_params(),
json_body=app_portal_access_in.to_dict(),
json_body=app_portal_access_in.model_dump_json(exclude_unset=True),
)
return AppPortalAccessOut.from_dict(response.json())
return AppPortalAccessOut.model_validate(response.json())

async def expire_all(
self,
Expand All @@ -96,7 +96,7 @@ async def expire_all(
},
query_params=options._query_params(),
header_params=options._header_params(),
json_body=application_token_expire_in.to_dict(),
json_body=application_token_expire_in.model_dump_json(exclude_unset=True),
)

@deprecated
Expand All @@ -117,7 +117,7 @@ async def dashboard_access(
query_params=options._query_params(),
header_params=options._header_params(),
)
return DashboardAccessOut.from_dict(response.json())
return DashboardAccessOut.model_validate(response.json())

async def logout(
self, options: AuthenticationLogoutOptions = AuthenticationLogoutOptions()
Expand Down Expand Up @@ -150,9 +150,9 @@ def app_portal_access(
},
query_params=options._query_params(),
header_params=options._header_params(),
json_body=app_portal_access_in.to_dict(),
json_body=app_portal_access_in.model_dump_json(exclude_unset=True),
)
return AppPortalAccessOut.from_dict(response.json())
return AppPortalAccessOut.model_validate(response.json())

def expire_all(
self,
Expand All @@ -169,7 +169,7 @@ def expire_all(
},
query_params=options._query_params(),
header_params=options._header_params(),
json_body=application_token_expire_in.to_dict(),
json_body=application_token_expire_in.model_dump_json(exclude_unset=True),
)

@deprecated
Expand All @@ -190,7 +190,7 @@ def dashboard_access(
query_params=options._query_params(),
header_params=options._header_params(),
)
return DashboardAccessOut.from_dict(response.json())
return DashboardAccessOut.model_validate(response.json())

def logout(
self, options: AuthenticationLogoutOptions = AuthenticationLogoutOptions()
Expand Down
15 changes: 6 additions & 9 deletions python/svix/api/background_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
import typing as t
from dataclasses import dataclass

from ..internal.openapi_client import models
from ..internal.openapi_client.models.background_task_out import BackgroundTaskOut
from ..internal.openapi_client.models.list_response_background_task_out import (
ListResponseBackgroundTaskOut,
)
from .. import models
from ..models import BackgroundTaskOut, ListResponseBackgroundTaskOut
from .common import ApiBase, BaseOptions, serialize_params


Expand Down Expand Up @@ -47,7 +44,7 @@ async def list(
query_params=options._query_params(),
header_params=options._header_params(),
)
return ListResponseBackgroundTaskOut.from_dict(response.json())
return ListResponseBackgroundTaskOut.model_validate(response.json())

async def get(self, task_id: str) -> BackgroundTaskOut:
"""Get a background task by ID."""
Expand All @@ -58,7 +55,7 @@ async def get(self, task_id: str) -> BackgroundTaskOut:
"task_id": task_id,
},
)
return BackgroundTaskOut.from_dict(response.json())
return BackgroundTaskOut.model_validate(response.json())


class BackgroundTask(ApiBase):
Expand All @@ -73,7 +70,7 @@ def list(
query_params=options._query_params(),
header_params=options._header_params(),
)
return ListResponseBackgroundTaskOut.from_dict(response.json())
return ListResponseBackgroundTaskOut.model_validate(response.json())

def get(self, task_id: str) -> BackgroundTaskOut:
"""Get a background task by ID."""
Expand All @@ -84,4 +81,4 @@ def get(self, task_id: str) -> BackgroundTaskOut:
"task_id": task_id,
},
)
return BackgroundTaskOut.from_dict(response.json())
return BackgroundTaskOut.model_validate(response.json())
16 changes: 10 additions & 6 deletions python/svix/api/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import httpx

from ..internal.openapi_client.client import AuthenticatedClient
from ..internal.openapi_client.models import HttpError
from .client import AuthenticatedClient
from .http_error import HttpError


def ensure_tz(x: t.Optional[datetime]) -> t.Optional[datetime]:
Expand Down Expand Up @@ -80,7 +80,7 @@ def _get_httpx_kwargs(
path_params: t.Optional[t.Dict[str, str]],
query_params: t.Optional[t.Dict[str, str]],
header_params: t.Optional[t.Dict[str, str]],
json_body: t.Optional[t.Dict[str, t.Any]],
json_body: t.Optional[str],
) -> t.Dict[str, t.Any]:
if path_params is not None:
path = path.format(**path_params)
Expand All @@ -107,7 +107,11 @@ def _get_httpx_kwargs(
httpx_kwargs["params"] = query_params

if json_body is not None:
httpx_kwargs["json"] = json_body
encoded_body = json_body.encode("utf-8")
httpx_kwargs["content"] = encoded_body
headers["content-type"]= "application/json"
headers["content-length"]= str(len(encoded_body))

return httpx_kwargs

async def _request_asyncio(
Expand All @@ -117,7 +121,7 @@ async def _request_asyncio(
path_params: t.Optional[t.Dict[str, str]] = None,
query_params: t.Optional[t.Dict[str, str]] = None,
header_params: t.Optional[t.Dict[str, str]] = None,
json_body: t.Optional[t.Dict[str, t.Any]] = None,
json_body: t.Optional[str] = None,
) -> httpx.Response:
httpx_kwargs = self._get_httpx_kwargs(
method,
Expand Down Expand Up @@ -150,7 +154,7 @@ def _request_sync(
path_params: t.Optional[t.Dict[str, str]] = None,
query_params: t.Optional[t.Dict[str, str]] = None,
header_params: t.Optional[t.Dict[str, str]] = None,
json_body: t.Optional[t.Dict[str, t.Any]] = None,
json_body: t.Optional[str] = None,
) -> httpx.Response:
httpx_kwargs = self._get_httpx_kwargs(
method,
Expand Down
Loading

0 comments on commit e03896a

Please sign in to comment.