-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
241 lines (228 loc) · 7.17 KB
/
app.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import warnings
from litestar import Litestar, get
from litestar.response import Redirect
from litestar.config.cors import CORSConfig
from litestar.openapi import OpenAPIConfig
from litestar.openapi.plugins import ScalarRenderPlugin
from routes.core.router import CoreRouter
from routes.germplasm.router import GermplasmRouter
from routes.phenotyping.router import PhenotypingRouter
from routes.search.router import SearchRouter
from dotenv import load_dotenv
import os
import git
from utils import arc
# IDEA: Maybe query the Gitlab for groups and their repsitories and list the groups as programs.
# This would allow this tool to be deployed for a DataHUB instead of a single ARC.
# e.g. ARC https://git.nfdi4plants.de/shape/spring and https://git.nfdi4plants.de/shape/winter would be in the program
# shape and contain the results for the spring and winter barley phenotyping results.
#
# TODO: How would the study type be determined?
# TODO: How would the ARC be stored?
# TODO: How to handle study/trial unspecific requests?
def init_app():
load_dotenv()
app.state.pat = os.getenv('DATAHUB_PAT')
if not os.path.exists('data'):
git.Repo.clone_from(
f"{os.getenv('DATAHUB_URL')}{os.getenv('ARC_URI')}", 'data')
metadata_path = 'data/metadata.json'
if os.path.exists(metadata_path):
with open(metadata_path, 'r') as file:
app.state.rocrate = file.read()
else:
warnings.warn(RuntimeWarning(
'No metadata found. Trying to create a RO-Crate.json from ARC...'))
arc_obj = arc.read('data')
rocrate_json = arc.ARC().to_rocrate_json_string()(arc_obj)
# FIXME: Needs manual intervention to convert the values to strings.
with open(metadata_path, 'w') as file:
file.write(rocrate_json)
app.state.rocrate = rocrate_json
@get('/brapi/v2/serverinfo', include_in_schema=False)
async def server_info() -> dict:
return {
"metadata": {
"datafiles": [],
"pagination": {
"currentPage": 0,
"pageSize": 1000,
"totalCount": 10,
"totalPages": 1
},
"status": [
{
"message": "Request accepted, response successful",
"messageType": "INFO"
}
]
},
"result": {
"calls": [
{
"contentTypes": [
"application/json"
],
"methods": [
"GET"
],
"service": "trials",
"versions": [
"2.1"
]
},
{
"contentTypes": [
"application/json"
],
"methods": [
"GET"
],
"service": "programs",
"versions": [
"2.1"
]
},
{
"contentTypes": [
"application/json"
],
"methods": [
"GET"
],
"service": "studies",
"versions": [
"2.1"
]
},
{
"contentTypes": [
"application/json"
],
"methods": [
"GET"
],
"service": "studytypes",
"versions": [
"2.1"
]
},
{
"contentTypes": [
"application/json"
],
"methods": [
"POST"
],
"service": "search/germplasm",
"versions": [
"2.1"
]
},
{
"contentTypes": [
"application/json"
],
"methods": [
"GET"
],
"service": "germplasm",
"versions": [
"2.1"
]
},
{
"contentTypes": [
"application/json"
],
"methods": [
"POST"
],
"service": "search/variables",
"versions": [
"2.1"
]
},
{
"contentTypes": [
"application/json"
],
"methods": [
"GET"
],
"service": "observationunits",
"versions": [
"2.1"
]
},
{
"contentTypes": [
"application/json"
],
"methods": [
"POST"
],
"service": "observationunits",
"versions": [
"2.1"
]
},
{
"contentTypes": [
"application/json"
],
"methods": [
"POST"
],
"service": "observations",
"versions": [
"2.1"
]
},
{
"contentTypes": [
"application/json"
],
"methods": [
"GET"
],
"service": "variables",
"versions": [
"2.1"
]
}
],
"contactEmail": "feser@ipk-gatersleben.de",
"documentationURL": "",
"location": "Germany",
"organizationName": "IPK Gatersleben",
"organizationURL": "leibniz-ipk.de",
"serverDescription": "Brapi2ARC Test Server",
"serverName": "The BrAPI 2 ARC Test Server"
}
}
@get('/', include_in_schema=False)
async def root() -> None:
return Redirect('/schema')
cors_config = CORSConfig(
allow_origins=['*'],
allow_methods=['GET', 'POST', 'OPTIONS'],
allow_credentials=True
)
app = Litestar(
route_handlers=[
server_info,
root,
CoreRouter,
GermplasmRouter,
PhenotypingRouter,
SearchRouter
],
cors_config=cors_config,
on_startup=[init_app],
openapi_config=OpenAPIConfig(
title='BrAPI 2 ARC',
version='0.1.0',
render_plugins=[ScalarRenderPlugin()]
)
)