Skip to content

Commit

Permalink
refactor: many tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
drish committed Apr 17, 2024
1 parent 9b98802 commit 589d275
Show file tree
Hide file tree
Showing 27 changed files with 158 additions and 140 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml → .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ name: tests
on: [push, pull_request]

jobs:
lint:
name: Lint
ci:
name: Lint, Pyroma, Mypy
runs-on: ubuntu-latest
strategy:
matrix:
Expand Down
6 changes: 3 additions & 3 deletions examples/api_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"name": "example.com",
}

key = resend.ApiKeys.create(params = create_params)
key = resend.ApiKeys.create(params=create_params)
print("Created new api key")
print(f'Key id: {key.id} and token: {key.token}')
print(f"Key id: {key.id} and token: {key.token}")

keys = resend.ApiKeys.list()
for key in keys:
Expand All @@ -20,4 +20,4 @@
print(key.created_at)

if len(keys) > 0:
resend.ApiKeys.remove(api_key_id= keys[0].id)
resend.ApiKeys.remove(api_key_id=keys[0].id)
10 changes: 5 additions & 5 deletions examples/audiences.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
"name": "New Audience from Python SDK",
}
audience = resend.Audiences.create(create_params)
print(f'Created audience: {audience.id}')
print(f'{audience.name} created')
print(f"Created audience: {audience.id}")
print(f"{audience.name} created")

aud = resend.Audiences.get(audience.id)
print('Retrieved audience:', aud.id, aud.name, aud.created_at)
print("Retrieved audience:", aud.id, aud.name, aud.created_at)

audiences = resend.Audiences.list()
print('List of audiences:', [a.id for a in audiences])
print("List of audiences:", [a.id for a in audiences])

rmed = resend.Audiences.remove(id=audience.id)
print(f'Deleted audience: {rmed.id} {rmed.deleted}')
print(f"Deleted audience: {rmed.id} {rmed.deleted}")
3 changes: 2 additions & 1 deletion examples/batch_email_send.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from typing import List

import resend

if not os.environ["RESEND_API_KEY"]:
Expand All @@ -23,4 +24,4 @@

emails = resend.Batch.send(params)
for email in emails:
print(f'Email sent with id: {email.id}')
print(f"Email sent with id: {email.id}")
8 changes: 5 additions & 3 deletions examples/contacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
}

contact = resend.Contacts.create(create_params)
print(f'Created contact with ID: {contact.id}')
print(f"Created contact with ID: {contact.id}")

update_params: resend.Contacts.UpdateParams = {
"audience_id": audience_id,
Expand All @@ -40,11 +40,13 @@
contacts = resend.Contacts.list(audience_id=audience_id)
print("List of contacts")
for contact in contacts:
print(f'ID: {contact.id}, Email: {contact.email}, First Name: {contact.first_name}, Last Name: {contact.last_name}, Created At: {contact.created_at}, Unsubscribed: {contact.unsubscribed}')
print(
f"ID: {contact.id}, Email: {contact.email}, First Name: {contact.first_name}, Last Name: {contact.last_name}, Created At: {contact.created_at}, Unsubscribed: {contact.unsubscribed}"
)

# remove by email
# rmed = resend.Contacts.remove(audience_id=audience_id, email=cont.email)

# remove by id
rmed = resend.Contacts.remove(audience_id=audience_id, id=cont.id)
print(f'Removed contact - ID: {rmed.id} Deleted: {rmed.deleted}')
print(f"Removed contact - ID: {rmed.id} Deleted: {rmed.deleted}")
10 changes: 5 additions & 5 deletions examples/domains.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"name": "example.com",
"region": "us-east-1",
}
domain = resend.Domains.create(params = create_params)
print(f'Crated domain {domain.name} with id {domain.id}')
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__)
Expand All @@ -25,7 +25,7 @@
}

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

domains = resend.Domains.list()
if not domains:
Expand All @@ -40,5 +40,5 @@
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)
print(f"Verified domain: {domain.id}")
print(domain.id)
4 changes: 2 additions & 2 deletions examples/simple_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
}

email = resend.Emails.send(params)
print(f'Email sent with id: {email.id}')
print(f"Email sent with id: {email.id}")

email_resp = resend.Emails.get(email_id=email.id)
print(f'Retrieved email: {email_resp.id}')
print(f"Retrieved email: {email_resp.id}")
print(email_resp.__dict__)
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
requests==2.31.0
requests==2.31.0
typing_extensions
4 changes: 2 additions & 2 deletions resend/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os

from .api_keys._api_keys import ApiKeys
from .audiences._audiences import Audiences
from .contacts._contacts import Contacts
from .domains._domains import Domains
from .emails._emails import Emails
from .audiences._audiences import Audiences
from .emails._batch import Batch
from .emails._emails import Emails
from .request import Request
from .version import get_version

Expand Down
2 changes: 1 addition & 1 deletion resend/api_keys/_api_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ def new_from_request(val) -> "ApiKey":
token=val["token"] if "token" in val else None,
name=val["name"] if "name" in val else None,
created_at=val["created_at"] if "created_at" in val else None,
)
)
9 changes: 4 additions & 5 deletions resend/api_keys/_api_keys.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from typing import List, Any, Dict, cast
from typing import Any, Dict, List, cast

from typing_extensions import TypedDict, NotRequired
from typing_extensions import NotRequired, TypedDict

from resend import request
from resend.api_keys._api_key import ApiKey


class ApiKeys:

class CreateParams(TypedDict):
name: str
"""
Expand Down Expand Up @@ -46,7 +45,7 @@ def list(cls) -> List[ApiKey]:
"""
path = "/api-keys"
resp = request.Request(path=path, params={}, verb="get").perform()
return [ApiKey.new_from_request(val) for val in resp['data']]
return [ApiKey.new_from_request(val) for val in resp["data"]]

@classmethod
def remove(cls, api_key_id: str = "") -> None:
Expand All @@ -58,4 +57,4 @@ def remove(cls, api_key_id: str = "") -> None:

# This would raise if failed
request.Request(path=path, params={}, verb="delete").perform()
return None
return None
17 changes: 10 additions & 7 deletions resend/audiences/_audiences.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from typing import List, Any, Dict, cast
from typing import Any, Dict, List, cast

from typing_extensions import TypedDict, NotRequired
from typing_extensions import TypedDict

from resend import request

from ._audience import Audience

class Audiences:

class Audiences:
class CreateParams(TypedDict):
name: str
"""
Expand All @@ -22,9 +23,7 @@ def create(cls, params: CreateParams) -> Audience:
path = "/audiences"
return Audience.new_from_request(
request.Request(
path=path,
params=cast(Dict[Any, Any], params),
verb="post"
path=path, params=cast(Dict[Any, Any], params), verb="post"
).perform()
)

Expand All @@ -36,7 +35,11 @@ def list(cls) -> List[Audience]:
"""
path = "/audiences/"
resp = request.Request(path=path, params={}, verb="get").perform()
return [Audience.new_from_request(aud) for aud in resp["data"]] if "data" in resp else []
return (
[Audience.new_from_request(aud) for aud in resp["data"]]
if "data" in resp
else []
)

@classmethod
def get(cls, id: str) -> Audience:
Expand Down
15 changes: 11 additions & 4 deletions resend/contacts/_contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ class Contact:
Wether the contact is deleted or not.
"""

def __init__(self, id: str, email: str,
first_name: str, last_name: str, created_at: str,
unsubscribed: bool, deleted: bool):
def __init__(
self,
id: str,
email: str,
first_name: str,
last_name: str,
created_at: str,
unsubscribed: bool,
deleted: bool,
):
self.id = id
self.email = email
self.first_name = first_name
Expand All @@ -48,6 +55,6 @@ def new_from_request(val) -> "Contact":
last_name=val["last_name"] if "last_name" in val else "",
created_at=val["created_at"] if "created_at" in val else "",
unsubscribed=val["unsubscribed"] if "unsubscribed" in val else False,
deleted=val["deleted"] if "deleted" in val else False
deleted=val["deleted"] if "deleted" in val else False,
)
return contact
24 changes: 13 additions & 11 deletions resend/contacts/_contacts.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from typing import Dict, Any, cast, List
from typing_extensions import TypedDict, NotRequired
from typing import Any, Dict, List, cast

from typing_extensions import NotRequired, TypedDict

from resend import request

from ._contact import Contact

class Contacts:

class Contacts:
class CreateParams(TypedDict):
audience_id: str
"""
Expand Down Expand Up @@ -63,9 +65,7 @@ def create(cls, params: CreateParams) -> Contact:
path = f"/audiences/{params['audience_id']}/contacts"
return Contact.new_from_request(
request.Request(
path=path,
params=cast(Dict[Any, Any], params),
verb="post"
path=path, params=cast(Dict[Any, Any], params), verb="post"
).perform()
)

Expand All @@ -78,9 +78,7 @@ def update(cls, params: UpdateParams) -> Contact:
path = f"/audiences/{params['audience_id']}/contacts/{params['id']}"
return Contact.new_from_request(
request.Request(
path=path,
params=cast(Dict[Any, Any], params),
verb="patch"
path=path, params=cast(Dict[Any, Any], params), verb="patch"
).perform()
)

Expand All @@ -92,7 +90,11 @@ def list(cls, audience_id: str) -> List[Contact]:
"""
path = f"/audiences/{audience_id}/contacts"
resp = request.Request(path=path, params={}, verb="get").perform()
return [Contact.new_from_request(contact) for contact in resp["data"]] if "data" in resp else []
return (
[Contact.new_from_request(contact) for contact in resp["data"]]
if "data" in resp
else []
)

@classmethod
def get(cls, id, audience_id: str) -> Contact:
Expand All @@ -118,4 +120,4 @@ def remove(cls, audience_id: str, id: str = "", email: str = "") -> Contact:

return Contact.new_from_request(
request.Request(path=path, params={}, verb="delete").perform()
)
)
9 changes: 5 additions & 4 deletions resend/domains/_domain.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import List

from resend.domains._record import Record


Expand Down Expand Up @@ -32,9 +33,7 @@ class Domain:
Wether the domain is deleted or not
"""

def __init__(
self, id, name, region, created_at,
status, records, deleted=False):
def __init__(self, id, name, region, created_at, status, records, deleted=False):
self.id = id
self.name = name
self.created_at = created_at
Expand All @@ -51,7 +50,9 @@ def new_from_request(val) -> "Domain":
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,
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
15 changes: 8 additions & 7 deletions resend/domains/_domains.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from typing import List, Any, Dict, cast
from typing import Any, Dict, List, cast

from typing_extensions import NotRequired, TypedDict

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

class Domains:

class Domains:
class UpdateParams(TypedDict):
id: str
"""
Expand Down Expand Up @@ -53,8 +54,8 @@ def update(cls, params: UpdateParams) -> Domain:
path = f"/domains/{params['id']}"
return Domain.new_from_request(
request.Request(
path=path, params=cast(Dict[Any, Any], params), verb="patch")
.perform()
path=path, params=cast(Dict[Any, Any], params), verb="patch"
).perform()
)

@classmethod
Expand All @@ -76,7 +77,7 @@ def list(cls) -> List[Domain]:
"""
path = "/domains"
resp = request.Request(path=path, params={}, verb="get").perform()
return [Domain.new_from_request(val) for val in resp['data']]
return [Domain.new_from_request(val) for val in resp["data"]]

@classmethod
def remove(cls, domain_id: str = "") -> Domain:
Expand All @@ -90,7 +91,7 @@ def remove(cls, domain_id: str = "") -> Domain:
)

@classmethod
def verify(cls, domain_id: str = "" ) -> Domain:
def verify(cls, domain_id: str = "") -> Domain:
"""
Verify an existing domain.
see more: https://resend.com/docs/api-reference/domains/verify-domain
Expand Down
Loading

0 comments on commit 589d275

Please sign in to comment.