Skip to content

feat: Add support for custom http clients #146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

drish
Copy link
Collaborator

@drish drish commented May 26, 2025

Introduced a new global level attribute default_http_client, that can be overwritten by users to set their own HTTP Clients. Custom clients need to base off the "interface" HTTPClient defined in resend/http_client.py

Example:

# resend's abstract HTTP Client class.
from resend.http_client import HTTPClient

class CustomRequestsClient(HTTPClient):
    def __init__(self, timeout: int = 30):
        self.timeout = timeout

    def request(
        self,
        method: str,
        url: str,
        headers: Mapping[str, str],
        json: Optional[Union[Dict[str, Any], List[Any]]] = None,
    ) -> Tuple[bytes, int, Dict[str, str]]:
        print(f"[HTTP] {method.upper()} {url} with timeout={self.timeout}")
        try:
            response = requests.request(
                method=method,
                url=url,
                headers=headers,
                json=json,
                timeout=self.timeout,
            )
            return (
                response.content,
                response.status_code,
                dict(response.headers),
            )
        except requests.RequestException as e:
            raise RuntimeError(f"HTTP request failed: {e}") from e


resend.api_key = "re_123"
resend.default_http_client = CustomRequestsClient(timeout=400)

email: resend.Email = resend.Emails.send({...})

closes #145

Copy link

graphite-app bot commented May 26, 2025

How to use the Graphite Merge Queue

Add the label merge-queue to this PR to add it to the merge queue.

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

@drish drish marked this pull request as ready for review May 27, 2025 00:01
@drish drish requested a review from felipevolpone May 27, 2025 00:01
@drish drish changed the title feat: Add support for custom http client feat: Add support for custom http clients May 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Request timeout needed
1 participant