MCPcopy Create free account
hub / github.com/Materials-Consortia/optimade-python-tools / get_providers

Function get_providers

optimade/utils.py:139–210  ·  view source on GitHub ↗

Retrieve Materials-Consortia providers (from https://providers.optimade.org/v1/links). Fallback order if providers.optimade.org is not available: 1. Try Materials-Consortia/providers on GitHub. 2. Try submodule `providers`' list of providers. 3. Log warning that providers list from

(
    add_mongo_id: bool = False, session: requests.Session | None = None
)

Source from the content-addressed store, hash-verified

137
138
139def get_providers(
140 add_mongo_id: bool = False, session: requests.Session | None = None
141) -> list:
142 """Retrieve Materials-Consortia providers (from https://providers.optimade.org/v1/links).
143
144 Fallback order if providers.optimade.org is not available:
145
146 1. Try Materials-Consortia/providers on GitHub.
147 2. Try submodule `providers`' list of providers.
148 3. Log warning that providers list from Materials-Consortia is not included in the
149 `/links`-endpoint.
150
151 Arguments:
152 add_mongo_id: Whether to populate the `_id` field of the provider with MongoDB
153 ObjectID.
154 session: An optional `requests.Session` to use for the request, allowing custom
155 HTTP configuration (e.g. proxies). Defaults to the module-level `requests`.
156
157 Returns:
158 List of raw JSON-decoded providers including MongoDB object IDs.
159
160 """
161 import json
162
163 _get = session.get if session is not None else requests.get
164
165 for provider_list_url in PROVIDER_LIST_URLS:
166 try:
167 providers = _get(provider_list_url, timeout=10).json()
168 except (
169 requests.exceptions.ConnectionError,
170 requests.exceptions.ConnectTimeout,
171 json.JSONDecodeError,
172 requests.exceptions.SSLError,
173 ):
174 pass
175 else:
176 break
177 else:
178 try:
179 from optimade.server.data import providers # type: ignore
180 except ImportError:
181 from optimade.server.logger import LOGGER
182
183 LOGGER.warning(
184 """Could not retrieve a list of providers!
185
186 Tried the following resources:
187
188{}
189 The list of providers will not be included in the `/links`-endpoint.
190""".format("".join([f" * {_}\n" for _ in PROVIDER_LIST_URLS]))
191 )
192 return []
193
194 providers_list = []
195 for provider in providers.get("data", []):
196 # Remove/skip "exmpl"

Callers 6

load_entriesFunction · 0.90
insert_index_dataFunction · 0.90
test_get_providersFunction · 0.90
get_all_databasesFunction · 0.85

Calls 5

_getFunction · 0.85
mongo_id_for_databaseFunction · 0.85
updateMethod · 0.80
jsonMethod · 0.45
getMethod · 0.45

Tested by 3

test_get_providersFunction · 0.72