|
| 1 | +from typing import List, Optional, Tuple |
| 2 | + |
| 3 | +from pydantic import validate_call |
| 4 | +from vonage_http_client.http_client import HttpClient |
| 5 | +from vonage_numbers.errors import NumbersError |
| 6 | +from .requests import ( |
| 7 | + SearchAvailableNumbersFilter, |
| 8 | + ListOwnedNumbersFilter, |
| 9 | + NumberParams, |
| 10 | + UpdateNumberParams, |
| 11 | +) |
| 12 | +from .responses import AvailableNumber, NumbersStatus, OwnedNumber |
| 13 | + |
| 14 | + |
| 15 | +class Numbers: |
| 16 | + """Class containing methods for Vonage Application management.""" |
| 17 | + |
| 18 | + def __init__(self, http_client: HttpClient) -> None: |
| 19 | + self._http_client = http_client |
| 20 | + self._auth_type = 'basic' |
| 21 | + self._sent_data_type = 'form' |
| 22 | + |
| 23 | + @property |
| 24 | + def http_client(self) -> HttpClient: |
| 25 | + """The HTTP client used to make requests to the Numbers API. |
| 26 | +
|
| 27 | + Returns: |
| 28 | + HttpClient: The HTTP client used to make requests to the Numbers API. |
| 29 | + """ |
| 30 | + return self._http_client |
| 31 | + |
| 32 | + @validate_call |
| 33 | + def list_owned_numbers( |
| 34 | + self, filter: ListOwnedNumbersFilter = ListOwnedNumbersFilter() |
| 35 | + ) -> Tuple[List[OwnedNumber], int, Optional[int]]: |
| 36 | + """List numbers you own. |
| 37 | +
|
| 38 | + By default, returns the first 100 numbers and the page index of |
| 39 | + the next page of results, if there are more than 100 numbers. |
| 40 | +
|
| 41 | + Args: |
| 42 | + filter (ListOwnedNumbersFilter): The filter object. |
| 43 | +
|
| 44 | + Returns: |
| 45 | + Tuple[List[OwnedNumber], int, Optional[int]]: A tuple containing a |
| 46 | + list of owned numbers, the total count of owned phone numbers |
| 47 | + and the next page index, if applicable. |
| 48 | + i.e. |
| 49 | + number_list: List[OwnedNumber], count: int, next_page_index: Optional[int]) |
| 50 | + """ |
| 51 | + response = self._http_client.get( |
| 52 | + self._http_client.rest_host, |
| 53 | + '/account/numbers', |
| 54 | + filter.model_dump(exclude_none=True), |
| 55 | + self._auth_type, |
| 56 | + ) |
| 57 | + |
| 58 | + index = filter.index or 1 |
| 59 | + page_size = filter.size |
| 60 | + |
| 61 | + numbers = [] |
| 62 | + try: |
| 63 | + for number in response['numbers']: |
| 64 | + numbers.append(OwnedNumber(**number)) |
| 65 | + except KeyError: |
| 66 | + return [], 0, None |
| 67 | + |
| 68 | + count = response['count'] |
| 69 | + if count > page_size * index: |
| 70 | + return numbers, count, index + 1 |
| 71 | + return numbers, count, None |
| 72 | + |
| 73 | + @validate_call |
| 74 | + def search_available_numbers( |
| 75 | + self, filter: SearchAvailableNumbersFilter |
| 76 | + ) -> Tuple[List[AvailableNumber], int, Optional[int]]: |
| 77 | + """Search for available numbers to buy. |
| 78 | +
|
| 79 | + By default, returns the first 100 numbers and the page index of |
| 80 | + the next page of results, if there are more than 100 numbers. |
| 81 | +
|
| 82 | + Args: |
| 83 | + filter (SearchAvailableNumbersFilter): The filter object. |
| 84 | +
|
| 85 | + Returns: |
| 86 | + Tuple[List[AvailableNumber], int, Optional[int]]: A tuple containing a |
| 87 | + list of available numbers, the total count of available phone numbers |
| 88 | + and the next page index, if applicable. |
| 89 | + i.e. |
| 90 | + number_list: List[AvailableNumber], count: int, next_page_index: Optional[int]) |
| 91 | + """ |
| 92 | + response = self._http_client.get( |
| 93 | + self._http_client.rest_host, |
| 94 | + '/number/search', |
| 95 | + filter.model_dump(exclude_none=True), |
| 96 | + self._auth_type, |
| 97 | + ) |
| 98 | + |
| 99 | + index = filter.index or 1 |
| 100 | + page_size = filter.size |
| 101 | + |
| 102 | + numbers = [] |
| 103 | + try: |
| 104 | + for number in response['numbers']: |
| 105 | + numbers.append(AvailableNumber(**number)) |
| 106 | + except KeyError: |
| 107 | + return [], 0, None |
| 108 | + |
| 109 | + count = response['count'] |
| 110 | + if count > page_size * index: |
| 111 | + return numbers, count, index + 1 |
| 112 | + return numbers, count, None |
| 113 | + |
| 114 | + @validate_call |
| 115 | + def buy_number(self, params: NumberParams) -> NumbersStatus: |
| 116 | + """Buy a number. |
| 117 | +
|
| 118 | + Args: |
| 119 | + params (NumberParams): The number parameters. |
| 120 | +
|
| 121 | + Returns: |
| 122 | + NumbersStatus: The status of the number purchase. |
| 123 | + """ |
| 124 | + response = self._http_client.post( |
| 125 | + self._http_client.rest_host, |
| 126 | + '/number/buy', |
| 127 | + params.model_dump(exclude_none=True), |
| 128 | + self._auth_type, |
| 129 | + self._sent_data_type, |
| 130 | + ) |
| 131 | + |
| 132 | + self._check_for_error(response) |
| 133 | + return NumbersStatus(**response) |
| 134 | + |
| 135 | + @validate_call |
| 136 | + def cancel_number(self, params: NumberParams) -> NumbersStatus: |
| 137 | + """Cancel a number. |
| 138 | +
|
| 139 | + Args: |
| 140 | + params (NumberParams): The number parameters. |
| 141 | +
|
| 142 | + Returns: |
| 143 | + NumbersStatus: The status of the number cancellation. |
| 144 | + """ |
| 145 | + response = self._http_client.post( |
| 146 | + self._http_client.rest_host, |
| 147 | + '/number/cancel', |
| 148 | + params.model_dump(exclude_none=True), |
| 149 | + self._auth_type, |
| 150 | + self._sent_data_type, |
| 151 | + ) |
| 152 | + |
| 153 | + self._check_for_error(response) |
| 154 | + return NumbersStatus(**response) |
| 155 | + |
| 156 | + @validate_call |
| 157 | + def update_number(self, params: UpdateNumberParams) -> NumbersStatus: |
| 158 | + """Update a number. |
| 159 | +
|
| 160 | + Args: |
| 161 | + params (UpdateNumberParams): The number parameters. |
| 162 | +
|
| 163 | + Returns: |
| 164 | + NumbersStatus: The status of the number update. |
| 165 | + """ |
| 166 | + response = self._http_client.post( |
| 167 | + self._http_client.rest_host, |
| 168 | + '/number/update', |
| 169 | + params.model_dump(exclude_none=True), |
| 170 | + self._auth_type, |
| 171 | + self._sent_data_type, |
| 172 | + ) |
| 173 | + |
| 174 | + self._check_for_error(response) |
| 175 | + return NumbersStatus(**response) |
| 176 | + |
| 177 | + def _check_for_error(self, response_data): |
| 178 | + if response_data['error-code'] != '200': |
| 179 | + raise NumbersError( |
| 180 | + f'Numbers API operation failed: {response_data["error-code"]} {response_data["error-code-label"]}' |
| 181 | + ) |
0 commit comments