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 b5169a8 commit 8ca2fd0
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
6 changes: 6 additions & 0 deletions openapi/index_openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1710,6 +1710,12 @@
"type": "string",
"description": "response string from the server"
},
"request_delay": {
"title": "Request Delay",
"minimum": 0.0,
"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
6 changes: 6 additions & 0 deletions openapi/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -3280,6 +3280,12 @@
"type": "string",
"description": "response string from the server"
},
"request_delay": {
"title": "Request Delay",
"minimum": 0.0,
"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
14 changes: 13 additions & 1 deletion optimade/models/optimade_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
from enum import Enum
from typing import Any, Dict, List, Optional, Type, Union

from pydantic import AnyHttpUrl, AnyUrl, BaseModel, EmailStr, root_validator
from pydantic import (
AnyHttpUrl,
AnyUrl,
BaseModel,
EmailStr,
NonNegativeFloat,
root_validator,
)

from optimade.models import jsonapi
from optimade.models.utils import SemanticVersion, StrictField
Expand Down Expand Up @@ -313,6 +320,11 @@ class ResponseMeta(jsonapi.Meta):
None, description="response string from the server"
)

request_delay: Optional[NonNegativeFloat] = 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 8ca2fd0

Please sign in to comment.