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 | async def load_optimade_providers_databases() -> None:
"""Load in the providers' OPTIMADE databases from Materials-Consortia
Utilize the Materials-Consortia list of OPTIMADE providers at
[https://providers.optimade.org](https://providers.optimade.org).
Load in all databases with a valid base URL.
"""
import asyncio
import httpx
from optimade import __api_version__
from optimade.models import LinksResponse
from optimade.models.links import LinkType
from optimade.server.routers.utils import BASE_URL_PREFIXES
from optimade_gateway.common.utils import clean_python_types, get_resource_attribute
from optimade_gateway.models.databases import DatabaseCreate
from optimade_gateway.queries.perform import db_get_all_resources
from optimade_gateway.routers.utils import resource_factory
if not CONFIG.load_optimade_providers_databases:
LOGGER.debug(
"Will not load databases from Materials-Consortia list of providers."
)
return
if TYPE_CHECKING or bool(os.getenv("MKDOCS_BUILD", "")): # pragma: no cover
providers: httpx.Response | LinksResponse
async with httpx.AsyncClient(timeout=5.0) as client:
providers = await client.get(
"https://providers.optimade.org/v"
f"{__api_version__.split('.', maxsplit=1)[0]}/links"
)
if providers.is_error:
LOGGER.warning(
"Response from Materials-Consortia's list of OPTIMADE providers was not "
"successful (status code != 200). No databases will therefore be added at "
"server startup."
)
return
LOGGER.info(
"Registering Materials-Consortia list of OPTIMADE providers' databases."
)
providers = LinksResponse(**providers.json())
valid_providers = []
for provider in providers.data:
if get_resource_attribute(provider, "id") in ("exmpl", "optimade"):
LOGGER.info(
"- %s (id=%r) - Skipping: Not a real provider.",
get_resource_attribute(provider, "attributes.name", "N/A"),
get_resource_attribute(provider, "id"),
)
continue
if not get_resource_attribute(provider, "attributes.base_url"):
LOGGER.info(
"- %s (id=%r) - Skipping: No base URL information.",
get_resource_attribute(provider, "attributes.name", "N/A"),
get_resource_attribute(provider, "id"),
)
continue
valid_providers.append(provider)
# Run queries to each database using the supported major versioned base URL to get a
# list of the provider's databases.
# There is no need to use ThreadPoolExecutor here, since we want this to block
# everything and then finish, before the server actually starts up.
provider_queries = [
asyncio.create_task(
db_get_all_resources(
database=provider,
endpoint="links",
response_model=LinksResponse,
)
)
for provider in valid_providers
]
for query in asyncio.as_completed(provider_queries):
provider_databases, provider = await query
LOGGER.info(
"- %s (id=%r) - Processing",
get_resource_attribute(provider, "attributes.name", "N/A"),
get_resource_attribute(provider, "id"),
)
if not provider_databases:
LOGGER.info(" - No OPTIMADE databases found.")
continue
provider_databases = [
db
for db in provider_databases
if await clean_python_types(
get_resource_attribute(db, "attributes.link_type", "")
)
== LinkType.CHILD.value
]
if not provider_databases:
LOGGER.info(" - No OPTIMADE databases found.")
continue
for database in provider_databases:
if not get_resource_attribute(database, "attributes.base_url"):
LOGGER.info(
" - %s (id=%r) - Skipping: No base URL information.",
get_resource_attribute(database, "attributes.name", "N/A"),
get_resource_attribute(database, "id"),
)
continue
LOGGER.info(
" - %s (id=%r) - Checking versioned base URL and /structures",
get_resource_attribute(database, "attributes.name", "N/A"),
get_resource_attribute(database, "id"),
)
async with httpx.AsyncClient(timeout=5.0) as client:
try:
db_response = await client.get(
f"{str(get_resource_attribute(database, 'attributes.base_url')).rstrip('/')}" # noqa: E501
f"{BASE_URL_PREFIXES['major']}/structures",
)
except httpx.ReadTimeout:
LOGGER.info(
" - %s (id=%r) - Skipping: Timeout while requesting "
"%s/structures.",
get_resource_attribute(database, "attributes.name", "N/A"),
get_resource_attribute(database, "id"),
BASE_URL_PREFIXES["major"],
)
continue
if db_response.status_code != 200:
LOGGER.info(
" - %s (id=%r) - Skipping: Response from %s/structures is not "
"200 OK.",
get_resource_attribute(database, "attributes.name", "N/A"),
get_resource_attribute(database, "id"),
BASE_URL_PREFIXES["major"],
)
continue
new_id = (
f"{get_resource_attribute(provider, 'id')}"
f"/{get_resource_attribute(database, 'id')}"
if len(provider_databases) > 1
else get_resource_attribute(database, "id")
)
registered_database, _ = await resource_factory(
DatabaseCreate(
id=new_id,
**await clean_python_types(
get_resource_attribute(database, "attributes", {})
),
)
)
LOGGER.info(
" - %s (id=%r) - Registered database with id=%r",
get_resource_attribute(database, "attributes.name", "N/A"),
get_resource_attribute(database, "id"),
registered_database.id,
)
|