From 0e2d8044430c29b081582b5952ad4c6569972478 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Mon, 18 Sep 2023 21:58:24 +0300 Subject: [PATCH] fix: get custom field name from model #31 --- yatracker/tracker/base.py | 16 ++++++++++++++++ yatracker/tracker/categories/issues.py | 12 ++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/yatracker/tracker/base.py b/yatracker/tracker/base.py index b79cd91..cf3401d 100644 --- a/yatracker/tracker/base.py +++ b/yatracker/tracker/base.py @@ -18,6 +18,7 @@ from .client import BaseClient T = TypeVar("T") +B = TypeVar("B", bound=Base) logger = logging.getLogger(__name__) @@ -70,12 +71,16 @@ def _decode(self, type_: type[T], data: bytes) -> T: def _prepare_payload( payload: dict[str, Any], exclude: Collection[str] | None = None, + type_: type[B] | None = None, ) -> dict[str, Any]: """Remove empty fields from payload.""" payload = payload.copy() exclude = exclude or [] kwargs = payload.pop("kwargs", None) + if kwargs: + if type_ is not None: + kwargs = _replace_custom_fields(kwargs, type_) payload.update(kwargs) return { @@ -135,3 +140,14 @@ def _convert_value(obj: Any) -> Any: # noqa: ANN401 return {k: _convert_value(v) for k, v in obj.items()} case _: return obj + + +def _replace_custom_fields(kwargs: dict[str, Any], type_: type[B]) -> dict[str, Any]: + """Replace kwarg key with original field name.""" + new_kwargs: dict[str, Any] = {} + for key, value in kwargs.items(): + if not hasattr(type_, key): + continue + field = getattr(type_, key) + new_kwargs[field.name] = value + return new_kwargs diff --git a/yatracker/tracker/categories/issues.py b/yatracker/tracker/categories/issues.py index 32a8652..807d7c7 100644 --- a/yatracker/tracker/categories/issues.py +++ b/yatracker/tracker/categories/issues.py @@ -95,7 +95,7 @@ async def edit_issue( method="PATCH", uri=f"/issues/{issue_id}", params={"version": str(version)} if version else None, - payload=self._prepare_payload(kwargs), + payload=self._prepare_payload(kwargs, type_=_type), ) return self._decode(_type, data) @@ -161,7 +161,7 @@ async def create_issue( Source: https://cloud.yandex.ru/docs/tracker/concepts/issues/create-issue """ - payload = self._prepare_payload(locals()) + payload = self._prepare_payload(locals(), type_=_type) data = await self._client.request( method="POST", uri="/issues/", @@ -262,7 +262,7 @@ async def move_issue( method="POST", uri=f"/issues/{issue_id}/_move", params=params, - payload=self._prepare_payload(kwargs), + payload=self._prepare_payload(kwargs, type_=_type), ) return self._decode(_type, data) @@ -330,7 +330,11 @@ async def find_issues( If there are more than 10,000 issues in the response, use paging. :return: """ - payload = self._prepare_payload(locals(), exclude=["expand", "order"]) + payload = self._prepare_payload( + locals(), + exclude=["expand", "order"], + type_=_type, + ) params = {} if order: