Skip to content

Commit 41b27d0

Browse files
authored
[62077] Fix adding a tracking object to an existing draft (nylas#153)
The Python SDK creates a special payload specifically for sending an already-existing `draft` object that extracts the draft ID and the draft's version and sends that as a payload. However, it does not check if a tracking object exists, and thus if a user were to save a draft, and come add a tracking object to it (or if the user saves the draft with the tracking object first) and tries to send it after the save, the SDK never sends the tracking object in the payload. This PR adds a check to add a tracking object if present in the `draft`.
1 parent f5742d4 commit 41b27d0

4 files changed

Lines changed: 56 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Unreleased (dev)
55
----------------
66
* Add `metadata` field in the Event model to support new event metadata feature
77
* Add filtering support for `metadata_pair`
8+
* Fix adding a tracking object to an existing `draft`
89

910
v4.12.1
1011
-------

nylas/client/restful_models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,8 @@ def send(self):
460460
data = {"draft_id": self.id}
461461
if hasattr(self, "version"):
462462
data["version"] = self.version
463+
if hasattr(self, "tracking") and self.tracking is not None:
464+
data["tracking"] = self.tracking
463465

464466
msg = self.api._create_resource(Send, data)
465467
if msg:

tests/conftest.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,21 @@ def callback(request):
703703
)
704704

705705

706+
@pytest.fixture
707+
def mock_draft_send_unsaved_response(mocked_responses, api_url):
708+
def callback(request):
709+
payload = json.loads(request.body)
710+
payload["draft_id"] = "2h111aefv8pzwzfykrn7hercj"
711+
return 200, {}, json.dumps(payload)
712+
713+
mocked_responses.add_callback(
714+
responses.POST,
715+
api_url + "/send/",
716+
callback=callback,
717+
content_type="application/json",
718+
)
719+
720+
706721
@pytest.fixture
707722
def mock_files(mocked_responses, api_url, account_id):
708723
files_content = {"3qfe4k3siosfjtjpfdnon8zbn": b"Hello, World!"}

tests/test_drafts.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
from datetime import datetime
23

34
import pytest
@@ -35,6 +36,43 @@ def test_save_send_draft(api_client):
3536
draft.send()
3637

3738

39+
@pytest.mark.usefixtures("mock_draft_saved_response", "mock_draft_sent_response")
40+
def test_save_send_draft_with_tracking(mocked_responses, api_client):
41+
tracking = {
42+
"links": "true",
43+
"opens": "true",
44+
"thread_replies": "true",
45+
"payload": "new-payload",
46+
}
47+
draft = api_client.drafts.create()
48+
draft.to = [{"name": "My Friend", "email": "my.friend@example.com"}]
49+
draft.subject = "Here's an attachment"
50+
draft.body = "Cheers mate!"
51+
draft.save()
52+
53+
draft.tracking = tracking
54+
draft.save()
55+
assert draft.tracking == tracking
56+
57+
draft.send()
58+
send_payload = json.loads(mocked_responses.calls[-1].request.body)
59+
assert send_payload["tracking"] == tracking
60+
61+
62+
@pytest.mark.usefixtures("mock_draft_send_unsaved_response")
63+
def test_send_draft_with_tracking(mocked_responses, api_client):
64+
tracking = {"opens": "true", "payload": "payload"}
65+
draft = api_client.drafts.create()
66+
draft.to = [{"name": "My Friend", "email": "my.friend@example.com"}]
67+
draft.subject = "Newsletter"
68+
draft.body = "Our latest sale!"
69+
draft.tracking = tracking
70+
draft.send()
71+
72+
send_payload = json.loads(mocked_responses.calls[-1].request.body)
73+
assert send_payload["tracking"] == tracking
74+
75+
3876
@pytest.mark.usefixtures("mock_files")
3977
def test_draft_attachment(api_client):
4078
draft = api_client.drafts.create()

0 commit comments

Comments
 (0)