Skip to content

Commit

Permalink
feat: Type hinting (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
drish authored Apr 19, 2024
1 parent 544b451 commit 8353425
Show file tree
Hide file tree
Showing 49 changed files with 1,083 additions and 426 deletions.
6 changes: 2 additions & 4 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
lint-mypy:
name: Lint, Mypy
runs-on: ubuntu-latest
strategy:
matrix:
Expand All @@ -18,8 +18,6 @@ jobs:
run: pip install tox
- name: Check code quality with flake8
run: tox -e lint
- name: Check package metadata with Pyroma
run: tox -e pyroma
- name: Check static typing with MyPy
run: tox -e mypy
tests:
Expand Down
20 changes: 12 additions & 8 deletions examples/api_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
if not os.environ["RESEND_API_KEY"]:
raise EnvironmentError("RESEND_API_KEY is missing")

create_params: resend.ApiKeys.CreateParams = {
"name": "example.com",
}

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

keys = resend.ApiKeys.list()
print(keys)
for key in keys:
print(key.id)
print(key.name)
print(key.created_at)

resend.ApiKeys.remove(key["id"])
if len(keys) > 0:
resend.ApiKeys.remove(api_key_id=keys[0].id)
22 changes: 11 additions & 11 deletions examples/audiences.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
raise EnvironmentError("RESEND_API_KEY is missing")


audience = resend.Audiences.create(
{
"name": "New Audience from Python SDK",
}
)
print(audience)
create_params: resend.Audiences.CreateParams = {
"name": "New Audience from Python SDK",
}
audience = resend.Audiences.create(create_params)
print(f"Created audience: {audience.id}")
print(f"{audience.name} created")

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

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

rmed = resend.Audiences.remove(audience["id"])
print(rmed)
rmed = resend.Audiences.remove(id=audience.id)
print(f"Deleted audience: {rmed.id} {rmed.deleted}")
14 changes: 8 additions & 6 deletions examples/batch_email_send.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import os
from typing import List

import resend

if not os.environ["RESEND_API_KEY"]:
raise EnvironmentError("RESEND_API_KEY is missing")


params = [
params: List[resend.Emails.SendParams] = [
{
"from": "[email protected]",
"to": ["[email protected]"],
"sender": "[email protected]",
"to": ["[email protected]"],
"subject": "hey",
"html": "<strong>hello, world!</strong>",
},
{
"from": "[email protected]",
"to": ["[email protected]"],
"sender": "[email protected]",
"to": ["[email protected]"],
"subject": "hello",
"html": "<strong>hello, world!</strong>",
},
]

emails = resend.Batch.send(params)
print(emails)
for email in emails:
print(f"Email sent with id: {email.id}")
53 changes: 30 additions & 23 deletions examples/contacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,48 @@
if not os.environ["RESEND_API_KEY"]:
raise EnvironmentError("RESEND_API_KEY is missing")

# replace with some audience id
audience_id: str = "ca4e37c5-a82a-4199-a3b8-bf912a6472aa"

audience_id = "78b8d3bc-a55a-45a3-aee6-6ec0a5e13d7e"

contact = resend.Contacts.create(
{
"audience_id": audience_id,
"email": "[email protected]",
"first_name": "Steve",
"last_name": "Wozniak",
"unsubscribed": True,
}
)
print("created contact !")
print(contact)

update_params = {
create_params: resend.Contacts.CreateParams = {
"audience_id": audience_id,
"id": contact["id"],
"last_name": "Updated",
"email": "[email protected]",
"first_name": "Steve",
"last_name": "Wozniak",
"unsubscribed": False,
}

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

update_params: resend.Contacts.UpdateParams = {
"audience_id": audience_id,
"id": contact.id,
"unsubscribed": False,
"first_name": "Steve1",
}

updated = resend.Contacts.update(update_params)
print("updated contact !")
print(updated)

cont = resend.Contacts.get(audience_id=audience_id, id=contact["id"])
print(cont)
cont = resend.Contacts.get(audience_id=audience_id, id=contact.id)
print("Retrieved contact")
print(cont.id)
print(cont.email)
print(cont.first_name)
print(cont.last_name)

contacts = resend.Contacts.list(audience_id=audience_id)
print(contacts)
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}"
)

# remove by email
rmed = resend.Contacts.remove(audience_id=audience_id, email="[email protected]")
# rmed = resend.Contacts.remove(audience_id=audience_id, email=cont.email)

# remove by id
# rmed = resend.Contacts.remove(audience_id=audience_id, id=contact["id"])
print(rmed)
rmed = resend.Contacts.remove(audience_id=audience_id, id=cont.id)
print(f"Removed contact - ID: {rmed.id} Deleted: {rmed.deleted}")
45 changes: 27 additions & 18 deletions examples/domains.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,39 @@
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"],
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.UpdateParams = {
"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)
14 changes: 7 additions & 7 deletions examples/simple_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
if not os.environ["RESEND_API_KEY"]:
raise EnvironmentError("RESEND_API_KEY is missing")


params = {
"from": "[email protected]",
"to": ["[email protected]"],
params: resend.Emails.SendParams = {
"sender": "[email protected]",
"to": ["[email protected]"],
"subject": "hi",
"html": "<strong>hello, world!</strong>",
"reply_to": "[email protected]",
Expand All @@ -21,7 +20,8 @@
}

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

email_resp = resend.Emails.get(email_id=email["id"])
print(email_resp)
email_resp = resend.Emails.get(email_id=email.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
16 changes: 7 additions & 9 deletions resend/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import os

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

Expand All @@ -15,11 +14,10 @@
api_url = os.environ.get("RESEND_API_URL", "https://api.resend.com")

# API resources
from .emails import Emails # noqa
from .emails._emails import Emails # noqa

__all__ = [
"get_version",
"Resend",
"Request",
"Emails",
"ApiKeys",
Expand Down
43 changes: 0 additions & 43 deletions resend/api.py

This file was deleted.

25 changes: 0 additions & 25 deletions resend/api_keys.py

This file was deleted.

Empty file added resend/api_keys/__init__.py
Empty file.
Loading

0 comments on commit 8353425

Please sign in to comment.