-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver-setup.py
152 lines (124 loc) · 4.12 KB
/
server-setup.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import requests
import json
import sys
import os
import random
import time
def now():
return time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
container = {
"@context": ["http://www.w3.org/ns/anno.jsonld", "http://www.w3c.org/ns/ldp.jsonld"],
"type": ["BasicContainer", "AnnotationCollection"],
"label": "A Basic Annotation Container",
"creator": {
"id": "https://orcid.org/0000-0003-4441-6852",
"type": "Person",
"name": "Rob Sanderson",
"nickname": "azaroth42",
"email": "mailto:azaroth42@gmail.com"
},
"created": now(),
"rights": "https://creativecommons.org/publicdomain/zero/1.0"
}
anno = {
"@context": "http://www.w3.org/ns/anno.jsonld",
"type": "Annotation",
"body": {"value": "I like this thing"},
"target": "http://www.example.org/"
}
hdrs = {'Content-Type': 'application/ld+json;profile="http://www.w3.org/ns/anno.jsonld"'}
url = "http://localhost:8080/annos/"
if '--container' in sys.argv:
data = json.dumps(container)
print "Sending: " + data
req = requests.put(url=url, data=data, headers=hdrs)
if '--delete-container' in sys.argv:
req = requests.delete(url=url)
if '--annotation' in sys.argv:
data = json.dumps(anno)
req = requests.post(url=url, data=data, headers=hdrs)
if '--examples' in sys.argv:
where = "/Users/rsanderson/Development/OpenAnno/web-annotation/model/wd2/examples/correct/"
files = os.listdir(where)
for f in files:
if f.startswith('anno'):
fn = os.path.join(where, f)
fh = file(fn)
data = fh.read()
fh.close()
js = json.loads(data)
uri = js['id']
slug = os.path.split(uri)[1]
hdrs['Slug'] = slug
req = requests.post(url=url, data=data, headers=hdrs)
if '--many-annotations' in sys.argv:
for x in range(500):
anno['body']['value'] = "Annotation {0}".format(x)
hdrs['Slug'] = 'anno_{0}'.format(x)
data = json.dumps(anno)
req = requests.post(url=url, data=data, headers=hdrs)
if '--slug-annotation' in sys.argv:
data = json.dumps(anno)
hdrs['Slug'] = 'my_first_annoation'
req = requests.post(url=url, data=data, headers=hdrs)
if '--put' in sys.argv:
# First get list of annotations
phdrs = {"Prefer": 'return=representation;include="http://www.w3.org/ns/oa#PreferContainedURIs"'}
req = requests.get(url=url, headers=phdrs)
body = req.json()
annos = body['first']['items']
rnd = random.randrange(0, len(annos))
annoUrl = annos[rnd]['id']
req = requests.get(url=annoUrl)
et = req.headers['etag']
anno = req.json()
anno['testing'] = "random stuff"
data = json.dumps(anno)
print data
req = requests.put(url=annoUrl, data=data, headers=hdrs)
print req.text
hdrs['If-Match'] = et
req = requests.put(url=annoUrl, data=data, headers=hdrs)
print ""
print req.text
if '--delete' in sys.argv:
phdrs = {"Prefer": 'return=representation;include="http://www.w3.org/ns/oa#PreferContainedURIs"'}
req = requests.get(url=url, headers=phdrs)
body = req.json()
annos = body['first']['items']
rnd = random.randrange(0, len(annos))
annoUrl = annos[rnd]['id']
req = requests.get(url=annoUrl)
et = req.headers['etag']
hdrs['If-Match'] = et
req = requests.delete(url=annoUrl, headers=hdrs)
if '--ttl' in sys.argv:
# First get list of annotations
annoUrl = "http://localhost:8000/annos/my_first_annoation"
# Fetch it as turtle
fetchHdrs = {"Accept": "text/turtle"}
req = requests.get(url=annoUrl, headers=fetchHdrs)
body = req.text
print body
body = body.replace("this thing", 'this thing FROM TURTLE :)')
ttlhdrs = {"Content-Type": "text/turtle"}
et = req.headers['etag']
ttlhdrs['If-Match'] = et
req = requests.put(url=annoUrl, data=body, headers=ttlhdrs)
print req.text
if '--patch' in sys.argv:
phdrs = {"Prefer": 'return=representation;include="http://www.w3.org/ns/oa#PreferContainedURIs"'}
req = requests.get(url=url, headers=phdrs)
body = req.json()
annos = body['first']['items']
rnd = random.randrange(0, len(annos))
annoUrl = annos[rnd]['id']
req = requests.get(url=annoUrl)
et = req.headers['etag']
data = json.dumps({"testing_patch": "Some Value Here"})
req = requests.patch(url=annoUrl, data=data, headers=hdrs)
print req.text
hdrs['If-Match'] = et
req = requests.patch(url=annoUrl, data=data, headers=hdrs)
print ""
print req.text