This repository has been archived by the owner on Oct 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest_client.py
48 lines (36 loc) · 1.64 KB
/
rest_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
import requests
class DataViewRestClient():
def __init__(self, endpoint, authtoken, certificate=None):
self.ENDPOINT = endpoint
self.AUTHTOKEN = authtoken
self.CERTIFICATE = certificate
if not self.ENDPOINT.endswith('/'):
self.ENDPOINT = self.ENDPOINT + '/'
def get_headers(self):
headers = {'Authorization': 'Token: ' + self.AUTHTOKEN, 'Accept': 'application/json'}
def list_models(self, name):
r = requests.get(self.ENDPOINT + '' + name, headers=self.get_headers(), verify=self.CERTIFICATE)
if r.status_code == 200:
return r.json()
else:
raise Exception(r.status_code, r.text)
def create_model(self, name, values):
r = requests.post(self.ENDPOINT + '' + name + '/', headers=self.get_headers(), verify=self.CERTIFICATE, data=values)
if r.status_code == 201:
return r.json()
else:
raise Exception(r.status_code, r.text)
def get_model(self, name, key):
r = requests.get(self.ENDPOINT + '' + name + '/' + str(key), headers=self.get_headers(), verify=self.CERTIFICATE)
if r.status_code == 200:
return r.json()
else:
raise Exception(r.status_code, r.text)
def update_model(self, name, key, values):
r= requests.get(self.ENDPOINT + '' + name + '/' + str(key), headers=self.get_headers(), verify=self.CERTIFICATE, data=values)
def delete_model(self):
r= requests.delete(self.ENDPOINT + '' + name + '/' + str(key), headers=self.get_headers(), verify=self.CERTIFICATE)
if r.status_code == 200:
return r.json()
else:
raise Exception(r.status_code, r.text)