utils¶
Utility functions for all routers.
COLLECTIONS: dict[str, AsyncMongoCollection] = {}
module-attribute
¶
A lazy-loaded dictionary of asynchronous MongoDB entry-endpoint collections.
aretrieve_queryable_properties(schema, queryable_properties, entry_type=None)
async
¶
Asynchronous implementation of retrieve_queryable_properties()
from optimade
Reference to the function in the optimade
API documentation:
retrieve_queryable_properties()
.
Recursively loops through the schema of a pydantic model and resolves all references, returning a dictionary of all the OPTIMADE-queryable properties of that model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema
|
type[EntryResource]
|
The schema of the pydantic model. |
required |
queryable_properties
|
Iterable[str]
|
The list of properties to find in the schema. |
required |
entry_type
|
str | None
|
The entry type of the model, if any. |
None
|
Returns:
Type | Description |
---|---|
QueryableProperties
|
A flat dictionary with properties as keys, containing the field description, |
QueryableProperties
|
unit, sortability, support level, queryability and type, where provided. |
Source code in optimade_gateway/routers/utils.py
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 |
|
collection_factory(name)
async
¶
Get or initiate an entry-endpoint resource collection.
This factory utilizes the global dictionary
COLLECTIONS
.
It lazily instantiates the collections and then caches them in the dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The configured name for the entry-endpoint resource collection. |
required |
Returns:
Type | Description |
---|---|
AsyncMongoCollection
|
The OPTIMADE Gateway asynchronous implementation of the |
AsyncMongoCollection
|
Raises:
Type | Description |
---|---|
ValueError
|
If the supplied |
Source code in optimade_gateway/routers/utils.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
|
get_entries(collection, response_cls, request, params)
async
¶
Generalized /{entries}
endpoint getter
Source code in optimade_gateway/routers/utils.py
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 |
|
get_valid_resource(collection, entry_id)
async
¶
Validate and retrieve a resource
Source code in optimade_gateway/routers/utils.py
129 130 131 132 133 134 |
|
resource_factory(create_resource)
async
¶
Get or create a resource
Currently supported resources:
"databases"
(DatabaseCreate
->LinksResource
)"gateways"
(GatewayCreate
->GatewayResource
)"queries"
(QueryCreate
->QueryResource
)
For each of the resources, "uniqueness" is determined in the following way:
The base_url
field is considered unique across all databases.
If a base_url
is provided via a
Link
model, the base_url.href
value is used to query the MongoDB.
The collected list of databases.attributes.base_url
values is considered
unique across all gateways.
In the database, the search is done as a combination of the length/size of the
databases
' Python list/MongoDB array and a match on all (using the MongoDB
$all
operator) of the
databases.attributes.base_url
element values, when compared with the create_resource
.
Important
The database_ids
attribute must not contain values that are not also
included in the databases
attribute, in the form of the IDs for the
individual databases. If this should be the case an
OptimadeGatewayError
will be thrown.
The gateway_id
, query_parameters
, and endpoint
fields are collectively
considered to define uniqueness for a
QueryResource
in the MongoDB
collection.
Attention
Only the /structures
entry endpoint can be queried with multiple expected
responses.
This means the endpoint
field defaults to "structures"
, i.e., the
StructureResource
resource model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
create_resource
|
DatabaseCreate | GatewayCreate | QueryCreate
|
The resource to be retrieved or created anew. |
required |
Returns:
Type | Description |
---|---|
tuple[LinksResource | GatewayResource | QueryResource, bool]
|
Two things in a tuple:
|
Source code in optimade_gateway/routers/utils.py
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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
|
validate_resource(collection, entry_id)
async
¶
Validate whether a resource exists in a collection
Source code in optimade_gateway/routers/utils.py
121 122 123 124 125 126 |
|