telesign 2.2.7__py2.py3-none-any.whl → 2.3.1__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- telesign/rest.py +49 -14
- {telesign-2.2.7.dist-info → telesign-2.3.1.dist-info}/METADATA +1 -1
- {telesign-2.2.7.dist-info → telesign-2.3.1.dist-info}/RECORD +6 -6
- {telesign-2.2.7.dist-info → telesign-2.3.1.dist-info}/LICENSE.txt +0 -0
- {telesign-2.2.7.dist-info → telesign-2.3.1.dist-info}/WHEEL +0 -0
- {telesign-2.2.7.dist-info → telesign-2.3.1.dist-info}/top_level.txt +0 -0
telesign/rest.py
CHANGED
|
@@ -6,6 +6,7 @@ from base64 import b64encode, b64decode
|
|
|
6
6
|
from email.utils import formatdate
|
|
7
7
|
from hashlib import sha256
|
|
8
8
|
from platform import python_version
|
|
9
|
+
import time
|
|
9
10
|
|
|
10
11
|
import requests
|
|
11
12
|
import json
|
|
@@ -16,8 +17,8 @@ from telesign.util import AuthMethod
|
|
|
16
17
|
|
|
17
18
|
class RestClient(requests.models.RequestEncodingMixin):
|
|
18
19
|
"""
|
|
19
|
-
The
|
|
20
|
-
|
|
20
|
+
The Telesign RestClient is a generic HTTP REST client that can be extended to make requests against any of
|
|
21
|
+
Telesign's REST API endpoints.
|
|
21
22
|
|
|
22
23
|
RequestEncodingMixin offers the function _encode_params for url encoding the body for use in string_to_sign outside
|
|
23
24
|
of a regular HTTP request.
|
|
@@ -52,9 +53,10 @@ class RestClient(requests.models.RequestEncodingMixin):
|
|
|
52
53
|
sdk_version_dependency=None,
|
|
53
54
|
proxies=None,
|
|
54
55
|
timeout=10,
|
|
55
|
-
auth_method=None
|
|
56
|
+
auth_method=None,
|
|
57
|
+
pool_recycle=480):
|
|
56
58
|
"""
|
|
57
|
-
|
|
59
|
+
Telesign RestClient useful for making generic RESTful requests against our API.
|
|
58
60
|
|
|
59
61
|
:param customer_id: Your customer_id string associated with your account.
|
|
60
62
|
:param api_key: Your api_key string associated with your account.
|
|
@@ -62,13 +64,22 @@ class RestClient(requests.models.RequestEncodingMixin):
|
|
|
62
64
|
:param proxies: (optional) Dictionary mapping protocol or protocol and hostname to the URL of the proxy.
|
|
63
65
|
:param timeout: (optional) How long to wait for the server to send data before giving up, as a float,
|
|
64
66
|
or as a (connect timeout, read timeout) tuple
|
|
67
|
+
:param pool_recycle: (optional) Time in seconds to recycle the HTTP session to avoid stale connections (default 480).
|
|
68
|
+
If a session is older than this value, it will be closed and a new session will be created automatically before each request.
|
|
69
|
+
This helps prevent errors due to HTTP keep-alive connections being closed by the server after inactivity.
|
|
70
|
+
|
|
71
|
+
HTTP Keep-Alive behavior:
|
|
72
|
+
TeleSign endpoints close idle HTTP keep-alive connections after 499 seconds. If you attempt to reuse a connection older than this, you may get a 'connection reset by peer' error.
|
|
73
|
+
By default, pool_recycle=480 ensures sessions are refreshed before this limit.
|
|
65
74
|
"""
|
|
66
75
|
self.customer_id = customer_id
|
|
67
76
|
self.api_key = api_key
|
|
68
77
|
|
|
69
78
|
self.api_host = rest_endpoint
|
|
70
79
|
|
|
71
|
-
self.
|
|
80
|
+
self.pool_recycle = pool_recycle
|
|
81
|
+
self._session_created_at = None
|
|
82
|
+
self.session = self._create_session()
|
|
72
83
|
|
|
73
84
|
self.session.proxies = proxies if proxies else {}
|
|
74
85
|
|
|
@@ -99,17 +110,17 @@ class RestClient(requests.models.RequestEncodingMixin):
|
|
|
99
110
|
content_type=None,
|
|
100
111
|
auth_method=None):
|
|
101
112
|
"""
|
|
102
|
-
Generates the
|
|
113
|
+
Generates the Telesign REST API headers used to authenticate requests.
|
|
103
114
|
|
|
104
115
|
Creates the canonicalized string_to_sign and generates the HMAC signature. This is used to authenticate requests
|
|
105
|
-
against the
|
|
116
|
+
against the Telesign REST API.
|
|
106
117
|
|
|
107
118
|
See https://developer.telesign.com/docs/authentication for detailed API documentation.
|
|
108
119
|
|
|
109
120
|
:param customer_id: Your account customer_id.
|
|
110
121
|
:param api_key: Your account api_key.
|
|
111
122
|
:param method_name: The HTTP method name of the request as a upper case string, should be one of 'POST', 'GET',
|
|
112
|
-
'PUT' or 'DELETE'.
|
|
123
|
+
'PUT', 'PATCH' or 'DELETE'.
|
|
113
124
|
:param resource: The partial resource URI to perform the request against, as a string.
|
|
114
125
|
:param url_encoded_fields: HTTP body parameters to perform the HTTP request with, must be a urlencoded string.
|
|
115
126
|
:param date_rfc2616: The date and time of the request formatted in rfc 2616, as a string.
|
|
@@ -117,7 +128,7 @@ class RestClient(requests.models.RequestEncodingMixin):
|
|
|
117
128
|
:param user_agent: (optional) User Agent associated with the request, as a string.
|
|
118
129
|
:param content_type: (optional) ContentType of the request, as a string.
|
|
119
130
|
:param auth_method: (optional) Authentication type ex: Basic, HMAC etc
|
|
120
|
-
:return: The
|
|
131
|
+
:return: The Telesign authentication headers.
|
|
121
132
|
"""
|
|
122
133
|
if date_rfc2616 is None:
|
|
123
134
|
date_rfc2616 = formatdate(usegmt=True)
|
|
@@ -177,7 +188,7 @@ class RestClient(requests.models.RequestEncodingMixin):
|
|
|
177
188
|
|
|
178
189
|
def post(self, resource, body=None, json_fields=None, **query_params):
|
|
179
190
|
"""
|
|
180
|
-
Generic
|
|
191
|
+
Generic Telesign REST API POST handler.
|
|
181
192
|
|
|
182
193
|
:param resource: The partial resource URI to perform the request against, as a string.
|
|
183
194
|
:param body: (optional) A dictionary sent as a part of request body.
|
|
@@ -188,7 +199,7 @@ class RestClient(requests.models.RequestEncodingMixin):
|
|
|
188
199
|
|
|
189
200
|
def get(self, resource, body=None, json_fields=None, **query_params):
|
|
190
201
|
"""
|
|
191
|
-
Generic
|
|
202
|
+
Generic Telesign REST API GET handler.
|
|
192
203
|
|
|
193
204
|
:param resource: The partial resource URI to perform the request against, as a string.
|
|
194
205
|
:param body: (optional) A dictionary sent as a part of request body.
|
|
@@ -199,7 +210,7 @@ class RestClient(requests.models.RequestEncodingMixin):
|
|
|
199
210
|
|
|
200
211
|
def put(self, resource, body=None, json_fields=None, **query_params):
|
|
201
212
|
"""
|
|
202
|
-
Generic
|
|
213
|
+
Generic Telesign REST API PUT handler.
|
|
203
214
|
|
|
204
215
|
:param resource: The partial resource URI to perform the request against, as a string.
|
|
205
216
|
:param body: (optional) A dictionary sent as a part of request body.
|
|
@@ -213,7 +224,7 @@ class RestClient(requests.models.RequestEncodingMixin):
|
|
|
213
224
|
|
|
214
225
|
def delete(self, resource, body=None, json_fields=None, **query_params):
|
|
215
226
|
"""
|
|
216
|
-
Generic
|
|
227
|
+
Generic Telesign REST API DELETE handler.
|
|
217
228
|
|
|
218
229
|
:param resource: The partial resource URI to perform the request against, as a string.
|
|
219
230
|
:param body: (optional) A dictionary sent as a part of request body.
|
|
@@ -221,10 +232,33 @@ class RestClient(requests.models.RequestEncodingMixin):
|
|
|
221
232
|
:return: The RestClient Response object.
|
|
222
233
|
"""
|
|
223
234
|
return self._execute(self.session.delete, 'DELETE', resource, body, json_fields, **query_params)
|
|
235
|
+
|
|
236
|
+
def patch(self, resource, body=None, json_fields=None, **query_params):
|
|
237
|
+
"""
|
|
238
|
+
Generic Telesign REST API PATCH handler.
|
|
239
|
+
|
|
240
|
+
:param resource: The partial resource URI to perform the request against, as a string.
|
|
241
|
+
:param body: (optional) A dictionary sent as a part of request body.
|
|
242
|
+
:param json_fields: (optional) A dictionary sent as a JSON body.
|
|
243
|
+
:param query_params: query_params to perform the PATCH request with, as a dictionary.
|
|
244
|
+
:return: The RestClient Response object.
|
|
245
|
+
"""
|
|
246
|
+
return self._execute(self.session.patch, 'PATCH', resource, body, json_fields, **query_params)
|
|
247
|
+
|
|
248
|
+
def _create_session(self):
|
|
249
|
+
session = requests.Session()
|
|
250
|
+
self._session_created_at = time.time()
|
|
251
|
+
return session
|
|
252
|
+
|
|
253
|
+
def _ensure_session(self):
|
|
254
|
+
if self._session_created_at is None or (time.time() - self._session_created_at > self.pool_recycle):
|
|
255
|
+
if self.session:
|
|
256
|
+
self.session.close()
|
|
257
|
+
self.session = self._create_session()
|
|
224
258
|
|
|
225
259
|
def _execute(self, method_function, method_name, resource, body=None, json_fields=None, **query_params):
|
|
226
260
|
"""
|
|
227
|
-
Generic
|
|
261
|
+
Generic Telesign REST API request handler.
|
|
228
262
|
|
|
229
263
|
:param method_function: The Requests HTTP request function to perform the request.
|
|
230
264
|
:param method_name: The HTTP method name, as an upper case string.
|
|
@@ -233,6 +267,7 @@ class RestClient(requests.models.RequestEncodingMixin):
|
|
|
233
267
|
:param query_params: query_params to perform the HTTP request with, as a dictionary.
|
|
234
268
|
:return: The RestClient Response object.
|
|
235
269
|
"""
|
|
270
|
+
self._ensure_session()
|
|
236
271
|
resource_uri = "{api_host}{resource}".format(api_host=self.api_host, resource=resource)
|
|
237
272
|
|
|
238
273
|
url_encoded_fields = self._encode_params(query_params)
|
|
@@ -3,12 +3,12 @@ telesign/appverify.py,sha256=wBeaeUxGmYcZkrbxkkXlG3-0oZr5sVvegzVpdvaoO2A,1195
|
|
|
3
3
|
telesign/intelligence.py,sha256=t-xwlgxkXw1OMupF5K4_UKWeTArOr9XPvddmqgZftPI,1452
|
|
4
4
|
telesign/messaging.py,sha256=v7l-eKUPqz7Nrwka0YNPMkC2TaoFUvQm3yp_djuTPu0,1367
|
|
5
5
|
telesign/phoneid.py,sha256=mk03NxL3wlaXpd8b-BB6q_SKfCxbtuTO19ayAHxmzz0,954
|
|
6
|
-
telesign/rest.py,sha256=
|
|
6
|
+
telesign/rest.py,sha256=AHNDyVZLa1vm2RWD4TSxr6uM4K50eH29C2Kjr3eCq8c,13911
|
|
7
7
|
telesign/score.py,sha256=Kceu6IrirN9SsuT1geEWs0QHAn6H9ZNFyIDVHcBJdwg,908
|
|
8
8
|
telesign/util.py,sha256=stDxpL5ZQeGskE_OSGP04UFHs3ZP2ACaRpW1WyQGMHg,1624
|
|
9
9
|
telesign/voice.py,sha256=DJaWyQpun-T7LfNjpxpsUjupiv2ShW1jqoFQA-CE2-Y,1341
|
|
10
|
-
telesign-2.
|
|
11
|
-
telesign-2.
|
|
12
|
-
telesign-2.
|
|
13
|
-
telesign-2.
|
|
14
|
-
telesign-2.
|
|
10
|
+
telesign-2.3.1.dist-info/LICENSE.txt,sha256=a--xZ4PCKUL5rpct8RrUC6ODU_8ZKIU51kJPfsA4HUs,1058
|
|
11
|
+
telesign-2.3.1.dist-info/METADATA,sha256=n3TqPKDa2uPs5_RJhzRxJVhYoiMATdENw7xPPzxz5ms,4532
|
|
12
|
+
telesign-2.3.1.dist-info/WHEEL,sha256=z9j0xAa_JmUKMpmz72K0ZGALSM_n-wQVmGbleXx2VHg,110
|
|
13
|
+
telesign-2.3.1.dist-info/top_level.txt,sha256=6LG7Jcr1jnpVhOWaIShoTdwYJn0QCfPhfudyVJNz1qg,9
|
|
14
|
+
telesign-2.3.1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|