Skip to content

Commit 156c856

Browse files
Pull request #2: Fix failing phoneid/REST client in python_telesign sdk
Merge in SDK/python_telesign from hotfix/92035 to master * commit 'b36f998c8b3457f59c07ddf08a247d410d4b7eec': Fix import Take variables of env fix import module Fix issue method phoneid Fix issue method phoneid
2 parents efdf977 + b36f998 commit 156c856

File tree

3 files changed

+70
-42
lines changed

3 files changed

+70
-42
lines changed

telesign/phoneid.py

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,5 @@ def phoneid(self, phone_number, **params):
2424
See https://developer.telesign.com/docs/phoneid-api for detailed API documentation.
2525
"""
2626
return self.post(PHONEID_RESOURCE.format(phone_number=phone_number),
27-
**params)
28-
29-
def _execute(self, method_function, method_name, resource, **params):
30-
resource_uri = "{api_host}{resource}".format(api_host=self.api_host, resource=resource)
31-
32-
json_fields = json.dumps(params)
33-
34-
content_type = "application/json" if method_name in ("POST", "PUT") else ""
35-
36-
headers = self.generate_telesign_headers(self.customer_id,
37-
self.api_key,
38-
method_name,
39-
resource,
40-
json_fields,
41-
user_agent=self.user_agent,
42-
content_type=content_type)
43-
44-
if method_name in ['POST', 'PUT']:
45-
payload = {'data': json_fields}
46-
else:
47-
payload = {'params': json_fields}
48-
49-
response = self.Response(method_function(resource_uri,
50-
headers=headers,
51-
timeout=self.timeout,
52-
**payload))
53-
54-
return response
27+
json_fields=params,
28+
**params)

telesign/rest.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from platform import python_version
99

1010
import requests
11+
import json
1112

1213
import telesign
1314
from 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

tests/test_phoneid.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from __future__ import unicode_literals
2+
import os
3+
from unittest import TestCase
4+
from telesign.phoneid import PhoneIdClient
5+
6+
class TestPhoneId(TestCase):
7+
def setUp(self):
8+
self.customer_id = os.getenv('CUSTOMER_ID', 'FFFFFFFF-EEEE-DDDD-1234-AB1234567890')
9+
self.api_key = os.getenv('API_KEY', 'EXAMPLE----TE8sTgg45yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==')
10+
self.phone_number_test = "11234567890"
11+
12+
def test_phoneid_constructor(self):
13+
14+
client = PhoneIdClient(self.customer_id,
15+
self.api_key)
16+
17+
self.assertEqual(client.customer_id, self.customer_id)
18+
self.assertEqual(client.api_key, self.api_key)
19+
20+
21+
def test_phoneid_pid_contact(self):
22+
23+
client = PhoneIdClient(self.customer_id, self.api_key)
24+
content_type_expected = 'application/json'
25+
status_code_expected = 200
26+
27+
payload = {
28+
"addons": {
29+
"contact": {}
30+
},
31+
"phone_number": self.phone_number_test
32+
}
33+
34+
response = client.phoneid(**payload)
35+
36+
self.assertEqual(response.headers.get('Content-Type'), content_type_expected, "Content-Type args do not match expected")
37+
self.assertEqual(response.status_code, status_code_expected, "Status code args do not match expected")
38+
39+
def test_phoneid_pid(self):
40+
41+
client = PhoneIdClient(self.customer_id, self.api_key)
42+
content_type_expected = 'application/json'
43+
status_code_expected = 200
44+
45+
response = client.phoneid(self.phone_number_test)
46+
47+
self.assertEqual(response.headers.get('Content-Type'), content_type_expected, "Content-Type args do not match expected")
48+
self.assertEqual(response.status_code, status_code_expected, "Status code args do not match expected")

0 commit comments

Comments
 (0)