4
4
import json
5
5
from os import environ
6
6
7
- import requests
8
-
9
7
import yoti_python_sdk
10
8
from yoti_python_sdk import aml
11
9
from yoti_python_sdk .activity_details import ActivityDetails
26
24
27
25
28
26
class Client (object ):
29
- def __init__ (self , sdk_id = None , pem_file_path = None ):
27
+ def __init__ (self , sdk_id = None , pem_file_path = None , request_handler = None ):
30
28
self .sdk_id = sdk_id or environ .get ("YOTI_CLIENT_SDK_ID" )
31
29
pem_file_path_env = environ .get ("YOTI_KEY_FILE_PATH" , pem_file_path )
32
30
33
- self .__crypto = Crypto .read_pem_file (pem_file_path_env )
31
+ if pem_file_path is not None :
32
+ error_source = "argument specified in Client()"
33
+ self .__crypto = Crypto .read_pem_file (pem_file_path , error_source )
34
+ elif pem_file_path_env is not None :
35
+ error_source = "specified by the YOTI_KEY_FILE_PATH env variable"
36
+ self .__crypto = Crypto .read_pem_file (pem_file_path_env , error_source )
37
+ else :
38
+ raise RuntimeError (NO_KEY_FILE_SPECIFIED_ERROR )
39
+
34
40
self .__endpoint = Endpoint (sdk_id )
41
+ self .__request_handler = request_handler
42
+
43
+ def make_request (self , http_method , endpoint , body ):
44
+ signed_request = (
45
+ SignedRequest .builder ()
46
+ .with_pem_file (self .__crypto )
47
+ .with_base_url (yoti_python_sdk .YOTI_API_ENDPOINT )
48
+ .with_endpoint (endpoint )
49
+ .with_http_method (http_method )
50
+ .with_payload (body )
51
+ .with_request_handler (self .__request_handler )
52
+ .build ()
53
+ )
54
+
55
+ response = signed_request .execute ()
56
+ return response
35
57
36
58
def get_activity_details (self , encrypted_request_token ):
37
59
response = self .__make_activity_details_request (encrypted_request_token )
@@ -70,24 +92,10 @@ def perform_aml_check(self, aml_profile):
70
92
if aml_profile is None :
71
93
raise TypeError ("aml_profile not set" )
72
94
73
- http_method = "POST"
74
-
75
- response = self .__make_aml_check_request (http_method , aml_profile )
95
+ response = self .__make_aml_check_request (aml_profile )
76
96
77
97
return aml .AmlResult (response .text )
78
98
79
- def make_request (self , http_method , endpoint , body ):
80
- url = yoti_python_sdk .YOTI_API_ENDPOINT + endpoint
81
- headers = self .__get_request_headers (endpoint , http_method , body )
82
- response = requests .request (
83
- http_method ,
84
- url ,
85
- headers = headers ,
86
- data = body ,
87
- verify = yoti_python_sdk .YOTI_API_VERIFY_SSL ,
88
- )
89
- return response
90
-
91
99
@property
92
100
def endpoints (self ):
93
101
return self .__endpoint
@@ -123,6 +131,7 @@ def __make_activity_details_request(self, encrypted_request_token):
123
131
.with_base_url (yoti_python_sdk .YOTI_API_ENDPOINT )
124
132
.with_endpoint (path )
125
133
.with_param ("appId" , self .sdk_id )
134
+ .with_request_handler (self .__request_handler )
126
135
.build ()
127
136
)
128
137
@@ -134,20 +143,25 @@ def __make_activity_details_request(self, encrypted_request_token):
134
143
135
144
return response
136
145
137
- def __make_aml_check_request (self , http_method , aml_profile ):
146
+ def __make_aml_check_request (self , aml_profile ):
138
147
aml_profile_json = json .dumps (aml_profile .__dict__ , sort_keys = True )
139
148
aml_profile_bytes = aml_profile_json .encode ()
140
- path = self .__endpoint .get_aml_request_url ()
141
- url = yoti_python_sdk .YOTI_API_ENDPOINT + path
142
- headers = self .__get_request_headers (path , http_method , aml_profile_bytes )
143
-
144
- response = requests .post (
145
- url = url ,
146
- headers = headers ,
147
- data = aml_profile_bytes ,
148
- verify = yoti_python_sdk .YOTI_API_VERIFY_SSL ,
149
+ path = self .__endpoint .get_aml_request_url (no_params = True )
150
+
151
+ signed_request = (
152
+ SignedRequest .builder ()
153
+ .with_pem_file (self .__crypto )
154
+ .with_base_url (yoti_python_sdk .YOTI_API_ENDPOINT )
155
+ .with_endpoint (path )
156
+ .with_payload (aml_profile_bytes )
157
+ .with_param ("appId" , self .sdk_id )
158
+ .with_post ()
159
+ .with_request_handler (self .__request_handler )
160
+ .build ()
149
161
)
150
162
163
+ response = signed_request .execute ()
164
+
151
165
self .http_error_handler (
152
166
response , {"default" : "Unsuccessful Yoti API call: {} {}" }
153
167
)
0 commit comments