|
| 1 | +from .errors import PricingTypeError |
| 2 | + |
| 3 | +from deprecated import deprecated |
1 | 4 | class Account:
|
| 5 | + account_auth_type = 'params' |
| 6 | + pricing_auth_type = 'params' |
| 7 | + secrets_auth_type = 'header' |
| 8 | + |
| 9 | + allowed_pricing_types = {'sms', 'sms-transit', 'voice'} |
| 10 | + |
2 | 11 | def __init__(self, client):
|
3 | 12 | self._client = client
|
4 | 13 |
|
5 | 14 | def get_balance(self):
|
6 |
| - return self._client.get(self._client.host(), "/account/get-balance") |
| 15 | + return self._client.get(self._client.host(), "/account/get-balance", auth_type=Account.account_auth_type) |
7 | 16 |
|
8 | 17 | def topup(self, params=None, **kwargs):
|
9 |
| - return self._client.post(self._client.host(), "/account/top-up", params or kwargs) |
| 18 | + return self._client.post( |
| 19 | + self._client.host(), |
| 20 | + "/account/top-up", |
| 21 | + params or kwargs, |
| 22 | + auth_type=Account.account_auth_type, |
| 23 | + body_is_json=False, |
| 24 | + ) |
| 25 | + |
| 26 | + def get_country_pricing(self, country_code: str, type: str = 'sms'): |
| 27 | + self._check_allowed_pricing_type(type) |
| 28 | + return self._client.get( |
| 29 | + self._client.host(), |
| 30 | + f"/account/get-pricing/outbound/{type}", |
| 31 | + {"country": country_code}, |
| 32 | + auth_type=Account.pricing_auth_type |
| 33 | + ) |
10 | 34 |
|
11 |
| - def get_country_pricing(self, country_code): |
| 35 | + def get_all_countries_pricing(self, type: str = 'sms'): |
| 36 | + self._check_allowed_pricing_type(type) |
12 | 37 | return self._client.get(
|
13 |
| - self._client.host(), "/account/get-pricing/outbound", {"country": country_code} |
| 38 | + self._client.host(), f"/account/get-full-pricing/outbound/{type}", auth_type=Account.pricing_auth_type |
14 | 39 | )
|
15 | 40 |
|
16 |
| - def get_prefix_pricing(self, prefix): |
| 41 | + def get_prefix_pricing(self, prefix: str, type: str = 'sms'): |
| 42 | + self._check_allowed_pricing_type(type) |
17 | 43 | return self._client.get(
|
18 |
| - self._client.host(), "/account/get-prefix-pricing/outbound", {"prefix": prefix} |
| 44 | + self._client.host(), |
| 45 | + f"/account/get-prefix-pricing/outbound/{type}", |
| 46 | + {"prefix": prefix}, |
| 47 | + auth_type=Account.pricing_auth_type, |
19 | 48 | )
|
20 | 49 |
|
21 |
| - def get_sms_pricing(self, number): |
| 50 | + @deprecated(version='3.0.0', reason='The "account/get-phone-pricing" endpoint is deprecated.') |
| 51 | + def get_sms_pricing(self, number: str): |
22 | 52 | return self._client.get(
|
23 |
| - self._client.host(), "/account/get-phone-pricing/outbound/sms", {"phone": number} |
| 53 | + self._client.host(), |
| 54 | + "/account/get-phone-pricing/outbound/sms", |
| 55 | + {"phone": number}, |
| 56 | + auth_type=Account.pricing_auth_type, |
24 | 57 | )
|
25 | 58 |
|
26 |
| - def get_voice_pricing(self, number): |
| 59 | + @deprecated(version='3.0.0', reason='The "account/get-phone-pricing" endpoint is deprecated.') |
| 60 | + def get_voice_pricing(self, number: str): |
27 | 61 | return self._client.get(
|
28 |
| - self._client.host(), "/account/get-phone-pricing/outbound/voice", {"phone": number} |
| 62 | + self._client.host(), |
| 63 | + "/account/get-phone-pricing/outbound/voice", |
| 64 | + {"phone": number}, |
| 65 | + auth_type=Account.pricing_auth_type, |
29 | 66 | )
|
30 | 67 |
|
31 | 68 | def update_default_sms_webhook(self, params=None, **kwargs):
|
32 |
| - return self._client.post(self._client.host(), "/account/settings", params or kwargs) |
| 69 | + return self._client.post( |
| 70 | + self._client.host(), |
| 71 | + "/account/settings", |
| 72 | + params or kwargs, |
| 73 | + auth_type=Account.account_auth_type, |
| 74 | + body_is_json=False, |
| 75 | + ) |
33 | 76 |
|
34 | 77 | def list_secrets(self, api_key):
|
35 | 78 | return self._client.get(
|
36 | 79 | self._client.api_host(),
|
37 | 80 | f"/accounts/{api_key}/secrets",
|
38 |
| - header_auth=True, |
| 81 | + auth_type=Account.secrets_auth_type, |
39 | 82 | )
|
40 | 83 |
|
41 | 84 | def get_secret(self, api_key, secret_id):
|
42 | 85 | return self._client.get(
|
43 | 86 | self._client.api_host(),
|
44 | 87 | f"/accounts/{api_key}/secrets/{secret_id}",
|
45 |
| - header_auth=True, |
| 88 | + auth_type=Account.secrets_auth_type, |
46 | 89 | )
|
47 | 90 |
|
48 | 91 | def create_secret(self, api_key, secret):
|
49 | 92 | body = {"secret": secret}
|
50 |
| - return self._client._post_json( |
51 |
| - self._client.api_host(), f"/accounts/{api_key}/secrets", body |
| 93 | + return self._client.post( |
| 94 | + self._client.api_host(), |
| 95 | + f"/accounts/{api_key}/secrets", |
| 96 | + body, |
| 97 | + auth_type=Account.secrets_auth_type, |
| 98 | + body_is_json=False, |
52 | 99 | )
|
53 | 100 |
|
54 | 101 | def revoke_secret(self, api_key, secret_id):
|
55 | 102 | return self._client.delete(
|
56 | 103 | self._client.api_host(),
|
57 | 104 | f"/accounts/{api_key}/secrets/{secret_id}",
|
58 |
| - header_auth=True, |
| 105 | + auth_type=Account.secrets_auth_type, |
59 | 106 | )
|
| 107 | + |
| 108 | + def _check_allowed_pricing_type(self, type): |
| 109 | + if type not in Account.allowed_pricing_types: |
| 110 | + raise PricingTypeError('Invalid pricing type specified.') |
0 commit comments