|
| 1 | +from typing import Optional |
| 2 | +from urllib.parse import quote |
| 3 | + |
| 4 | +from mailtrap.http import HttpClient |
| 5 | +from mailtrap.models.accounts import AccountAccess |
| 6 | +from mailtrap.models.accounts import AccountAccessFilterParams |
| 7 | +from mailtrap.models.common import DeletedObject |
| 8 | + |
| 9 | + |
| 10 | +class AccountAccessesApi: |
| 11 | + def __init__(self, client: HttpClient) -> None: |
| 12 | + self._client = client |
| 13 | + |
| 14 | + def get_list( |
| 15 | + self, account_id: int, filter_params: Optional[AccountAccessFilterParams] = None |
| 16 | + ) -> list[AccountAccess]: |
| 17 | + """ |
| 18 | + Get list of account accesses for which specifier_type is User or Invite. |
| 19 | + You have to have account admin/owner permissions for this endpoint to work. |
| 20 | + If you specify project_ids, inbox_ids or domain_ids, the endpoint will return |
| 21 | + account accesses for these resources. |
| 22 | + """ |
| 23 | + response = self._client.get( |
| 24 | + self._api_path(account_id), |
| 25 | + params=filter_params.api_data if filter_params else None, |
| 26 | + ) |
| 27 | + return [AccountAccess(**account_access) for account_access in response] |
| 28 | + |
| 29 | + def delete(self, account_id: int, account_access_id: int) -> DeletedObject: |
| 30 | + """ |
| 31 | + If specifier type is User, it removes user permissions. |
| 32 | + If specifier type is Invite or ApiToken, it removes specifier |
| 33 | + along with permissions. You have to be an account admin/owner |
| 34 | + for this method to work. |
| 35 | + """ |
| 36 | + self._client.delete(self._api_path(account_id, account_access_id)) |
| 37 | + return DeletedObject(account_access_id) |
| 38 | + |
| 39 | + def _api_path(self, account_id: int, account_access_id: Optional[int] = None) -> str: |
| 40 | + path = f"/api/accounts/{account_id}/account_accesses" |
| 41 | + if account_access_id is not None: |
| 42 | + return f"{path}/{quote(str(account_access_id), safe='')}" |
| 43 | + return path |
0 commit comments