Skip to content

Commit 8ca2fd0

Browse files
committed
Add configurable request_delay support to server and models
1 parent b5169a8 commit 8ca2fd0

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

openapi/index_openapi.json

+6
Original file line numberDiff line numberDiff line change
@@ -1710,6 +1710,12 @@
17101710
"type": "string",
17111711
"description": "response string from the server"
17121712
},
1713+
"request_delay": {
1714+
"title": "Request Delay",
1715+
"minimum": 0.0,
1716+
"type": "number",
1717+
"description": "A non-negative float giving time in seconds that the client is suggested to wait before issuing a subsequent request."
1718+
},
17131719
"implementation": {
17141720
"title": "Implementation",
17151721
"allOf": [

openapi/openapi.json

+6
Original file line numberDiff line numberDiff line change
@@ -3280,6 +3280,12 @@
32803280
"type": "string",
32813281
"description": "response string from the server"
32823282
},
3283+
"request_delay": {
3284+
"title": "Request Delay",
3285+
"minimum": 0.0,
3286+
"type": "number",
3287+
"description": "A non-negative float giving time in seconds that the client is suggested to wait before issuing a subsequent request."
3288+
},
32833289
"implementation": {
32843290
"title": "Implementation",
32853291
"allOf": [

optimade/models/optimade_json.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44
from enum import Enum
55
from typing import Any, Dict, List, Optional, Type, Union
66

7-
from pydantic import AnyHttpUrl, AnyUrl, BaseModel, EmailStr, root_validator
7+
from pydantic import (
8+
AnyHttpUrl,
9+
AnyUrl,
10+
BaseModel,
11+
EmailStr,
12+
NonNegativeFloat,
13+
root_validator,
14+
)
815

916
from optimade.models import jsonapi
1017
from optimade.models.utils import SemanticVersion, StrictField
@@ -313,6 +320,11 @@ class ResponseMeta(jsonapi.Meta):
313320
None, description="response string from the server"
314321
)
315322

323+
request_delay: Optional[NonNegativeFloat] = StrictField(
324+
None,
325+
description="A non-negative float giving time in seconds that the client is suggested to wait before issuing a subsequent request.",
326+
)
327+
316328
implementation: Optional[Implementation] = StrictField(
317329
None, description="a dictionary describing the server implementation"
318330
)

optimade/server/config.py

+14
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,27 @@ class ServerConfig(BaseSettings):
292292
description="If True, the server will check whether the query parameters given in the request are correct.",
293293
)
294294

295+
request_delay: Optional[float] = Field(
296+
None,
297+
description=(
298+
"The value to use for the `meta->request_delay` field, which indicates to clients how long they should leave between success queries."
299+
),
300+
)
301+
295302
@validator("implementation", pre=True)
296303
def set_implementation_version(cls, v):
297304
"""Set defaults and modify bypassed value(s)"""
298305
res = {"version": __version__}
299306
res.update(v)
300307
return res
301308

309+
@validator("request_delay", pre=True)
310+
def check_request_delay(cls, v):
311+
"""Check `request_delay` is non-negative."""
312+
if v is not None and v < 0:
313+
raise ValueError("`request_delay` must be non-negative")
314+
return v
315+
302316
@root_validator(pre=True)
303317
def use_real_mongo_override(cls, values):
304318
"""Overrides the `database_backend` setting with MongoDB and

optimade/server/routers/utils.py

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ def meta_values(
7676
if schema is None:
7777
schema = CONFIG.schema_url if not CONFIG.is_index else CONFIG.index_schema_url
7878

79+
if CONFIG.request_delay is not None:
80+
# Add request delay via **kwargs only so that it is not set to null by default
81+
kwargs["request_delay"] = CONFIG.request_delay
82+
7983
return ResponseMeta(
8084
query=ResponseMetaQuery(representation=f"{url_path}?{url.query}"),
8185
api_version=__api_version__,

0 commit comments

Comments
 (0)