1- from pydantic import validate_call
1+ import re
2+ from typing import List , Optional , Union
3+ from pydantic import Field , validate_call
24from vonage_http_client .http_client import HttpClient
35
46from .errors import VerifyError
57from .requests import BaseVerifyRequest , Psd2Request , VerifyRequest
6- from .responses import CheckCodeResponse , StartVerificationResponse
8+ from .responses import (
9+ CheckCodeResponse ,
10+ NetworkUnblockStatus ,
11+ StartVerificationResponse ,
12+ VerifyControlStatus ,
13+ VerifyStatus ,
14+ )
715
816
917class Verify :
@@ -67,6 +75,85 @@ def check_code(self, request_id: str, code: str) -> CheckCodeResponse:
6775 self ._check_for_error (response )
6876 return CheckCodeResponse (** response )
6977
78+ @validate_call
79+ def search (
80+ self , request : Union [str , List [str ]]
81+ ) -> Union [VerifyStatus , List [VerifyStatus ]]:
82+ """Search for past or current verification requests.
83+
84+ Args:
85+ request (str | list[str]): The request ID, or a list of request IDs.
86+
87+ Returns:
88+ Union[VerifyStatus, List[VerifyStatus]]: Either the response object
89+ containing the verification result, or a list of response objects.
90+ """
91+ params = {}
92+ if type (request ) == str :
93+ params ['request_id' ] = request
94+ elif type (request ) == list :
95+ params ['request_ids' ] = request
96+
97+ response = self ._http_client .get (
98+ self ._http_client .api_host , '/verify/search/json' , params , self ._auth_type
99+ )
100+
101+ if 'verification_requests' in response :
102+ parsed_response = []
103+ for verification_request in response ['verification_requests' ]:
104+ parsed_response .append (VerifyStatus (** verification_request ))
105+ return parsed_response
106+ elif 'error_text' in response :
107+ error_message = f'Error with the following details: { response } '
108+ raise VerifyError (error_message )
109+ else :
110+ parsed_response = VerifyStatus (** response )
111+ return parsed_response
112+
113+ @validate_call
114+ def cancel_verification (self , request_id : str ) -> VerifyControlStatus :
115+ """Cancel a verification request.
116+
117+ Args:
118+ request_id (str): The request ID.
119+
120+ Returns:
121+ VerifyControlStatus: The response object containing details of the submitted
122+ verification control.
123+ """
124+ response = self ._http_client .post (
125+ self ._http_client .api_host ,
126+ '/verify/control/json' ,
127+ {'request_id' : request_id , 'cmd' : 'cancel' },
128+ self ._auth_type ,
129+ self ._sent_data_type ,
130+ )
131+ self ._check_for_error (response )
132+
133+ return VerifyControlStatus (** response )
134+
135+ @validate_call
136+ def trigger_next_event (self , request_id : str ) -> VerifyControlStatus :
137+ """Trigger the next event in the verification process.
138+
139+ Args:
140+ request_id (str): The request ID.
141+
142+ Returns:
143+ VerifyControlStatus: The response object containing details of the submitted
144+ verification control.
145+ """
146+ response = self ._http_client .post (
147+ self ._http_client .api_host ,
148+ '/verify/control/json' ,
149+ {'request_id' : request_id , 'cmd' : 'trigger_next_event' },
150+ self ._auth_type ,
151+ self ._sent_data_type ,
152+ )
153+ self ._check_for_error (response )
154+
155+ return VerifyControlStatus (** response )
156+
70157 def _make_verify_request (
71158 self , verify_request : BaseVerifyRequest
72159 ) -> StartVerificationResponse :
@@ -84,6 +171,7 @@ def _make_verify_request(
84171 request_path = '/verify/json'
85172 elif type (verify_request ) == Psd2Request :
86173 request_path = '/verify/psd2/json'
174+
87175 response = self ._http_client .post (
88176 self ._http_client .api_host ,
89177 request_path ,
@@ -92,24 +180,21 @@ def _make_verify_request(
92180 self ._sent_data_type ,
93181 )
94182 self ._check_for_error (response )
95- parsed_response = StartVerificationResponse (** response )
96183
97- return parsed_response
184+ return StartVerificationResponse ( ** response )
98185
99186 def _check_for_error (self , response : dict ) -> None :
100187 """Check for error in the response.
101188
102- This method checks if the response contains an error and raises a VerifyError if an error is found.
189+ This method checks if the response contains a non-zero status code
190+ and raises a VerifyError if this is found.
103191
104192 Args:
105193 response (dict): The response object.
106194
107195 Raises:
108196 VerifyError: If an error is found in the response.
109197 """
110- print (self ._http_client .last_request .body )
111198 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" ]} '
199+ error_message = f'Error with the following details: { response } '
115200 raise VerifyError (error_message )
0 commit comments