Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions jmapc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,23 @@ def upload_blob(self, file_name: Union[str, Path]) -> Blob:
r.raise_for_status()
return Blob.from_dict(r.json())

@overload
def download_attachment(
self, attachment: EmailBodyPart, file_name: None
) -> bytes: ... # pragma: no cover

@overload
def download_attachment(
self,
attachment: EmailBodyPart,
file_name: Union[str, Path],
) -> None:
if not file_name:
raise Exception("Destination file name is required")
file_name = Path(file_name)
) -> None: ... # pragma: no cover

def download_attachment(
self,
attachment: EmailBodyPart,
file_name: Union[str, Path, None],
) -> Optional[bytes]:
blob_url = self.jmap_session.download_url.format(
accountId=self.account_id,
blobId=attachment.blob_id,
Expand All @@ -166,8 +175,12 @@ def download_attachment(
blob_url, stream=True, timeout=REQUEST_TIMEOUT
)
r.raise_for_status()
with open(file_name, "wb") as f:
f.write(r.raw.data)
if file_name:
with open(file_name, "wb") as f:
f.write(r.raw.data)
return None
else:
return r.raw.data

@overload
def request(
Expand Down
13 changes: 5 additions & 8 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,15 +451,12 @@ def test_download_attachment(
body=blob_content,
)
dest_file = tempdir / "download.txt"
with pytest.raises(Exception) as e:
client.download_attachment(
EmailBodyPart(
name="download.txt", blob_id="C2187", type="text/plain"
),
"",
)
assert str(e.value) == "Destination file name is required"
data = client.download_attachment(
EmailBodyPart(name="download.txt", blob_id="C2187", type="text/plain"),
None,
)
assert not dest_file.exists()
assert data.decode("utf-8") == blob_content
client.download_attachment(
EmailBodyPart(name="download.txt", blob_id="C2187", type="text/plain"),
dest_file,
Expand Down