Skip to content

Commit 21ea88c

Browse files
committed
start adding account api
1 parent f65afe5 commit 21ea88c

22 files changed

+478
-6
lines changed

account/BUILD

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
resource(name='pyproject', source='pyproject.toml')
2+
file(name='readme', source='README.md')
3+
4+
files(sources=['tests/data/*'])
5+
6+
python_distribution(
7+
name='vonage-account',
8+
dependencies=[
9+
':pyproject',
10+
':readme',
11+
'account/src/vonage_account',
12+
],
13+
provides=python_artifact(),
14+
generate_setup=False,
15+
repositories=['@pypi'],
16+
)

account/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# 1.0.0
2+
- Initial upload

account/README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Vonage Users Package
2+
3+
This package contains the code to use Vonage's Application API in Python.
4+
5+
It includes methods for managing applications.
6+
7+
## Usage
8+
9+
It is recommended to use this as part of the main `vonage` package. The examples below assume you've created an instance of the `vonage.Vonage` class called `vonage_client`.
10+
11+
### List Applications
12+
13+
With no custom options specified, this method will get the first 100 applications. It returns a tuple consisting of a list of `ApplicationData` objects and an int showing the page number of the next page of results.
14+
15+
```python
16+
from vonage_application import ListApplicationsFilter, ApplicationData
17+
18+
applications, next_page = vonage_client.application.list_applications()
19+
20+
# With options
21+
options = ListApplicationsFilter(page_size=3, page=2)
22+
applications, next_page = vonage_client.application.list_applications(options)
23+
```
24+
25+
### Create a New Application
26+
27+
```python
28+
from vonage_application import ApplicationConfig
29+
30+
app_data = vonage_client.application.create_application()
31+
32+
# Create with custom options (can also be done with a dict)
33+
from vonage_application import ApplicationConfig, Keys, Voice, VoiceWebhooks
34+
voice = Voice(
35+
webhooks=VoiceWebhooks(
36+
event_url=VoiceUrl(
37+
address='https://example.com/event',
38+
http_method='POST',
39+
connect_timeout=500,
40+
socket_timeout=3000,
41+
),
42+
),
43+
signed_callbacks=True,
44+
)
45+
capabilities = Capabilities(voice=voice)
46+
keys = Keys(public_key='MY_PUBLIC_KEY')
47+
config = ApplicationConfig(
48+
name='My Customised Application',
49+
capabilities=capabilities,
50+
keys=keys,
51+
)
52+
app_data = vonage_client.application.create_application(config)
53+
```
54+
55+
### Get an Application
56+
57+
```python
58+
app_data = client.application.get_application('MY_APP_ID')
59+
app_data_as_dict = app.model_dump(exclude_none=True)
60+
```
61+
62+
### Update an Application
63+
64+
To update an application, pass config for the updated field(s) in an ApplicationConfig object
65+
66+
```python
67+
from vonage_application import ApplicationConfig, Keys, Voice, VoiceWebhooks
68+
69+
config = ApplicationConfig(name='My Updated Application')
70+
app_data = vonage_client.application.update_application('MY_APP_ID', config)
71+
```
72+
73+
### Delete an Application
74+
75+
```python
76+
vonage_client.applications.delete_application('MY_APP_ID')
77+
```

account/pyproject.toml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[project]
2+
name = 'vonage-account'
3+
version = '1.0.0'
4+
description = 'Vonage Account API package'
5+
readme = "README.md"
6+
authors = [{ name = "Vonage", email = "devrel@vonage.com" }]
7+
requires-python = ">=3.8"
8+
dependencies = [
9+
"vonage-http-client>=1.3.1",
10+
"vonage-utils>=1.1.2",
11+
"pydantic>=2.7.1",
12+
]
13+
classifiers = [
14+
"Programming Language :: Python",
15+
"Programming Language :: Python :: 3",
16+
"Programming Language :: Python :: 3.8",
17+
"Programming Language :: Python :: 3.9",
18+
"Programming Language :: Python :: 3.10",
19+
"Programming Language :: Python :: 3.11",
20+
"Programming Language :: Python :: 3.12",
21+
"License :: OSI Approved :: Apache Software License",
22+
]
23+
24+
[project.urls]
25+
homepage = "https://github.com/Vonage/vonage-python-sdk"
26+
27+
[build-system]
28+
requires = ["setuptools>=61.0", "wheel"]
29+
build-backend = "setuptools.build_meta"

account/src/vonage_account/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python_sources()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .account import Account
2+
3+
__all__ = ['Account']

account/src/vonage_account/account.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
from pydantic import validate_call
2+
from vonage_http_client.http_client import HttpClient
3+
4+
from .requests import Balance
5+
from .responses import SettingsResponse, TopUpResponse
6+
7+
8+
class Account:
9+
"""Class containing methods for management of a Vonage account."""
10+
11+
def __init__(self, http_client: HttpClient) -> None:
12+
self._http_client = http_client
13+
self._auth_type = 'basic'
14+
15+
@property
16+
def http_client(self) -> HttpClient:
17+
"""The HTTP client used to make requests to the Users API.
18+
19+
Returns:
20+
HttpClient: The HTTP client used to make requests to the Users API.
21+
"""
22+
return self._http_client
23+
24+
@validate_call
25+
def get_balance(self) -> Balance:
26+
"""Get the balance of the account.
27+
28+
Returns:
29+
Balance: Object containing the account balance and whether auto-reload is
30+
enabled for the account.
31+
"""
32+
33+
response = self._http_client.get(
34+
self._http_client.rest_host,
35+
'/account/get-balance',
36+
auth_type=self._auth_type,
37+
)
38+
return Balance(**response)
39+
40+
@validate_call
41+
def top_up(self, trx: str) -> TopUpResponse:
42+
"""Top-up the account balance.
43+
44+
Args:
45+
trx (str): The transaction reference of the transaction when auto-reload
46+
was enabled on your account.
47+
48+
Returns:
49+
TopUpResponse: Object containing the top-up response.
50+
"""
51+
52+
response = self._http_client.post(
53+
self._http_client.rest_host,
54+
'/account/top-up',
55+
params={'trx': trx},
56+
auth_type=self._auth_type,
57+
sent_data_type='form',
58+
)
59+
return TopUpResponse(**response)
60+
61+
@validate_call
62+
def update_default_sms_webhook(
63+
self, mo_callback_url: str = None, dr_callback_url: str = None
64+
) -> SettingsResponse:
65+
"""Update the default SMS webhook URLs for the account.
66+
In order to unset any default value, pass an empty string as the value.
67+
68+
Args:
69+
mo_callback_url (str, optional): The URL to which inbound SMS messages will be
70+
sent.
71+
dr_callback_url (str, optional): The URL to which delivery receipts will be sent.
72+
73+
Returns:
74+
SettingsResponse: Object containing the response to the settings update.
75+
"""
76+
77+
params = {}
78+
if mo_callback_url is not None:
79+
params['moCallbackUrl'] = mo_callback_url
80+
if dr_callback_url is not None:
81+
params['drCallbackUrl'] = dr_callback_url
82+
83+
response = self._http_client.post(
84+
self._http_client.rest_host,
85+
'/account/settings',
86+
params=params,
87+
auth_type=self._auth_type,
88+
sent_data_type='form',
89+
)
90+
return SettingsResponse(**response)
91+
92+
def list_secrets(self) -> SecretList:
93+
"""List all secrets associated with the account.
94+
95+
Returns:
96+
SecretList: List of Secret objects.
97+
"""
98+
pass

account/src/vonage_account/errors.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from vonage_utils.errors import VonageError
2+
3+
4+
class AccountError(VonageError):
5+
"""Indicates an error with the Account package."""
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from typing import Optional
2+
3+
from pydantic import BaseModel, Field
4+
5+
6+
class Balance(BaseModel):
7+
value: float
8+
auto_reload: Optional[bool] = Field(None, validation_alias='autoReload')
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import Optional
2+
3+
from pydantic import BaseModel, Field
4+
5+
6+
class TopUpResponse(BaseModel):
7+
error_code: Optional[str] = Field(None, validation_alias='error-code')
8+
error_code_label: Optional[str] = Field(None, validation_alias='error-code-label')
9+
10+
11+
class SettingsResponse(BaseModel):
12+
mo_callback_url: Optional[str] = Field(None, validation_alias='mo-callback-url')
13+
dr_callback_url: Optional[str] = Field(None, validation_alias='dr-callback-url')
14+
max_outbound_request: Optional[int] = Field(
15+
None, validation_alias='max-outbound-request'
16+
)
17+
max_inbound_request: Optional[int] = Field(
18+
None, validation_alias='max-inbound-request'
19+
)
20+
max_calls_per_second: Optional[int] = Field(
21+
None, validation_alias='max-calls-per-second'
22+
)

account/tests/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python_tests(dependencies=['account', 'testutils'])

account/tests/data/get_balance.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"value": 29.18202293,
3+
"autoReload": false
4+
}

account/tests/data/top_up.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"error-code": "200",
3+
"error-code-label": "success"
4+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"mo-callback-url": "https://example.com/inbound_sms_webhook",
3+
"dr-callback-url": "https://example.com/delivery_receipt_webhook",
4+
"max-outbound-request": 30,
5+
"max-inbound-request": 30,
6+
"max-calls-per-second": 30
7+
}

0 commit comments

Comments
 (0)