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
24 changes: 23 additions & 1 deletion jmapc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
Response,
ResponseOrError,
)
from .models import Blob, EmailBodyPart, Event
from .models import Blob, Email, EmailBodyPart, Event
from .session import Session

RequestsAuth = Union[requests.auth.AuthBase, tuple[str, str]]
Expand Down Expand Up @@ -169,6 +169,28 @@ def download_attachment(
with open(file_name, "wb") as f:
f.write(r.raw.data)

def download_email(
self,
email: Email,
file_name: Union[str, Path],
) -> None:
if not file_name:
raise Exception("Destination file name is required")
file_name = Path(file_name)
blob_url = self.jmap_session.download_url.format(
accountId=self.account_id,
blobId=email.blob_id,
name="",
type="message/rfc822",
)
print(blob_url)
r = self.requests_session.get(
blob_url, stream=True, timeout=REQUEST_TIMEOUT
)
r.raise_for_status()
with open(file_name, "wb") as f:
f.write(r.raw.data)

@overload
def request(
self,
Expand Down
2 changes: 1 addition & 1 deletion jmapc/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@ class UnknownMethod(Error):

@dataclass
class UnsupportedFilter(Error):
_type = "UnsupportedFilter"
_type = "unsupportedFilter"
29 changes: 28 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import requests
import responses

from jmapc import Blob, Client, ClientError, EmailBodyPart, constants
from jmapc import Blob, Client, ClientError, Email, EmailBodyPart, constants
from jmapc.auth import BearerAuth
from jmapc.methods import (
CoreEcho,
Expand Down Expand Up @@ -465,3 +465,30 @@ def test_download_attachment(
dest_file,
)
assert dest_file.read_text() == blob_content


def test_download_email(
client: Client, http_responses: responses.RequestsMock, tempdir: Path
) -> None:
blob_content = "test download blob content"
http_responses.add(
method=responses.GET,
url=(
"https://jmap-api.localhost/jmap/download"
"/u1138/E5402/?type=message/rfc822"
),
body=blob_content,
)
dest_file = tempdir / "email.eml"
with pytest.raises(Exception) as e:
client.download_email(
Email(blob_id="E5402"),
"",
)
assert str(e.value) == "Destination file name is required"
assert not dest_file.exists()
client.download_email(
Email(blob_id="E5402"),
dest_file,
)
assert dest_file.read_text() == blob_content