-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support deserializing of API payloads to user-defined model cla…
…sses Users can provide a custom deserializer that allows customising which models should be returned by the client.
- Loading branch information
Showing
3 changed files
with
103 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
import dacite | ||
|
||
from rossum_api.api_client import Resource | ||
from rossum_api.models.annotation import Annotation | ||
from rossum_api.models.connector import Connector | ||
from rossum_api.models.hook import Hook | ||
from rossum_api.models.inbox import Inbox | ||
from rossum_api.models.organization import Organization | ||
from rossum_api.models.queue import Queue | ||
from rossum_api.models.schema import Schema | ||
from rossum_api.models.user import User | ||
from rossum_api.models.user_role import UserRole | ||
from rossum_api.models.workspace import Workspace | ||
|
||
if TYPE_CHECKING: | ||
from typing import Any, Callable, Dict | ||
|
||
JsonDict = Dict[str, Any] | ||
Deserializer = Callable[[Resource, JsonDict], Any] | ||
|
||
|
||
RESOURCE_TO_MODEL = { | ||
Resource.Annotation: Annotation, | ||
Resource.Connector: Connector, | ||
Resource.Group: UserRole, | ||
Resource.Hook: Hook, | ||
Resource.Inbox: Inbox, | ||
Resource.Organization: Organization, | ||
Resource.Queue: Queue, | ||
Resource.Schema: Schema, | ||
Resource.User: User, | ||
Resource.Workspace: Workspace, | ||
} | ||
|
||
|
||
def deserialize_default(resource: Resource, payload: JsonDict) -> Any: | ||
"""Deserialize payload into dataclasses using dacite.""" | ||
model_class = RESOURCE_TO_MODEL[resource] | ||
return dacite.from_dict(model_class, payload) |