General /queries endpoint to handle gateway queries
This file describes the router for:
where, id
may be left out.
get_queries(request, params)
async
GET /queries
Return overview of all (active) queries.
Source code in optimade_gateway/routers/queries.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 | @ROUTER.get(
"/queries",
response_model=QueriesResponse,
response_model_exclude_defaults=False,
response_model_exclude_none=False,
response_model_exclude_unset=True,
tags=["Queries"],
responses=ERROR_RESPONSES,
)
async def get_queries(
request: Request,
params: Annotated[EntryListingQueryParams, Depends()],
) -> QueriesResponse:
"""`GET /queries`
Return overview of all (active) queries.
"""
return await get_entries(
collection=await collection_factory(CONFIG.queries_collection),
response_cls=QueriesResponse,
request=request,
params=params,
)
|
get_query(request, query_id, response)
async
GET /queries/{query_id}
Return a single QueryResource
.
Source code in optimade_gateway/routers/queries.py
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 | @ROUTER.get(
"/queries/{query_id}",
response_model=QueriesResponseSingle,
response_model_exclude_defaults=False,
response_model_exclude_none=False,
response_model_exclude_unset=True,
tags=["Queries"],
responses=ERROR_RESPONSES,
)
async def get_query(
request: Request,
query_id: str,
response: Response,
) -> QueriesResponseSingle:
"""`GET /queries/{query_id}`
Return a single [`QueryResource`][optimade_gateway.models.queries.QueryResource].
"""
collection = await collection_factory(CONFIG.queries_collection)
query: QueryResource = await get_valid_resource(collection, query_id)
if query.attributes.response and query.attributes.response.errors:
for error in query.attributes.response.errors:
if error.status:
for part in error.status.split(" "):
try:
response.status_code = int(part)
break
except ValueError:
pass
if response.status_code and response.status_code >= 300:
break
else:
response.status_code = 500
return QueriesResponseSingle(
links=ToplevelLinks(next=None),
data=query,
meta=meta_values(
url=request.url,
data_returned=1,
data_available=await collection.acount(),
more_data_available=False,
schema=CONFIG.schema_url,
),
)
|
post_queries(request, query)
async
POST /queries
Create or return existing gateway query according to query
.
Source code in optimade_gateway/routers/queries.py
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 | @ROUTER.post(
"/queries",
response_model=QueriesResponseSingle,
response_model_exclude_defaults=False,
response_model_exclude_none=False,
response_model_exclude_unset=True,
tags=["Queries"],
status_code=status.HTTP_202_ACCEPTED,
responses=ERROR_RESPONSES,
)
async def post_queries(
request: Request,
query: QueryCreate,
) -> QueriesResponseSingle:
"""`POST /queries`
Create or return existing gateway query according to `query`.
"""
await validate_resource(
await collection_factory(CONFIG.gateways_collection), query.gateway_id
)
result, created = await resource_factory(query)
background_tasks: set[asyncio.Task] = set()
if created:
task = asyncio.create_task(perform_query(url=request.url, query=result))
# Add task to the set. This creates a strong reference.
background_tasks.add(task)
# To prevent keeping references to finished tasks forever,
# make each task remove its own reference from the set after
# completion:
task.add_done_callback(background_tasks.discard)
collection = await collection_factory(CONFIG.queries_collection)
return QueriesResponseSingle(
links=ToplevelLinks(next=None),
data=result,
meta=meta_values(
url=request.url,
data_returned=1,
data_available=await collection.acount(),
more_data_available=False,
schema=CONFIG.schema_url,
**{f"_{CONFIG.provider.prefix}_created": created},
),
)
|