From 4717e7d53e0697c5fb83ea621cd506bd920c2e2d Mon Sep 17 00:00:00 2001 From: maxkahan Date: Tue, 5 Nov 2024 18:44:23 +0000 Subject: [PATCH] add pricing methods --- account/README.md | 32 +++++++- account/src/vonage_account/__init__.py | 19 ++++- account/src/vonage_account/account.py | 77 ++++++++++++++++++- account/src/vonage_account/requests.py | 43 +++++++++++ account/src/vonage_account/responses.py | 60 +++++++++++++++ application/CHANGES.md | 4 +- .../src/vonage_application/_version.py | 2 +- sms/src/vonage_sms/sms.py | 2 +- vonage/pyproject.toml | 2 +- 9 files changed, 230 insertions(+), 11 deletions(-) create mode 100644 account/src/vonage_account/requests.py diff --git a/account/README.md b/account/README.md index 2974f04f..c3dcde0d 100644 --- a/account/README.md +++ b/account/README.md @@ -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 @@ -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. diff --git a/account/src/vonage_account/__init__.py b/account/src/vonage_account/__init__.py index 84b76801..fb3b3b93 100644 --- a/account/src/vonage_account/__init__.py +++ b/account/src/vonage_account/__init__.py @@ -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', diff --git a/account/src/vonage_account/account.py b/account/src/vonage_account/account.py index fec0fccc..6a29df7b 100644 --- a/account/src/vonage_account/account.py +++ b/account/src/vonage_account/account.py @@ -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: @@ -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. diff --git a/account/src/vonage_account/requests.py b/account/src/vonage_account/requests.py new file mode 100644 index 00000000..3a95d035 --- /dev/null +++ b/account/src/vonage_account/requests.py @@ -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 diff --git a/account/src/vonage_account/responses.py b/account/src/vonage_account/responses.py index 9e980e95..c094557d 100644 --- a/account/src/vonage_account/responses.py +++ b/account/src/vonage_account/responses.py @@ -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. diff --git a/application/CHANGES.md b/application/CHANGES.md index c6b3e0f6..37a93036 100644 --- a/application/CHANGES.md +++ b/application/CHANGES.md @@ -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 diff --git a/application/src/vonage_application/_version.py b/application/src/vonage_application/_version.py index 3f390799..afced147 100644 --- a/application/src/vonage_application/_version.py +++ b/application/src/vonage_application/_version.py @@ -1 +1 @@ -__version__ = '2.0.1' +__version__ = '2.0.0' diff --git a/sms/src/vonage_sms/sms.py b/sms/src/vonage_sms/sms.py index 5e78eeb8..b9e4b6ba 100644 --- a/sms/src/vonage_sms/sms.py +++ b/sms/src/vonage_sms/sms.py @@ -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' diff --git a/vonage/pyproject.toml b/vonage/pyproject.toml index 8d0d4e0d..bbabbf14 100644 --- a/vonage/pyproject.toml +++ b/vonage/pyproject.toml @@ -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",