-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhttps_connection.py
More file actions
47 lines (40 loc) · 1.65 KB
/
https_connection.py
File metadata and controls
47 lines (40 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
This module implements the Requests API.
"""
import logging
import platform
import requests
from requests.adapters import HTTPAdapter
import contentstack
from contentstack.controller import get_request
def __get_os_platform():
os_platform = platform.system()
if os_platform == 'Darwin':
os_platform = 'macOS'
elif not os_platform or os_platform == 'Java':
os_platform = None
elif os_platform and os_platform not in ['macOS', 'Windows']:
os_platform = 'Linux'
os_platform = {'name': os_platform, 'version': platform.release()}
return os_platform
def user_agents():
header = {'sdk': dict(name=contentstack.__package__,
version=contentstack.__version__
), 'os': __get_os_platform(), 'Content-Type': 'application/json'}
package = f"{contentstack.__title__}/{contentstack.__version__}"
return {'User-Agent': str(header), "X-User-Agent": package}
class HTTPSConnection: # R0903: Too few public methods
def __init__(self, endpoint, headers, timeout, retry_strategy, live_preview):
if None not in (endpoint, headers):
self.session = requests.Session()
self.payload = None
self.endpoint = endpoint
self.headers = headers
self.timeout = timeout
self.retry_strategy = retry_strategy
self.live_preview = live_preview
def get(self, url):
self.headers.update(user_agents())
adapter = HTTPAdapter(max_retries=self.retry_strategy)
self.session.mount('https://', adapter)
return get_request(self.session, url, headers=self.headers, timeout=self.timeout)