Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
feat(response): makes serializer configurable. (#84)
Browse files Browse the repository at this point in the history
The custom response class only exists to deliver this. So that is now
dynamically constructed with the serializer passed to `PluginConfig` if
`do_response_class` is `True` and `response_class` doesn't already
exist on `AppConfig`.

Closes #51
  • Loading branch information
peterschutt committed Nov 5, 2022
1 parent 9e89434 commit 3439d26
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 34 deletions.
2 changes: 0 additions & 2 deletions src/starlite_saqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def example_handler() -> dict:
orm,
redis,
repository,
response,
sentry,
service,
settings,
Expand All @@ -58,7 +57,6 @@ def example_handler() -> dict:
"orm",
"redis",
"repository",
"response",
"sentry",
"service",
"settings",
Expand Down
18 changes: 16 additions & 2 deletions src/starlite_saqlalchemy/init_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def example_handler() -> dict:
from pydantic import BaseModel
from starlite.app import DEFAULT_CACHE_CONFIG, DEFAULT_OPENAPI_CONFIG
from starlite.plugins.sql_alchemy import SQLAlchemyPlugin
from starlite.response import Response

from starlite_saqlalchemy import (
cache,
Expand All @@ -45,17 +46,19 @@ def example_handler() -> dict:
logging,
openapi,
redis,
response,
sentry,
sqlalchemy_plugin,
static_files,
)
from starlite_saqlalchemy.health import health_check
from starlite_saqlalchemy.repository.exceptions import RepositoryException
from starlite_saqlalchemy.serializer import default_serializer
from starlite_saqlalchemy.service import ServiceException, make_service_callback
from starlite_saqlalchemy.worker import create_worker_instance

if TYPE_CHECKING:
from collections.abc import Callable
from typing import Any

from starlite.config.app import AppConfig

Expand Down Expand Up @@ -143,6 +146,15 @@ class PluginConfig(BaseModel):
[`AppConfig.on_shutdown`][starlite.config.app.AppConfig.on_shutdown] that manage the lifecycle
of the `SAQ` worker.
"""
serializer: Callable[[Any], Any] = default_serializer
"""
The serializer callable that is used by the custom [`Response`][starlite.response.Response]
class that is created.
If [`AppConfig.response_class`][starlite.config.app.AppConfig.response_class] is not `None`,
this is ignored.
If [`PluginConfig.do_response_class`][PluginConfig.do_response_class] is `False`, this is
ignored.
"""


class ConfigureApp:
Expand Down Expand Up @@ -290,7 +302,9 @@ def configure_response_class(self, app_config: AppConfig) -> None:
app_config: The Starlite application config object.
"""
if self.config.do_response_class and app_config.response_class is None:
app_config.response_class = response.Response
app_config.response_class = type(
"Response", (Response,), {"serializer": staticmethod(self.config.serializer)}
)

def configure_sentry(self, app_config: AppConfig) -> None:
"""Add handler to configure Sentry integration.
Expand Down
30 changes: 0 additions & 30 deletions src/starlite_saqlalchemy/response.py

This file was deleted.

19 changes: 19 additions & 0 deletions src/starlite_saqlalchemy/serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""Default serializer used by plugin if one not provided."""
from typing import Any

from asyncpg.pgproto import pgproto
from starlite import Response


def default_serializer(value: Any) -> Any:
"""Serialize `value`.
Args:
value: To be serialized.
Returns:
Serialized representation of `value`.
"""
if isinstance(value, pgproto.UUID):
return str(value)
return Response[Any].serializer(value)

0 comments on commit 3439d26

Please sign in to comment.