Skip to content

Commit

Permalink
feat: Impl Contacts API
Browse files Browse the repository at this point in the history
  • Loading branch information
drish committed Dec 17, 2023
1 parent 7b56860 commit 1e9f67f
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/batch_email_send.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
]

emails = resend.Batch.send(params)
print(emails)
print(emails)
31 changes: 31 additions & 0 deletions examples/contacts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import os

import resend

if not os.environ["RESEND_API_KEY"]:
raise EnvironmentError("RESEND_API_KEY is missing")


contact = resend.Contacts.create(
{
"audience_id": "48c269ed-9873-4d60-bdd9-cd7e6fc0b9b8",
"email": "[email protected]",
"first_name": "Steve",
"last_name": "Wozniak",
"unsubscribed": True,
}
)
print(contact)

cont = resend.Contacts.get(
audience_id="48c269ed-9873-4d60-bdd9-cd7e6fc0b9b8", id=contact["id"]
)
print(cont)

contacts = resend.Contacts.list(audience_id="48c269ed-9873-4d60-bdd9-cd7e6fc0b9b8")
print(contacts)

rmed = resend.Contacts.remove(
audience_id="48c269ed-9873-4d60-bdd9-cd7e6fc0b9b8", id=contact["id"]
)
print(rmed)
2 changes: 2 additions & 0 deletions resend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .api_keys import ApiKeys
from .audiences import Audiences
from .batch import Batch
from .contacts import Contacts
from .domains import Domains
from .emails import Emails
from .request import Request
Expand All @@ -25,4 +26,5 @@
"Domains",
"Batch",
"Audiences",
"Contacts",
]
31 changes: 31 additions & 0 deletions resend/contacts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import Dict

from resend import request


class Contacts:
"""Contacts API Wrapper"""

@classmethod
# https://resend.com/docs/api-reference/contacts/create-contact
def create(cls, params={}) -> Dict:
path = f"/audiences/{params['audience_id']}/contacts"
return request.Request(path=path, params=params, verb="post").perform()

@classmethod
# https://resend.com/docs/api-reference/audiences/list-audiences
def list(cls, audience_id) -> Dict:
path = f"/audiences/#{audience_id}/contacts"
return request.Request(path=path, params={}, verb="get").perform()

@classmethod
# https://resend.com/docs/api-reference/audiences/get-audience
def get(cls, audience_id, id) -> Dict:
path = f"/audiences/{audience_id}/contacts/{id}"
return request.Request(path=path, params={}, verb="get").perform()

@classmethod
# https://resend.com/docs/api-reference/audiences/delete-audience
def remove(cls, audience_id, id) -> Dict:
path = f"/audiences/{id}/contacts/{id}"
return request.Request(path=path, params={}, verb="delete").perform()
142 changes: 142 additions & 0 deletions tests/contacts_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import unittest
from unittest.mock import MagicMock, patch

import resend

# flake8: noqa


class TestResendContacts(unittest.TestCase):
def test_contacts_create(self):
resend.api_key = "re_123"

patcher = patch("resend.Request.make_request")
mock = patcher.start()
mock.status_code = 200
m = MagicMock()
m.status_code = 200

def mock_json():
return {"object": "contact", "id": "479e3145-dd38-476b-932c-529ceb705947"}

m.json = mock_json
mock.return_value = m

params = {
"audience_id": "48c269ed-9873-4d60-bdd9-cd7e6fc0b9b8",
"email": "[email protected]",
"first_name": "Steve",
"last_name": "Wozniak",
"unsubscribed": True,
}
contact = resend.Contacts.create(params)
assert contact["id"] == "479e3145-dd38-476b-932c-529ceb705947"
assert contact["object"] == "contact"

patcher.stop()

def test_contacts_get(self):
resend.api_key = "re_123"

patcher = patch("resend.Request.make_request")
mock = patcher.start()
mock.status_code = 200
m = MagicMock()
m.status_code = 200

def mock_json():
return {
"object": "contact",
"id": "e169aa45-1ecf-4183-9955-b1499d5701d3",
"email": "[email protected]",
"first_name": "Steve",
"last_name": "Wozniak",
"created_at": "2023-10-06T23:47:56.678Z",
"unsubscribed": False,
}

m.json = mock_json
mock.return_value = m

contact = resend.Contacts.get(
audience_id="48c269ed-9873-4d60-bdd9-cd7e6fc0b9b8",
id="e169aa45-1ecf-4183-9955-b1499d5701d3",
)
assert contact["object"] == "contact"
assert contact["id"] == "e169aa45-1ecf-4183-9955-b1499d5701d3"
assert contact["email"] == "[email protected]"
assert contact["first_name"] == "Steve"
assert contact["last_name"] == "Wozniak"
assert contact["created_at"] == "2023-10-06T23:47:56.678Z"
assert contact["unsubscribed"] is False

patcher.stop()

def test_contacts_remove(self):
resend.api_key = "re_123"

patcher = patch("resend.Request.make_request")
mock = patcher.start()
mock.status_code = 200
m = MagicMock()
m.status_code = 200

def mock_json():
return {
"object": "contact",
"id": "520784e2-887d-4c25-b53c-4ad46ad38100",
"deleted": True,
}

m.json = mock_json
mock.return_value = m

rmed = resend.Contacts.remove(
audience_id="48c269ed-9873-4d60-bdd9-cd7e6fc0b9b8",
id="78261eea-8f8b-4381-83c6-79fa7120f1cf",
)
assert rmed["object"] == "contact"
assert rmed["id"] == "520784e2-887d-4c25-b53c-4ad46ad38100"
assert rmed["deleted"] is True

patcher.stop()

def test_contacts_list(self):
resend.api_key = "re_123"

patcher = patch("resend.Request.make_request")
mock = patcher.start()
mock.status_code = 200
m = MagicMock()
m.status_code = 200

def mock_json():
return {
"object": "list",
"data": [
{
"id": "e169aa45-1ecf-4183-9955-b1499d5701d3",
"email": "[email protected]",
"first_name": "Steve",
"last_name": "Wozniak",
"created_at": "2023-10-06T23:47:56.678Z",
"unsubscribed": False,
}
],
}

m.json = mock_json
mock.return_value = m

contacts = resend.Contacts.list(
audience_id="48c269ed-9873-4d60-bdd9-cd7e6fc0b9b8"
)
assert contacts["object"] == "list"
assert contacts["data"][0]["id"] == "e169aa45-1ecf-4183-9955-b1499d5701d3"
assert contacts["data"][0]["email"] == "[email protected]"
assert contacts["data"][0]["first_name"] == "Steve"
assert contacts["data"][0]["last_name"] == "Wozniak"
assert contacts["data"][0]["created_at"] == "2023-10-06T23:47:56.678Z"
assert contacts["data"][0]["unsubscribed"] is False

patcher.stop()

0 comments on commit 1e9f67f

Please sign in to comment.