Skip to content

Commit

Permalink
Add configurable request_delay support to server and models
Browse files Browse the repository at this point in the history
  • Loading branch information
ml-evs committed Dec 6, 2022
1 parent a8c84c5 commit dd70f2e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions openapi/index_openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1710,6 +1710,11 @@
"type": "string",
"description": "response string from the server"
},
"request_delay": {
"title": "Request Delay",
"type": "number",
"description": "A non-negative float giving time in seconds that the client is suggested to wait before issuing a subsequent request."
},
"implementation": {
"title": "Implementation",
"allOf": [
Expand Down
5 changes: 5 additions & 0 deletions openapi/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -3280,6 +3280,11 @@
"type": "string",
"description": "response string from the server"
},
"request_delay": {
"title": "Request Delay",
"type": "number",
"description": "A non-negative float giving time in seconds that the client is suggested to wait before issuing a subsequent request."
},
"implementation": {
"title": "Implementation",
"allOf": [
Expand Down
5 changes: 5 additions & 0 deletions optimade/models/optimade_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,11 @@ class ResponseMeta(jsonapi.Meta):
None, description="response string from the server"
)

request_delay: Optional[float] = StrictField(
None,
description="A non-negative float giving time in seconds that the client is suggested to wait before issuing a subsequent request.",
)

implementation: Optional[Implementation] = StrictField(
None, description="a dictionary describing the server implementation"
)
Expand Down
14 changes: 14 additions & 0 deletions optimade/server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,27 @@ class ServerConfig(BaseSettings):
description="If True, the server will check whether the query parameters given in the request are correct.",
)

request_delay: Optional[float] = Field(
None,
description=(
"The value to use for the `meta->request_delay` field, which indicates to clients how long they should leave between success queries."
),
)

@validator("implementation", pre=True)
def set_implementation_version(cls, v):
"""Set defaults and modify bypassed value(s)"""
res = {"version": __version__}
res.update(v)
return res

@validator("request_delay", pre=True)
def check_request_delay(cls, v):
"""Check `request_delay` is non-negative."""
if v is not None and v < 0:
raise ValueError("`request_delay` must be non-negative")
return v

@root_validator(pre=True)
def use_real_mongo_override(cls, values):
"""Overrides the `database_backend` setting with MongoDB and
Expand Down
4 changes: 4 additions & 0 deletions optimade/server/routers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ def meta_values(
if schema is None:
schema = CONFIG.schema_url if not CONFIG.is_index else CONFIG.index_schema_url

if CONFIG.request_delay is not None:
# Add request delay via **kwargs only so that it is not set to null by default
kwargs["request_delay"] = CONFIG.request_delay

return ResponseMeta(
query=ResponseMetaQuery(representation=f"{url_path}?{url.query}"),
api_version=__api_version__,
Expand Down

0 comments on commit dd70f2e

Please sign in to comment.