forked from dataiku/dataiku-api-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_client.py
More file actions
44 lines (35 loc) · 1.68 KB
/
base_client.py
File metadata and controls
44 lines (35 loc) · 1.68 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
import json
from requests import Session, exceptions
from requests import exceptions
from requests.auth import HTTPBasicAuth
from .utils import DataikuException
class DSSBaseClient(object):
def __init__(self, base_uri, api_key=None, internal_ticket=None):
self.api_key = api_key
self.base_uri = base_uri
self._session = Session()
########################################################
# Internal Request handling
########################################################
def _perform_http(self, method, path, params=None, body=None, stream=False):
if body:
body = json.dumps(body)
auth = HTTPBasicAuth(self.api_key, "")
try:
http_res = self._session.request(
method, "%s/%s" % (self.base_uri, path),
params=params, data=body,
auth=auth, stream = stream)
http_res.raise_for_status()
return http_res
except exceptions.HTTPError:
ex = http_res.json()
raise DataikuException("%s: %s" % (ex.get("errorType", "Unknown error"), ex.get("message", "No message")))
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)