-
Notifications
You must be signed in to change notification settings - Fork 8
Fix issue #41: Add ContactExportsApi, related models, tests and examples #49
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,30 @@ | ||
| import mailtrap as mt | ||
| from mailtrap.models.contacts import ContactExportDetail | ||
|
|
||
| API_TOKEN = "YOUR_API_TOKEN" | ||
| ACCOUNT_ID = "YOUR_ACCOUNT_ID" | ||
|
|
||
| client = mt.MailtrapClient(token=API_TOKEN, account_id=ACCOUNT_ID) | ||
| contact_exports_api = client.contacts_api.contact_exports | ||
|
|
||
|
|
||
| def create_export_contacts( | ||
| contact_exports_params: mt.CreateContactExportParams, | ||
| ) -> ContactExportDetail: | ||
| return contact_exports_api.create(contact_exports_params=contact_exports_params) | ||
|
|
||
|
|
||
| def get_contact_export(export_id: int) -> ContactExportDetail: | ||
| return contact_exports_api.get_by_id(export_id) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| contact_export = create_export_contacts( | ||
| contact_exports_params=mt.CreateContactExportParams( | ||
| filters=[mt.ContactExportFilter(name="list_id", operator="equal", value=[10])] | ||
| ) | ||
| ) | ||
| print(contact_export) | ||
|
|
||
| contact_export = get_contact_export(contact_export.id) | ||
| print(contact_export) | ||
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,32 @@ | ||
| from typing import Optional | ||
|
|
||
| from mailtrap.http import HttpClient | ||
| from mailtrap.models.contacts import ContactExportDetail | ||
| from mailtrap.models.contacts import CreateContactExportParams | ||
|
|
||
|
|
||
| class ContactExportsApi: | ||
| def __init__(self, client: HttpClient, account_id: str) -> None: | ||
| self._account_id = account_id | ||
| self._client = client | ||
|
|
||
| def create( | ||
| self, contact_exports_params: CreateContactExportParams | ||
| ) -> ContactExportDetail: | ||
| """Create a new Contact Export""" | ||
| response = self._client.post( | ||
| self._api_path(), | ||
| json=contact_exports_params.api_data, | ||
| ) | ||
| return ContactExportDetail(**response) | ||
|
|
||
| def get_by_id(self, export_id: int) -> ContactExportDetail: | ||
| """Get Contact Export""" | ||
| response = self._client.get(self._api_path(export_id)) | ||
| return ContactExportDetail(**response) | ||
|
|
||
| def _api_path(self, export_id: Optional[int] = None) -> str: | ||
| path = f"/api/accounts/{self._account_id}/contacts/exports" | ||
| if export_id is not None: | ||
| return f"{path}/{export_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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,244 @@ | ||
| from typing import Any | ||
|
|
||
| import pytest | ||
| import responses | ||
|
|
||
| from mailtrap.api.resources.contact_exports import ContactExportsApi | ||
| from mailtrap.config import GENERAL_HOST | ||
| from mailtrap.exceptions import APIError | ||
| from mailtrap.http import HttpClient | ||
| from mailtrap.models.contacts import ContactExportDetail | ||
| from mailtrap.models.contacts import ContactExportFilter | ||
| from mailtrap.models.contacts import CreateContactExportParams | ||
| from tests import conftest | ||
|
|
||
| ACCOUNT_ID = "321" | ||
| EXPORT_ID = 1 | ||
| BASE_CONTACT_EXPORTS_URL = ( | ||
| f"https://{GENERAL_HOST}/api/accounts/{ACCOUNT_ID}/contacts/exports" | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def client() -> ContactExportsApi: | ||
| return ContactExportsApi(account_id=ACCOUNT_ID, client=HttpClient(GENERAL_HOST)) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def sample_contact_export_started_dict() -> dict[str, Any]: | ||
| return { | ||
| "id": EXPORT_ID, | ||
| "status": "started", | ||
| "created_at": "2021-01-01T00:00:00Z", | ||
| "updated_at": "2021-01-01T00:00:00Z", | ||
| "url": None, | ||
| } | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def sample_contact_export_finished_dict() -> dict[str, Any]: | ||
| return { | ||
| "id": EXPORT_ID, | ||
| "status": "finished", | ||
| "created_at": "2021-01-01T00:00:00Z", | ||
| "updated_at": "2021-01-01T00:00:00Z", | ||
| "url": "https://example.com/export.csv.gz", | ||
| } | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def sample_contact_export_filter() -> ContactExportFilter: | ||
| return ContactExportFilter(name="list_id", operator="in", value=[1, 2, 3]) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def sample_create_contact_export_params() -> CreateContactExportParams: | ||
| return CreateContactExportParams( | ||
| filters=[ContactExportFilter(name="list_id", operator="in", value=[1, 2, 3])] | ||
| ) | ||
|
|
||
|
|
||
| class TestContactExportsApi: | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "status_code,response_json,expected_error_message", | ||
| [ | ||
| ( | ||
| conftest.UNAUTHORIZED_STATUS_CODE, | ||
| conftest.UNAUTHORIZED_RESPONSE, | ||
| conftest.UNAUTHORIZED_ERROR_MESSAGE, | ||
| ), | ||
| ( | ||
| conftest.FORBIDDEN_STATUS_CODE, | ||
| conftest.FORBIDDEN_RESPONSE, | ||
| conftest.FORBIDDEN_ERROR_MESSAGE, | ||
| ), | ||
| ], | ||
| ) | ||
| @responses.activate | ||
| def test_create_should_raise_api_errors( | ||
| self, | ||
| client: ContactExportsApi, | ||
| status_code: int, | ||
| response_json: dict, | ||
| expected_error_message: str, | ||
| sample_create_contact_export_params: CreateContactExportParams, | ||
| ) -> None: | ||
| responses.post( | ||
| BASE_CONTACT_EXPORTS_URL, | ||
| status=status_code, | ||
| json=response_json, | ||
| ) | ||
|
|
||
| with pytest.raises(APIError) as exc_info: | ||
| client.create(sample_create_contact_export_params) | ||
|
|
||
| assert expected_error_message in str(exc_info.value) | ||
|
|
||
| @responses.activate | ||
| def test_create_should_return_contact_export_detail( | ||
| self, | ||
| client: ContactExportsApi, | ||
| sample_contact_export_started_dict: dict, | ||
| sample_create_contact_export_params: CreateContactExportParams, | ||
| ) -> None: | ||
| responses.post( | ||
| BASE_CONTACT_EXPORTS_URL, | ||
| json=sample_contact_export_started_dict, | ||
| status=201, | ||
| ) | ||
|
|
||
| result = client.create(sample_create_contact_export_params) | ||
|
|
||
| assert isinstance(result, ContactExportDetail) | ||
| assert result.id == EXPORT_ID | ||
| assert result.status == "started" | ||
| assert result.url is None | ||
| assert len(responses.calls) == 1 | ||
| request = responses.calls[0].request | ||
| assert request.url == BASE_CONTACT_EXPORTS_URL | ||
|
|
||
| @responses.activate | ||
| def test_create_should_handle_validation_errors( | ||
| self, | ||
| client: ContactExportsApi, | ||
| sample_create_contact_export_params: CreateContactExportParams, | ||
| ) -> None: | ||
| response_body = { | ||
| "errors": { | ||
| "filters": "invalid", | ||
| "base": [ | ||
| "There is a previous export initiated. " | ||
| "You will be notified by email once it is completed." | ||
| ], | ||
| } | ||
| } | ||
| responses.post( | ||
| BASE_CONTACT_EXPORTS_URL, | ||
| json=response_body, | ||
| status=422, | ||
| ) | ||
|
|
||
| with pytest.raises(APIError) as exc_info: | ||
| client.create(sample_create_contact_export_params) | ||
|
|
||
| assert exc_info.value.status == 422 | ||
| assert "filters: invalid" in str(exc_info.value.errors) | ||
| assert ( | ||
| "base: There is a previous export initiated. " | ||
| "You will be notified by email once it is completed." | ||
| ) in str(exc_info.value.errors) | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "status_code,response_json,expected_error_message", | ||
| [ | ||
| ( | ||
| conftest.UNAUTHORIZED_STATUS_CODE, | ||
| conftest.UNAUTHORIZED_RESPONSE, | ||
| conftest.UNAUTHORIZED_ERROR_MESSAGE, | ||
| ), | ||
| ( | ||
| conftest.FORBIDDEN_STATUS_CODE, | ||
| conftest.FORBIDDEN_RESPONSE, | ||
| conftest.FORBIDDEN_ERROR_MESSAGE, | ||
| ), | ||
| ( | ||
| conftest.NOT_FOUND_STATUS_CODE, | ||
| conftest.NOT_FOUND_RESPONSE, | ||
| conftest.NOT_FOUND_ERROR_MESSAGE, | ||
| ), | ||
| ], | ||
| ) | ||
| @responses.activate | ||
| def test_get_by_id_should_raise_api_errors( | ||
| self, | ||
| client: ContactExportsApi, | ||
| status_code: int, | ||
| response_json: dict, | ||
| expected_error_message: str, | ||
| ) -> None: | ||
| url = f"{BASE_CONTACT_EXPORTS_URL}/{EXPORT_ID}" | ||
| responses.get( | ||
| url, | ||
| status=status_code, | ||
| json=response_json, | ||
| ) | ||
|
|
||
| with pytest.raises(APIError) as exc_info: | ||
| client.get_by_id(EXPORT_ID) | ||
|
|
||
| assert expected_error_message in str(exc_info.value) | ||
|
|
||
| @responses.activate | ||
| def test_get_by_id_should_return_started_export( | ||
| self, client: ContactExportsApi, sample_contact_export_started_dict: dict | ||
| ) -> None: | ||
| url = f"{BASE_CONTACT_EXPORTS_URL}/{EXPORT_ID}" | ||
| responses.get( | ||
| url, | ||
| json=sample_contact_export_started_dict, | ||
| status=200, | ||
| ) | ||
|
|
||
| result = client.get_by_id(EXPORT_ID) | ||
|
|
||
| assert isinstance(result, ContactExportDetail) | ||
| assert result.id == EXPORT_ID | ||
| assert result.status == "started" | ||
| assert result.url is None | ||
|
|
||
| @responses.activate | ||
| def test_get_by_id_should_return_finished_export( | ||
| self, client: ContactExportsApi, sample_contact_export_finished_dict: dict | ||
| ) -> None: | ||
| url = f"{BASE_CONTACT_EXPORTS_URL}/{EXPORT_ID}" | ||
| responses.get( | ||
| url, | ||
| json=sample_contact_export_finished_dict, | ||
| status=200, | ||
| ) | ||
|
|
||
| result = client.get_by_id(EXPORT_ID) | ||
|
|
||
| assert isinstance(result, ContactExportDetail) | ||
| assert result.id == EXPORT_ID | ||
| assert result.status == "finished" | ||
| assert result.url == "https://example.com/export.csv.gz" | ||
|
|
||
| @responses.activate | ||
| def test_get_by_id_with_different_export_id_should_use_correct_url( | ||
| self, client: ContactExportsApi, sample_contact_export_started_dict: dict | ||
| ) -> None: | ||
| different_export_id = 999 | ||
| url = f"{BASE_CONTACT_EXPORTS_URL}/{different_export_id}" | ||
| responses.get( | ||
| url, | ||
| json=sample_contact_export_started_dict, | ||
| status=200, | ||
| ) | ||
|
|
||
| client.get_by_id(different_export_id) | ||
|
|
||
| assert len(responses.calls) == 1 | ||
| request = responses.calls[0].request | ||
| assert request.url == url |
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.