Skip to content

Commit 3f7e3d1

Browse files
request tuple (result, err) added
1 parent 82af963 commit 3f7e3d1

File tree

3 files changed

+30
-39
lines changed

3 files changed

+30
-39
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ wheels/
2424
.installed.cfg
2525
*.egg
2626
MANIFEST
27-
.DS_Store
28-
.DS_Store?
27+
*.DS_Store
28+
*.DS_Store?
2929

3030
# PyInstaller
3131
# Usually these files are written by a python script from a template

contentstack/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@
3939

4040
__author__ = 'contentstack - (www.github.con/contentstack)'
4141
__email__ = 'shailesh.mishra@contentstack.com'
42-
__version__ = '0.0.1'
42+
__version__ = '1.0.0'
4343
__endpoint__ = 'cdn.contentstack.io'

contentstack/http_request.py

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,36 @@
88
class HTTPRequestConnection(object):
99

1010
def __init__(self, url_path, query=dict, local_headers=dict):
11+
self.result = None
12+
self.error = None
1113
self.url_path = url_path
1214
self._query_prams = query
1315
self._local_headers = local_headers
16+
self._local_headers['X-User-Agent'] = self._contentstack_user_agent()
17+
self._local_headers['Content-Type'] = 'application/json'
1418
self.url_path = config.Config().get_endpoint(self.url_path)
1519
if 'environment' in self._local_headers:
1620
self._query_prams['environment'] = self._local_headers['environment']
1721

18-
def http_request(self) -> dict:
19-
self._local_headers['X-User-Agent'] = self._contentstack_user_agent()
20-
self._local_headers['Content-Type'] = 'application/json'
22+
def http_request(self) -> tuple:
2123
response = requests.get(self.url_path, params=self._query_prams, headers=self._local_headers)
22-
23-
print('request url:: ', response.url)
2424
if response.ok:
25-
json_response = response.json()
26-
if 'stack' in json_response:
27-
logging.info('stack response')
28-
return json_response['stack']
29-
if 'content_types' in json_response:
30-
logging.info('contenttypes response')
31-
return json_response['content_types']
32-
if 'items' in json_response:
33-
logging.info('sync response')
34-
return json_response['items']
35-
if 'content_type' in json_response:
36-
logging.info('content type response')
37-
return json_response['content_type']
38-
if 'entry' in json_response:
39-
logging.info('entry response')
40-
return json_response['entry']
41-
if 'entries' in json_response:
42-
logging.info('entries response')
43-
return json_response['entries']
44-
25+
self.result = response.json()
4526
else:
46-
error_response = response.json()
47-
# error_message = error_response["error_message"]
48-
# error_code = error_response["error_code"]
49-
# error_code = error_response["errors"]
50-
return error_response
27+
self.error = response.json()
28+
29+
return self.result, self.error
5130

5231
@staticmethod
5332
def _contentstack_user_agent() -> str:
5433
"""
5534
X-Contentstack-User-Agent header.
5635
"""
57-
header = {}
58-
from . import __version__
59-
header['sdk'] = {
36+
header = {'sdk': {
6037
'name': 'contentstack.python',
61-
'version': __version__
62-
}
63-
38+
'version': "1.0.0"
39+
}}
40+
# from contentstack import __version__
6441
# from sys import platform as cs_plateforom
6542
# os_name = cs_plateforom.system()
6643
# if os_name == 'Darwin':
@@ -86,4 +63,18 @@ def set_query_model(self):
8663
pass
8764

8865
def set_asset_model(self):
66+
pass
67+
68+
def request_api(self, urls: str):
69+
try:
70+
r = requests.get(urls, timeout=3)
71+
r.raise_for_status()
72+
except requests.exceptions.HTTPError as errh:
73+
print("Http Error:", errh)
74+
except requests.exceptions.ConnectionError as errc:
75+
print("Error Connecting:", errc)
76+
except requests.exceptions.Timeout as errt:
77+
print("Timeout Error:", errt)
78+
except requests.exceptions.RequestException as err:
79+
print("OOps: Something Else", err)
8980
pass

0 commit comments

Comments
 (0)