-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathbase_client.py
56 lines (45 loc) · 2.19 KB
/
base_client.py
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
48
49
50
51
52
53
54
55
56
import json
import warnings
from requests import Session, exceptions
from requests import exceptions
from requests.auth import HTTPBasicAuth
from .auth import HTTPBearerAuth
from .utils import handle_http_exception
class DSSBaseClient(object):
def __init__(self, base_uri, api_key=None, internal_ticket=None, bearer_token=None, no_check_certificate=False, **kwargs):
if "insecure_tls" in kwargs:
# Backward compatibility before removing insecure_tls option
warnings.warn("insecure_tls field is now deprecated. It has been replaced by no_check_certificate.", DeprecationWarning)
no_check_certificate = kwargs.get("insecure_tls") or no_check_certificate
self.api_key = api_key
self.bearer_token = bearer_token
self.base_uri = base_uri
self._session = Session()
if no_check_certificate:
self._session.verify = False
########################################################
# Internal Request handling
########################################################
def _perform_http(self, method, path, params=None, body=None, stream=False):
if body:
body = json.dumps(body)
headers = None
auth = None
if self.api_key:
auth = HTTPBasicAuth(self.api_key, "")
elif self.bearer_token:
auth = HTTPBearerAuth(self.bearer_token)
http_res = self._session.request(
method, "%s/%s" % (self.base_uri, path),
params=params, data=body, headers=headers,
auth=auth, stream = stream)
handle_http_exception(http_res)
return http_res
def _perform_empty(self, method, path, params=None, body=None):
self._perform_http(method, path, params, body, False)
def _perform_text(self, method, path, params=None, body=None):
return self._perform_http(method, path, params, body, False).text
def _perform_json(self, method, path, params=None, body=None):
return self._perform_http(method, path, params, body, False).json()
def _perform_raw(self, method, path, params=None, body=None):
return self._perform_http(method, path, params, body, True)