1010import requests
1111from requests .adapters import HTTPAdapter
1212import contentstack
13+ from contentstack .controller import get_request
1314
1415log = logging .getLogger (__name__ )
1516
1617
1718def __get_os_platform ():
18- """ Returns client platform """
1919 os_platform = platform .system ()
2020 if os_platform == 'Darwin' :
2121 os_platform = 'macOS'
@@ -28,14 +28,14 @@ def __get_os_platform():
2828
2929
3030def user_agents ():
31- """User Agents for the Https"""
31+ """Default User Agents for the Https"""
3232 header = {'sdk' : dict (
3333 name = contentstack .__package__ ,
3434 version = contentstack .__version__
3535 ),
3636 'os' : __get_os_platform ,
3737 'Content-Type' : 'application/json' }
38- package = f"contentstack-python /{ contentstack .__version__ } "
38+ package = f"{ contentstack . __title__ } /{ contentstack .__version__ } "
3939 return {'User-Agent' : str (header ), "X-User-Agent" : package }
4040
4141
@@ -54,6 +54,7 @@ class HTTPSConnection: # R0903: Too few public methods
5454
5555 def __init__ (self , endpoint , headers , timeout , retry_strategy , live_preview ):
5656 if None not in (endpoint , headers ):
57+ self .session = requests .Session ()
5758 self .payload = None
5859 self .endpoint = endpoint
5960 self .headers = headers
@@ -62,37 +63,7 @@ def __init__(self, endpoint, headers, timeout, retry_strategy, live_preview):
6263 self .live_preview = live_preview
6364
6465 def get (self , url ):
65-
66- """
67- Here we create a response object, `response` which will store the request-response.
68- We use requests. Get method since we are sending a GET request.
69- The four arguments we pass are url, verify(ssl), timeout, headers
70- """
71- try :
72- self .headers .update (user_agents ())
73- session = requests .Session ()
74- adapter = HTTPAdapter (max_retries = self .retry_strategy )
75- session .mount ('https://' , adapter )
76- response = session .get (url , verify = True , headers = self .headers , timeout = self .timeout )
77- response .encoding = "utf-8"
78- # response.raise_for_status()
79- session .close ()
80- except requests .exceptions .HTTPError as http_error :
81- print (f"HTTP error occurred: { http_error } " )
82- except requests .exceptions .Timeout as timeout_error :
83- print (f"Timeout error occurred: { timeout_error } " )
84- except requests .exceptions .ConnectionError as connection_error :
85- print (f"Connection error occurred: { connection_error } " )
86- except requests .exceptions .RequestException as request_exception :
87- print (f"An error occurred: { request_exception } " )
88- else :
89- print ("API request successful" )
90- return response .json ()
91- finally :
92- print ("API request complete" )
93-
94- # if response.status_code == 200 and response.encoding is None:
95- # response.encoding = 'utf-8'
96- # return response.json()
97- # else:
98- # return get_api_data(response)
66+ self .headers .update (user_agents ())
67+ adapter = HTTPAdapter (max_retries = self .retry_strategy )
68+ self .session .mount ('https://' , adapter )
69+ return get_request (self .session , url , headers = self .headers , timeout = self .timeout )
0 commit comments