-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
212 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import os | ||
|
||
import resend | ||
|
||
if not os.environ["RESEND_API_KEY"]: | ||
raise EnvironmentError("RESEND_API_KEY is missing") | ||
|
||
|
||
audience = resend.Audiences.create( | ||
{ | ||
"name": "New Audience from Python SDK", | ||
} | ||
) | ||
print(audience) | ||
|
||
aud = resend.Audiences.get(audience["id"]) | ||
print(aud) | ||
|
||
audiences = resend.Audiences.list() | ||
print(audiences) | ||
|
||
rmed = resend.Audiences.remove(audience["id"]) | ||
print(rmed) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import base64 | ||
import os | ||
|
||
import resend | ||
import base64 | ||
|
||
if not os.environ["RESEND_API_KEY"]: | ||
raise EnvironmentError("RESEND_API_KEY is missing") | ||
|
@@ -10,7 +11,7 @@ | |
).read() | ||
|
||
b64 = base64.b64encode(f) | ||
b64_str = b64.decode('utf-8') | ||
b64_str = b64.decode("utf-8") | ||
|
||
params = { | ||
"from": "[email protected]", | ||
|
@@ -21,4 +22,4 @@ | |
} | ||
|
||
email = resend.Emails.send(params) | ||
print(email) | ||
print(email) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import base64 | ||
import os | ||
|
||
import resend | ||
import base64 | ||
|
||
if not os.environ["RESEND_API_KEY"]: | ||
raise EnvironmentError("RESEND_API_KEY is missing") | ||
|
@@ -10,7 +11,7 @@ | |
).read() | ||
|
||
b64 = base64.b64encode(f) | ||
b64_str = b64.decode('utf-8') | ||
b64_str = b64.decode("utf-8") | ||
|
||
params = { | ||
"from": "[email protected]", | ||
|
@@ -21,4 +22,4 @@ | |
} | ||
|
||
email = resend.Emails.send(params) | ||
print(email) | ||
print(email) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from typing import Dict | ||
|
||
from resend import request | ||
|
||
|
||
class Audiences: | ||
"""Audiences API Wrapper""" | ||
|
||
@classmethod | ||
# https://resend.com/docs/api-reference/audiences/create-audience | ||
def create(cls, params={}) -> Dict: | ||
path = "/audiences" | ||
return request.Request(path=path, params=params, verb="post").perform() | ||
|
||
@classmethod | ||
# https://resend.com/docs/api-reference/audiences/list-audiences | ||
def list(cls) -> Dict: | ||
path = "/audiences/" | ||
return request.Request(path=path, params={}, verb="get").perform() | ||
|
||
@classmethod | ||
# https://resend.com/docs/api-reference/audiences/get-audience | ||
def get(cls, id) -> Dict: | ||
path = f"/audiences/{id}" | ||
return request.Request(path=path, params={}, verb="get").perform() | ||
|
||
@classmethod | ||
# https://resend.com/docs/api-reference/audiences/delete-audience | ||
def remove(cls, id="") -> Dict: | ||
path = f"/audiences/{id}" | ||
return request.Request(path=path, params={}, verb="delete").perform() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from typing import List, Dict | ||
from typing import Dict, List | ||
|
||
from resend import request | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import unittest | ||
from unittest.mock import MagicMock, patch | ||
|
||
import resend | ||
|
||
# flake8: noqa | ||
|
||
|
||
class TestResendAudiences(unittest.TestCase): | ||
def test_audiences_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": "audience", | ||
"id": "78261eea-8f8b-4381-83c6-79fa7120f1cf", | ||
"name": "Registered Users", | ||
} | ||
|
||
m.json = mock_json | ||
mock.return_value = m | ||
|
||
params = { | ||
"name": "Python SDK Audience", | ||
} | ||
audience = resend.Audiences.create(params) | ||
assert audience["id"] == "78261eea-8f8b-4381-83c6-79fa7120f1cf" | ||
assert audience["name"] == "Registered Users" | ||
assert audience["object"] == "audience" | ||
|
||
patcher.stop() | ||
|
||
def test_audiences_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": "audience", | ||
"id": "78261eea-8f8b-4381-83c6-79fa7120f1cf", | ||
"name": "Registered Users", | ||
"created_at": "2023-10-06T22:59:55.977Z", | ||
} | ||
|
||
m.json = mock_json | ||
mock.return_value = m | ||
|
||
audience = resend.Audiences.list() | ||
assert audience["object"] == "audience" | ||
assert audience["id"] == "78261eea-8f8b-4381-83c6-79fa7120f1cf" | ||
assert audience["name"] == "Registered Users" | ||
|
||
patcher.stop() | ||
|
||
def test_audiences_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": "audience", | ||
"id": "78261eea-8f8b-4381-83c6-79fa7120f1cf", | ||
"deleted": True, | ||
} | ||
|
||
m.json = mock_json | ||
mock.return_value = m | ||
|
||
rmed = resend.Audiences.remove("78261eea-8f8b-4381-83c6-79fa7120f1cf") | ||
assert rmed["object"] == "audience" | ||
assert rmed["id"] == "78261eea-8f8b-4381-83c6-79fa7120f1cf" | ||
assert rmed["deleted"] is True | ||
|
||
patcher.stop() | ||
|
||
def test_audiences_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": "78261eea-8f8b-4381-83c6-79fa7120f1cf", | ||
"name": "Registered Users", | ||
"created_at": "2023-10-06T22:59:55.977Z", | ||
} | ||
], | ||
} | ||
|
||
m.json = mock_json | ||
mock.return_value = m | ||
|
||
audience = resend.Audiences.list() | ||
assert audience["object"] == "list" | ||
assert audience["data"][0]["id"] == "78261eea-8f8b-4381-83c6-79fa7120f1cf" | ||
assert audience["data"][0]["name"] == "Registered Users" | ||
|
||
patcher.stop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,35 +18,31 @@ def test_batch_email_send(self): | |
|
||
def mock_json(): | ||
return { | ||
"data": [ | ||
{ | ||
"id": "ae2014de-c168-4c61-8267-70d2662a1ce1" | ||
}, | ||
{ | ||
"id": "faccb7a5-8a28-4e9a-ac64-8da1cc3bc1cb" | ||
} | ||
] | ||
"data": [ | ||
{"id": "ae2014de-c168-4c61-8267-70d2662a1ce1"}, | ||
{"id": "faccb7a5-8a28-4e9a-ac64-8da1cc3bc1cb"}, | ||
] | ||
} | ||
|
||
m.json = mock_json | ||
mock.return_value = m | ||
|
||
params = [ | ||
{ | ||
"from": "[email protected]", | ||
"to": ["[email protected]"], | ||
"subject": "hey", | ||
"html": "<strong>hello, world!</strong>", | ||
"from": "[email protected]", | ||
"to": ["[email protected]"], | ||
"subject": "hey", | ||
"html": "<strong>hello, world!</strong>", | ||
}, | ||
{ | ||
"from": "[email protected]", | ||
"to": ["[email protected]"], | ||
"subject": "hello", | ||
"html": "<strong>hello, world!</strong>", | ||
"from": "[email protected]", | ||
"to": ["[email protected]"], | ||
"subject": "hello", | ||
"html": "<strong>hello, world!</strong>", | ||
}, | ||
] | ||
emails = resend.Batch.send(params) | ||
assert len(emails["data"]) == 2 | ||
assert emails["data"][0]["id"] == "ae2014de-c168-4c61-8267-70d2662a1ce1" | ||
assert emails["data"][1]["id"] == "faccb7a5-8a28-4e9a-ac64-8da1cc3bc1cb" | ||
patcher.stop() | ||
patcher.stop() |