Skip to content

Commit

Permalink
fix: get custom field name from model
Browse files Browse the repository at this point in the history
  • Loading branch information
Olegt0rr committed Sep 18, 2023
1 parent c067717 commit 0e2d804
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
16 changes: 16 additions & 0 deletions yatracker/tracker/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from .client import BaseClient

T = TypeVar("T")
B = TypeVar("B", bound=Base)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
12 changes: 8 additions & 4 deletions yatracker/tracker/categories/issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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/",
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 0e2d804

Please sign in to comment.