Skip to content

Commit c0caea8

Browse files
committed
fix: remove contact from DD
1 parent 49f2836 commit c0caea8

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

codeforlife/mail.py

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -151,69 +151,60 @@ def add_contact(
151151

152152
# pylint: disable-next=unused-argument
153153
def remove_contact(
154-
contact_identifier: str,
154+
value: str,
155+
identifier: t.Literal["contact-id", "email", "mobile-number"] = "email",
155156
region: str = "r1",
156157
auth: t.Optional[str] = None,
157158
timeout: int = 30,
158159
):
159160
# pylint: disable=line-too-long
160161
"""Remove an existing contact from Dotdigital.
161162
162-
https://developer.dotdigital.com/reference/get-contact
163-
https://developer.dotdigital.com/reference/delete-contact
163+
https://developer.dotdigital.com/reference/deletecontact-1
164164
165165
Args:
166-
contact_identifier: Either the contact id or email address of the contact.
166+
value: The unique value to identify the contact. Note: Must be the same type as the identifier.
167+
identifier: Field to use to uniquely identify the contact.
167168
region: The Dotdigital region id your account belongs to e.g. r1, r2 or r3.
168169
auth: The authorization header used to enable API access. If None, the value will be retrieved from the MAIL_AUTH environment variable.
169170
timeout: Send timeout to avoid hanging.
170171
171172
Raises:
172-
AssertionError: If failed to get contact.
173173
AssertionError: If failed to delete contact.
174+
175+
Returns:
176+
A flag designating whether the contact was removed. True if the contact
177+
was found, False if the contact was not found.
174178
"""
175179
# pylint: enable=line-too-long
176180

177181
if not settings.MAIL_ENABLED:
178-
logging.info("Removed contact from DotDigital: %s", contact_identifier)
179-
return
182+
logging.info("Removed contact from DotDigital: %s", value)
183+
return True
180184

181185
if auth is None:
182186
auth = settings.MAIL_AUTH
183187

184188
response = requests.get(
185189
# pylint: disable-next=line-too-long
186-
url=f"https://{region}-api.dotdigital.com/v2/contacts/{contact_identifier}",
190+
url=f"https://{region}-api.dotdigital.com/contacts/v3/{identifier}/{value}",
187191
headers={
188192
"accept": "application/json",
189193
"authorization": auth,
190194
},
191195
timeout=timeout,
192196
)
193197

194-
assert response.ok, (
195-
"Failed to get contact."
196-
f" Reason: {response.reason}."
197-
f" Text: {response.text}."
198-
)
199-
200-
contact_id: int = response.json()["id"]
201-
202-
response = requests.delete(
203-
url=f"https://{region}-api.dotdigital.com/v2/contacts/{contact_id}",
204-
headers={
205-
"accept": "application/json",
206-
"authorization": auth,
207-
},
208-
timeout=timeout,
209-
)
198+
not_found = response.status_code == 404
210199

211-
assert response.ok, (
200+
assert response.ok or not_found, (
212201
"Failed to delete contact."
213202
f" Reason: {response.reason}."
214203
f" Text: {response.text}."
215204
)
216205

206+
return not not_found
207+
217208

218209
@dataclass
219210
class EmailAttachment:

0 commit comments

Comments
 (0)