-
Notifications
You must be signed in to change notification settings - Fork 7
Fix issue #19: Add ContactFieldsApi, related models, tests, examples #35
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,49 @@ | ||
from typing import Optional | ||
|
||
import mailtrap as mt | ||
from mailtrap.models.common import DeletedObject | ||
from mailtrap.models.contacts import ContactField | ||
|
||
API_TOKEN = "YOU_API_TOKEN" | ||
ACCOUNT_ID = "YOU_ACCOUNT_ID" | ||
|
||
client = mt.MailtrapClient(token=API_TOKEN, account_id=ACCOUNT_ID) | ||
contact_fields_api = client.contacts_api.contact_fields | ||
|
||
|
||
def create_contact_field( | ||
name: str, | ||
data_type: str, | ||
merge_tag: str, | ||
) -> ContactField: | ||
params = mt.CreateContactFieldParams( | ||
name=name, | ||
data_type=data_type, | ||
merge_tag=merge_tag, | ||
) | ||
return contact_fields_api.create(params) | ||
|
||
|
||
def update_contact_field( | ||
contact_field_id: int, | ||
name: Optional[str] = None, | ||
merge_tag: Optional[str] = None, | ||
) -> ContactField: | ||
params = mt.UpdateContactFieldParams(name=name, merge_tag=merge_tag) | ||
return contact_fields_api.update(contact_field_id, params) | ||
|
||
|
||
def list_contact_fields() -> list[ContactField]: | ||
return contact_fields_api.get_list() | ||
|
||
|
||
def get_contact_field(contact_field_id: int) -> ContactField: | ||
return contact_fields_api.get_by_id(contact_field_id) | ||
|
||
|
||
def delete_contact_field(contact_field_id: int) -> DeletedObject: | ||
return contact_fields_api.delete(contact_field_id) | ||
|
||
|
||
if __name__ == "__main__": | ||
print(list_contact_fields()) |
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,12 @@ | ||
from mailtrap.api.resources.contact_fields import ContactFieldsApi | ||
from mailtrap.http import HttpClient | ||
|
||
|
||
class ContactsBaseApi: | ||
def __init__(self, client: HttpClient, account_id: str) -> None: | ||
self._account_id = account_id | ||
self._client = client | ||
|
||
@property | ||
def contact_fields(self) -> ContactFieldsApi: | ||
return ContactFieldsApi(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from typing import Optional | ||
|
||
from mailtrap.http import HttpClient | ||
from mailtrap.models.common import DeletedObject | ||
from mailtrap.models.contacts import ContactField | ||
from mailtrap.models.contacts import CreateContactFieldParams | ||
from mailtrap.models.contacts import UpdateContactFieldParams | ||
|
||
|
||
class ContactFieldsApi: | ||
def __init__(self, client: HttpClient, account_id: str) -> None: | ||
self._account_id = account_id | ||
self._client = client | ||
|
||
def get_list(self) -> list[ContactField]: | ||
response = self._client.get(self._api_path()) | ||
return [ContactField(**field) for field in response] | ||
|
||
def get_by_id(self, field_id: int) -> ContactField: | ||
response = self._client.get( | ||
self._api_path(field_id), | ||
) | ||
return ContactField(**response) | ||
|
||
def create(self, field_params: CreateContactFieldParams) -> ContactField: | ||
response = self._client.post( | ||
self._api_path(), | ||
json=field_params.api_data, | ||
) | ||
return ContactField(**response) | ||
|
||
def update( | ||
self, field_id: int, field_params: UpdateContactFieldParams | ||
) -> ContactField: | ||
response = self._client.patch( | ||
self._api_path(field_id), | ||
json=field_params.api_data, | ||
) | ||
return ContactField(**response) | ||
|
||
def delete(self, field_id: int) -> DeletedObject: | ||
self._client.delete(self._api_path(field_id)) | ||
return DeletedObject(field_id) | ||
|
||
def _api_path(self, field_id: Optional[int] = None) -> str: | ||
path = f"/api/accounts/{self._account_id}/contacts/fields" | ||
if field_id: | ||
return f"{path}/{field_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
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,30 @@ | ||
from typing import Optional | ||
|
||
from pydantic.dataclasses import dataclass | ||
|
||
from mailtrap.models.common import RequestParams | ||
|
||
|
||
@dataclass | ||
class CreateContactFieldParams(RequestParams): | ||
name: str | ||
data_type: str | ||
merge_tag: str | ||
|
||
|
||
@dataclass | ||
class UpdateContactFieldParams(RequestParams): | ||
name: Optional[str] = None | ||
merge_tag: Optional[str] = None | ||
|
||
def __post_init__(self) -> None: | ||
if all(value is None for value in [self.name, self.merge_tag]): | ||
raise ValueError("At least one field must be provided for update action") | ||
|
||
|
||
@dataclass | ||
class ContactField: | ||
id: int | ||
name: str | ||
data_type: str | ||
merge_tag: str |
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.