Skip to content
Open
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
10 changes: 7 additions & 3 deletions resend/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,13 @@ def make_request(self, url: str) -> Union[Dict[str, Any], List[Any]]:
# fall back to 500 if the status looks successful but the body is not.
error_code = status_code if status_code >= 400 else 500

content_type = {k.lower(): v for k, v in resp_headers.items()}.get(
"content-type", ""
)
normalized_headers = {k.lower(): v for k, v in resp_headers.items()}
if status_code == 429 and "retry-after" in normalized_headers:
self._response_headers.setdefault(
"retry-after", normalized_headers["retry-after"]
)

content_type = normalized_headers.get("content-type", "")

if "application/json" not in content_type:
raise_for_code_and_type(
Expand Down
11 changes: 6 additions & 5 deletions tests/request_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from unittest.mock import AsyncMock, MagicMock, Mock, patch

import pytest
from requests.structures import CaseInsensitiveDict

from resend import request
from resend.exceptions import (ApplicationError, RateLimitError, ResendError,
Expand Down Expand Up @@ -223,10 +224,9 @@ def test_json_error_uses_http_status_when_body_omits_status_code(
b'{"name":"rate_limit_exceeded","message":"Too many requests"}'
)
mock_response.status_code = 429
mock_response.headers = {
"content-type": "application/json",
"retry-after": "2",
}
mock_response.headers = CaseInsensitiveDict(
{"Content-Type": "application/json", "Retry-After": "5"}
)
mock_requests.return_value = mock_response

req = request.Request[Dict[str, Any]](
Expand All @@ -242,7 +242,8 @@ def test_json_error_uses_http_status_when_body_omits_status_code(
self.assertEqual(err.code, 429)
self.assertEqual(err.error_type, "rate_limit_exceeded")
self.assertEqual(err.message, "Too many requests")
self.assertEqual(err.headers.get("retry-after"), "2")
self.assertEqual(err.headers["Retry-After"], "5")
self.assertEqual(err.headers.get("retry-after"), "5")


@pytest.mark.asyncio
Expand Down