-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollectiveaccess.py
More file actions
144 lines (119 loc) · 4.94 KB
/
collectiveaccess.py
File metadata and controls
144 lines (119 loc) · 4.94 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import requests
from urlparse import urlparse, urljoin
from urllib import urlencode
import json
import os, sys
_SCRIPT = 'service.php' #only script, currently, for CA API.
class CollectiveAccess(object):
"""creates a session for collective access actions
and provides a set of methods for doing basic
interactions:
GET, PUT (create and update), and DELETE objects and entities"""
def __init__(self, base, user, passw, header={'content-type':'application/json'}):
super(CollectiveAccess, self).__init__()
self.base = base
self.user = user
self.passw = passw
self.header = header
self.sesh = requests.Session()
try:
self.sesh.auth = (self.user, self.passw)
except Exception as e:
sys.exit("Error creating login session. Check your credentials and try again: {0}".format(e))
def _request_get(self, target, headers=None):
res = self.sesh.get(target, headers=headers)
if res.status_code == 200:
return json.loads(res.text)
else:
print _raise_status_error(res)
def _request_put(self, target, data=None, headers=None):
res = self.sesh.put(target, data=data, headers=self.header)
if res.status_code == 200:
return json.loads(res.text)
else:
print _raise_status_error(res)
def _request_delete(self, target, headers=None):
res = self.sesh.delete(target, headers=headers)
if res.status_code == 200:
return json.loads(res.text)
else:
print _raise_status_error(res)
#get many
def get_entities(self, q=None):
query = '*' if q is None else q
path = _build_api_uri(endpoint='find', table='ca_entities', q={'q':query})
target = os.path.join(self.base, path)
return self._request_get(target, headers=self.header)
def get_objects(self, q=None):
query = '*' if q is None else q
path = _build_api_uri(endpoint='find', table='ca_objects', q={'q':query})
target = os.path.join(self.base, path)
return self._request_get(target, headers=self.header)
#get one
def get_entity(self, entity_id):
path = _build_api_uri(endpoint='item', table='ca_entities', identifier='id', item=entity_id)
target = os.path.join(self.base, path)
return self._request_get(target, headers=self.header)
def get_object(self, object_id):
path = _build_api_uri(endpoint='item', table='ca_objects', identifier='id', item=object_id)
target = os.path.join(self.base, path)
return self._request_get(target, headers=self.header)
#create one
def create_entity(self, data):
path = _build_api_uri(endpoint='item', table='ca_entities')
data = json.dumps(data)
target = os.path.join(self.base, path)
return self._request_put(target, data=data, headers=self.header)
def create_object(self, data):
path = _build_api_uri(endpoint='item', table="ca_objects")
data = json.dumps(data)
target = os.path.join(self.base, path)
return self._request_put(target, data=data, headers=self.header)
#update one
def update_entity(self, entity_id, data):
path = _build_api_uri(endpoint='item', table='ca_entities', identifier='id', item=entity_id)
data = json.dumps(data)
target = os.path.join(self.base, path)
return self._request_put(target, data=data, headers=self.header)
def update_object(self, object_id, data):
path = _build_api_uri(endpoint='item', table='ca_objects', identifier='id', item=object_id)
data = json.dumps(data)
target = os.path.join(self.base, path)
return self._request_put(target, data=data, headers=self.header)
def update_collection(self, collection_id, data):
path = _build_api_uri(endpoint='item', table='ca_collections', identifier='id', item=collection_id)
data = json.dumps(data)
target = os.path.join(self.base, path)
return self._request_put(target, data=data, headers=self.header)
#delete one
def delete_entity(self, entity_id):
path = _build_api_uri(endpoint='item', table='ca_entities', identifier='id', item=entity_id)
target = os.path.join(self.base, path)
return self._request_delete(target, headers=self.header)
def delete_object(self, object_id):
path = _build_api_uri(endpoint='item', table='ca_entities', identifier='id', item=object_id)
target = os.path.join(self.base, path)
return self._request_delete(target, headers=self.header)
# helper methods
def _build_api_uri(**kwargs):
'''create the full URL we make the request to'''
path = (kwargs.pop('endpoint', ''),
kwargs.pop('table', ''),
kwargs.pop('identifier', ''),
str(kwargs.pop('item', '')))
path_items = [p for p in path if p != '']
query = urlencode(kwargs.pop('q', {}))
# join them
url_path = os.path.join(_SCRIPT, '/'.join(path_items))
if query != '':
return url_path + '?' + query
else:
return url_path
def _raise_status_error(response):
'''format the error for more information if there is an HTTP error'''
status_error = ""
if 400 <= response.status_code < 500:
status_error = "_-_{0}_-_ Client Error: {1}".format(response.status_code, response.reason)
elif 500 <= response.status_code < 600:
status_error = "_-_{0}_-_ Server Error: {1}".format(response.status_code, response.reason)
return status_error