Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions telesign/phoneid.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import unicode_literals

import json

from telesign.rest import RestClient

PHONEID_RESOURCE = "/v1/phoneid/{phone_number}"
Expand All @@ -23,3 +25,30 @@ def phoneid(self, phone_number, **params):
"""
return self.post(PHONEID_RESOURCE.format(phone_number=phone_number),
**params)

def _execute(self, method_function, method_name, resource, **params):
Copy link
Contributor

@jarradsl jarradsl Jan 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer to see this incorporated into the base class RestClient function so that other endpoints can take advantage of it. If not, you need test coverage for this implementation.

resource_uri = "{api_host}{resource}".format(api_host=self.api_host, resource=resource)

json_fields = json.dumps(params)

content_type = "application/json" if method_name in ("POST", "PUT") else ""

headers = self.generate_telesign_headers(self.customer_id,
self.api_key,
method_name,
resource,
json_fields,
user_agent=self.user_agent,
content_type=content_type)

if method_name in ['POST', 'PUT']:
payload = {'data': json_fields}
else:
payload = {'params': json_fields}

response = self.Response(method_function(resource_uri,
headers=headers,
timeout=self.timeout,
**payload))

return response
9 changes: 6 additions & 3 deletions telesign/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def generate_telesign_headers(customer_id,
url_encoded_fields,
date_rfc2616=None,
nonce=None,
user_agent=None):
user_agent=None,
content_type=None):
"""
Generates the TeleSign REST API headers used to authenticate requests.

Expand All @@ -99,15 +100,17 @@ def generate_telesign_headers(customer_id,
:param date_rfc2616: The date and time of the request formatted in rfc 2616, as a string.
:param nonce: A unique cryptographic nonce for the request, as a string.
:param user_agent: (optional) User Agent associated with the request, as a string.
:param content_type: (optional) ContentType of the request, as a string.
:return: The TeleSign authentication headers.
"""
if date_rfc2616 is None:
date_rfc2616 = formatdate(usegmt=True)

if nonce is None:
nonce = str(uuid.uuid4())

content_type = "application/x-www-form-urlencoded" if method_name in ("POST", "PUT") else ""

if not content_type:
content_type = "application/x-www-form-urlencoded" if method_name in ("POST", "PUT") else ""

auth_method = "HMAC-SHA256"

Expand Down