Skip to content

Commit

Permalink
feat: annotate domains module
Browse files Browse the repository at this point in the history
  • Loading branch information
drish committed Apr 14, 2024
1 parent 3ab693c commit c49682a
Show file tree
Hide file tree
Showing 8 changed files with 281 additions and 49 deletions.
44 changes: 26 additions & 18 deletions examples/domains.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,38 @@
raise EnvironmentError("RESEND_API_KEY is missing")


domain = resend.Domains.create(
{
"name": "domain.io",
}
)
print(domain)

retrieved = resend.Domains.get(domain_id=domain["id"])
print(retrieved)

update_params = {
"id": domain["id"],
domain = resend.Domains.create({
"name": "example.com",
"region": "us-east-1",
})
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 = {
"id": domain.id,
"open_tracking": True,
"click_tracking": True,
}

updated = resend.Domains.update(update_params)
print(updated)
updated_domain = resend.Domains.update(update_params)
print(f'Updated domain: {updated_domain.id}')

domains = resend.Domains.list()
print(domains)
if not domains:
print("No domains found")
for domain in domains:
print(domain.__dict__)

resend.Domains.verify(domain_id=domain["id"])
resend.Domains.verify(domain_id=domain.id)
print("domain verified")

resend.Domains.remove(domain_id=domain["id"])
print("domain removed")
domain = resend.Domains.remove(domain_id=domain.id)
print(f"domain id: {domain.id} deleted: {domain.deleted}")

domain = resend.Domains.verify(domain_id=domain.id)
print(f'Verified domain: {domain.id}')
print(domain.id)
2 changes: 1 addition & 1 deletion resend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .audiences import Audiences
from .batch import Batch
from .contacts import Contacts
from .domains import Domains
from .domains._domains import Domains
from .emails import Emails
from .request import Request
from .version import get_version
Expand Down
3 changes: 0 additions & 3 deletions resend/api_keys/_api_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@


class ApiKeys:
"""
Api Keys module
"""

class CreateApiKeyRequestParams(TypedDict):
name: str
Expand Down
Empty file added resend/domains/__init__.py
Empty file.
57 changes: 57 additions & 0 deletions resend/domains/_domain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from typing import List
from resend.domains._record import Record


class Domain:
id: str
"""
The domain ID
"""
name: str
"""
The domain name
"""
created_at: str
"""
When domain was created
"""
status: str
"""
Status of the domain: not_started, etc..
"""
region: str
"""
The region where emails will be sent from. Possible values: us-east-1' | 'eu-west-1' | 'sa-east-1' | 'ap-northeast-1'
"""
records: List[Record]
"""
The list of domain records
"""
deleted: bool
"""
Wether the domain is deleted or not
"""

def __init__(
self, id, name, region, created_at,
status, records, deleted=False):
self.id = id
self.name = name
self.created_at = created_at
self.status = status
self.region = region
self.records = records
self.deleted = deleted

@staticmethod
def new_from_request(val) -> "Domain":
domain = Domain(
id=val["id"] if "id" in val else None,
name=val["name"] if "name" in val else None,
region=val["region"] if "region" in val else None,
created_at=val["created_at"] if "created_at" in val else None,
status=val["status"] if "status" in val else None,
records = [Record.new_from_request(record) for record in val["records"]] if "records" in val else None,
deleted=val["deleted"] if "deleted" in val else None,
)
return domain
99 changes: 99 additions & 0 deletions resend/domains/_domains.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
from typing import List, Any, Dict, Optional, cast

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

class Domains:

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

class CreateDomainRequestParams(TypedDict):
name: str
"""
The domain name.
"""
region: 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:
"""
Create a domain through the Resend Email API.
see more: https://resend.com/docs/api-reference/domains/create-domain
"""
path = "/domains"
return Domain.new_from_request(
request.Request(
path=path, params=cast(Dict[Any, Any], params), verb="post"
).perform()
)

@classmethod
def update(cls, params: UpdateDomainRequestParams = {}) -> 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()
)

@classmethod
def get(cls, domain_id: str = "") -> Domain:
"""
Retrieve a single domain for the authenticated user.
see more: https://resend.com/docs/api-reference/domains/get-domain
"""
path = f"/domains/{domain_id}"
return Domain.new_from_request(
request.Request(path=path, params={}, verb="get").perform()
)

@classmethod
def list(cls) -> List[Domain]:
"""
Retrieve a list of domains for the authenticated user.
see more: https://resend.com/docs/api-reference/domains/list-domains
"""
path = "/domains"
resp = request.Request(path=path, params={}, verb="get").perform()
return [Domain.new_from_request(val) for val in resp['data']]

@classmethod
def remove(cls, domain_id: str = "") -> Domain:
"""
Remove an existing domain.
see more: https://resend.com/docs/api-reference/domains/remove-domain
"""
path = f"/domains/{domain_id}"
return Domain.new_from_request(
request.Request(path=path, params={}, verb="delete").perform()
)

@classmethod
def verify(cls, domain_id: str = "" ) -> Domain:
"""
Verify an existing domain.
see more: https://resend.com/docs/api-reference/domains/verify-domain
"""
path = f"/domains/{domain_id}/verify"
return Domain.new_from_request(
request.Request(path=path, params={}, verb="post").perform()
)
52 changes: 52 additions & 0 deletions resend/domains/_record.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class Record:
record: str
"""
The domain record type, ie: SPF.
"""
name: str
"""
The domain record name.
"""
type: str
"""
The domain record type, ie: MX.
"""
ttl: str
"""
The domain record time to live.
"""
status: str
"""
The domain record status: not_started, etc..
"""
value: str
"""
The domain record value.
"""
priority: int
"""
The domain record priority.
"""

def __init__(
self, record, name, type, ttl,
status, value, priority):
self.record = record
self.name = name
self.type = type
self.ttl = ttl
self.status = status
self.value = value
self.priority = priority

@staticmethod
def new_from_request(val) -> "Record":
return Record(
record=val["record"] if "record" in val else None,
name=val["name"] if "name" in val else None,
type=val["type"] if "type" in val else None,
ttl=val["ttl"] if "ttl" in val else None,
status=val["status"] if "status" in val else None,
value=val["value"] if "value" in val else None,
priority=val["priority"] if "priority" in val else None,
)
Loading

0 comments on commit c49682a

Please sign in to comment.