|
| 1 | +from pydantic import validate_call |
| 2 | +from vonage_http_client.http_client import HttpClient |
| 3 | + |
| 4 | +from .errors import NumberInsightError |
| 5 | +from .requests import ( |
| 6 | + AdvancedAsyncInsightRequest, |
| 7 | + AdvancedSyncInsightRequest, |
| 8 | + BasicInsightRequest, |
| 9 | + StandardInsightRequest, |
| 10 | +) |
| 11 | +from .responses import ( |
| 12 | + AdvancedAsyncInsightResponse, |
| 13 | + AdvancedSyncInsightResponse, |
| 14 | + BasicInsightResponse, |
| 15 | + StandardInsightResponse, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +class NumberInsight: |
| 20 | + """Calls Vonage's Number Insight API.""" |
| 21 | + |
| 22 | + def __init__(self, http_client: HttpClient) -> None: |
| 23 | + self._http_client = http_client |
| 24 | + self._auth_type = 'body' |
| 25 | + |
| 26 | + @property |
| 27 | + def http_client(self) -> HttpClient: |
| 28 | + """The HTTP client used to make requests to the Verify V2 API. |
| 29 | +
|
| 30 | + Returns: |
| 31 | + HttpClient: The HTTP client used to make requests to the Verify V2 API. |
| 32 | + """ |
| 33 | + return self._http_client |
| 34 | + |
| 35 | + @validate_call |
| 36 | + def basic_number_insight(self, options: BasicInsightRequest) -> BasicInsightResponse: |
| 37 | + """Get basic number insight information about a phone number. |
| 38 | +
|
| 39 | + Args: |
| 40 | + Options (BasicInsightRequest): The options for the request. The `number` paramerter |
| 41 | + is required, and the `country_code` parameter is optional. |
| 42 | +
|
| 43 | + Returns: |
| 44 | + BasicInsightResponse: The response object containing the basic number insight |
| 45 | + information about the phone number. |
| 46 | + """ |
| 47 | + response = self._http_client.get( |
| 48 | + self._http_client.api_host, |
| 49 | + '/ni/basic/json', |
| 50 | + params=options.model_dump(exclude_none=True), |
| 51 | + auth_type=self._auth_type, |
| 52 | + ) |
| 53 | + self._check_for_error(response) |
| 54 | + |
| 55 | + return BasicInsightResponse(**response) |
| 56 | + |
| 57 | + @validate_call |
| 58 | + def standard_number_insight( |
| 59 | + self, options: StandardInsightRequest |
| 60 | + ) -> StandardInsightResponse: |
| 61 | + """Get standard number insight information about a phone number. |
| 62 | +
|
| 63 | + Args: |
| 64 | + Options (StandardInsightRequest): The options for the request. The `number` paramerter |
| 65 | + is required, and the `country_code` and `cnam` parameters are optional. |
| 66 | +
|
| 67 | + Returns: |
| 68 | + StandardInsightResponse: The response object containing the standard number insight |
| 69 | + information about the phone number. |
| 70 | + """ |
| 71 | + response = self._http_client.get( |
| 72 | + self._http_client.api_host, |
| 73 | + '/ni/standard/json', |
| 74 | + params=options.model_dump(exclude_none=True), |
| 75 | + auth_type=self._auth_type, |
| 76 | + ) |
| 77 | + self._check_for_error(response) |
| 78 | + |
| 79 | + return StandardInsightResponse(**response) |
| 80 | + |
| 81 | + @validate_call |
| 82 | + def advanced_async_number_insight( |
| 83 | + self, options: AdvancedAsyncInsightRequest |
| 84 | + ) -> AdvancedAsyncInsightResponse: |
| 85 | + """Get advanced number insight information about a phone number asynchronously. |
| 86 | +
|
| 87 | + Args: |
| 88 | + Options (AdvancedAsyncInsightRequest): The options for the request. You must provide values |
| 89 | + for the `callback` and `number` parameters. The `country_code` and `cnam` parameters |
| 90 | + are optional. |
| 91 | +
|
| 92 | + Returns: |
| 93 | + AdvancedAsyncInsightResponse: The response object containing the advanced number insight |
| 94 | + information about the phone number. |
| 95 | + """ |
| 96 | + response = self._http_client.get( |
| 97 | + self._http_client.api_host, |
| 98 | + '/ni/advanced/async/json', |
| 99 | + params=options.model_dump(exclude_none=True), |
| 100 | + auth_type=self._auth_type, |
| 101 | + ) |
| 102 | + |
| 103 | + return AdvancedAsyncInsightResponse(**response) |
| 104 | + |
| 105 | + @validate_call |
| 106 | + def advanced_sync_number_insight( |
| 107 | + self, options: AdvancedSyncInsightRequest |
| 108 | + ) -> AdvancedSyncInsightResponse: |
| 109 | + """Get advanced number insight information about a phone number synchronously. |
| 110 | +
|
| 111 | + Args: |
| 112 | + Options (AdvancedSyncInsightRequest): The options for the request. The `number` parameter |
| 113 | + is required, and the `country_code`, `cnam`, and `real_time_data` parameters are optional. |
| 114 | +
|
| 115 | + Returns: |
| 116 | + AdvancedSyncInsightResponse: The response object containing the advanced number insight |
| 117 | + information about the phone number. |
| 118 | + """ |
| 119 | + response = self._http_client.get( |
| 120 | + self._http_client.api_host, |
| 121 | + '/ni/advanced/json', |
| 122 | + params=options.model_dump(exclude_none=True), |
| 123 | + auth_type=self._auth_type, |
| 124 | + ) |
| 125 | + |
| 126 | + return AdvancedSyncInsightResponse(**response) |
| 127 | + |
| 128 | + def _check_for_error(self, response: dict) -> None: |
| 129 | + """Check for an error in the response from the Number Insight API. |
| 130 | +
|
| 131 | + Args: |
| 132 | + response (dict): The response from the Number Insight API. |
| 133 | +
|
| 134 | + Raises: |
| 135 | + NumberInsightError: If the response contains an error. |
| 136 | + """ |
| 137 | + if response['status'] != 0: |
| 138 | + error_message = f'Error with the following details: {response}' |
| 139 | + raise NumberInsightError(error_message) |
0 commit comments