Skip to content

Commit

Permalink
domains: tweaks on type hinting
Browse files Browse the repository at this point in the history
  • Loading branch information
drish committed Apr 14, 2024
1 parent c49682a commit 588b15e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 24 deletions.
7 changes: 4 additions & 3 deletions examples/domains.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
raise EnvironmentError("RESEND_API_KEY is missing")


domain = resend.Domains.create({
create_params: resend.Domains.CreateParams = {
"name": "example.com",
"region": "us-east-1",
})
}
domain = resend.Domains.create(params = create_params)
print(f'Crated domain {domain.name} with id {domain.id}')

retrieved = resend.Domains.get(domain_id=domain.id)
print(retrieved.__dict__)
for record in retrieved.records:
print(record.__dict__)

update_params: resend.Domains.UpdateDomainRequestParams = {
update_params: resend.Domains.UpdateParams = {
"id": domain.id,
"open_tracking": True,
"click_tracking": True,
Expand Down
2 changes: 1 addition & 1 deletion resend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from .api_keys._api_keys import ApiKeys
from .audiences import Audiences
from .batch import Batch
# from .batch import Batch
from .contacts import Contacts
from .domains._domains import Domains
from .emails import Emails
Expand Down
22 changes: 12 additions & 10 deletions resend/domains/_domains.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
from typing import List, Any, Dict, Optional, cast
from typing import List, Any, Dict, cast

from typing_extensions import TypedDict
from typing_extensions import TypedDict, NotRequired
from resend import request
from resend.domains._domain import Domain

class Domains:

class UpdateDomainRequestParams(TypedDict):
class UpdateParams(TypedDict):
id: str
"""
The domain ID.
"""
click_tracking: bool
click_tracking: NotRequired[bool]
"""
Track clicks within the body of each HTML email.
"""
open_tracking: bool
open_tracking: NotRequired[bool]
"""
Track the open rate of each email.
"""

class CreateDomainRequestParams(TypedDict):
class CreateParams(TypedDict):
name: str
"""
The domain name.
"""
region: str
region: NotRequired[str]
"""
The region where emails will be sent from.
Possible values: us-east-1' | 'eu-west-1' | 'sa-east-1' | 'ap-northeast-1'
"""

@classmethod
def create(cls, params: CreateDomainRequestParams = {}) -> Domain:
def create(cls, params: CreateParams) -> Domain:
"""
Create a domain through the Resend Email API.
see more: https://resend.com/docs/api-reference/domains/create-domain
Expand All @@ -45,14 +45,16 @@ def create(cls, params: CreateDomainRequestParams = {}) -> Domain:
)

@classmethod
def update(cls, params: UpdateDomainRequestParams = {}) -> Domain:
def update(cls, params: UpdateParams) -> Domain:
"""
Update an existing domain.
see more: https://resend.com/docs/api-reference/domains/update-domain
"""
path = f"/domains/{params['id']}"
return Domain.new_from_request(
request.Request(path=path, params=params, verb="patch").perform()
request.Request(
path=path, params=cast(Dict[Any, Any], params), verb="patch")
.perform()
)

@classmethod
Expand Down
34 changes: 27 additions & 7 deletions resend/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(
code: str,
error_type: str,
message: str,
suggested_action: str = None,
suggested_action: str,
):
Exception.__init__(self, message)
self.code = code
Expand Down Expand Up @@ -157,8 +157,8 @@ def __init__(
error_type=error_type,
)


ERRORS: Dict[str, Dict[str, ResendError]] = {
# Dict with error code -> error type mapping
ERRORS: Dict[str, Dict[str, type[ResendError]]] = {
"400": {"validation_error": ValidationError},
"422": {"missing_required_fields": MissingRequiredFieldsError},
"401": {"missing_api_key": MissingApiKeyError},
Expand All @@ -167,14 +167,34 @@ def __init__(
}


def raise_for_code_and_type(code, error_type, message: str) -> ResendError:
def raise_for_code_and_type(
code: str,
error_type: str,
message: str) -> ResendError:
error = ERRORS.get(str(code))

# Handle the case where the error might be unknown
if error is None or error.get(error_type) is None:
raise ResendError(code=code, message=message, error_type=error_type)
raise ResendError(
code=code,
message=message,
error_type=error_type,
suggested_action=get_suggested_action_for_error(code)
)

# Raise error from errors list
error: ResendError = error.get(error_type)
new_error: type[ResendError] | None = error.get(error_type)

if new_error is not None:
raise new_error(
code=code,
message=message,
error_type=error_type,
suggested_action=get_suggested_action_for_error(code)
)
raise TypeError("Error type not found")



raise error(code=code, message=message, error_type=error_type)
def get_suggested_action_for_error(code: str):
return ""
6 changes: 3 additions & 3 deletions tests/domains_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ def mock_json():
m.json = mock_json
mock.return_value = m

params: resend.Domains.UpdateDomainRequestParams = {
create_params: resend.Domains.CreateParams = {
"name": "example.com",
}
domain = resend.Domains.create(params)
domain = resend.Domains.create(params = create_params)
assert domain.id == "4dd369bc-aa82-4ff3-97de-514ae3000ee0"
assert domain.name == "example.com"
assert domain.status == "not_started"
Expand Down Expand Up @@ -217,7 +217,7 @@ def mock_json():
m.json = mock_json
mock.return_value = m

params: resend.Domains.UpdateDomainRequestParams = {
params: resend.Domains.UpdateParams = {
"id": "479e3145-dd38-476b-932c-529ceb705947",
"open_tracking": True,
"click_tracking": True,
Expand Down

0 comments on commit 588b15e

Please sign in to comment.