|
| 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