-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathescholIntf.py
125 lines (105 loc) · 3.36 KB
/
escholIntf.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
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
import creds
import sys
import traceback
from urllib import request
from urllib import error
import json
import time
########################################
#
# Interfaces with escholarship graphQL
#
########################################
class graphClient:
def __init__(self, ep):
self.endpoint = ep
self.headers = {'Accept': 'application/json',
'Content-Type': 'application/json',
'Privileged':creds.eschol.privKey
}
if creds.eschol.cookie:
self.headers['Cookie'] = creds.eschol.cookie
def execute(self, query):
return self._send(query)
def _send(self, query):
data = {'query': query}
time.sleep(10)
req = request.Request(self.endpoint, json.dumps(data).encode('utf-8'), self.headers)
try:
response = request.urlopen(req, timeout=1800)
return response.read().decode('utf-8')
except error.HTTPError as e:
print(e.read())
print(e.msg)
raise e
########################################
#
# Uses escholClient to connect with eschol
# Provides functionality for id minting and deposit
#
########################################
class eschol:
"""Interface to connect to eschol"""
mintMutation = '''
mutation{
mintProvisionalID(input:{sourceName: "oa_harvester", sourceID: "PUB_ID" })
{
id
}
}
'''
depositMutation = '''
mutation{
depositItem(input:{DEP_INPUT})
{
id
message
}
}
'''
def __init__(self):
self.client = graphClient(creds.eschol.url)
def testConnection(self):
try:
result = self.client.execute('''
mutation{
mintProvisionalID(input:{sourceName: "oa_harvester", sourceID: "18" })
{
id
}
}
''')
print(result)
res = json.loads(result)
return res['data']['mintProvisionalID']['id']
except error.HTTPError as e:
print(e.read())
except:
print("Exception Info")
print(str(sys.exc_info()[1]))
print(traceback.format_exc())
raise
def createItem(self, pubId):
mintquery = self.mintMutation.replace("PUB_ID",str(pubId))
try:
result = self.client.execute(mintquery)
print(result)
res = json.loads(result)
return res['data']['mintProvisionalID']['id']
except:
print("Exception Info")
print(str(sys.exc_info()[1]))
print(traceback.format_exc())
raise
def depositItem(self, depositInput):
depositquery = self.depositMutation.replace("DEP_INPUT",depositInput)
try:
result = self.client.execute(depositquery)
print(result)
#return just the id
return result
except:
print("Exception Info")
print(str(sys.exc_info()[1]))
print(traceback.format_exc())
raise