Skip to content
Draft
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
7 changes: 5 additions & 2 deletions resend/async_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,11 @@ def __get_headers(self) -> HeadersType:
async def make_request(self, url: str) -> Union[Dict[str, Any], List[Any]]:
headers = self.__get_headers()

if isinstance(self.params, dict):
json_params: Optional[Union[Dict[str, Any], List[Any]]] = {
json_params: Optional[Union[Dict[str, Any], List[Any]]]
if self.verb == "get":
json_params = None
elif isinstance(self.params, dict):
json_params = {
str(k): v for k, v in self.params.items()
}
elif isinstance(self.params, list):
Expand Down
7 changes: 5 additions & 2 deletions resend/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,11 @@ def __get_headers(self) -> HeadersType:
def make_request(self, url: str) -> Union[Dict[str, Any], List[Any]]:
headers = self.__get_headers()

if isinstance(self.params, dict):
json_params: Optional[Union[Dict[str, Any], List[Any]]] = {
json_params: Optional[Union[Dict[str, Any], List[Any]]]
if self.verb == "get":
json_params = None
elif isinstance(self.params, dict):
json_params = {
str(k): v for k, v in self.params.items()
}
elif isinstance(self.params, list):
Expand Down
91 changes: 91 additions & 0 deletions tests/get_request_body_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import json

import httpx
import pytest
import requests

import resend
from resend.http_client_httpx import HTTPXClient

RESPONSE = {"id": "email-1", "object": "list", "data": [], "has_more": False}
SEND_PARAMS = {
"from": "sender@example.test",
"to": ["recipient@example.test"],
"subject": "Test",
"text": "Test content",
}


@pytest.mark.parametrize("operation", ["get", "list", "list_page", "send"])
def test_sync_email_request_body(monkeypatch, operation):
seen = []

def send(session, request, **kwargs):
seen.append(request)
response = requests.Response()
response.status_code = 200
response.headers["Content-Type"] = "application/json"
response._content = json.dumps(RESPONSE).encode()
return response

monkeypatch.setattr(requests.Session, "send", send)
monkeypatch.setattr(resend, "api_key", "test-key")
monkeypatch.setattr(resend, "default_http_client", resend.RequestsClient())

if operation == "get":
result = resend.Emails.get("email-1")
elif operation == "list":
result = resend.Emails.list()
elif operation == "list_page":
result = resend.Emails.list({"limit": 5, "after": "cursor-1"})
else:
result = resend.Emails.send(SEND_PARAMS)

assert result["id"] == "email-1"
request = seen[0]
assert request.headers["Authorization"] == "Bearer test-key"
if operation == "send":
assert json.loads(request.body) == SEND_PARAMS
else:
assert request.body is None
assert "Content-Type" not in request.headers
if operation == "list_page":
assert request.url.endswith("/emails?limit=5&after=cursor-1")


@pytest.mark.parametrize("operation", ["get", "list", "list_page", "send"])
async def test_async_email_request_body(monkeypatch, operation):
seen = []

def respond(request):
seen.append(request)
return httpx.Response(200, json=RESPONSE)

original_client = httpx.AsyncClient

def client_factory(**kwargs):
return original_client(transport=httpx.MockTransport(respond), **kwargs)

monkeypatch.setattr(httpx, "AsyncClient", client_factory)
monkeypatch.setattr(resend, "api_key", "test-key")
monkeypatch.setattr(resend, "default_async_http_client", HTTPXClient())

if operation == "get":
result = await resend.Emails.get_async("email-1")
elif operation == "list":
result = await resend.Emails.list_async()
elif operation == "list_page":
result = await resend.Emails.list_async({"limit": 5, "after": "cursor-1"})
else:
result = await resend.Emails.send_async(SEND_PARAMS)

assert result["id"] == "email-1"
request = seen[0]
assert request.headers["Authorization"] == "Bearer test-key"
if operation == "send":
assert json.loads(request.content) == SEND_PARAMS
else:
assert request.content == b""
assert "Content-Type" not in request.headers
if operation == "list_page":
assert str(request.url).endswith("/emails?limit=5&after=cursor-1")