Skip to content

Commit bdfd9b3

Browse files
Ihor BilousIhor Bilous
authored andcommitted
Fix issue #19: Add _api_path method in Api classes
1 parent 6e002ac commit bdfd9b3

File tree

4 files changed

+42
-28
lines changed

4 files changed

+42
-28
lines changed

examples/contacts/contact_fields.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def create_contact_field(
2525

2626

2727
def update_contact_field(
28-
contact_field_id: str,
28+
contact_field_id: int,
2929
name: Optional[str] = None,
3030
merge_tag: Optional[str] = None,
3131
) -> ContactField:
@@ -37,11 +37,11 @@ def list_contact_fields() -> list[ContactField]:
3737
return contact_fields_api.get_list()
3838

3939

40-
def get_contact_field(contact_field_id: str) -> ContactField:
40+
def get_contact_field(contact_field_id: int) -> ContactField:
4141
return contact_fields_api.get_by_id(contact_field_id)
4242

4343

44-
def delete_contact_field(contact_field_id: str) -> DeletedObject:
44+
def delete_contact_field(contact_field_id: int) -> DeletedObject:
4545
return contact_fields_api.delete(contact_field_id)
4646

4747

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
from mailtrap.http import HttpClient
24
from mailtrap.models.common import DeletedObject
35
from mailtrap.models.contacts import ContactField
@@ -11,18 +13,18 @@ def __init__(self, client: HttpClient, account_id: str) -> None:
1113
self._client = client
1214

1315
def get_list(self) -> list[ContactField]:
14-
response = self._client.get(f"/api/accounts/{self._account_id}/contacts/fields")
16+
response = self._client.get(self._api_path())
1517
return [ContactField(**field) for field in response]
1618

1719
def get_by_id(self, field_id: int) -> ContactField:
1820
response = self._client.get(
19-
f"/api/accounts/{self._account_id}/contacts/fields/{field_id}"
21+
self._api_path(field_id),
2022
)
2123
return ContactField(**response)
2224

2325
def create(self, field_params: CreateContactFieldParams) -> ContactField:
2426
response = self._client.post(
25-
f"/api/accounts/{self._account_id}/contacts/fields",
27+
self._api_path(),
2628
json=field_params.api_data,
2729
)
2830
return ContactField(**response)
@@ -31,13 +33,17 @@ def update(
3133
self, field_id: int, field_params: UpdateContactFieldParams
3234
) -> ContactField:
3335
response = self._client.patch(
34-
f"/api/accounts/{self._account_id}/contacts/fields/{field_id}",
36+
self._api_path(field_id),
3537
json=field_params.api_data,
3638
)
3739
return ContactField(**response)
3840

3941
def delete(self, field_id: int) -> DeletedObject:
40-
self._client.delete(
41-
f"/api/accounts/{self._account_id}/contacts/fields/{field_id}"
42-
)
42+
self._client.delete(self._api_path(field_id))
4343
return DeletedObject(field_id)
44+
45+
def _api_path(self, field_id: Optional[int] = None) -> str:
46+
path = f"/api/accounts/{self._account_id}/contacts/fields"
47+
if field_id:
48+
return f"{path}/{field_id}"
49+
return path

mailtrap/api/resources/projects.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
from mailtrap.http import HttpClient
24
from mailtrap.models.common import DeletedObject
35
from mailtrap.models.projects import Project
@@ -9,31 +11,33 @@ def __init__(self, client: HttpClient, account_id: str) -> None:
911
self._client = client
1012

1113
def get_list(self) -> list[Project]:
12-
response = self._client.get(f"/api/accounts/{self._account_id}/projects")
14+
response = self._client.get(self._api_path())
1315
return [Project(**project) for project in response]
1416

1517
def get_by_id(self, project_id: int) -> Project:
16-
response = self._client.get(
17-
f"/api/accounts/{self._account_id}/projects/{project_id}"
18-
)
18+
response = self._client.get(self._api_path(project_id))
1919
return Project(**response)
2020

2121
def create(self, project_name: str) -> Project:
2222
response = self._client.post(
23-
f"/api/accounts/{self._account_id}/projects",
23+
self._api_path(),
2424
json={"project": {"name": project_name}},
2525
)
2626
return Project(**response)
2727

2828
def update(self, project_id: int, project_name: str) -> Project:
2929
response = self._client.patch(
30-
f"/api/accounts/{self._account_id}/projects/{project_id}",
30+
self._api_path(project_id),
3131
json={"project": {"name": project_name}},
3232
)
3333
return Project(**response)
3434

3535
def delete(self, project_id: int) -> DeletedObject:
36-
response = self._client.delete(
37-
f"/api/accounts/{self._account_id}/projects/{project_id}",
38-
)
36+
response = self._client.delete(self._api_path(project_id))
3937
return DeletedObject(**response)
38+
39+
def _api_path(self, project_id: Optional[int] = None) -> str:
40+
path = f"/api/accounts/{self._account_id}/projects"
41+
if project_id:
42+
return f"{path}/{project_id}"
43+
return path

mailtrap/api/resources/templates.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
from mailtrap.http import HttpClient
24
from mailtrap.models.common import DeletedObject
35
from mailtrap.models.templates import CreateEmailTemplateParams
@@ -11,18 +13,16 @@ def __init__(self, client: HttpClient, account_id: str) -> None:
1113
self._client = client
1214

1315
def get_list(self) -> list[EmailTemplate]:
14-
response = self._client.get(f"/api/accounts/{self._account_id}/email_templates")
16+
response = self._client.get(self._api_path())
1517
return [EmailTemplate(**template) for template in response]
1618

1719
def get_by_id(self, template_id: int) -> EmailTemplate:
18-
response = self._client.get(
19-
f"/api/accounts/{self._account_id}/email_templates/{template_id}"
20-
)
20+
response = self._client.get(self._api_path(template_id))
2121
return EmailTemplate(**response)
2222

2323
def create(self, template_params: CreateEmailTemplateParams) -> EmailTemplate:
2424
response = self._client.post(
25-
f"/api/accounts/{self._account_id}/email_templates",
25+
self._api_path(),
2626
json={"email_template": template_params.api_data},
2727
)
2828
return EmailTemplate(**response)
@@ -31,13 +31,17 @@ def update(
3131
self, template_id: int, template_params: UpdateEmailTemplateParams
3232
) -> EmailTemplate:
3333
response = self._client.patch(
34-
f"/api/accounts/{self._account_id}/email_templates/{template_id}",
34+
self._api_path(template_id),
3535
json={"email_template": template_params.api_data},
3636
)
3737
return EmailTemplate(**response)
3838

3939
def delete(self, template_id: int) -> DeletedObject:
40-
self._client.delete(
41-
f"/api/accounts/{self._account_id}/email_templates/{template_id}"
42-
)
40+
self._client.delete(self._api_path(template_id))
4341
return DeletedObject(template_id)
42+
43+
def _api_path(self, template_id: Optional[int] = None) -> str:
44+
path = f"/api/accounts/{self._account_id}/email_templates"
45+
if template_id:
46+
return f"{path}/{template_id}"
47+
return path

0 commit comments

Comments
 (0)