forked from alebas-fr/BadgeuseDokos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dokos_connector.py
91 lines (74 loc) · 3.2 KB
/
dokos_connector.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import requests
class DokosConnector:
def __init__(self, api_url, dokos_client, dokos_token):
"""
Init the dokos connector with the required parameters
:param api_url: The Dokos url that ends with api/
:param dokos_client: The Dokos API Client
:param dokos_token: The Dokos API Client Token
"""
self.api_url = api_url
self.api_login = dokos_client
self.api_password = dokos_token
# Init the headers used in the authenticated request
self.headers = {
'Authorization': "token " + dokos_client + ":" + dokos_token,
'Accept': 'application/json'
}
def insert_resource(self, resource, data):
"""
Insert a resource
:param resource: The DocType name
:param data: Data to be inserted, should be a json formatted String
:return: True if inserted, false if not
"""
return requests.post(self.api_url + "resource/" + resource, data=data, headers=self.headers).status_code == 200
def get_resources(self, resource):
"""
Get a list of resources for the required DocType
:param resource: The DocType name
:return: Return a JSON that contain the resources's name
"""
r = requests.get(self.api_url + "resource/" + resource, headers=self.headers)
return r
def get_resource(self, resource, name):
"""
Get a unique resource by name
:param resource: The DocType name
:param name: The resource's name
:return: : Return a JSON that contain the resource
"""
r = requests.get(self.api_url + "resource/" + resource + "/" + name, headers=self.headers)
return r
def get_resources_by_filter(self, resource, filters):
"""
Get a list of filtered resources
:param resource: The Doctype name
:param filters: Should be an array that contains filters
:return: Return a JSON that contain the resources's names
"""
request = self.api_url + 'resource/' + resource + '?filters=' + str(filters)
request = request.replace("'", '"')
r = requests.get(request, headers=self.headers)
return r
def update_resource_by_name(self, resource, name, data):
"""
Update a resource by his name
:param resource: The Doctype name
:param name: The resource name
:param data: The data, should in dict format
:return: Return the updated content in json and request the status code
"""
data = str(data)
data = data.replace("'", '"')
r = requests.put(self.api_url + "resource/" + resource + "/" + name, data=data, headers=self.headers)
return r.content, r.status_code
def delete_resource_by_name(self, resource, name):
"""
Delete a specified resource by name
:param resource: The Doctype name
:param name: The resource name
:return: The request status codee
"""
r = requests.delete(self.api_url + "resource/" + resource + "/" + name, headers=self.headers)
return r.status_code