-
Notifications
You must be signed in to change notification settings - Fork 8
Fix issue #54: Add SendingDomainsApi, related models, tests, examples #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import mailtrap as mt | ||
| from mailtrap.models.common import DeletedObject | ||
| from mailtrap.models.sending_domains import SendingDomain | ||
| from mailtrap.models.sending_domains import SendSetupInstructionsResponse | ||
|
|
||
| API_TOKEN = "YOUR_API_TOKEN" | ||
| ACCOUNT_ID = "YOUR_ACCOUNT_ID" | ||
|
|
||
| client = mt.MailtrapClient(token=API_TOKEN, account_id=ACCOUNT_ID) | ||
| sending_domains_api = client.sending_domains_api.sending_domains | ||
|
|
||
|
|
||
| def list_sending_domains() -> list[SendingDomain]: | ||
| return sending_domains_api.get_list() | ||
|
|
||
|
|
||
| def get_sending_domain(domain_id: int) -> SendingDomain: | ||
| return sending_domains_api.get_by_id(domain_id) | ||
|
|
||
|
|
||
| def create_sending_domain(domain_name: str) -> SendingDomain: | ||
| params = mt.CreateSendingDomainParams(domain_name=domain_name) | ||
| return sending_domains_api.create(params) | ||
|
|
||
|
|
||
| def delete_sending_domain(domain_id: int) -> DeletedObject: | ||
| return sending_domains_api.delete(domain_id) | ||
|
|
||
|
|
||
| def send_setup_instructions(domain_id: int, email: str) -> SendSetupInstructionsResponse: | ||
| params = mt.SendSetupInstructionsParams(email=email) | ||
| return sending_domains_api.send_setup_instructions(domain_id, params) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| new_domain = create_sending_domain("example.com") | ||
| print(new_domain) | ||
|
|
||
| domains = list_sending_domains() | ||
| print(domains) | ||
|
|
||
| domain = get_sending_domain(new_domain.id) | ||
| print(domain) | ||
|
|
||
| response = send_setup_instructions(new_domain.id, "example@mail.com") | ||
| print(response) | ||
|
|
||
| deleted_domain = delete_sending_domain(new_domain.id) | ||
| print(deleted_domain) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| from typing import Optional | ||
|
|
||
| from mailtrap.http import HttpClient | ||
| from mailtrap.models.common import DeletedObject | ||
| from mailtrap.models.sending_domains import CreateSendingDomainParams | ||
| from mailtrap.models.sending_domains import SendingDomain | ||
| from mailtrap.models.sending_domains import SendSetupInstructionsParams | ||
| from mailtrap.models.sending_domains import SendSetupInstructionsResponse | ||
|
|
||
|
|
||
| class SendingDomainsApi: | ||
| def __init__(self, client: HttpClient, account_id: str) -> None: | ||
| self._account_id = account_id | ||
| self._client = client | ||
|
|
||
| def get_list(self) -> list[SendingDomain]: | ||
| """ | ||
| Get sending domains and their statuses. | ||
| """ | ||
| response = self._client.get(self._api_path()) | ||
| domains = response.get("data", []) | ||
| return [SendingDomain(**domain) for domain in domains] | ||
|
|
||
| def get_by_id(self, sending_domain_id: int) -> SendingDomain: | ||
| """ | ||
| Get domain data and its status. | ||
| """ | ||
| response = self._client.get(self._api_path(sending_domain_id)) | ||
| return SendingDomain(**response) | ||
|
|
||
| def create(self, domain_params: CreateSendingDomainParams) -> SendingDomain: | ||
| """ | ||
| Create a sending domain. To later check the status of the newly created domain, | ||
| review the compliance_status and dns_verified fields in the response | ||
| of the Get domain by ID or Get sending domains endpoints. | ||
| """ | ||
| response = self._client.post( | ||
| self._api_path(), json={"sending_domain": domain_params.api_data} | ||
| ) | ||
| return SendingDomain(**response) | ||
|
|
||
| def delete(self, sending_domain_id: int) -> DeletedObject: | ||
| """ | ||
| Delete a sending domain. | ||
| """ | ||
| self._client.delete(self._api_path(sending_domain_id)) | ||
| return DeletedObject(id=sending_domain_id) | ||
|
|
||
| def send_setup_instructions( | ||
| self, | ||
| sending_domain_id: int, | ||
| instructions_params: SendSetupInstructionsParams, | ||
| ) -> SendSetupInstructionsResponse: | ||
| """ | ||
| Send sending domain setup instructions. | ||
| """ | ||
| self._client.post( | ||
| f"{self._api_path(sending_domain_id)}/send_setup_instructions", | ||
| json=instructions_params.api_data, | ||
| ) | ||
| return SendSetupInstructionsResponse( | ||
| message="Instructions email has been sent successfully" | ||
| ) | ||
|
|
||
| def _api_path(self, sending_domain_id: Optional[int] = None) -> str: | ||
| path = f"/api/accounts/{self._account_id}/sending_domains" | ||
| if sending_domain_id is not None: | ||
| path = f"{path}/{sending_domain_id}" | ||
| return path | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| from mailtrap.api.resources.sending_domains import SendingDomainsApi | ||
| from mailtrap.http import HttpClient | ||
|
|
||
|
|
||
| class SendingDomainsBaseApi: | ||
| def __init__(self, client: HttpClient, account_id: str) -> None: | ||
| self._account_id = account_id | ||
| self._client = client | ||
|
|
||
| @property | ||
| def sending_domains(self) -> SendingDomainsApi: | ||
| return SendingDomainsApi(account_id=self._account_id, client=self._client) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| from typing import Optional | ||
|
|
||
| from pydantic import Field | ||
| from pydantic.dataclasses import dataclass | ||
|
|
||
| from mailtrap.models.common import RequestParams | ||
|
|
||
|
|
||
| @dataclass | ||
| class SendingDomainPermissions: | ||
| can_read: bool | ||
| can_update: bool | ||
| can_destroy: bool | ||
|
|
||
|
|
||
| @dataclass | ||
| class DnsRecord: | ||
| key: str | ||
| domain: str | ||
| type: str | ||
| value: str | ||
| status: str | ||
| name: str | ||
|
|
||
|
|
||
| @dataclass | ||
| class SendingDomain: | ||
| id: int | ||
| domain_name: str | ||
| demo: bool | ||
| compliance_status: str | ||
| dns_verified: bool | ||
| open_tracking_enabled: bool | ||
| click_tracking_enabled: bool | ||
| auto_unsubscribe_link_enabled: bool | ||
| custom_domain_tracking_enabled: bool | ||
| health_alerts_enabled: bool | ||
| critical_alerts_enabled: bool | ||
| permissions: SendingDomainPermissions | ||
| alert_recipient_email: Optional[str] = None | ||
| dns_verified_at: Optional[str] = None | ||
| dns_records: list[DnsRecord] = Field(default_factory=list) | ||
|
|
||
|
|
||
| @dataclass | ||
| class CreateSendingDomainParams(RequestParams): | ||
| domain_name: str | ||
|
|
||
|
|
||
| @dataclass | ||
| class SendSetupInstructionsParams(RequestParams): | ||
| email: str | ||
|
|
||
|
|
||
| @dataclass | ||
| class SendSetupInstructionsResponse: | ||
| message: str |
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.