Skip to content

Commit

Permalink
start adding account api
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkahan committed Aug 5, 2024
1 parent f65afe5 commit 21ea88c
Show file tree
Hide file tree
Showing 22 changed files with 478 additions and 6 deletions.
16 changes: 16 additions & 0 deletions account/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
resource(name='pyproject', source='pyproject.toml')
file(name='readme', source='README.md')

files(sources=['tests/data/*'])

python_distribution(
name='vonage-account',
dependencies=[
':pyproject',
':readme',
'account/src/vonage_account',
],
provides=python_artifact(),
generate_setup=False,
repositories=['@pypi'],
)
2 changes: 2 additions & 0 deletions account/CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# 1.0.0
- Initial upload
77 changes: 77 additions & 0 deletions account/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Vonage Users Package

This package contains the code to use Vonage's Application API in Python.

It includes methods for managing applications.

## Usage

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`.

### List Applications

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.

```python
from vonage_application import ListApplicationsFilter, ApplicationData

applications, next_page = vonage_client.application.list_applications()

# With options
options = ListApplicationsFilter(page_size=3, page=2)
applications, next_page = vonage_client.application.list_applications(options)
```

### Create a New Application

```python
from vonage_application import ApplicationConfig

app_data = vonage_client.application.create_application()

# Create with custom options (can also be done with a dict)
from vonage_application import ApplicationConfig, Keys, Voice, VoiceWebhooks
voice = Voice(
webhooks=VoiceWebhooks(
event_url=VoiceUrl(
address='https://example.com/event',
http_method='POST',
connect_timeout=500,
socket_timeout=3000,
),
),
signed_callbacks=True,
)
capabilities = Capabilities(voice=voice)
keys = Keys(public_key='MY_PUBLIC_KEY')
config = ApplicationConfig(
name='My Customised Application',
capabilities=capabilities,
keys=keys,
)
app_data = vonage_client.application.create_application(config)
```

### Get an Application

```python
app_data = client.application.get_application('MY_APP_ID')
app_data_as_dict = app.model_dump(exclude_none=True)
```

### Update an Application

To update an application, pass config for the updated field(s) in an ApplicationConfig object

```python
from vonage_application import ApplicationConfig, Keys, Voice, VoiceWebhooks

config = ApplicationConfig(name='My Updated Application')
app_data = vonage_client.application.update_application('MY_APP_ID', config)
```

### Delete an Application

```python
vonage_client.applications.delete_application('MY_APP_ID')
```
29 changes: 29 additions & 0 deletions account/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[project]
name = 'vonage-account'
version = '1.0.0'
description = 'Vonage Account API package'
readme = "README.md"
authors = [{ name = "Vonage", email = "devrel@vonage.com" }]
requires-python = ">=3.8"
dependencies = [
"vonage-http-client>=1.3.1",
"vonage-utils>=1.1.2",
"pydantic>=2.7.1",
]
classifiers = [
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"License :: OSI Approved :: Apache Software License",
]

[project.urls]
homepage = "https://github.com/Vonage/vonage-python-sdk"

[build-system]
requires = ["setuptools>=61.0", "wheel"]
build-backend = "setuptools.build_meta"
1 change: 1 addition & 0 deletions account/src/vonage_account/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python_sources()
3 changes: 3 additions & 0 deletions account/src/vonage_account/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .account import Account

__all__ = ['Account']
98 changes: 98 additions & 0 deletions account/src/vonage_account/account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
from pydantic import validate_call
from vonage_http_client.http_client import HttpClient

from .requests import Balance
from .responses import SettingsResponse, TopUpResponse


class Account:
"""Class containing methods for management of a Vonage account."""

def __init__(self, http_client: HttpClient) -> None:
self._http_client = http_client
self._auth_type = 'basic'

@property
def http_client(self) -> HttpClient:
"""The HTTP client used to make requests to the Users API.
Returns:
HttpClient: The HTTP client used to make requests to the Users API.
"""
return self._http_client

@validate_call
def get_balance(self) -> Balance:
"""Get the balance of the account.
Returns:
Balance: Object containing the account balance and whether auto-reload is
enabled for the account.
"""

response = self._http_client.get(
self._http_client.rest_host,
'/account/get-balance',
auth_type=self._auth_type,
)
return Balance(**response)

@validate_call
def top_up(self, trx: str) -> TopUpResponse:
"""Top-up the account balance.
Args:
trx (str): The transaction reference of the transaction when auto-reload
was enabled on your account.
Returns:
TopUpResponse: Object containing the top-up response.
"""

response = self._http_client.post(
self._http_client.rest_host,
'/account/top-up',
params={'trx': trx},
auth_type=self._auth_type,
sent_data_type='form',
)
return TopUpResponse(**response)

@validate_call
def update_default_sms_webhook(
self, mo_callback_url: str = None, dr_callback_url: str = None
) -> SettingsResponse:
"""Update the default SMS webhook URLs for the account.
In order to unset any default value, pass an empty string as the value.
Args:
mo_callback_url (str, optional): The URL to which inbound SMS messages will be
sent.
dr_callback_url (str, optional): The URL to which delivery receipts will be sent.
Returns:
SettingsResponse: Object containing the response to the settings update.
"""

params = {}
if mo_callback_url is not None:
params['moCallbackUrl'] = mo_callback_url
if dr_callback_url is not None:
params['drCallbackUrl'] = dr_callback_url

response = self._http_client.post(
self._http_client.rest_host,
'/account/settings',
params=params,
auth_type=self._auth_type,
sent_data_type='form',
)
return SettingsResponse(**response)

def list_secrets(self) -> SecretList:
"""List all secrets associated with the account.
Returns:
SecretList: List of Secret objects.
"""
pass
5 changes: 5 additions & 0 deletions account/src/vonage_account/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from vonage_utils.errors import VonageError


class AccountError(VonageError):
"""Indicates an error with the Account package."""
8 changes: 8 additions & 0 deletions account/src/vonage_account/requests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from typing import Optional

from pydantic import BaseModel, Field


class Balance(BaseModel):
value: float
auto_reload: Optional[bool] = Field(None, validation_alias='autoReload')
22 changes: 22 additions & 0 deletions account/src/vonage_account/responses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import Optional

from pydantic import BaseModel, Field


class TopUpResponse(BaseModel):
error_code: Optional[str] = Field(None, validation_alias='error-code')
error_code_label: Optional[str] = Field(None, validation_alias='error-code-label')


class SettingsResponse(BaseModel):
mo_callback_url: Optional[str] = Field(None, validation_alias='mo-callback-url')
dr_callback_url: Optional[str] = Field(None, validation_alias='dr-callback-url')
max_outbound_request: Optional[int] = Field(
None, validation_alias='max-outbound-request'
)
max_inbound_request: Optional[int] = Field(
None, validation_alias='max-inbound-request'
)
max_calls_per_second: Optional[int] = Field(
None, validation_alias='max-calls-per-second'
)
1 change: 1 addition & 0 deletions account/tests/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python_tests(dependencies=['account', 'testutils'])
4 changes: 4 additions & 0 deletions account/tests/data/get_balance.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"value": 29.18202293,
"autoReload": false
}
4 changes: 4 additions & 0 deletions account/tests/data/top_up.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"error-code": "200",
"error-code-label": "success"
}
7 changes: 7 additions & 0 deletions account/tests/data/update_default_sms_webhook.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"mo-callback-url": "https://example.com/inbound_sms_webhook",
"dr-callback-url": "https://example.com/delivery_receipt_webhook",
"max-outbound-request": 30,
"max-inbound-request": 30,
"max-calls-per-second": 30
}
Loading

0 comments on commit 21ea88c

Please sign in to comment.