-
Notifications
You must be signed in to change notification settings - Fork 8
Fix issue #21: Add ContactsApi, related models, tests, examples #37
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| from typing import Optional | ||
| from typing import Union | ||
|
|
||
| import mailtrap as mt | ||
| from mailtrap.models.common import DeletedObject | ||
| from mailtrap.models.contacts import Contact | ||
|
|
||
| API_TOKEN = "YOU_API_TOKEN" | ||
| ACCOUNT_ID = "YOU_ACCOUNT_ID" | ||
|
|
||
| client = mt.MailtrapClient(token=API_TOKEN, account_id=ACCOUNT_ID) | ||
| contacts_api = client.contacts_api.contacts | ||
|
|
||
|
|
||
| def create_contact( | ||
| email: str, | ||
| fields: Optional[dict[str, Union[str, int, float, bool]]] = None, | ||
| list_ids: Optional[list[int]] = None, | ||
| ) -> Contact: | ||
| params = mt.CreateContactParams( | ||
| email=email, | ||
| fields=fields, | ||
| list_ids=list_ids, | ||
| ) | ||
| return contacts_api.create(params) | ||
|
|
||
|
|
||
| def update_contact( | ||
| contact_id_or_email: str, | ||
| new_email: Optional[str] = None, | ||
| fields: Optional[dict[str, Union[str, int, float, bool]]] = None, | ||
| list_ids_included: Optional[list[int]] = None, | ||
| list_ids_excluded: Optional[list[int]] = None, | ||
| unsubscribed: Optional[bool] = None, | ||
| ) -> Contact: | ||
| params = mt.UpdateContactParams( | ||
| email=new_email, | ||
| fields=fields, | ||
| list_ids_included=list_ids_included, | ||
| list_ids_excluded=list_ids_excluded, | ||
| unsubscribed=unsubscribed, | ||
| ) | ||
| return contacts_api.update(contact_id_or_email, params) | ||
|
|
||
|
|
||
| def get_contact(contact_id_or_email: str) -> Contact: | ||
| return contacts_api.get_by_id(contact_id_or_email) | ||
|
|
||
|
|
||
| def delete_contact(contact_id_or_email: str) -> DeletedObject: | ||
| return contacts_api.delete(contact_id_or_email) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| created_contact = create_contact( | ||
| email="[email protected]", | ||
| fields={ | ||
| "first_name": "Test", | ||
| "last_name": "Test", | ||
| }, | ||
| ) | ||
| print(created_contact) | ||
| updated_contact = update_contact( | ||
| created_contact.id, | ||
| fields={ | ||
| "first_name": "John", | ||
| "last_name": "Doe", | ||
| }, | ||
| ) | ||
| print(updated_contact) | ||
| deleted_contact = delete_contact(updated_contact.id) | ||
| print(deleted_contact) |
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
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,45 @@ | ||
| from typing import Optional | ||
| from urllib.parse import quote | ||
|
|
||
| from mailtrap.http import HttpClient | ||
| from mailtrap.models.common import DeletedObject | ||
| from mailtrap.models.contacts import Contact | ||
| from mailtrap.models.contacts import ContactResponse | ||
| from mailtrap.models.contacts import CreateContactParams | ||
| from mailtrap.models.contacts import UpdateContactParams | ||
|
|
||
|
|
||
| class ContactsApi: | ||
| def __init__(self, client: HttpClient, account_id: str) -> None: | ||
| self._account_id = account_id | ||
| self._client = client | ||
|
|
||
| def get_by_id(self, contact_id_or_email: str) -> Contact: | ||
| response = self._client.get(self._api_path(contact_id_or_email)) | ||
| return ContactResponse(**response).data | ||
|
|
||
| def create(self, contact_params: CreateContactParams) -> Contact: | ||
| response = self._client.post( | ||
| self._api_path(), | ||
| json={"contact": contact_params.api_data}, | ||
| ) | ||
| return ContactResponse(**response).data | ||
|
|
||
| def update( | ||
| self, contact_id_or_email: str, contact_params: UpdateContactParams | ||
| ) -> Contact: | ||
| response = self._client.patch( | ||
| self._api_path(contact_id_or_email), | ||
| json={"contact": contact_params.api_data}, | ||
| ) | ||
| return ContactResponse(**response).data | ||
|
|
||
| def delete(self, contact_id_or_email: str) -> DeletedObject: | ||
| self._client.delete(self._api_path(contact_id_or_email)) | ||
| return DeletedObject(contact_id_or_email) | ||
|
|
||
| def _api_path(self, contact_id_or_email: Optional[str] = None) -> str: | ||
| path = f"/api/accounts/{self._account_id}/contacts" | ||
| if contact_id_or_email is not None: | ||
| return f"{path}/{quote(contact_id_or_email, safe='')}" | ||
| 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
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
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
Oops, something went wrong.
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.