Skip to content

Commit c6acfde

Browse files
shaileshmishrashaileshmishra
authored andcommitted
retrypolicy support added
improved timeout
1 parent ea9286a commit c6acfde

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

contentstack/https_connection.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
# ************* Module https_connection.py **************
66
# Your code has been rated at 10.00/10 by pylint
77

8+
import logging
89
import platform
910
from json import JSONDecodeError
10-
from requests.adapters import HTTPAdapter
11-
from urllib3.util import Retry
11+
1212
import requests
13+
from requests.adapters import HTTPAdapter
1314
from requests.exceptions import Timeout, HTTPError
14-
import logging
15+
1516
import contentstack
1617

1718
log = logging.getLogger(__name__)
@@ -41,15 +42,14 @@ def user_agents():
4142

4243
class HTTPSConnection: # R0903: Too few public methods
4344
"""Make Https Request to fetch the result as per requested url"""
44-
# BACKOFF_MAX = 120
4545

4646
def __init__(self, endpoint, headers, timeout, retry_strategy):
4747
if None not in (endpoint, headers):
4848
self.payload = None
4949
self.endpoint = endpoint
5050
self.headers = headers
51-
self.timeout = timeout
52-
self.retry_strategy = retry_strategy # default timeout (period=30) seconds
51+
self.timeout = timeout # default timeout (period=30) seconds
52+
self.retry_strategy = retry_strategy
5353

5454
def get(self, url):
5555
"""
@@ -59,28 +59,17 @@ def get(self, url):
5959
"""
6060
try:
6161
self.headers.update(user_agents())
62-
63-
# Setting up custom retry adapter
6462
session = requests.Session()
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-
7263
adapter = HTTPAdapter(max_retries=self.retry_strategy)
7364
session.mount('https://', adapter)
74-
log.info('url: %s', url)
65+
# log.info('url: %s', url)
7566
response = session.get(url, verify=True, headers=self.headers, timeout=self.timeout)
7667
if response.encoding is None:
7768
response.encoding = 'utf-8'
78-
7969
if response is not None:
8070
return response.json()
8171
else:
82-
return {"error": "Unknown error", "error_code": 000, "error_message": "Unknown error"}
83-
72+
return {"error": "error details not found", "error_code": 422, "error_message": "unknown error"}
8473
except Timeout:
8574
raise TimeoutError('The request timed out')
8675
except ConnectionError:
@@ -89,4 +78,3 @@ def get(self, url):
8978
raise TypeError('Invalid JSON in request')
9079
except HTTPError:
9180
raise HTTPError('Http Error Occurred')
92-

0 commit comments

Comments
 (0)