Skip to content

Commit

Permalink
blackening the sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkahan committed Aug 24, 2023
1 parent e2cdd38 commit 41c3135
Show file tree
Hide file tree
Showing 33 changed files with 423 additions and 182 deletions.
36 changes: 21 additions & 15 deletions src/vonage/account.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from .errors import PricingTypeError

from deprecated import deprecated


class Account:
account_auth_type = 'params'
pricing_auth_type = 'params'
Expand All @@ -12,36 +14,40 @@ def __init__(self, client):
self._client = client

def get_balance(self):
return self._client.get(self._client.host(), "/account/get-balance", auth_type=Account.account_auth_type)
return self._client.get(
self._client.host(), "/account/get-balance", auth_type=Account.account_auth_type
)

def topup(self, params=None, **kwargs):
return self._client.post(
self._client.host(),
self._client.host(),
"/account/top-up",
params or kwargs,
params or kwargs,
auth_type=Account.account_auth_type,
body_is_json=False,
)

def get_country_pricing(self, country_code: str, type: str = 'sms'):
self._check_allowed_pricing_type(type)
return self._client.get(
self._client.host(),
f"/account/get-pricing/outbound/{type}",
self._client.host(),
f"/account/get-pricing/outbound/{type}",
{"country": country_code},
auth_type=Account.pricing_auth_type
auth_type=Account.pricing_auth_type,
)

def get_all_countries_pricing(self, type: str = 'sms'):
self._check_allowed_pricing_type(type)
return self._client.get(
self._client.host(), f"/account/get-full-pricing/outbound/{type}", auth_type=Account.pricing_auth_type
self._client.host(),
f"/account/get-full-pricing/outbound/{type}",
auth_type=Account.pricing_auth_type,
)

def get_prefix_pricing(self, prefix: str, type: str = 'sms'):
self._check_allowed_pricing_type(type)
return self._client.get(
self._client.host(),
self._client.host(),
f"/account/get-prefix-pricing/outbound/{type}",
{"prefix": prefix},
auth_type=Account.pricing_auth_type,
Expand All @@ -50,25 +56,25 @@ def get_prefix_pricing(self, prefix: str, type: str = 'sms'):
@deprecated(version='3.0.0', reason='The "account/get-phone-pricing" endpoint is deprecated.')
def get_sms_pricing(self, number: str):
return self._client.get(
self._client.host(),
"/account/get-phone-pricing/outbound/sms",
self._client.host(),
"/account/get-phone-pricing/outbound/sms",
{"phone": number},
auth_type=Account.pricing_auth_type,
)

@deprecated(version='3.0.0', reason='The "account/get-phone-pricing" endpoint is deprecated.')
def get_voice_pricing(self, number: str):
return self._client.get(
self._client.host(),
"/account/get-phone-pricing/outbound/voice",
self._client.host(),
"/account/get-phone-pricing/outbound/voice",
{"phone": number},
auth_type=Account.pricing_auth_type,
)

def update_default_sms_webhook(self, params=None, **kwargs):
return self._client.post(
self._client.host(),
"/account/settings",
self._client.host(),
"/account/settings",
params or kwargs,
auth_type=Account.account_auth_type,
body_is_json=False,
Expand All @@ -91,7 +97,7 @@ def get_secret(self, api_key, secret_id):
def create_secret(self, api_key, secret):
body = {"secret": secret}
return self._client.post(
self._client.api_host(),
self._client.api_host(),
f"/accounts/{api_key}/secrets",
body,
auth_type=Account.secrets_auth_type,
Expand Down
9 changes: 7 additions & 2 deletions src/vonage/application.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from deprecated import deprecated

@deprecated(version='3.0.0', reason='Renamed to Application as V1 is out of support and this new \
naming is in line with other APIs. Please use Application instead.')

@deprecated(
version='3.0.0',
reason='Renamed to Application as V1 is out of support and this new \
naming is in line with other APIs. Please use Application instead.',
)
class ApplicationV2:
auth_type = 'header'

Expand Down Expand Up @@ -83,6 +87,7 @@ def list_applications(self, page_size=None, page=None):
auth_type=ApplicationV2.auth_type,
)


class Application:
auth_type = 'header'

Expand Down
13 changes: 10 additions & 3 deletions src/vonage/ncco_builder/ncco.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,13 @@ def ensure_url_in_list(cls, v):
@validator('advancedMachineDetection')
def validate_advancedMachineDetection(cls, v):
if 'behavior' in v and v['behavior'] not in ('continue', 'hangup'):
raise ValueError('advancedMachineDetection["behavior"] must be one of: "continue", "hangup".')
raise ValueError(
'advancedMachineDetection["behavior"] must be one of: "continue", "hangup".'
)
if 'mode' in v and v['mode'] not in ('detect, detect_beep'):
raise ValueError('advancedMachineDetection["mode"] must be one of: "detect", "detect_beep".')
raise ValueError(
'advancedMachineDetection["mode"] must be one of: "detect", "detect_beep".'
)
return v

class Config:
Expand Down Expand Up @@ -145,7 +149,10 @@ class Input(Action):

action = Field('input', const=True)
type: Union[
Literal['dtmf', 'speech'], List[Literal['dtmf']], List[Literal['speech']], List[Literal['dtmf', 'speech']]
Literal['dtmf', 'speech'],
List[Literal['dtmf']],
List[Literal['speech']],
List[Literal['dtmf', 'speech']],
]
dtmf: Optional[Union[InputTypes.Dtmf, dict]]
speech: Optional[Union[InputTypes.Speech, dict]]
Expand Down
12 changes: 10 additions & 2 deletions src/vonage/ncco_builder/pay_prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ class TextPrompt(BaseModel):
type: Literal['CardNumber', 'ExpirationDate', 'SecurityCode']
text: str
errors: Dict[
Literal['InvalidCardType', 'InvalidCardNumber', 'InvalidExpirationDate', 'InvalidSecurityCode', 'Timeout'],
Literal[
'InvalidCardType',
'InvalidCardNumber',
'InvalidExpirationDate',
'InvalidSecurityCode',
'Timeout',
],
Dict[Literal['text'], str],
]

Expand All @@ -32,7 +38,9 @@ def check_valid_error_format(cls, v, values):
def check_allowed_values(errors, allowed_values, prompt_type):
for key in errors:
if key not in allowed_values:
raise ValueError(f'Value "{key}" is not a valid error for the "{prompt_type}" prompt type.')
raise ValueError(
f'Value "{key}" is not a valid error for the "{prompt_type}" prompt type.'
)

@classmethod
def create_voice_model(cls, dict) -> VoicePrompt:
Expand Down
40 changes: 30 additions & 10 deletions src/vonage/number_insight.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,48 @@
from .errors import CallbackRequiredError


class NumberInsight:
auth_type = 'params'

def __init__(self, client):
self._client = client

def get_basic_number_insight(self, params=None, **kwargs):
return self._client.get(self._client.api_host(), "/ni/basic/json", params or kwargs, auth_type=NumberInsight.auth_type)
return self._client.get(
self._client.api_host(),
"/ni/basic/json",
params or kwargs,
auth_type=NumberInsight.auth_type,
)

def get_standard_number_insight(self, params=None, **kwargs):
return self._client.get(self._client.api_host(), "/ni/standard/json", params or kwargs, auth_type=NumberInsight.auth_type)
return self._client.get(
self._client.api_host(),
"/ni/standard/json",
params or kwargs,
auth_type=NumberInsight.auth_type,
)

def get_advanced_number_insight(self, params=None, **kwargs):
return self._client.get(self._client.api_host(), "/ni/advanced/json", params or kwargs, auth_type=NumberInsight.auth_type)
return self._client.get(
self._client.api_host(),
"/ni/advanced/json",
params or kwargs,
auth_type=NumberInsight.auth_type,
)

def get_async_advanced_number_insight(self, params=None, **kwargs):
argoparams = params or kwargs
if "callback" in argoparams and type(argoparams["callback"]) == str and argoparams["callback"] != "":
if (
"callback" in argoparams
and type(argoparams["callback"]) == str
and argoparams["callback"] != ""
):
return self._client.get(
self._client.api_host(), "/ni/advanced/async/json", params or kwargs, auth_type=NumberInsight.auth_type
self._client.api_host(),
"/ni/advanced/async/json",
params or kwargs,
auth_type=NumberInsight.auth_type,
)
else:
raise CallbackRequiredError(
"A callback is needed for async advanced number insight"
)

raise CallbackRequiredError("A callback is needed for async advanced number insight")
16 changes: 12 additions & 4 deletions src/vonage/number_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ def __init__(self, client):
self._client = client

def get_account_numbers(self, params=None, **kwargs):
return self._client.get(self._client.host(), "/account/numbers", params or kwargs, auth_type=Numbers.auth_type)
return self._client.get(
self._client.host(), "/account/numbers", params or kwargs, auth_type=Numbers.auth_type
)

def get_available_numbers(self, country_code, params=None, **kwargs):
return self._client.get(
Expand All @@ -17,10 +19,16 @@ def get_available_numbers(self, country_code, params=None, **kwargs):
)

def buy_number(self, params=None, **kwargs):
return self._client.post(self._client.host(), "/number/buy", params or kwargs, **Numbers.defaults)
return self._client.post(
self._client.host(), "/number/buy", params or kwargs, **Numbers.defaults
)

def cancel_number(self, params=None, **kwargs):
return self._client.post(self._client.host(), "/number/cancel", params or kwargs, **Numbers.defaults)
return self._client.post(
self._client.host(), "/number/cancel", params or kwargs, **Numbers.defaults
)

def update_number(self, params=None, **kwargs):
return self._client.post(self._client.host(), "/number/update", params or kwargs, **Numbers.defaults)
return self._client.post(
self._client.host(), "/number/update", params or kwargs, **Numbers.defaults
)
12 changes: 9 additions & 3 deletions src/vonage/redact.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

from deprecated import deprecated

@deprecated(version='3.0.0', reason='This is a dev preview product and as such is not supported in this SDK.')

@deprecated(
version='3.0.0',
reason='This is a dev preview product and as such is not supported in this SDK.',
)
class Redact:
auth_type = 'header'

Expand All @@ -16,10 +20,12 @@ def redact_transaction(self, id: str, product: str, type=None):
params = {"id": id, "product": product}
if type is not None:
params["type"] = type
return self._client.post(self._client.api_host(), "/v1/redact/transaction", params, auth_type=Redact.auth_type)
return self._client.post(
self._client.api_host(), "/v1/redact/transaction", params, auth_type=Redact.auth_type
)

def _check_allowed_product_name(self, product):
if product not in self.allowed_product_names:
raise RedactError(
f'Invalid product name in redact request. Must be one of {self.allowed_product_names}.'
)
)
23 changes: 17 additions & 6 deletions src/vonage/short_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,29 @@ def __init__(self, client):
self._client = client

def send_2fa_message(self, params=None, **kwargs):
return self._client.post(self._client.host(), "/sc/us/2fa/json", params or kwargs, **ShortCodes.defaults)
return self._client.post(
self._client.host(), "/sc/us/2fa/json", params or kwargs, **ShortCodes.defaults
)

def send_event_alert_message(self, params=None, **kwargs):
return self._client.post(self._client.host(), "/sc/us/alert/json", params or kwargs, **ShortCodes.defaults)
return self._client.post(
self._client.host(), "/sc/us/alert/json", params or kwargs, **ShortCodes.defaults
)

def send_marketing_message(self, params=None, **kwargs):
return self._client.post(self._client.host(), "/sc/us/marketing/json", params or kwargs, **ShortCodes.defaults)
return self._client.post(
self._client.host(), "/sc/us/marketing/json", params or kwargs, **ShortCodes.defaults
)

def get_event_alert_numbers(self):
return self._client.get(self._client.host(), "/sc/us/alert/opt-in/query/json", auth_type=ShortCodes.auth_type)
return self._client.get(
self._client.host(), "/sc/us/alert/opt-in/query/json", auth_type=ShortCodes.auth_type
)

def resubscribe_event_alert_number(self, params=None, **kwargs):
return self._client.post(
self._client.host(), "/sc/us/alert/opt-in/manage/json", params or kwargs, **ShortCodes.defaults)

self._client.host(),
"/sc/us/alert/opt-in/manage/json",
params or kwargs,
**ShortCodes.defaults,
)
17 changes: 10 additions & 7 deletions src/vonage/sms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@
from datetime import datetime
from ._internal import _format_date_param


class Sms:
defaults = {'auth_type': 'params', 'body_is_json': False}

def __init__(self, client):
self._client = client

def send_message(self, params):
"""
Send an SMS message.
Requires a client initialized with `key` and either `secret` or `signature_secret`.
:param dict params: A dict of values described at `Send an SMS <https://developer.vonage.com/api/sms#send-an-sms>`_
"""
return self._client.post(
self._client.host(),
"/sms/json",
params,
self._client.host(),
"/sms/json",
params,
supports_signature_auth=True,
**Sms.defaults,
)

def submit_sms_conversion(self, message_id, delivered=True, timestamp=None):
"""
Notify Vonage that an SMS was successfully received.
Expand All @@ -37,8 +38,10 @@ def submit_sms_conversion(self, message_id, delivered=True, timestamp=None):
params = {
"message-id": message_id,
"delivered": delivered,
"timestamp": timestamp or datetime.now(pytz.utc)
"timestamp": timestamp or datetime.now(pytz.utc),
}
# Ensure timestamp is a string:
_format_date_param(params, "timestamp")
return self._client.post(self._client.api_host(), "/conversions/sms", params, **Sms.defaults)
return self._client.post(
self._client.api_host(), "/conversions/sms", params, **Sms.defaults
)
10 changes: 7 additions & 3 deletions src/vonage/ussd.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
class Ussd:
defaults = {'auth_type': 'params', 'body_is_json': False}

def __init__(self, client):
self._client = client

def send_ussd_push_message(self, params=None, **kwargs):
return self._client.post(self._client.host(), "/ussd/json", params or kwargs, **Ussd.defaults)
return self._client.post(
self._client.host(), "/ussd/json", params or kwargs, **Ussd.defaults
)

def send_ussd_prompt_message(self, params=None, **kwargs):
return self._client.post(self._client.host(), "/ussd-prompt/json", params or kwargs, **Ussd.defaults)
return self._client.post(
self._client.host(), "/ussd-prompt/json", params or kwargs, **Ussd.defaults
)
Loading

0 comments on commit 41c3135

Please sign in to comment.