Skip to content

Commit 03b2741

Browse files
authored
Add Users (#269)
* add users api and tests * updated README * updated changelog * Bump version: 3.7.1 → 3.8.0
1 parent 7930960 commit 03b2741

21 files changed

+738
-3
lines changed

.bumpversion.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 3.7.1
2+
current_version = 3.8.0
33
commit = True
44
tag = False
55

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 3.8.0
2+
- Adding support for the [Users component of the Vonage Application API](https://developer.vonage.com/en/api/application.v2#User)
3+
14
# 3.7.1
25
- Fixing dependency version to a specific major version
36

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ need a Vonage account. Sign up [for free at vonage.com][signup].
2727
- [Pricing API](#pricing-api)
2828
- [Managing Secrets](#managing-secrets)
2929
- [Application API](#application-api)
30+
- [Users API](#users-api)
3031
- [Validating Webhook Signatures](#validate-webhook-signatures)
3132
- [JWT Parameters](#jwt-parameters)
3233
- [Overriding API Attributes](#overriding-api-attributes)
@@ -1082,6 +1083,42 @@ response = client.application.delete_application(uuid)
10821083

10831084
Docs: [https://developer.nexmo.com/api/application.v2#deleteApplication](https://developer.nexmo.com/api/application.v2#deleteApplication?utm_source=DEV_REL&utm_medium=github&utm_campaign=python-client-library#destroy-an-application)
10841085

1086+
1087+
## Users API
1088+
1089+
These API methods are part of the [Application (v2) API](https://developer.vonage.com/en/application/overview) but are a in separate module in the SDK. [See the API reference for more details](https://developer.vonage.com/en/api/application.v2#User).
1090+
1091+
### List all Users
1092+
1093+
```python
1094+
client.users.list_users()
1095+
```
1096+
1097+
### Create a new user
1098+
1099+
```python
1100+
client.users.create_user() # Default values generated
1101+
client.users.create_user(params={...}) # Specify custom values
1102+
```
1103+
1104+
### Get detailed information about a user
1105+
1106+
```python
1107+
client.users.get_user('USER_ID')
1108+
```
1109+
1110+
### Update user details
1111+
1112+
```python
1113+
client.users.update_user('USER_ID', params={...})
1114+
```
1115+
1116+
### Delete a user
1117+
1118+
```python
1119+
client.users.delete_user('USER_ID')
1120+
```
1121+
10851122
## Validate webhook signatures
10861123

10871124
```python

setup.py

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

1010
setup(
1111
name="vonage",
12-
version="3.7.1",
12+
version="3.8.0",
1313
description="Vonage Server SDK for Python",
1414
long_description=long_description,
1515
long_description_content_type="text/markdown",

src/vonage/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from .client import *
22
from .ncco_builder.ncco import *
33

4-
__version__ = "3.7.1"
4+
__version__ = "3.8.0"

src/vonage/client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from .short_codes import ShortCodes
1414
from .sms import Sms
1515
from .subaccounts import Subaccounts
16+
from .users import Users
1617
from .ussd import Ussd
1718
from .voice import Voice
1819
from .verify import Verify
@@ -122,6 +123,7 @@ def __init__(
122123
self.short_codes = ShortCodes(self)
123124
self.sms = Sms(self)
124125
self.subaccounts = Subaccounts(self)
126+
self.users = Users(self)
125127
self.ussd = Ussd(self)
126128
self.verify = Verify(self)
127129
self.verify2 = Verify2(self)

src/vonage/errors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,7 @@ class SubaccountsError(ClientError):
5050

5151
class ProactiveConnectError(ClientError):
5252
"""An error relating to the Proactive Connect API."""
53+
54+
55+
class UsersError(ClientError):
56+
"""An error relating to the Users API."""

src/vonage/users.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from __future__ import annotations
2+
from typing import TYPE_CHECKING
3+
4+
if TYPE_CHECKING:
5+
from vonage import Client
6+
7+
from .errors import UsersError
8+
from ._internal import set_auth_type
9+
10+
11+
class Users:
12+
"""Class containing methods for user management as part of the Application API."""
13+
14+
def __init__(self, client: Client):
15+
self._client = client
16+
self._auth_type = set_auth_type(self._client)
17+
18+
def list_users(
19+
self,
20+
page_size: int = None,
21+
order: str = 'asc',
22+
cursor: str = None,
23+
name: str = None,
24+
):
25+
"""
26+
Lists the name and user id of all users associated with the account.
27+
For complete information on a user, call Users.get_user, passing in the user id.
28+
"""
29+
30+
if order.lower() not in ('asc', 'desc'):
31+
raise UsersError(
32+
'Invalid order parameter. Must be one of: "asc", "desc", "ASC", "DESC".'
33+
)
34+
35+
params = {'page_size': page_size, 'order': order.lower(), 'cursor': cursor, 'name': name}
36+
return self._client.get(
37+
self._client.api_host(),
38+
'/v1/users',
39+
params,
40+
auth_type=self._auth_type,
41+
)
42+
43+
def create_user(self, params: dict = None):
44+
self._client.headers['Content-Type'] = 'application/json'
45+
return self._client.post(
46+
self._client.api_host(),
47+
'/v1/users',
48+
params,
49+
auth_type=self._auth_type,
50+
)
51+
52+
def get_user(self, user_id: str):
53+
return self._client.get(
54+
self._client.api_host(),
55+
f'/v1/users/{user_id}',
56+
auth_type=self._auth_type,
57+
)
58+
59+
def update_user(self, user_id: str, params: dict):
60+
return self._client.patch(
61+
self._client.api_host(),
62+
f'/v1/users/{user_id}',
63+
params,
64+
auth_type=self._auth_type,
65+
)
66+
67+
def delete_user(self, user_id: str):
68+
return self._client.delete(
69+
self._client.api_host(),
70+
f'/v1/users/{user_id}',
71+
auth_type=self._auth_type,
72+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"title": "Bad request.",
3+
"type": "https://developer.nexmo.com/api/conversation#http:error:validation-fail",
4+
"code": "http:error:validation-fail",
5+
"detail": "Invalid Content-Type.",
6+
"instance": "9d0e245d-fac0-450e-811f-52343041df61",
7+
"invalid_parameters": [
8+
{
9+
"name": "content-type",
10+
"reason": "content-type \"application/octet-stream\" is not supported. Supported versions are [application/json]"
11+
}
12+
]
13+
}

tests/data/users/list_users_400.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"title": "Bad request.",
3+
"type": "https://developer.nexmo.com/api/conversation#http:error:validation-fail",
4+
"code": "http:error:validation-fail",
5+
"detail": "Input validation failure.",
6+
"instance": "04ee4d32-78c9-4acf-bdc1-b7d1fa860c92",
7+
"invalid_parameters": [
8+
{
9+
"name": "page_size",
10+
"reason": "\"page_size\" must be a number"
11+
}
12+
]
13+
}

0 commit comments

Comments
 (0)