Skip to content

Commit

Permalink
use Response.apparent_encoding when returning attachment content as text
Browse files Browse the repository at this point in the history
remove Article.encoding property

Signed-off-by: flashdagger <flashdagger@googlemail.com>
  • Loading branch information
flashdagger committed Oct 28, 2023
1 parent 9faaf62 commit f449c4e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
18 changes: 10 additions & 8 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ History
0.2.0 (tbd)
-----------
* **added features**

* extend supported Python version to 3.8
* added :meth:`tickets.Ticket.history` method
* added ``weburl`` property for :class:`client.Client`, :class:`tickets.Ticket`,
:class:`users.User` and :class:`organizations.Organization`
* added property :attr:`tickets.Ticket.create_article_sender`

* **breaking changes**

* :class:`client.Client` now uses ``http_auth=(username, password)`` as authentication parameter
Expand All @@ -22,12 +14,22 @@ History
mimicking the logic provided by the Web UI
* :meth:`tags.Tags.delete()` and :meth:`tags.Tags.delete()` now only accept the tag name
* changed signature of :meth:`tickets.Tickets.create()` to assure an article body or the article itself is provided
* remove :attr:`articles.Article.encoding` property

* **added features**

* extend supported Python version to 3.8
* added :meth:`tickets.Ticket.history` method
* added ``weburl`` property for :class:`client.Client`, :class:`tickets.Ticket`,
:class:`users.User` and :class:`organizations.Organization`
* added property :attr:`tickets.Ticket.create_article_sender`

* **fixes**

* resource items are now cached when using iteration
* fromisoformat conversion in Python <3.10 supporting Zulu offset format
* :class:`roles.Roles` and :class:`groups.Groups` wrongly supported `search`
* use :attr:`requests.Response.apparent_encoding` when returning attachment content as text

0.1.0 (2023-10-08)
------------------
Expand Down
3 changes: 0 additions & 3 deletions tests/test_articles.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def test_article_attachments_attribute(client):
repr(attachment) == f"<Attachment '{client.url}/ticket_attachment/12345/67/89'>"
)
assert attachment.filename == "zammad_logo_white.png"
assert attachment.encoding is None
assert attachment.size == 3253
assert attachment.view()["size"] == "3253"

Expand Down Expand Up @@ -106,13 +105,11 @@ def test_create_article_via_ticket(rclient, ticket_pair):

assert binary_attachment.filename == filename_binary
assert binary_attachment.size == 256
assert binary_attachment.encoding is None
assert binary_attachment.read_bytes() == content_binary
with pytest.raises(AssertionError, match="content is binary only"):
binary_attachment.iter_text()

assert text_attachment.filename == filename_text
assert text_attachment.encoding is None
assert text_attachment.read_text() == content_text
assert tuple(text_attachment.iter_text(chunk_size=16)) == (
"Mogę jeść szk",
Expand Down
16 changes: 6 additions & 10 deletions zammadoo/articles.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ def __repr__(self):
def __getattr__(self, item):
return self._info[item]

@property
def encoding(self) -> Optional[str]:
preferences = info_cast(self._info).get("preferences", {})
return preferences.get("Charset")

@property
def size(self) -> int:
return int(info_cast(self._info)["size"])
Expand Down Expand Up @@ -84,11 +79,12 @@ def info_from_files(*paths: "PathType"):
)
return info_list

def _response(self, encoding: Optional[str] = None) -> requests.Response:
def _response(self) -> requests.Response:
response = self._client.response("GET", self._url, stream=True)
response.raise_for_status()
if encoding:
response.encoding = encoding

preferences = info_cast(self._info).get("preferences", {})
response.encoding = preferences.get("Charset") or response.apparent_encoding

return response

Expand All @@ -107,10 +103,10 @@ def read_bytes(self) -> bytes:
return self._response().content

def read_text(self) -> str:
return self._response(self.encoding).text
return self._response().text

def iter_text(self, chunk_size=8192):
response = self._response(encoding=self.encoding)
response = self._response()
assert response.encoding, "content is binary only, use .iter_bytes() instead"
return response.iter_content(chunk_size=chunk_size, decode_unicode=True)

Expand Down

0 comments on commit f449c4e

Please sign in to comment.