-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_details.py
More file actions
69 lines (59 loc) · 2.59 KB
/
service_details.py
File metadata and controls
69 lines (59 loc) · 2.59 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
import base64
import sys
import requests
def main():
api_key = None
api_password = None
try:
import settings
api_key = settings.API_KEY
api_password = settings.API_PASSWORD
except:
# Ignore settings file
pass
endpoint = sys.argv[1]
if endpoint.startswith("https://cloud-api.gate.ac.uk"):
# This is a standard GATE Cloud endpoint, so first try the sheets services file
with requests.get("https://github.com/GateNLP/gate-metadata/raw/refs/heads/master/sheets-services/services-1.0.0.json") as resp:
resp.raise_for_status()
services = resp.json()
_, _, slug = endpoint.rpartition("/")
service_info = next((s for s in services if s["slug"] == slug), None)
if service_info:
print(f"Service {slug} has the following recommended result specifiers:")
print()
for spec in service_info["configs"].get("standard", []):
print(f" {spec}")
if "extra" in service_info["configs"]:
print()
print("and the following additional examples:")
print()
for spec in service_info["configs"]["extra"]:
print(f" {spec}")
sys.exit(0)
# We didn't find any info in the sheets services file, so try the service metadata
headers = {"Accept": "application/json"}
if api_key:
auth_header = "Basic " + base64.b64encode(bytes(f"{api_key}:{api_password}", "utf-8")).decode("ascii")
headers["Authorization"] = auth_header
with requests.get(endpoint + "/metadata", headers=headers) as resp:
resp.raise_for_status()
service_metadata = resp.json()
all_selectors = []
if service_metadata.get("defaultAnnotations"):
all_selectors.extend(sel.strip() for sel in service_metadata["defaultAnnotations"].split(","))
if service_metadata.get("additionalAnnotations"):
all_selectors.extend(sel.strip() for sel in service_metadata["additionalAnnotations"].split(","))
all_types = set()
for sel in all_selectors:
_, _, ann_type = sel.partition(":")
all_types.add(ann_type)
print(f"Service {endpoint} can return the following annotation types:")
print()
for t in all_types:
print(f" {t}")
print()
print("For details on the features of each annotation, see the documentation or try the")
print("service yourself with some sample data.")
if __name__ == "__main__":
main()