Skip to content

rs_dpr_service/openapi_validation.md

<< Back to index

Openapi_core methods for OGC validation of the endpoints

validate_request(request) async

Validate an endpoint request according to the ogc specifications

Parameters:

Name Type Description Default
request Request

endpoint request

required

Returns:

Type Description
dict[Any, Any]

(dict) dictionary corresponding to the valid body

Source code in docs/rs-dpr-service/rs_dpr_service/openapi_validation.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
async def validate_request(request: Request) -> dict[Any, Any]:
    """Validate an endpoint request according to the ogc specifications

    Args:
        request (Request): endpoint request

    Returns:
        (dict) dictionary corresponding to the valid body

    """
    if not os.path.isfile(PATH_TO_YAML_OPENAPI):
        raise FileNotFoundError(f"The following file path was not found: {PATH_TO_YAML_OPENAPI}")
    try:
        body = await request.body()
        openapi_request = StarletteOpenAPIRequest(request, body)
        OPENAPI.validate_request(openapi_request)
        return json.loads(body) if body else None  # type: ignore
    except Exception as e:
        # Handle exceptions and return an appropriate error message
        raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail=f"Request body validation failed: {e}") from e

validate_response(request, data, status_code=HTTP_200_OK)

Validate an endpoint response according to the ogc specifications (described as yaml schemas) - Raises an exception if the response has an unvalid format

Parameters:

Name Type Description Default
request Request

input request

required
data dict

data to send in the endpoint response

required
Source code in docs/rs-dpr-service/rs_dpr_service/openapi_validation.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def validate_response(request: Request, data: dict, status_code=HTTP_200_OK):
    """
    Validate an endpoint response according to the ogc specifications
    (described as yaml schemas) - Raises an exception if the response
    has an unvalid format

    Args:
        request (Request): input request
        data (dict): data to send in the endpoint response
    """
    json_response = JSONResponse(status_code=status_code, content=data)
    openapi_request = StarletteOpenAPIRequest(request)
    openapi_response = StarletteOpenAPIResponse(json_response)
    OPENAPI.validate_response(openapi_request, openapi_response)