Skip to content

Commit

Permalink
chore: pass tracker instance link
Browse files Browse the repository at this point in the history
  • Loading branch information
Olegt0rr committed Aug 27, 2023
1 parent 880869f commit 2987b41
Show file tree
Hide file tree
Showing 21 changed files with 72 additions and 117 deletions.
38 changes: 30 additions & 8 deletions yatracker/tracker/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import msgspec.json

from yatracker.types.base import Base
from yatracker.utils.camel_case import camel_case
from yatracker.utils.mixins import ContextInstanceMixin

from .client import AIOHTTPClient

Expand All @@ -22,7 +22,7 @@
logger = logging.getLogger(__name__)


class BaseTracker(ContextInstanceMixin):
class BaseTracker:
"""Represents technical methods for using YaTracker."""

# ruff: noqa: PLR0913
Expand Down Expand Up @@ -56,11 +56,15 @@ def __init__(
api_version=api_version,
)

# ruff: noqa: B019
@lru_cache
def _get_decoder(self, struct: type[T]) -> msgspec.json.Decoder:
"""Get cached msgspec encoder."""
return msgspec.json.Decoder(struct)
def _decode(self, type_: type[T], data: bytes) -> T:
"""Decode bytes object to struct.
Also add producer client object to `_tracker` field.
"""
decoder = _get_decoder(type_) # type: ignore[arg-type]
obj = decoder.decode(data)
_add_tracker(self, obj)
return obj

@staticmethod
def clear_payload(
Expand All @@ -87,7 +91,6 @@ async def close(self) -> None:
# ruff: noqa: PYI034
async def __aenter__(self) -> BaseTracker:
"""Return async Tracker with async context."""
self.set_current(self)
return self

async def __aexit__(
Expand All @@ -98,3 +101,22 @@ async def __aexit__(
) -> None:
"""Close async context."""
await self.close()


@lru_cache
def _get_decoder(type_: type[T]) -> msgspec.json.Decoder:
"""Get cached msgspec encoder."""
return msgspec.json.Decoder(type_)


def _add_tracker(tracker: BaseTracker, obj: Any) -> None: # noqa: ANN401
"""Add tracker link to the object."""
match obj:
case Base():
obj._tracker = tracker # noqa: SLF001
case list():
for o in obj:
_add_tracker(tracker, o)
case dict():
for v in obj.values():
_add_tracker(tracker, v)
9 changes: 3 additions & 6 deletions yatracker/tracker/categories/attached_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ async def get_attachments(self, issue_id: str) -> list[Attachment]:
method="GET",
uri=f"/issues/{issue_id}/attachments",
)
decoder = self._get_decoder(list[Attachment])
return decoder.decode(data)
return self._decode(list[Attachment], data)

async def download_attachment(
self,
Expand Down Expand Up @@ -72,8 +71,7 @@ async def attach_file(
params={"filename": filename} if filename else None,
form=form,
)
decoder = self._get_decoder(Attachment)
return decoder.decode(data)
return self._decode(Attachment, data)

async def upload_temp_file(
self,
Expand All @@ -96,8 +94,7 @@ async def upload_temp_file(
params={"filename": filename} if filename else None,
form=form,
)
decoder = self._get_decoder(Attachment)
return decoder.decode(data)
return self._decode(Attachment, data)

async def delete_attachment(self, issue_id: str, attachment_id: str | int) -> bool:
"""Delete attached file.
Expand Down
7 changes: 2 additions & 5 deletions yatracker/tracker/categories/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ async def get_comments(self, issue_id: str) -> list[Comment]:
method="GET",
uri=f"/issues/{issue_id}/comments",
)

decoder = self._get_decoder(list[Comment])
return decoder.decode(data)
return self._decode(list[Comment], data)

async def post_comment(self, issue_id: str, text: str, **kwargs) -> Comment:
"""Comment the issue."""
Expand All @@ -30,5 +28,4 @@ async def post_comment(self, issue_id: str, text: str, **kwargs) -> Comment:
uri=f"/issues/{issue_id}/comments/",
payload=payload,
)
decoder = self._get_decoder(Comment)
return decoder.decode(data)
return self._decode(Comment, data)
28 changes: 9 additions & 19 deletions yatracker/tracker/categories/issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ async def get_issue(self, issue_id: str, expand: str | None = None) -> FullIssue
uri=f"/issues/{issue_id}",
params={"expand": expand} if expand else None,
)
decoder = self._get_decoder(FullIssue)
return decoder.decode(data)
return self._decode(FullIssue, data)

async def edit_issue(
self,
Expand All @@ -54,8 +53,7 @@ async def edit_issue(
params={"version": str(version)} if version else None,
payload=self.clear_payload(kwargs),
)
decoder = self._get_decoder(FullIssue)
return decoder.decode(data)
return self._decode(FullIssue, data)

# ruff: noqa: ARG002 PLR0913
async def create_issue(
Expand All @@ -79,8 +77,7 @@ async def create_issue(
uri="/issues/",
payload=payload,
)
decoder = self._get_decoder(FullIssue)
return decoder.decode(data)
return self._decode(FullIssue, data)

async def move_issue(
self,
Expand Down Expand Up @@ -145,8 +142,7 @@ async def move_issue(
params=params,
payload=self.clear_payload(kwargs),
)
decoder = self._get_decoder(FullIssue)
return decoder.decode(data)
return self._decode(FullIssue, data)

async def count_issues(
self,
Expand All @@ -169,8 +165,7 @@ async def count_issues(
uri="/issues/_count",
payload=payload,
)
decoder = self._get_decoder(int)
return decoder.decode(data)
return self._decode(int, data)

async def find_issues(
self,
Expand Down Expand Up @@ -201,8 +196,7 @@ async def find_issues(
params=params,
payload=payload,
)
decoder = self._get_decoder(list[FullIssue])
return decoder.decode(data)
return self._decode(list[FullIssue], data)

async def get_issue_links(self, issue_id: str) -> list[FullIssue]:
"""Get issue links.
Expand All @@ -214,9 +208,7 @@ async def get_issue_links(self, issue_id: str) -> list[FullIssue]:
method="GET",
uri=f"/issues/{issue_id}/links",
)

decoder = self._get_decoder(list[FullIssue])
return decoder.decode(data)
return self._decode(list[FullIssue], data)

async def get_transitions(self, issue_id: str) -> Transitions:
"""Get transitions.
Expand All @@ -228,8 +220,7 @@ async def get_transitions(self, issue_id: str) -> Transitions:
method="GET",
uri=f"/issues/{issue_id}/transitions",
)
decoder = self._get_decoder(list[Transition])
transitions = decoder.decode(data)
transitions = self._decode(list[Transition], data)
return Transitions(**{t.id: t for t in transitions})

async def execute_transition(
Expand All @@ -244,5 +235,4 @@ async def execute_transition(
uri=f"{transition.url}/_execute",
payload=payload,
)
decoder = self._get_decoder(list[Transition])
return decoder.decode(data)
return self._decode(list[Transition], data)
4 changes: 1 addition & 3 deletions yatracker/tracker/categories/priorities.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@ async def get_priorities(self, localized: bool = True) -> list[Priority]:
uri="/priorities",
params=params,
)

decoder = self._get_decoder(list[Priority])
return decoder.decode(data)
return self._decode(list[Priority], data)
12 changes: 4 additions & 8 deletions yatracker/tracker/categories/worklogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ async def post_worklog(
uri=f"/issues/{issue_id}/worklog/",
payload=payload,
)
decoder = self._get_decoder(Worklog)
return decoder.decode(data)
return self._decode(Worklog, data)

async def edit_worklog(
self,
Expand All @@ -58,8 +57,7 @@ async def edit_worklog(
uri=f"/issues/{issue_id}/worklog/{worklog_id}",
payload=payload,
)
decoder = self._get_decoder(Worklog)
return decoder.decode(data)
return self._decode(Worklog, data)

async def delete_worklog(
self,
Expand Down Expand Up @@ -87,8 +85,7 @@ async def get_issue_worklog(self, issue_id: str) -> list[Worklog]:
method="GET",
uri=f"/issues/{issue_id}/worklog",
)
decoder = self._get_decoder(list[Worklog])
return decoder.decode(data)
return self._decode(list[Worklog], data)

async def get_worklog(
self,
Expand All @@ -111,8 +108,7 @@ async def get_worklog(
uri="/worklog/_search",
payload=payload,
)
decoder = self._get_decoder(list[Worklog])
return decoder.decode(data)
return self._decode(list[Worklog], data)


def _process_created_at(
Expand Down
4 changes: 2 additions & 2 deletions yatracker/types/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .user import User


class Attachment(Base, kw_only=True, frozen=True):
class Attachment(Base, kw_only=True):
"""Represents attachment object."""

url: str = field(name="self")
Expand All @@ -25,7 +25,7 @@ class Attachment(Base, kw_only=True, frozen=True):
comment_id: str | None = None


class Metadata(Base, kw_only=True, frozen=True):
class Metadata(Base, kw_only=True):
"""Represents attachment metadata."""

size: str | None = None
15 changes: 6 additions & 9 deletions yatracker/types/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

__all__ = ["Base", "field"]

from typing import Any

from msgspec import Struct, field

from .mixins import Printable, TrackerAccess
from .mixins import Printable


class Base(
Printable,
TrackerAccess,
Struct,
frozen=True,
omit_defaults=True,
rename="camel",
):
class Base(Printable, Struct, omit_defaults=True, rename="camel"):
"""Base structure class."""

_tracker: Any = None # this field will be filled after decoding
2 changes: 1 addition & 1 deletion yatracker/types/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .user import User


class Comment(Base, kw_only=True, frozen=True):
class Comment(Base, kw_only=True):
url: str = field(name="self")
id: str
text: str
Expand Down
8 changes: 4 additions & 4 deletions yatracker/types/full_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from .user import User


class FullIssue(Base, kw_only=True, frozen=True):
class FullIssue(Base, kw_only=True):
url: str = field(name="self")
id: str
key: str
Expand Down Expand Up @@ -60,15 +60,15 @@ async def get_transitions(self) -> Transitions:
>>> if close:
>>> await close.execute()
"""
return await self.tracker.get_transitions(self.id)
return await self._tracker.get_transitions(self.id)

async def get_comments(self) -> list[Comment]:
"""Get comments for self.
:return:
"""
return await self.tracker.get_comments(self.id)
return await self._tracker.get_comments(self.id)

async def post_comment(self, text: str, **kwargs) -> Comment:
"""Post comment for self."""
return await self.tracker.post_comment(self.id, text=text, **kwargs)
return await self._tracker.post_comment(self.id, text=text, **kwargs)
2 changes: 1 addition & 1 deletion yatracker/types/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .base import Base, field


class Issue(Base, kw_only=True, frozen=True):
class Issue(Base, kw_only=True):
"""Represents short view of issue."""

url: str = field(name="self")
Expand Down
2 changes: 1 addition & 1 deletion yatracker/types/issue_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .base import Base, field


class IssueType(Base, kw_only=True, frozen=True):
class IssueType(Base, kw_only=True):
url: str = field(name="self")
id: str
key: str
Expand Down
14 changes: 0 additions & 14 deletions yatracker/types/mixins.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from yatracker.tracker import YaTracker


class TrackerAccess:
@property
def tracker(self) -> YaTracker:
"""Get Tracker client."""
from yatracker.tracker import YaTracker

return YaTracker.get_current()


class Printable:
display: str | None
Expand Down
2 changes: 1 addition & 1 deletion yatracker/types/priority.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .base import Base, field


class Priority(Base, kw_only=True, frozen=True):
class Priority(Base, kw_only=True):
"""Represents Priority.
Attributes
Expand Down
2 changes: 1 addition & 1 deletion yatracker/types/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .base import Base, field


class Queue(Base, kw_only=True, frozen=True):
class Queue(Base, kw_only=True):
url: str = field(name="self")
id: str
key: str
Expand Down
2 changes: 1 addition & 1 deletion yatracker/types/sprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .base import Base, field


class Sprint(Base, kw_only=True, frozen=True):
class Sprint(Base, kw_only=True):
url: str = field(name="self")
id: str
display: str
Loading

0 comments on commit 2987b41

Please sign in to comment.