From 47b8c63eecbb3386c3f48e2a157da5d31dbaa1b0 Mon Sep 17 00:00:00 2001 From: Mendy Man Date: Tue, 14 Jan 2025 13:59:16 -0500 Subject: [PATCH] python: Cleanup ListOptions to_dict to use BaeOptions --- python/svix/api/common.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/python/svix/api/common.py b/python/svix/api/common.py index 8a76bfa94..27bea049b 100644 --- a/python/svix/api/common.py +++ b/python/svix/api/common.py @@ -15,22 +15,29 @@ def ensure_tz(x: t.Optional[datetime]) -> t.Optional[datetime]: return x +def sanitize_field(v: t.Any) -> t.Any: + if isinstance(v, datetime): + return ensure_tz(v) + + return v + + @dataclass -class ListOptions: +class BaseOptions: + def to_dict(self) -> t.Dict[str, t.Any]: + return {k: sanitize_field(v) for k, v in asdict(self).items() if v is not None} + + +@dataclass +class ListOptions(BaseOptions): iterator: t.Optional[str] = None limit: t.Optional[int] = None - def to_dict(self) -> t.Dict[str, t.Any]: - return {k: v for k, v in asdict(self).items() if v is not None} - @dataclass -class PostOptions: +class PostOptions(BaseOptions): idempotency_key: t.Optional[str] = None - def to_dict(self) -> t.Dict[str, t.Any]: - return {k: v for k, v in asdict(self).items() if v is not None} - class ApiBase: _client: AuthenticatedClient