-
Notifications
You must be signed in to change notification settings - Fork 1
/
openneuro_graphql.py
100 lines (81 loc) · 3.21 KB
/
openneuro_graphql.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
import os.path
import os
import requests
import argparse
from dotenv import load_dotenv
OPENNEURO_URL = 'https://openneuro.org/'
DOTENV_PATH = '/corral/utexas/poldracklab/data/OpenNeuro/software/tacc-openneuro/openneuro_api_key_wbhi.env'
def graphql_query(query, openneuro_url, openneuro_api_key):
headers = {"Content-Type": "application/json"}
cookies = {"accessToken": openneuro_api_key}
url = os.path.join(openneuro_url, "crn/graphql")
json = {"query": query}
response = requests.post(url, headers=headers, json=json, cookies=cookies)
return response.json()
def get_ds_url(accession_number, openneuro_url, openneuro_api_key):
query = """
query {
dataset(id: "accession_number") {
worker
}
}
""".replace("accession_number", accession_number)
response_json = graphql_query(query, openneuro_url, openneuro_api_key)
git_worker_number = str(response_json["data"]["dataset"]["worker"][-1])
ds_url = os.path.join(openneuro_url, 'git', git_worker_number, accession_number)
return ds_url
def new_dataset_query(openneuro_url, openneuro_api_key):
query = """
mutation {
createDataset(affirmedDefaced: true) {
id
}
}
"""
response_json = graphql_query(query, openneuro_url, openneuro_api_key)
accession_number = response_json["data"]["createDataset"]["id"]
return accession_number
def snapshot(accession_number, openneuro_url, openneuro_api_key):
query = """
mutation {
createSnapshot(
datasetId: "accession_number",
tag: "1.0.0",
changes: ["Initial snapshot"]
) {
id
}
}
""".replace("accession_number", accession_number)
return graphql_query(query, openneuro_url, openneuro_api_key)
def publish(accession_number, openneuro_url, openneuro_api_key):
query = """
mutation PublishDataset($datasetId: ID!) {
publishDataset(datasetId: "accession_number")
}
""".replace("accession_number", accession_number)
return graphql_query(query, openneuro_url, openneuro_api_key)
def main():
load_dotenv(dotenv_path=DOTENV_PATH)
openneuro_api_key = os.getenv('OPENNEURO_API_KEY')
parser = argparse.ArgumentParser()
parser.add_argument('-u', '--upload', action='store_true', help='Upload dataset')
parser.add_argument('-s', '--snapshot', action='store_true', help='Create initial snapshot')
parser.add_argument('-p', '--publish', action='store_true', help='Publish snapshot')
parser.add_argument('-n', '--new', action='store_true', help='New dataset')
parser.add_argument('-d', '--dataset', type=str, help='Dataset number')
args = parser.parse_args()
if args.upload:
if args.new:
accession_number = new_dataset_query(OPENNEURO_URL, openneuro_api_key)
elif args.dataset:
accession_number = args.dataset
ds_url = get_ds_url(accession_number, OPENNEURO_URL, openneuro_api_key)
print(ds_url)
elif args.snapshot:
response = snapshot(args.dataset, OPENNEURO_URL, openneuro_api_key)
elif args.publish:
response = publish(args.dataset, OPENNEURO_URL, openneuro_api_key)
print(response)
if __name__ == "__main__":
main()