Skip to content

Commit

Permalink
refactor Attachment class
Browse files Browse the repository at this point in the history
* derive from FrozenInfo

Signed-off-by: flashdagger <flashdagger@googlemail.com>
  • Loading branch information
flashdagger committed Mar 13, 2024
1 parent a26fd64 commit da72779
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
3 changes: 3 additions & 0 deletions tests/test_articles.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ def test_article_attachments_attribute(client):
)
attachment = article.attachments[0]

with pytest.raises(AttributeError, match="read-only"):
attachment.filename = ""

assert attachment.url == f"{client.url}/ticket_attachment/12345/67/89"
assert (
repr(attachment) == f"<Attachment '{client.url}/ticket_attachment/12345/67/89'>"
Expand Down
30 changes: 7 additions & 23 deletions zammadoo/articles.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
from datetime import datetime
from mimetypes import guess_type
from pathlib import Path
from types import MappingProxyType
from typing import TYPE_CHECKING, Any, Dict, Iterable, Iterator, List, Optional

import requests
from charset_normalizer import is_binary

from .resource import Resource
from .resources import CreatableT, ResourcesT
from .utils import info_cast
from .utils import FrozenInfo, info_cast

if TYPE_CHECKING:
from typing import Union
Expand All @@ -26,43 +25,28 @@
OptionalFiles = Union[None, "PathType", Iterable["PathType"]]


class Attachment:
class Attachment(FrozenInfo):
"""Attachment(...)"""

id: int #:
filename: str #:
preferences: Dict[str, Any] #:
store_file_id: int #:
url: str #: attachment content url

def __init__(self, client: "Client", content_url: str, info: "JsonDict") -> None:
self._client = client
self._url = content_url
self._info = info
self.url = content_url
super().__init__(info)

def __repr__(self):
return f"<{self.__class__.__qualname__} {self._url!r}>"

def __getattr__(self, item):
return self._info[item]
return f"<{self.__class__.__qualname__} {self.url!r}>"

@property
def size(self) -> int:
"""attachment size in bytes"""
return int(info_cast(self._info)["size"])

@property
def url(self) -> str:
"""attachment content url"""
return self._url

def view(self) -> "MappingProxyType[str, Any]":
"""
A mapping view of the objects internal properties as returned by the REST API.
:rtype: :class:`MappingProxyType[str, Any]`
"""
return MappingProxyType(self._info)

@staticmethod
def info_from_files(*paths: "PathType"):
"""
Expand Down Expand Up @@ -94,7 +78,7 @@ def info_from_files(*paths: "PathType"):
return info_list

def _response(self) -> requests.Response:
response = self._client.response("GET", self._url, stream=True)
response = self._client.response("GET", self.url, stream=True)
response.raise_for_status()

preferences = info_cast(self._info).get("preferences", {})
Expand Down

0 comments on commit da72779

Please sign in to comment.