Skip to content

Commit

Permalink
add pricing methods
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkahan committed Nov 5, 2024
1 parent c34b9cb commit 4717e7d
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 11 deletions.
32 changes: 30 additions & 2 deletions account/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

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

It includes methods for managing Vonage accounts.
It includes methods for managing Vonage accounts, managing account secrets and querying country pricing.

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


### Get Account Balance

```python
Expand All @@ -23,6 +22,35 @@ response = vonage_client.account.top_up(trx='1234567890')
print(response)
```

### Get Service Pricing for a Specific Country

```python
from vonage_account import GetCountryPricingRequest

response = vonage_client.account.get_country_pricing(
GetCountryPricingRequest(type='sms', country_code='US')
)
print(response)
```

### Get Service Pricing for All Countries

```python
response = vonage_client.account.get_all_countries_pricing(service_type='sms')
print(response)
```

### Get Service Pricing by Dialing Prefix

```python
from vonage_account import GetPrefixPricingRequest

response = client.account.get_prefix_pricing(
GetPrefixPricingRequest(prefix='44', type='sms')
)
print(response)
```

### Update the Default SMS Webhook

This will return a Pydantic object (`SettingsResponse`) containing multiple settings for your account.
Expand Down
19 changes: 17 additions & 2 deletions account/src/vonage_account/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
from .account import Account
from .errors import InvalidSecretError
from .responses import Balance, SettingsResponse, TopUpResponse, VonageApiSecret
from .requests import GetCountryPricingRequest, GetPrefixPricingRequest, ServiceType
from .responses import (
Balance,
GetPricingResponse,
GetMultiplePricingResponse,
NetworkPricing,
SettingsResponse,
TopUpResponse,
VonageApiSecret,
)

__all__ = [
'Account',
'Balance',
'InvalidSecretError',
'GetCountryPricingRequest',
'GetPrefixPricingRequest',
'ServiceType',
'Balance',
'GetPricingResponse',
'GetMultiplePricingResponse',
'NetworkPricing',
'SettingsResponse',
'TopUpResponse',
'VonageApiSecret',
Expand Down
77 changes: 76 additions & 1 deletion account/src/vonage_account/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@

from pydantic import validate_call
from vonage_account.errors import InvalidSecretError
from vonage_account.requests import (
GetCountryPricingRequest,
GetPrefixPricingRequest,
ServiceType,
)
from vonage_http_client.http_client import HttpClient

from .responses import Balance, SettingsResponse, TopUpResponse, VonageApiSecret
from .responses import (
Balance,
GetMultiplePricingResponse,
GetPricingResponse,
SettingsResponse,
TopUpResponse,
VonageApiSecret,
)


class Account:
Expand Down Expand Up @@ -90,6 +102,69 @@ def update_default_sms_webhook(
)
return SettingsResponse(**response)

@validate_call
def get_country_pricing(
self, options: GetCountryPricingRequest
) -> GetPricingResponse:
"""Get the pricing for a specific country.
Args:
options (GetCountryPricingRequest): The options for the request.
Returns:
GetCountryPricingResponse: The response from the API.
"""
response = self._http_client.get(
self._http_client.rest_host,
f'/account/get-pricing/outbound/{options.type.value}',
params={'country': options.country_code},
auth_type=self._auth_type,
)

return GetPricingResponse(**response)

@validate_call
def get_all_countries_pricing(
self, service_type: ServiceType
) -> GetMultiplePricingResponse:
"""Get the pricing for all countries.
Args:
service_type (ServiceType): The type of service to retrieve pricing data about.
Returns:
GetMultiplePricingResponse: Model containing the pricing data for all countries.
"""
response = self._http_client.get(
self._http_client.rest_host,
f'/account/get-full-pricing/outbound/{service_type.value}',
auth_type=self._auth_type,
)

return GetMultiplePricingResponse(**response)

@validate_call
def get_prefix_pricing(
self, options: GetPrefixPricingRequest
) -> GetMultiplePricingResponse:
"""Get the pricing for a specific prefix.
Args:
options (GetPrefixPricingRequest): The options for the request.
Returns:
GetMultiplePricingResponse: Model containing the pricing data for all
countries using the dialling prefix.
"""
response = self._http_client.get(
self._http_client.rest_host,
f'/account/get-prefix-pricing/outbound/{options.type.value}',
params={'prefix': options.prefix},
auth_type=self._auth_type,
)

return GetMultiplePricingResponse(**response)

def list_secrets(self) -> list[VonageApiSecret]:
"""List all secrets associated with the account.
Expand Down
43 changes: 43 additions & 0 deletions account/src/vonage_account/requests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from pydantic import BaseModel
from enum import Enum


class ServiceType(str, Enum):
"""The service you wish to retrieve outbound pricing data about.
Values:
```
SMS: SMS
SMS_TRANSIT: SMS transit
VOICE: Voice
```
"""

SMS = 'sms'
SMS_TRANSIT = 'sms-transit'
VOICE = 'voice'


class GetCountryPricingRequest(BaseModel):
"""The options for getting the pricing for a specific country.
Args:
country_code (str): The two-letter country code for the country to retrieve
pricing data about.
type (ServiceType, Optional): The type of service to retrieve pricing data about.
"""

country_code: str
type: ServiceType = ServiceType.SMS


class GetPrefixPricingRequest(BaseModel):
"""The options for getting the pricing for a specific prefix.
Args:
prefix (str): The numerical dialing prefix to look up pricing for, e.g. "1", "44".
type (ServiceType, Optional): The type of service to retrieve pricing data about.
"""

prefix: str
type: ServiceType = ServiceType.SMS
60 changes: 60 additions & 0 deletions account/src/vonage_account/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,66 @@ class SettingsResponse(BaseModel):
)


class NetworkPricing(BaseModel):
"""Model for network pricing data.
Args:
aliases (list[str], Optional): A list of aliases for the network.
currency (str, Optional): The currency code for the pricing data.
mcc (str, Optional): The mobile country code.
mnc (str, Optional): The mobile network code.
network_code (str, Optional): The network code.
network_name (str, Optional): The network name.
price (str, Optional): The price for the service.
type (str, Optional): The type of service.
"""

aliases: Optional[list[str]] = None
currency: Optional[str] = None
mcc: Optional[str] = None
mnc: Optional[str] = None
network_code: Optional[str] = Field(None, validation_alias='networkCode')
network_name: Optional[str] = Field(None, validation_alias='networkName')
price: Optional[str] = None
type: Optional[str] = None


class GetPricingResponse(BaseModel):
"""Model for a response to a request for pricing data.
Args:
country_code (str, Optional): The two-letter country code.
country_display_name (str, Optional): The display name of the country.
country_name (str, Optional): The name of the country.
currency (str, Optional): The currency code for the pricing data.
default_price (str, Optional): The default price for the service.
dialing_prefix (str, Optional): The dialing prefix for the country.
networks (list[NetworkPricing], Optional): A list of network pricing data.
"""

country_code: Optional[str] = Field(None, validation_alias='countryCode')
country_display_name: Optional[str] = Field(
None, validation_alias='countryDisplayName'
)
country_name: Optional[str] = Field(None, validation_alias='countryName')
currency: Optional[str] = None
default_price: Optional[str] = Field(None, validation_alias='defaultPrice')
dialing_prefix: Optional[str] = Field(None, validation_alias='dialingPrefix')
networks: Optional[list[NetworkPricing]] = None


class GetMultiplePricingResponse(BaseModel):
"""Model for multiple countries' pricing data.
Args:
count (int): The number of countries.
countries (list[GetCountryPricingResponse]): A list of country pricing data.
"""

count: int
countries: list[GetPricingResponse]


class VonageApiSecret(BaseModel):
"""Model for a Vonage API secret.
Expand Down
4 changes: 1 addition & 3 deletions application/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# 2.0.1
- Update dependency versions

# 2.0.0
- Rename `params` -> `config` in method arguments
- Update dependency versions

# 1.0.3
- Support for Python 3.13, drop support for 3.8
Expand Down
2 changes: 1 addition & 1 deletion application/src/vonage_application/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.0.1'
__version__ = '2.0.0'
2 changes: 1 addition & 1 deletion sms/src/vonage_sms/sms.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Sms:
def __init__(self, http_client: HttpClient) -> None:
self._http_client = http_client
self._sent_data_type = 'form'
if self._http_client._auth._signature_secret:
if self._http_client.auth._signature_secret:
self._auth_type = 'signature'
else:
self._auth_type = 'basic'
Expand Down
2 changes: 1 addition & 1 deletion vonage/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencies = [
"vonage-utils>=1.1.4",
"vonage-http-client>=1.4.3",
"vonage-account>=1.0.3",
"vonage-application>=2.0.1",
"vonage-application>=2.0.0",
"vonage-messages>=1.2.3",
"vonage-network-auth>=1.0.1",
"vonage-network-sim-swap>=1.1.1",
Expand Down

0 comments on commit 4717e7d

Please sign in to comment.