|
1 | | -from typing import Optional |
2 | | -from pydantic import validate_call |
| 1 | +from typing import Generator, Literal, Optional |
| 2 | + |
| 3 | +from pydantic import BaseModel, validate_call |
3 | 4 | from vonage_http_client.http_client import HttpClient |
4 | 5 |
|
5 | | -from .errors import UsersError |
| 6 | +from .common import User |
6 | 7 | from .requests import ListUsersRequest |
7 | | -from .responses import ListUsersResponse |
| 8 | +from .responses import CreateUserResponse, ListUsersResponse |
| 9 | + |
| 10 | + |
| 11 | +class Filters(BaseModel): |
| 12 | + order: Optional[Literal['asc', 'desc', 'ASC', 'DESC']] = None |
| 13 | + |
| 14 | + |
| 15 | +import urllib.parse |
| 16 | + |
| 17 | + |
| 18 | +def parse_cursor_from_url(url: str) -> Optional[str]: |
| 19 | + """Extract the cursor from the "next" URL.""" |
| 20 | + query_string = urllib.parse.urlparse(url).query |
| 21 | + params = urllib.parse.parse_qs(query_string) |
| 22 | + return params.get('cursor', [None])[0] |
8 | 23 |
|
9 | 24 |
|
10 | 25 | class Users: |
11 | 26 | """Class containing methods for user management. |
12 | 27 |
|
13 | | - When using APIs that require a Vonage Application to be created, |
14 | | - you can create users to associate with that application. |
| 28 | + When using APIs that require a Vonage Application to be created, you can create users to |
| 29 | + associate with that application. |
15 | 30 | """ |
16 | 31 |
|
17 | 32 | def __init__(self, http_client: HttpClient) -> None: |
18 | 33 | self._http_client = http_client |
19 | 34 | self._auth_type = 'jwt' |
20 | 35 |
|
21 | 36 | @validate_call |
22 | | - def list_users(self, params: Optional[ListUsersRequest] = None) -> ListUsersResponse: |
23 | | - """List all users.""" |
24 | | - response = self._http_client.get( |
| 37 | + def list_users( |
| 38 | + self, |
| 39 | + order: Literal['asc', 'desc', 'ASC', 'DESC'] = None, |
| 40 | + name: str = None, |
| 41 | + ) -> Generator: |
| 42 | + """List all users with pagination handled by a generator.""" |
| 43 | + cursor = None |
| 44 | + while True: |
| 45 | + params = ListUsersRequest(order=order, cursor=cursor, name=name) |
| 46 | + response = self._http_client.get( |
| 47 | + self._http_client.api_host, |
| 48 | + '/v1/users', |
| 49 | + # need to send the right stuff to the api |
| 50 | + params.model_dump() if params is not None else None, |
| 51 | + self._auth_type, |
| 52 | + ) |
| 53 | + users = ListUsersResponse(**response) |
| 54 | + for user in users.embedded.users: |
| 55 | + yield user |
| 56 | + if not users.links.next: |
| 57 | + break |
| 58 | + cursor = parse_cursor_from_url(users.links.next.href) |
| 59 | + |
| 60 | + @validate_call |
| 61 | + def create_user(self, params: Optional[User]): |
| 62 | + """Create a user.""" |
| 63 | + response = self._http_client.post( |
25 | 64 | self._http_client.api_host, |
26 | 65 | '/v1/users', |
27 | 66 | params.model_dump() if params is not None else None, |
28 | 67 | self._auth_type, |
29 | 68 | ) |
30 | | - return ListUsersResponse(**response) |
| 69 | + return CreateUserResponse(**response) |
0 commit comments