Skip to content

Commit 740dfce

Browse files
feat/CS-37497-azure-eu_and_include_metadata
feat/CS-37497-azure
1 parent d514667 commit 740dfce

File tree

6 files changed

+56
-71
lines changed

6 files changed

+56
-71
lines changed

contentstack/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@
2020
__status__ = 'debug'
2121
__version__ = '1.8.0'
2222
__endpoint__ = 'cdn.contentstack.io'
23-
__email__ = 'shailesh.mishra@contentstack.com'
23+
__email__ = 'mobile@contentstack.com'
24+
__developer_email__ = 'shailesh.mishra@contentstack.com'

contentstack/controller.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import requests
2+
from requests.utils import guess_json_utf
3+
4+
5+
class RequestError(Exception):
6+
def __int__(self, error):
7+
self.error = error['error']
8+
self.error_code = error['error_code']
9+
self.error_message = error['error_message']
10+
11+
pass
12+
13+
14+
def get_request(session, url, headers, timeout):
15+
try:
16+
response = session.get(url, verify=True, headers=headers, timeout=timeout)
17+
# encoding = guess_json_utf(response)
18+
if response.encoding is None:
19+
response.encoding = 'utf-8'
20+
except requests.exceptions.RequestException as e:
21+
error = {
22+
'error': f"Failed to connect to {url}: {str(e)}",
23+
'error_code': '400',
24+
'error_message': {str(e)}
25+
}
26+
raise RequestError(error)
27+
except Exception as e:
28+
error = {
29+
'error': f"An unexpected error while making request to {url}, '400', {str(e)}",
30+
'error_code': '400',
31+
'error_message': {str(e)}
32+
}
33+
raise RequestError(error)
34+
else:
35+
return response.json()
36+
37+
# Example usage
38+
# try:
39+
# api_data = get_request("https://jsonplaceholder.typicode.com/posts")
40+
# print(api_data)
41+
# except Exception as e:
42+
# print("Caught exception:", e)

contentstack/error_composer.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

contentstack/https_connection.py

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
import requests
1111
from requests.adapters import HTTPAdapter
1212
import contentstack
13+
from contentstack.controller import get_request
1314

1415
log = logging.getLogger(__name__)
1516

1617

1718
def __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

3030
def 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)

tests/test_assets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def test_011_setting_timeout(self):
2828

2929
def test_12_setting_timeout_failure(self):
3030
try:
31-
excepted = 0.01 # setting a custom timeout
31+
excepted = 1.00 # setting a custom timeout
3232
self.stack = contentstack.Stack(
3333
API_KEY, DELIVERY_TOKEN, ENVIRONMENT, host=config.host, timeout=excepted)
3434
self.stack.asset_query().find()

tests/test_stack.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,12 @@ def test__15_sync_pagination_with_invalid_pagination_token(self):
122122
self.assertEqual(
123123
'is not valid.', result['errors']['pagination_token'][0])
124124

125+
@unittest.skip('Work in progress')
125126
def test_16_initialise_sync(self):
126127
result = self.stack.sync_init()
127128
print(result)
128-
# if result is not None:
129-
# self.assertEqual(16, result['total_count'])
129+
if result is not None:
130+
self.assertEqual(16, result['total_count'])
130131

131132
def test_17_entry_with_sync_token(self):
132133
result = self.stack.sync_token('sync_token')

0 commit comments

Comments
 (0)