Skip to content

Commit 837f458

Browse files
shaileshmishrashaileshmishra
authored andcommitted
- Retry policy and timeout support included
- Set default timeout 30 sec
1 parent 0f2b759 commit 837f458

File tree

7 files changed

+78
-43
lines changed

7 files changed

+78
-43
lines changed

.gitignore

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
22
__pycache__/
33
*.py[cod]
44
*$py.class
5-
5+
config.py
6+
__pycache__
7+
.ipynb_checkpoints
68
# C extensions
79
*.so
10+
.env
11+
keys.py
12+
keys.py/
13+
config.py/
14+
.env/
815
/reports
916
/tests/reports
1017
# Distribution / packaging
@@ -31,7 +38,6 @@ MANIFEST
3138
.idea
3239
.vscode
3340
.report.log
34-
3541
# PyInstaller
3642
# Usually these files are written by a python script from a template
3743
# before PyInstaller builds the exe, so as to inject date/other infos into it.

CHANGELOG.md

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,49 @@
11
# CHANGELOG
22

3+
## _v1.2.1_
4+
5+
============
6+
7+
Date: 24-Feb-2021
8+
9+
- Retry policy and timeout support included
10+
- set default timeout 30 sec
11+
-----------------------------
12+
313

414
## _v1.2.0_
15+
516
============
617

7-
_Date: 08-Dec-2020_
18+
Date: 08-Dec-2020
819

920
- include_fallback Support Added
1021
- Timeout support included
1122

12-
- Entry
13-
- added support for include_fallback.
23+
- Entry
24+
- added support for include_fallback.
1425
- Asset
15-
- added support for include_fallback.
26+
- added support for include_fallback.
1627
- AssetQueryable
17-
- added support for include_fallback.
28+
- added support for include_fallback.
1829
- Query
19-
- added support for include_fallback.
30+
- added support for include_fallback.
2031

2132
-----------------------------
2233

23-
24-
25-
2634
## _v1.1.0_
2735

28-
_Date: 10-Aug-2020 - include_reference issue fixed_
36+
Date: 10-Aug-2020 - include_reference issue fixed
2937

3038
EntryQueryable
3139

3240
- updated include_reference function.
3341

3442
-----------------------------
3543

36-
37-
3844
## _v1.0.0_
3945

40-
_Date: 17-Jun-2020 - initial release_
46+
Date: 17-Jun-2020 - initial release
4147

4248
Stack
4349

@@ -59,10 +65,9 @@ Query
5965

6066
-----------------------------
6167

62-
6368
## _v0.1.0_
6469

65-
_November-18, 2019 -beta release_
70+
November-18, 2019 -beta release
6671

6772
Initial release for the contentstack-python-sdk for Content Delivery API
6873

changelog.rst

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22
**CHANGELOG**
33
================
44

5+
Retry Policy and timeout support added
6+
7+
*v1.2.1*
8+
9+
============
10+
11+
**Date: 24-Feb-2021**
12+
13+
- Retry policy and timeout support included
14+
- set default timeout 30 sec
15+
16+
============
17+
518
ENHANCEMENT, NEW FEATURE, BUG RESOLVE
619

720
*v1.2.0*
@@ -10,13 +23,13 @@ ENHANCEMENT, NEW FEATURE, BUG RESOLVE
1023
**Date: 20-Oct-2020**
1124

1225
- **include_fallback support added**
13-
`entry`
26+
entry
1427
- added support for include_fallback.
15-
`asset`
28+
asset
1629
- added support for include_fallback.
17-
`assetquery`
30+
assetquery
1831
- added support for include_fallback.
19-
`query`
32+
query
2033
- added support for include_fallback.
2134

2235
- **Timeout support included**

contentstack/assetquery.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ def __init__(self, http_instance):
2828
def environment(self, environment):
2929
r"""Provide the name of the environment if you wish to retrieve the assets published
3030
in a particular environment.
31-
3231
:param environment: environment of the stack
33-
3432
:return: AssetQuery - so we can chain the call
3533
3634
-----------------------------
@@ -49,9 +47,7 @@ def version(self, version):
4947
r"""Specify the version number of the asset that you wish to retrieve.
5048
If the version is not specified, the details of the latest version will be retrieved.
5149
To retrieve a specific version, keep the environment parameter blank.
52-
5350
:param version: version number of the asset that you wish to retrieve
54-
5551
:return: AssetQuery: so we can chain the call
5652
5753
-----------------------------

contentstack/contenttype.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
# Your code has been rated at 10.00/10 by pylint
1010

1111
from urllib import parse
12-
1312
from contentstack.entry import Entry
1413
from contentstack.query import Query
1514

contentstack/https_connection.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88
import platform
99
from json import JSONDecodeError
1010
from requests.adapters import HTTPAdapter
11-
from requests.packages.urllib3.util.retry import Retry
11+
from urllib3.util import Retry
1212
import requests
1313
from requests.exceptions import Timeout, HTTPError
14-
14+
import logging
1515
import contentstack
1616

17+
log = logging.getLogger(__name__)
18+
1719

18-
def get_os_platform():
20+
def __get_os_platform():
1921
""" returns client platform """
2022
os_platform = platform.system()
2123
if os_platform == 'Darwin':
@@ -31,38 +33,54 @@ def get_os_platform():
3133
def user_agents():
3234
"""User Agents for the Https"""
3335
header = {'sdk': dict(name=contentstack.__package__, version=contentstack.__version__),
34-
'os': get_os_platform,
36+
'os': __get_os_platform,
3537
'Content-Type': 'application/json'}
3638
package = "contentstack-python/{}".format(contentstack.__version__)
3739
return {'User-Agent': str(header), "X-User-Agent": package}
3840

3941

4042
class HTTPSConnection: # R0903: Too few public methods
4143
"""Make Https Request to fetch the result as per requested url"""
44+
# BACKOFF_MAX = 120
4245

43-
def __init__(self, endpoint, headers):
46+
def __init__(self, endpoint, headers, timeout, retry_strategy):
4447
if None not in (endpoint, headers):
4548
self.payload = None
4649
self.endpoint = endpoint
4750
self.headers = headers
48-
self.default_timeout = 10
51+
self.timeout = timeout
52+
self.retry_strategy = retry_strategy # default timeout (period=30) seconds
4953

5054
def get(self, url):
5155
"""
52-
Here we create a response objectresponse which will store the request-response.
56+
Here we create a response object, `response` which will store the request-response.
5357
We use requests.get method since we are sending a GET request.
5458
The four arguments we pass are url, verify(ssl), timeout, headers
5559
"""
5660
try:
5761
self.headers.update(user_agents())
62+
63+
# Setting up custom retry adapter
5864
session = requests.Session()
59-
retry = Retry(connect=3, backoff_factor=0.5)
60-
adapter = HTTPAdapter(max_retries=retry)
65+
# retry on 429 rate limit exceeded
66+
# Diagnosing a 408 request timeout
67+
# backoff_factor works on algorithm {backoff factor} * (2 ** ({number of total retries} - 1))
68+
# This value is by default 0, meaning no exponential backoff will be set and
69+
# retries will immediately execute. Make sure to set this to 1 in to avoid hammering your servers!.
70+
# retries = Retry(total=5, backoff_factor=1, status_forcelist=[408, 429], allowed_methods=["GET"])
71+
72+
adapter = HTTPAdapter(max_retries=self.retry_strategy)
6173
session.mount('https://', adapter)
62-
response = session.get(url, verify=True, headers=self.headers)
63-
# response = requests.get(url, verify=True, headers=self.headers)
64-
response.encoding = 'utf-8'
65-
return response.json()
74+
log.info('url: %s', url)
75+
response = session.get(url, verify=True, headers=self.headers, timeout=self.timeout)
76+
if response.encoding is None:
77+
response.encoding = 'utf-8'
78+
79+
if response is not None:
80+
return response.json()
81+
else:
82+
return {"error": "Unknown error", "error_code": 000, "error_message": "Unknown error"}
83+
6684
except Timeout:
6785
raise TimeoutError('The request timed out')
6886
except ConnectionError:
@@ -72,6 +90,3 @@ def get(self, url):
7290
except HTTPError:
7391
raise HTTPError('Http Error Occurred')
7492

75-
def update_connection_timeout(self, timeout: int):
76-
"""Facilitate to update timeout for the https request"""
77-
self.default_timeout = timeout

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ requests==2.25.0
55
pip~=20.3.1
66
py~=1.9.0
77
setuptools~=51.0.0
8-
html-testRunner==1.2.1
8+
wheel==0.35.1
9+
urllib3==1.26.3

0 commit comments

Comments
 (0)