-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
287 lines (270 loc) · 8.18 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
from icecream import ic
import os
import yaml
from rich import get_console
from litestar import Litestar, get
from litestar.openapi import OpenAPIConfig
from litestar.config.cors import CORSConfig
from litestar.openapi.plugins import ScalarRenderPlugin
from litestar.types import ASGIApp, Scope, Receive, Send
from etl.isatab import ingest_isatab
from etl.arc import ingest_arc
from routes.core.router import CoreRouter
from routes.germplasm.router import GermplasmRouter
from routes.phenotyping.router import PhenotypingRouter
from db.models.db_connect import db_connection, provide_transaction
import base64
def load_config():
with open('config.yml', 'r') as file:
config = yaml.safe_load(file)
return config
async def init():
app.state.console = get_console()
app.state.config = load_config()
if app.state.config['format'] == 'isa-tab':
data = ingest_isatab.extract(app.state.config['data'])
ingest_isatab.transform_and_load(data, app.state.config['data'])
elif app.state.config['format'] == 'arc':
data = ingest_arc.extract(app.state.config['data'])
ingest_arc.transform_and_load(data, app.state.config['data'])
else:
raise ValueError("Invalid format in config")
return
async def exit():
os.remove('miappe.db')
cors_config = CORSConfig(
allow_origins=['*'],
allow_methods=['GET'],
allow_headers=['*'],
allow_credentials=True,
)
@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": "Success",
"messageType": "INFO"
}
]
},
"result": {
"calls": [
{
"contentTypes": [
"application/json"
],
"methods": [
"GET"
],
"service": "trials",
"versions": [
"2.1"
]
},
{
"contentTypes": [
"application/json"
],
"methods": [
"GET"
],
"service": "trial/{trialDbId}",
"versions": [
"2.1"
]
},
{
"contentTypes": [
"application/json"
],
"methods": [
"GET"
],
"service": "studies",
"versions": [
"2.1"
]
},
{
"contentTypes": [
"application/json"
],
"methods": [
"GET"
],
"service": "studies/{studyDbId}",
"versions": [
"2.1"
]
},
{
"contentTypes": [
"application/json"
],
"methods": [
"GET"
],
"service": "germplasm",
"versions": [
"2.1"
]
},
{
"contentTypes": [
"application/json"
],
"methods": [
"GET"
],
"service": "germplasm/{germplasmDbId}",
"versions": [
"2.1"
]
},
{
"contentTypes": [
"application/json"
],
"methods": [
"GET"
],
"service": "observationunits",
"versions": [
"2.1"
]
},
{
"contentTypes": [
"application/json"
],
"methods": [
"GET"
],
"service": "observationunits/{observationUnitDbId}",
"versions": [
"2.1"
]
},
{
"contentTypes": [
"application/json"
],
"methods": [
"GET"
],
"service": "observations",
"versions": [
"2.1"
]
},
{
"contentTypes": [
"application/json"
],
"methods": [
"GET"
],
"service": "observations/{observationDbId}",
"versions": [
"2.1"
]
},
{
"contentTypes": [
"application/json"
],
"methods": [
"GET"
],
"service": "variables/{observationVariableDbId}",
"versions": [
"2.1"
]
},
{
"contentTypes": [
"application/json"
],
"methods": [
"GET"
],
"service": "variables",
"versions": [
"2.1"
]
},
{
"contentTypes": [
"application/json"
],
"methods": [
"GET"
],
"service": "observations/table",
"versions": [
"2.1"
]
}
],
"contactEmail": "[email protected]",
"documentationURL": "institute.org/server",
"location": "USA",
"organizationName": "The Institute",
"organizationURL": "institute.org/home",
"serverDescription": "The BrAPI Test Server\nWeb Server - Apache Tomcat 7.0.32\nDatabase - Postgres 10\nSupported BrAPI Version - V2.0",
"serverName": "The BrAPI Test Server"
}
}
scalar_plugin = ScalarRenderPlugin(version="1.19.5", path="/scalar")
middlewares = []
config = load_config()
if config.get('aai', False):
basic_auth = config['aai'][0]['username'] + ':' + config['aai'][0]['password']
token = base64.b64encode(basic_auth.encode('utf-8'))
def basic_auth_factory(app:ASGIApp)->ASGIApp:
async def basic_auth(scope:Scope, receive:Receive, send:Send):
if scope['type'] == 'http':
if scope['method'] == 'OPTIONS':
await app(scope, receive, send)
return
headers = dict(scope['headers'])
if headers.get(b'authorization', b'') != b'Basic ' + token:
await send({
'type': 'http.response.start',
'status': 401,
'headers': [
[b'www-authenticate', b'Basic realm="Login Required"']
]
})
await send({
'type': 'http.response.body',
'body': b'Unauthorized'
})
return
await app(scope, receive, send)
return basic_auth
middlewares.append(basic_auth_factory)
app = Litestar(
route_handlers=[server_info, CoreRouter, GermplasmRouter, PhenotypingRouter],
cors_config=cors_config,
lifespan=[db_connection],
middleware=middlewares,
on_startup=[init],
on_shutdown=[exit],
dependencies={'transaction': provide_transaction},
openapi_config=OpenAPIConfig(
title="MIRA",
version="0.1.0",
description="MIRA is a BrAPI server deployed on top of a MIAPPE compliant ISArchive.",
render_plugins=[ScalarRenderPlugin()],
),
)