|
1 | 1 | from pydantic import validate_call |
2 | 2 | from vonage_http_client.http_client import HttpClient |
3 | 3 |
|
| 4 | +from .errors import VerifyError |
4 | 5 | from .requests import BaseVerifyRequest, Psd2Request, VerifyRequest |
5 | | -from .responses import VerifyResponse |
| 6 | +from .responses import CheckCodeResponse, StartVerificationResponse |
6 | 7 |
|
7 | 8 |
|
8 | 9 | class Verify: |
9 | | - """Calls Vonage's Verify API.""" |
| 10 | + """Calls Vonage's Verify API. |
| 11 | +
|
| 12 | + This class provides methods to interact with Vonage's Verify API for starting verification |
| 13 | + processes. |
| 14 | + """ |
10 | 15 |
|
11 | 16 | def __init__(self, http_client: HttpClient) -> None: |
12 | 17 | self._http_client = http_client |
13 | | - self._sent_post_data_type = 'form' |
14 | | - self._sent_get_data_type = 'query_params' |
| 18 | + self._sent_data_type = 'form' |
15 | 19 | self._auth_type = 'body' |
16 | 20 |
|
17 | 21 | @validate_call |
18 | | - def start_verification(self, verify_request: VerifyRequest) -> VerifyResponse: |
19 | | - """Start a verification process.""" |
| 22 | + def start_verification( |
| 23 | + self, verify_request: VerifyRequest |
| 24 | + ) -> StartVerificationResponse: |
| 25 | + """Start a verification process. |
| 26 | +
|
| 27 | + Args: |
| 28 | + verify_request (VerifyRequest): The verification request object. |
| 29 | +
|
| 30 | + Returns: |
| 31 | + StartVerificationResponse: The response object containing the verification result. |
| 32 | + """ |
20 | 33 | return self._make_verify_request(verify_request) |
21 | 34 |
|
22 | 35 | @validate_call |
23 | | - def start_psd2_verification(self, verify_request: Psd2Request) -> VerifyResponse: |
24 | | - """Start a PSD2 verification process.""" |
| 36 | + def start_psd2_verification( |
| 37 | + self, verify_request: Psd2Request |
| 38 | + ) -> StartVerificationResponse: |
| 39 | + """Start a PSD2 verification process. |
| 40 | +
|
| 41 | + Args: |
| 42 | + verify_request (Psd2Request): The PSD2 verification request object. |
| 43 | +
|
| 44 | + Returns: |
| 45 | + StartVerificationResponse: The response object containing the verification result. |
| 46 | + """ |
25 | 47 | return self._make_verify_request(verify_request) |
26 | 48 |
|
27 | | - def _make_verify_request(self, verify_request: BaseVerifyRequest) -> VerifyResponse: |
| 49 | + @validate_call |
| 50 | + def check_code(self, request_id: str, code: str) -> CheckCodeResponse: |
| 51 | + """Check a verification code. |
| 52 | +
|
| 53 | + Args: |
| 54 | + request_id (str): The request ID. |
| 55 | + code (str): The verification code. |
| 56 | +
|
| 57 | + Returns: |
| 58 | + CheckCodeResponse: The response object containing the verification result. |
| 59 | + """ |
| 60 | + response = self._http_client.post( |
| 61 | + self._http_client.api_host, |
| 62 | + '/verify/check/json', |
| 63 | + {'request_id': request_id, 'code': code}, |
| 64 | + self._auth_type, |
| 65 | + self._sent_data_type, |
| 66 | + ) |
| 67 | + self._check_for_error(response) |
| 68 | + return CheckCodeResponse(**response) |
| 69 | + |
| 70 | + def _make_verify_request( |
| 71 | + self, verify_request: BaseVerifyRequest |
| 72 | + ) -> StartVerificationResponse: |
| 73 | + """Make a verify request. |
| 74 | +
|
| 75 | + This method makes a verify request to the Vonage Verify API. |
| 76 | +
|
| 77 | + Args: |
| 78 | + verify_request (BaseVerifyRequest): The verify request object. |
| 79 | +
|
| 80 | + Returns: |
| 81 | + VerifyResponse: The response object containing the verification result. |
| 82 | + """ |
28 | 83 | if type(verify_request) == VerifyRequest: |
29 | 84 | request_path = '/verify/json' |
30 | 85 | elif type(verify_request) == Psd2Request: |
31 | 86 | request_path = '/verify/psd2/json' |
32 | | - |
33 | 87 | response = self._http_client.post( |
34 | 88 | self._http_client.api_host, |
35 | 89 | request_path, |
36 | | - verify_request.model_dump(by_alias=True), |
| 90 | + verify_request.model_dump(by_alias=True, exclude_none=True), |
37 | 91 | self._auth_type, |
38 | | - self._sent_post_data_type, |
| 92 | + self._sent_data_type, |
39 | 93 | ) |
40 | | - return VerifyResponse(**response) |
41 | | - |
42 | | - # @validate_call |
43 | | - # def send(self, message: SmsMessage) -> SmsResponse: |
44 | | - # """Send an SMS message.""" |
45 | | - # response = self._http_client.post( |
46 | | - # self._http_client.rest_host, |
47 | | - # '/sms/json', |
48 | | - # message.model_dump(by_alias=True), |
49 | | - # self._auth_type, |
50 | | - # self._sent_data_type, |
51 | | - # ) |
52 | | - |
53 | | - # if int(response['message-count']) > 1: |
54 | | - # self._check_for_partial_failure(response) |
55 | | - # else: |
56 | | - # self._check_for_error(response) |
57 | | - # return SmsResponse(**response) |
58 | | - |
59 | | - # def _check_for_partial_failure(self, response_data): |
60 | | - # successful_messages = 0 |
61 | | - # total_messages = int(response_data['message-count']) |
62 | | - |
63 | | - # for message in response_data['messages']: |
64 | | - # if message['status'] == '0': |
65 | | - # successful_messages += 1 |
66 | | - # if successful_messages < total_messages: |
67 | | - # raise PartialFailureError(response_data) |
68 | | - |
69 | | - # def _check_for_error(self, response_data): |
70 | | - # message = response_data['messages'][0] |
71 | | - # if int(message['status']) != 0: |
72 | | - # raise SmsError( |
73 | | - # f'Sms.send_message method failed with error code {message["status"]}: {message["error-text"]}' |
74 | | - # ) |
75 | | - |
76 | | - # @validate_call |
77 | | - # def submit_sms_conversion( |
78 | | - # self, message_id: str, delivered: bool = True, timestamp: datetime = None |
79 | | - # ): |
80 | | - # """ |
81 | | - # Note: Not available without having this feature manually enabled on your account. |
82 | | - |
83 | | - # Notifies Vonage that an SMS was successfully received. |
84 | | - |
85 | | - # This method is used to submit conversion data about SMS messages that were successfully delivered. |
86 | | - # If you are using the Verify API for two-factor authentication (2FA), this information is sent to Vonage automatically, |
87 | | - # so you do not need to use this method for 2FA messages. |
88 | | - |
89 | | - # Args: |
90 | | - # message_id (str): The `message-id` returned by the `Sms.send` call. |
91 | | - # delivered (bool, optional): Set to `True` if the user replied to the message you sent. Otherwise, set to `False`. |
92 | | - # timestamp (datetime, optional): A `datetime` object containing the time the SMS arrived. |
93 | | - # """ |
94 | | - # params = { |
95 | | - # 'message-id': message_id, |
96 | | - # 'delivered': delivered, |
97 | | - # 'timestamp': (timestamp or datetime.now(timezone.utc)).strftime( |
98 | | - # '%Y-%m-%d %H:%M:%S' |
99 | | - # ), |
100 | | - # } |
101 | | - # self._http_client.post( |
102 | | - # self._http_client.api_host, |
103 | | - # '/conversions/sms', |
104 | | - # params, |
105 | | - # self._auth_type, |
106 | | - # self._sent_data_type, |
107 | | - # ) |
| 94 | + self._check_for_error(response) |
| 95 | + parsed_response = StartVerificationResponse(**response) |
| 96 | + |
| 97 | + return parsed_response |
| 98 | + |
| 99 | + def _check_for_error(self, response: dict) -> None: |
| 100 | + """Check for error in the response. |
| 101 | +
|
| 102 | + This method checks if the response contains an error and raises a VerifyError if an error is found. |
| 103 | +
|
| 104 | + Args: |
| 105 | + response (dict): The response object. |
| 106 | +
|
| 107 | + Raises: |
| 108 | + VerifyError: If an error is found in the response. |
| 109 | + """ |
| 110 | + print(self._http_client.last_request.body) |
| 111 | + if int(response['status']) != 0: |
| 112 | + error_message = f'Error with Vonage status code {response["status"]}: {response["error_text"]}.' |
| 113 | + if 'network' in response: |
| 114 | + error_message += f' Network ID: {response["network"]}' |
| 115 | + raise VerifyError(error_message) |
0 commit comments