Skip to content

Commit dfdf34e

Browse files
requests implemented in stack
1 parent 365af53 commit dfdf34e

File tree

5 files changed

+67
-43
lines changed

5 files changed

+67
-43
lines changed

.gitignore

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

2830
# PyInstaller
2931
# Usually these files are written by a python script from a template

contentstack/HTTPConnection.py

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

contentstack/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from .entry_model import EntryModel
99
from .errors import HTTPError, ConfigError
1010
from .group import Group
11-
from .HTTPConnection import HTTPConnection
11+
from .http_request import HTTPRequest
1212
from .stack import Stack
1313

1414
__author__ = 'contentstack - (www.github.con/contentstack)'

contentstack/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ def get_http_protocol(self):
5353
logging.info('get http protocol', self.http_protocol)
5454
return self.http_protocol
5555

56-
def _get_endpoint(self):
56+
def get_endpoint(self, url_path):
5757
api_version: str = self.get_version()
5858
host_url = self.get_host()
5959
http_protocol = self.get_http_protocol()
60-
config_url = "{0}{1}/{2}/".format(http_protocol, host_url, api_version)
60+
config_url = "{0}{1}/{2}/{3}".format(http_protocol, host_url, api_version, url_path)
6161
return config_url

contentstack/http_request.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import requests
2+
from contentstack import config
3+
import urllib.parse
4+
5+
6+
class HTTPRequest(object):
7+
8+
def __init__(self, url_path, query=dict, local_headers=dict):
9+
self.url_path = url_path
10+
self._query_prams = query
11+
self._local_headers = local_headers
12+
if 'environment' in self._local_headers:
13+
self._query_prams['environment'] = self._local_headers['environment']
14+
15+
# self._query_prams = urllib.parse.quote_plus(self._query_prams)
16+
self._local_headers['X-User-Agent'] = self._contentstack_user_agent()
17+
self._local_headers['Content-Type'] = 'application/json'
18+
# http request based on url_path and respected query
19+
self.url_path = config.Config().get_endpoint(self.url_path)
20+
self._http_request()
21+
22+
def _http_request(self):
23+
response = requests.get(
24+
self.url_path,
25+
params=self._query_prams,
26+
headers=self._local_headers,
27+
)
28+
29+
json_response = response.json()
30+
stack = json_response['stack']
31+
collaborators = stack['collaborators']
32+
print(collaborators, collaborators)
33+
34+
@staticmethod
35+
def _contentstack_user_agent() -> str:
36+
"""
37+
X-Contentstack-User-Agent header.
38+
"""
39+
header = {}
40+
from . import __version__
41+
header['sdk'] = {
42+
'name': 'contentstack.python',
43+
'version': __version__
44+
}
45+
46+
# from sys import platform as cs_plateforom
47+
# os_name = cs_plateforom.system()
48+
# if os_name == 'Darwin':
49+
# os_name = 'macOS'
50+
# elif not os_name or os_name == 'Java':
51+
# os_name = None
52+
# elif os_name and os_name not in ['macOS', 'Windows']:
53+
# os_name = 'Linux'
54+
# header['os'] = {
55+
# 'name': os_name,
56+
# 'version': cs_plateforom.release()
57+
# }
58+
59+
return header.__str__()
60+
61+
# connection = HTTPRequest(url_path='sync', query=None)
62+
# connection._http_request()

0 commit comments

Comments
 (0)