Skip to content

Commit

Permalink
add other applications endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkahan committed May 2, 2024
1 parent ec0d9f2 commit 7e1186a
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 86 deletions.
38 changes: 11 additions & 27 deletions application/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,24 @@ It includes methods for managing applications.

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 Users

With no custom options specified, this method will get the last 100 users. It returns a tuple consisting of a list of `UserSummary` objects and a string describing the cursor to the next page of results.
### 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_users import ListUsersRequest
from vonage_application import ListApplicationsFilter, ApplicationData

users, _ = vonage_client.users.list_users()
applications, next_page = vonage_client.application.list_applications()

# With options
params = ListUsersRequest(
page_size=10,
cursor=my_cursor,
order='desc',
)
users, next_cursor = vonage_client.users.list_users(params)
options = ListApplicationsFilter(page_size=3, page=2)
applications, next_page = vonage_client.applications.list_applications(options)
```


--------


### Create a New User

```python
Expand Down
40 changes: 40 additions & 0 deletions application/src/vonage_application/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
from . import errors
from .application import Application
from .common import (
ApplicationUrl,
Capabilities,
Keys,
Messages,
MessagesWebhooks,
Privacy,
Rtc,
RtcWebhooks,
Vbc,
Verify,
VerifyWebhooks,
Voice,
VoiceUrl,
VoiceWebhooks,
)
from .enums import Region
from .requests import ApplicationConfig, ListApplicationsFilter
from .responses import ApplicationData, ListApplicationsResponse

__all__ = [
'Application',
'ApplicationConfig',
'ApplicationData',
'ApplicationUrl',
'Capabilities',
'Keys',
'ListApplicationsFilter',
'ListApplicationsResponse',
'Messages',
'MessagesWebhooks',
'Privacy',
'Region',
'Rtc',
'RtcWebhooks',
'Vbc',
'Verify',
'VerifyWebhooks',
'Voice',
'VoiceUrl',
'VoiceWebhooks',
'errors',
]
74 changes: 38 additions & 36 deletions application/src/vonage_application/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pydantic import validate_call
from vonage_http_client.http_client import HttpClient

from .requests import ApplicationOptions, ListApplicationsFilter
from .requests import ApplicationConfig, ListApplicationsFilter
from .responses import ApplicationData, ListApplicationsResponse


Expand Down Expand Up @@ -56,12 +56,13 @@ def list_applications(

@validate_call
def create_application(
self, params: Optional[ApplicationOptions] = None
self, params: Optional[ApplicationConfig] = None
) -> ApplicationData:
"""Create a new application.
Args:
params (Optional[ApplicationOptions]): The application options.
params (Optional[ApplicationConfig]): Parameters describing the
application options to set.
Returns:
ApplicationData: The created application object.
Expand All @@ -85,39 +86,40 @@ def get_application(self, id: str) -> ApplicationData:
ApplicationData: The created application object.
"""
response = self._http_client.get(
self._http_client.api_host, f'/v1/users/{id}', None, self._auth_type
self._http_client.api_host, f'/v2/applications/{id}', None, self._auth_type
)
return ApplicationData(**response)

# @validate_call
# def update_application(self, id: str, params: User) -> User:
# """Update a user.

# Args:
# id (str): The ID of the user to update.
# params (User): The updated user object.

# Returns:
# User: The updated user object.
# """
# response = self._http_client.patch(
# self._http_client.api_host,
# f'/v1/users/{id}',
# params.model_dump(exclude_none=True),
# self._auth_type,
# )
# return User(**response)

# @validate_call
# def delete_application(self, id: str) -> None:
# """Delete an application.

# Args:
# id (str): The ID of the application to delete.

# Returns:
# None
# """
# self._http_client.delete(
# self._http_client.api_host, f'/v2/applications/{id}', None, self._auth_type
# )
@validate_call
def update_application(self, id: str, params: ApplicationConfig) -> ApplicationData:
"""Update an application.
Args:
id (str): The ID of the application to update.
params (ApplicationConfig): Parameters describing the
application options to update.
Returns:
ApplicationData: The updated application object.
"""
response = self._http_client.put(
self._http_client.api_host,
f'/v2/applications/{id}',
params.model_dump(exclude_none=True),
self._auth_type,
)
return ApplicationData(**response)

@validate_call
def delete_application(self, id: str) -> None:
"""Delete an application.
Args:
id (str): The ID of the application to delete.
Returns:
None
"""
self._http_client.delete(
self._http_client.api_host, f'/v2/applications/{id}', None, self._auth_type
)
9 changes: 8 additions & 1 deletion application/src/vonage_application/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Literal, Optional

from pydantic import BaseModel, Field, field_validator
from pydantic import BaseModel, ConfigDict, Field, field_validator

from .enums import Region
from .errors import ApplicationError
Expand Down Expand Up @@ -103,10 +103,17 @@ class Capabilities(BaseModel):
verify: Optional[Verify] = None


class Keys(BaseModel):
model_config = ConfigDict(extra='allow')

public_key: Optional[str] = None


class ApplicationBase(BaseModel):
"""Base application object used in requests and responses when communicating with the Vonage
Application API."""

name: str
capabilities: Optional[Capabilities] = None
privacy: Optional[Privacy] = None
keys: Optional[Keys] = None
8 changes: 2 additions & 6 deletions application/src/vonage_application/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,5 @@ class ListApplicationsFilter(BaseModel):
page: int = None


class RequestKeys(BaseModel):
public_key: str


class ApplicationOptions(ApplicationBase):
keys: Optional[RequestKeys] = None
class ApplicationConfig(ApplicationBase):
pass
9 changes: 2 additions & 7 deletions application/src/vonage_application/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@
from pydantic import BaseModel, Field, model_validator
from vonage_utils.models import HalLinks, ResourceLink

from .common import ApplicationBase


class ResponseKeys(BaseModel):
public_key: Optional[str] = None
private_key: Optional[str] = None
from .common import ApplicationBase, Keys


class ApplicationData(ApplicationBase):
id: str
keys: Optional[ResponseKeys] = None
keys: Optional[Keys] = None
links: Optional[ResourceLink] = Field(None, validation_alias='_links', exclude=True)
link: Optional[str] = None

Expand Down
36 changes: 36 additions & 0 deletions application/tests/data/get_application.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"id": "1b1b1b1b-1b1b-1b1b-1b1b-1b1b1b1b1b1b",
"name": "My Server Demo",
"keys": {
"public_key": "-----BEGIN PUBLIC KEY-----\npublic_key_info_goes_here\n-----END PUBLIC KEY-----\n"
},
"privacy": {
"improve_ai": false
},
"capabilities": {
"voice": {
"webhooks": {
"event_url": {
"address": "http://example.ngrok.app/webhooks/events",
"http_method": "POST",
"socket_timeout": 10000,
"connect_timeout": 1000
},
"answer_url": {
"address": "http://example.ngrok.app/webhooks/answer",
"http_method": "GET",
"socket_timeout": 5000,
"connect_timeout": 1000
}
},
"signed_callbacks": true,
"conversations_ttl": 48,
"leg_persistence_time": 7
}
},
"_links": {
"self": {
"href": "/v2/applications/1b1b1b1b-1b1b-1b1b-1b1b-1b1b1b1b1b1b"
}
}
}
13 changes: 13 additions & 0 deletions application/tests/data/update_application.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"id": "1b1b1b1b-1b1b-1b1b-1b1b-1b1b1b1b1b1b",
"name": "My Updated Application",
"keys": {
"public_key": "-----BEGIN PUBLIC KEY-----\nupdated_public_key_info\n-----END PUBLIC KEY-----\n"
},
"capabilities": {},
"_links": {
"self": {
"href": "/v2/applications/1b1b1b1b-1b1b-1b1b-1b1b-1b1b1b1b1b1b"
}
}
}
Loading

0 comments on commit 7e1186a

Please sign in to comment.