|
| 1 | +from typing import List, Optional, Tuple |
| 2 | + |
| 3 | +from pydantic import validate_call |
| 4 | +from vonage_http_client.http_client import HttpClient |
| 5 | + |
| 6 | +from .common import User |
| 7 | +from .requests import ApplicationOptions, ListApplicationsFilter |
| 8 | +from .responses import ApplicationInfo |
| 9 | + |
| 10 | + |
| 11 | +class Application: |
| 12 | + """Class containing methods for Vonage Application management.""" |
| 13 | + |
| 14 | + def __init__(self, http_client: HttpClient) -> None: |
| 15 | + self._http_client = http_client |
| 16 | + self._auth_type = 'basic' |
| 17 | + |
| 18 | + @property |
| 19 | + def http_client(self) -> HttpClient: |
| 20 | + """The HTTP client used to make requests to the Users API. |
| 21 | +
|
| 22 | + Returns: |
| 23 | + HttpClient: The HTTP client used to make requests to the Users API. |
| 24 | + """ |
| 25 | + return self._http_client |
| 26 | + |
| 27 | + @validate_call |
| 28 | + def list_applications( |
| 29 | + self, filter: ListApplicationsFilter = ListApplicationsFilter() |
| 30 | + ) -> Tuple[List[ApplicationInfo], Optional[str]]: |
| 31 | + """""" |
| 32 | + response = self._http_client.get( |
| 33 | + self._http_client.api_host, |
| 34 | + '/v2/applications', |
| 35 | + filter.model_dump(exclude_none=True), |
| 36 | + self._auth_type, |
| 37 | + ) |
| 38 | + |
| 39 | + # applications_response = ListApplicationsResponse(**response) |
| 40 | + # if applications_response.links.next is None: |
| 41 | + # return applications_response.embedded.users, None |
| 42 | + |
| 43 | + # parsed_url = urlparse(users_response.links.next.href) |
| 44 | + # query_params = parse_qs(parsed_url.query) |
| 45 | + # next_cursor = query_params.get('cursor', [None])[0] |
| 46 | + # return users_response.embedded.users, next_cursor |
| 47 | + |
| 48 | + @validate_call |
| 49 | + def create_application( |
| 50 | + self, params: Optional[ApplicationOptions] = None |
| 51 | + ) -> ApplicationData: |
| 52 | + """. |
| 53 | + . |
| 54 | + """ |
| 55 | + response = self._http_client.post( |
| 56 | + self._http_client.api_host, |
| 57 | + '/v2/applications', |
| 58 | + params.model_dump(exclude_none=True) if params is not None else None, |
| 59 | + self._auth_type, |
| 60 | + ) |
| 61 | + return User(**response) |
| 62 | + |
| 63 | + @validate_call |
| 64 | + def get_user(self, id: str) -> User: |
| 65 | + """Get a user by ID. |
| 66 | +
|
| 67 | + Args: |
| 68 | + id (str): The ID of the user to retrieve. |
| 69 | +
|
| 70 | + Returns: |
| 71 | + User: The user object. |
| 72 | + """ |
| 73 | + response = self._http_client.get( |
| 74 | + self._http_client.api_host, f'/v1/users/{id}', None, self._auth_type |
| 75 | + ) |
| 76 | + return User(**response) |
| 77 | + |
| 78 | + @validate_call |
| 79 | + def update_user(self, id: str, params: User) -> User: |
| 80 | + """Update a user. |
| 81 | +
|
| 82 | + Args: |
| 83 | + id (str): The ID of the user to update. |
| 84 | + params (User): The updated user object. |
| 85 | +
|
| 86 | + Returns: |
| 87 | + User: The updated user object. |
| 88 | + """ |
| 89 | + response = self._http_client.patch( |
| 90 | + self._http_client.api_host, |
| 91 | + f'/v1/users/{id}', |
| 92 | + params.model_dump(exclude_none=True), |
| 93 | + self._auth_type, |
| 94 | + ) |
| 95 | + return User(**response) |
| 96 | + |
| 97 | + @validate_call |
| 98 | + def delete_user(self, id: str) -> None: |
| 99 | + """Delete a user. |
| 100 | +
|
| 101 | + Args: |
| 102 | + id (str): The ID of the user to delete. |
| 103 | +
|
| 104 | + Returns: |
| 105 | + None |
| 106 | + """ |
| 107 | + self._http_client.delete( |
| 108 | + self._http_client.api_host, f'/v1/users/{id}', None, self._auth_type |
| 109 | + ) |
0 commit comments