88from platform import python_version
99
1010import requests
11+ import json
1112
1213import telesign
1314from telesign .util import AuthMethod
@@ -118,14 +119,14 @@ def generate_telesign_headers(customer_id,
118119 content_type = "application/x-www-form-urlencoded" if method_name in ("POST" , "PUT" ) else ""
119120
120121 # Default auth_method is Digest if not explicitly specified
121- if auth_method == AuthMethod .BASIC . value :
122+ if auth_method == AuthMethod .BASIC :
122123 usr_apikey = "{customer_id}:{api_key}" .format (customer_id = customer_id ,
123124 api_key = api_key )
124125 b64val = b64encode (usr_apikey .encode ())
125- authorization = "{auth_method} {b64val}" .format (auth_method = AuthMethod .BASIC . value ,
126+ authorization = "{auth_method} {b64val}" .format (auth_method = AuthMethod .BASIC ,
126127 b64val = b64val .decode ())
127128 else :
128- auth_method = AuthMethod .HMAC_SHA256 . value
129+ auth_method = AuthMethod .HMAC_SHA256
129130
130131 string_to_sign_builder = ["{method}" .format (method = method_name )]
131132
@@ -164,7 +165,7 @@ def generate_telesign_headers(customer_id,
164165
165166 return headers
166167
167- def post (self , resource , body = None , ** query_params ):
168+ def post (self , resource , body = None , json_fields = None , ** query_params ):
168169 """
169170 Generic TeleSign REST API POST handler.
170171
@@ -173,9 +174,9 @@ def post(self, resource, body=None, **query_params):
173174 :param query_params: query_params to perform the POST request with, as a dictionary.
174175 :return: The RestClient Response object.
175176 """
176- return self ._execute (self .session .post , 'POST' , resource , body , ** query_params )
177+ return self ._execute (self .session .post , 'POST' , resource , body , json_fields , ** query_params )
177178
178- def get (self , resource , body = None , ** query_params ):
179+ def get (self , resource , body = None , json_fields = None , ** query_params ):
179180 """
180181 Generic TeleSign REST API GET handler.
181182
@@ -184,9 +185,9 @@ def get(self, resource, body=None, **query_params):
184185 :param query_params: query_params to perform the GET request with, as a dictionary.
185186 :return: The RestClient Response object.
186187 """
187- return self ._execute (self .session .get , 'GET' , resource , body , ** query_params )
188+ return self ._execute (self .session .get , 'GET' , resource , body , json_fields , ** query_params )
188189
189- def put (self , resource , body = None , ** query_params ):
190+ def put (self , resource , body = None , json_fields = None , ** query_params ):
190191 """
191192 Generic TeleSign REST API PUT handler.
192193
@@ -195,9 +196,9 @@ def put(self, resource, body=None, **query_params):
195196 :param query_params: query_params to perform the PUT request with, as a dictionary.
196197 :return: The RestClient Response object.
197198 """
198- return self ._execute (self .session .put , 'PUT' , resource , body , ** query_params )
199+ return self ._execute (self .session .put , 'PUT' , resource , body , json_fields , ** query_params )
199200
200- def delete (self , resource , body = None , ** query_params ):
201+ def delete (self , resource , body = None , json_fields = None , ** query_params ):
201202 """
202203 Generic TeleSign REST API DELETE handler.
203204
@@ -206,9 +207,9 @@ def delete(self, resource, body=None, **query_params):
206207 :param query_params: query_params to perform the DELETE request with, as a dictionary.
207208 :return: The RestClient Response object.
208209 """
209- return self ._execute (self .session .delete , 'DELETE' , resource , body , ** query_params )
210+ return self ._execute (self .session .delete , 'DELETE' , resource , body , json_fields , ** query_params )
210211
211- def _execute (self , method_function , method_name , resource , body = None , ** query_params ):
212+ def _execute (self , method_function , method_name , resource , body = None , json_fields = None , ** query_params ):
212213 """
213214 Generic TeleSign REST API request handler.
214215
@@ -222,7 +223,11 @@ def _execute(self, method_function, method_name, resource, body=None, **query_pa
222223 resource_uri = "{api_host}{resource}" .format (api_host = self .api_host , resource = resource )
223224
224225 url_encoded_fields = self ._encode_params (query_params )
225- if body :
226+ if json_fields :
227+ fields = json .dumps (json_fields )
228+ url_encoded_fields = fields
229+
230+ if body or json_fields :
226231 content_type = "application/json"
227232 else :
228233 content_type = None # set later
@@ -235,13 +240,14 @@ def _execute(self, method_function, method_name, resource, body=None, **query_pa
235240 user_agent = self .user_agent ,
236241 content_type = content_type ,
237242 auth_method = self .auth_method )
238-
239243 if method_name in ['POST' , 'PUT' ]:
240244 payload = {}
241245 if body :
242246 payload ['json' ] = body
243247 if query_params :
244248 payload ['data' ] = url_encoded_fields
249+ if json_fields :
250+ payload = {'data' : url_encoded_fields }
245251 else :
246252 payload = {'params' : url_encoded_fields }
247253
0 commit comments