|
| 1 | +import os |
| 2 | +from typing import Any, Dict, List, Mapping, Optional, Tuple, Union |
| 3 | + |
| 4 | +import requests |
| 5 | + |
| 6 | +import resend |
| 7 | +from resend.http_client import HTTPClient |
| 8 | + |
| 9 | +if not os.environ["RESEND_API_KEY"]: |
| 10 | + raise EnvironmentError("RESEND_API_KEY is missing") |
| 11 | + |
| 12 | + |
| 13 | +# Define a custom HTTP client using the requests library with a higher timeout val |
| 14 | +class CustomRequestsClient(HTTPClient): |
| 15 | + def __init__(self, timeout: int = 300): |
| 16 | + self.timeout = timeout |
| 17 | + |
| 18 | + def request( |
| 19 | + self, |
| 20 | + method: str, |
| 21 | + url: str, |
| 22 | + headers: Mapping[str, str], |
| 23 | + json: Optional[Union[Dict[str, Any], List[Any]]] = None, |
| 24 | + ) -> Tuple[bytes, int, Dict[str, str]]: |
| 25 | + print(f"[HTTP] {method.upper()} {url} with timeout={self.timeout}") |
| 26 | + try: |
| 27 | + response = requests.request( |
| 28 | + method=method, |
| 29 | + url=url, |
| 30 | + headers=headers, |
| 31 | + json=json, |
| 32 | + timeout=self.timeout, |
| 33 | + ) |
| 34 | + return ( |
| 35 | + response.content, |
| 36 | + response.status_code, |
| 37 | + dict(response.headers), |
| 38 | + ) |
| 39 | + except requests.RequestException as e: |
| 40 | + raise RuntimeError(f"HTTP request failed: {e}") from e |
| 41 | + |
| 42 | + |
| 43 | +# use the custom HTTP client with a longer timeout |
| 44 | +resend.default_http_client = CustomRequestsClient(timeout=400) |
| 45 | + |
| 46 | +params: resend.Emails.SendParams = { |
| 47 | + |
| 48 | + |
| 49 | + "subject": "hi", |
| 50 | + "html": "<strong>hello, world!</strong>", |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + "tags": [ |
| 55 | + {"name": "tag1", "value": "tagvalue1"}, |
| 56 | + {"name": "tag2", "value": "tagvalue2"}, |
| 57 | + ], |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +email: resend.Email = resend.Emails.send(params) |
| 62 | +print(f"{email}") |
0 commit comments