Skip to content

Commit 3b0b152

Browse files
authored
Simple script to POST metadata definitions. (#762)
* Simple script to POST metadata definitions. Fixed syntax of one definition or it wouldn't POST. * Black formatting.
1 parent 197a4ba commit 3b0b152

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

scripts/metadata/definitions/time.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"context" : [
55
{
66
"doi" : "https://schema.org/Time"
7-
},
7+
}
88
],
99
"fields" : [
1010
{
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
""" Upload metadata definitions from a local directory of JSON definitions.
2+
3+
`python upload_definitions.py http://localhost:8000/api/v2 {API_KEY} definitions`
4+
"""
5+
6+
import json
7+
import os
8+
import sys
9+
10+
import requests
11+
12+
if __name__ == "__main__":
13+
# command line args
14+
args = sys.argv[1:]
15+
# instance url
16+
api: str = args[
17+
0
18+
] # dev: http://localhost:8000/api/v2 prod: http://localhost/api/v2
19+
# api key
20+
token: str = args[1]
21+
# directory with json metadata definitions
22+
dir: str = args[2]
23+
# header for POST request
24+
headers = {"X-API-KEY": token}
25+
# read definitions from local directory
26+
dir_list = os.listdir(dir)
27+
for file in dir_list:
28+
with open(os.path.join(dir, file)) as metadata_definition:
29+
json_definition = json.load(metadata_definition)
30+
response = requests.post(
31+
f"{api}/metadata/definition",
32+
json=json_definition,
33+
headers=headers,
34+
)
35+
response.raise_for_status()
36+
print(f"Uploaded {len(dir_list)} metadata definitions.")

0 commit comments

Comments
 (0)