Skip to content

Commit 7dee42f

Browse files
Ihor BilousIhor Bilous
authored andcommitted
Fix issue #29: Split Mailtrap client into current and new
1 parent 7bb3725 commit 7dee42f

File tree

14 files changed

+71
-103
lines changed

14 files changed

+71
-103
lines changed

examples/testing/projects.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import logging
22
from typing import Optional
33

4-
from mailtrap import MailtrapClient
5-
from mailtrap.schemas.projects import Project
4+
from mailtrap import MailtrapApiClient
5+
from mailtrap.models.projects import Project
66

77
logging.basicConfig(
88
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
@@ -21,7 +21,7 @@ def find_project_by_name(project_name: str, projects: list[Project]) -> Optional
2121

2222
logging.info("Starting Mailtrap Testing API example...")
2323

24-
client = MailtrapClient(token=API_TOKEN)
24+
client = MailtrapApiClient(token=API_TOKEN)
2525
testing_api = client.get_testing_api(ACCOUNT_ID)
2626
projects_api = testing_api.projects
2727

mailtrap/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from .client import MailtrapApiClient
12
from .client import MailtrapClient
23
from .exceptions import APIError
34
from .exceptions import AuthorizationError

mailtrap/api/resources/projects.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from mailtrap.http import HttpClient
2-
from mailtrap.schemas.base import DeletedObject
3-
from mailtrap.schemas.projects import Project
4-
from mailtrap.schemas.projects import ProjectInput
2+
from mailtrap.models.base import DeletedObject
3+
from mailtrap.models.projects import Project
54

65

76
class ProjectsApi:
@@ -20,15 +19,13 @@ def get_by_id(self, project_id: int) -> Project:
2019
return Project(**response)
2120

2221
def create(self, project_name: str) -> Project:
23-
ProjectInput(name=project_name)
2422
response = self.client.post(
2523
f"/api/accounts/{self.account_id}/projects",
2624
json={"project": {"name": project_name}},
2725
)
2826
return Project(**response)
2927

3028
def update(self, project_id: int, project_name: str) -> Project:
31-
ProjectInput(name=project_name)
3229
response = self.client.patch(
3330
f"/api/accounts/{self.account_id}/projects/{project_id}",
3431
json={"project": {"name": project_name}},

mailtrap/client.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import requests
66

77
from mailtrap.api.testing import TestingApi
8-
from mailtrap.config import MAILTRAP_HOST
8+
from mailtrap.config import GENERAL_ENDPOINT
99
from mailtrap.exceptions import APIError
1010
from mailtrap.exceptions import AuthorizationError
1111
from mailtrap.exceptions import ClientConfigurationError
@@ -37,15 +37,9 @@ def __init__(
3737

3838
self._validate_itself()
3939

40-
def get_testing_api(self, account_id: str) -> TestingApi:
41-
http_client = HttpClient(host=MAILTRAP_HOST, headers=self.get_headers())
42-
return TestingApi(
43-
account_id=account_id, inbox_id=self.inbox_id, client=http_client
44-
)
45-
4640
def send(self, mail: BaseMail) -> dict[str, Union[bool, list[str]]]:
4741
response = requests.post(
48-
self.api_send_url, headers=self.get_headers(), json=mail.api_data
42+
self.api_send_url, headers=self.headers, json=mail.api_data
4943
)
5044

5145
if response.ok:
@@ -66,7 +60,8 @@ def api_send_url(self) -> str:
6660

6761
return url
6862

69-
def get_headers(self) -> dict[str, str]:
63+
@property
64+
def headers(self) -> dict[str, str]:
7065
return {
7166
"Authorization": f"Bearer {self.token}",
7267
"Content-Type": "application/json",
@@ -106,3 +101,21 @@ def _validate_itself(self) -> None:
106101

107102
if self.bulk and self.sandbox:
108103
raise ClientConfigurationError("bulk mode is not allowed in sandbox mode")
104+
105+
106+
class MailtrapApiClient:
107+
def __init__(self, token: str) -> None:
108+
self.token = token
109+
110+
def testing_api(self, account_id: str, inbox_id: str) -> TestingApi:
111+
http_client = HttpClient(host=GENERAL_ENDPOINT, headers=self.get_headers())
112+
return TestingApi(account_id=account_id, inbox_id=inbox_id, client=http_client)
113+
114+
def get_headers(self) -> dict[str, str]:
115+
return {
116+
"Authorization": f"Bearer {self.token}",
117+
"Content-Type": "application/json",
118+
"User-Agent": (
119+
"mailtrap-python (https://github.com/railsware/mailtrap-python)"
120+
),
121+
}

mailtrap/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
MAILTRAP_HOST = "mailtrap.io"
1+
GENERAL_ENDPOINT = "mailtrap.io"
22

33
DEFAULT_REQUEST_TIMEOUT = 30 # in seconds

mailtrap/http.py

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,52 +42,38 @@ def _handle_failed_response(self, response: Response) -> NoReturn:
4242

4343
raise APIError(status_code, errors=errors)
4444

45-
def _process_response(self, response: Response, expected_type: type[T]) -> T:
45+
def _process_response(self, response: Response) -> Any:
4646
if not response.ok:
4747
self._handle_failed_response(response)
48-
data = response.json()
49-
if not isinstance(data, expected_type):
50-
raise APIError(
51-
response.status_code,
52-
errors=[f"Expected response type {expected_type.__name__}"],
53-
)
54-
return data
48+
return response.json()
5549

56-
def _process_response_dict(self, response: Response) -> dict[str, Any]:
57-
return self._process_response(response, dict)
58-
59-
def _process_response_list(self, response: Response) -> list[dict[str, Any]]:
60-
return self._process_response(response, list)
61-
62-
def get(self, path: str, params: Optional[dict[str, Any]] = None) -> dict[str, Any]:
50+
def get(self, path: str, params: Optional[dict[str, Any]] = None) -> Any:
6351
response = self._session.get(
6452
self._url(path), params=params, timeout=self._timeout
6553
)
66-
return self._process_response_dict(response)
54+
return self._process_response(response)
6755

68-
def list(
69-
self, path: str, params: Optional[dict[str, Any]] = None
70-
) -> list[dict[str, Any]]:
56+
def list(self, path: str, params: Optional[dict[str, Any]] = None) -> Any:
7157
response = self._session.get(
7258
self._url(path), params=params, timeout=self._timeout
7359
)
74-
return self._process_response_list(response)
60+
return self._process_response(response)
7561

76-
def post(self, path: str, json: Optional[dict[str, Any]] = None) -> dict[str, Any]:
62+
def post(self, path: str, json: Optional[dict[str, Any]] = None) -> Any:
7763
response = self._session.post(self._url(path), json=json, timeout=self._timeout)
78-
return self._process_response_dict(response)
64+
return self._process_response(response)
7965

80-
def put(self, path: str, json: Optional[dict[str, Any]] = None) -> dict[str, Any]:
66+
def put(self, path: str, json: Optional[dict[str, Any]] = None) -> Any:
8167
response = self._session.put(self._url(path), json=json, timeout=self._timeout)
82-
return self._process_response_dict(response)
68+
return self._process_response(response)
8369

84-
def patch(self, path: str, json: Optional[dict[str, Any]] = None) -> dict[str, Any]:
70+
def patch(self, path: str, json: Optional[dict[str, Any]] = None) -> Any:
8571
response = self._session.patch(self._url(path), json=json, timeout=self._timeout)
86-
return self._process_response_dict(response)
72+
return self._process_response(response)
8773

88-
def delete(self, path: str) -> dict[str, Any]:
74+
def delete(self, path: str) -> Any:
8975
response = self._session.delete(self._url(path), timeout=self._timeout)
90-
return self._process_response_dict(response)
76+
return self._process_response(response)
9177

9278

9379
def _extract_errors(data: dict[str, Any]) -> list[str]:
File renamed without changes.
File renamed without changes.

mailtrap/schemas/inboxes.py renamed to mailtrap/models/inboxes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from pydantic import BaseModel
44

5-
from mailtrap.schemas.permissions import Permissions
5+
from mailtrap.models.permissions import Permissions
66

77

88
class Inbox(BaseModel):
File renamed without changes.

0 commit comments

Comments
 (0)