diff --git a/telesign/phoneid.py b/telesign/phoneid.py index 6cf6ea3..0c014f9 100644 --- a/telesign/phoneid.py +++ b/telesign/phoneid.py @@ -1,5 +1,7 @@ from __future__ import unicode_literals +import json + from telesign.rest import RestClient PHONEID_RESOURCE = "/v1/phoneid/{phone_number}" @@ -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): + 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 diff --git a/telesign/rest.py b/telesign/rest.py index b53dadd..6833619 100644 --- a/telesign/rest.py +++ b/telesign/rest.py @@ -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. @@ -99,6 +100,7 @@ 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: @@ -106,8 +108,9 @@ def generate_telesign_headers(customer_id, 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"