1
|
#!/usr/bin/python
|
2
|
#
|
3
|
# TODO:
|
4
|
# validate
|
5
|
# getNextDocid
|
6
|
# getNextRevision(docid)
|
7
|
# getDocids(keyword=None)
|
8
|
# query(keyword, returnfields)
|
9
|
# queryDict => same as above but returns python dictionary data structure
|
10
|
|
11
|
import httplib, urllib
|
12
|
|
13
|
class MetacatClient:
|
14
|
|
15
|
def __init__(self, server="pmark.msi.ucsb.edu:8180", urlPath="/knb_test/metacat"):
|
16
|
self.metacatUrlPath = urlPath
|
17
|
self.metacatServer = server
|
18
|
self.sessionid = None
|
19
|
|
20
|
def getMetacatUrl(self):
|
21
|
return "http://" + self.metacatServer + self.metacatUrlPath
|
22
|
|
23
|
def login(self, username, password, organization=None):
|
24
|
|
25
|
if organization == 'NCEAS':
|
26
|
uid = 'uid=%s,o=NCEAS,dc=ecoinformatics,dc=org' % username
|
27
|
else:
|
28
|
uid = username
|
29
|
|
30
|
postdata = { 'action' : 'login',
|
31
|
'qformat' : 'xml',
|
32
|
'username' : uid,
|
33
|
'password' : password }
|
34
|
|
35
|
response = self.postRequest(postdata)
|
36
|
if response.find("<login>") != -1:
|
37
|
return True
|
38
|
else:
|
39
|
return False
|
40
|
|
41
|
def logout(self):
|
42
|
postdata = { 'action' : 'logout',
|
43
|
'qformat' : 'xml'}
|
44
|
|
45
|
response = self.postRequest(postdata)
|
46
|
if response.find("<logout>") != -1:
|
47
|
return True
|
48
|
else:
|
49
|
return False
|
50
|
|
51
|
def read(self, docid, qformat="xml"):
|
52
|
postdata = { 'action' : 'read',
|
53
|
'qformat' : qformat,
|
54
|
'docid' : docid }
|
55
|
response = self.postRequest(postdata)
|
56
|
# if error node returned
|
57
|
if response.find("<error>") != -1:
|
58
|
return False
|
59
|
else:
|
60
|
return response
|
61
|
|
62
|
|
63
|
def insert(self, docid, doctext):
|
64
|
postdata = { 'action' : 'insert',
|
65
|
'doctext' : doctext,
|
66
|
'docid' : docid }
|
67
|
response = self.postRequest(postdata)
|
68
|
# if error node returned
|
69
|
if response.find("<error>") != -1:
|
70
|
return False
|
71
|
else:
|
72
|
return response
|
73
|
|
74
|
def update(self, docid, doctext):
|
75
|
postdata = { 'action' : 'update',
|
76
|
'doctext' : doctext,
|
77
|
'docid' : docid }
|
78
|
response = self.postRequest(postdata)
|
79
|
return response
|
80
|
|
81
|
def delete(self, docid):
|
82
|
postdata = { 'action' : 'delete',
|
83
|
'docid' : docid }
|
84
|
response = self.postRequest(postdata)
|
85
|
return response
|
86
|
|
87
|
def squery(self, pathquery, qformat="xml"):
|
88
|
postdata = { 'action' : 'squery',
|
89
|
'qformat' : qformat,
|
90
|
'query' : pathquery }
|
91
|
response = self.postRequest(postdata)
|
92
|
return response
|
93
|
|
94
|
def postRequest(self, postdata):
|
95
|
conn = httplib.HTTPConnection( self.metacatServer )
|
96
|
params = urllib.urlencode( postdata )
|
97
|
headers = { "Content-type" : "application/x-www-form-urlencoded",
|
98
|
"Accept" : "*/*"}
|
99
|
|
100
|
# If we have an active session, set the cookie
|
101
|
if self.sessionid is not None:
|
102
|
headers['Cookie'] = self.sessionid
|
103
|
|
104
|
conn.request( "POST", self.metacatUrlPath, params, headers )
|
105
|
response = conn.getresponse()
|
106
|
|
107
|
# If metacat responds with a new session id,
|
108
|
# register it with the metacat client instance
|
109
|
setcookie = response.getheader("set-cookie", None)
|
110
|
if setcookie:
|
111
|
jsid = setcookie.split(';')[0]
|
112
|
if jsid[:11] == "JSESSIONID=":
|
113
|
self.sessionid = jsid
|
114
|
|
115
|
if response.status == 200:
|
116
|
content = response.read()
|
117
|
else:
|
118
|
print " !!! SERVER DID NOT RETURN 'OK'.... STATUS is " + str(response.status)
|
119
|
content = ""
|
120
|
conn.close()
|
121
|
return content
|
122
|
|