127 lines
3.6 KiB
Python
127 lines
3.6 KiB
Python
import logging
|
|
|
|
from fastapi import (
|
|
APIRouter,
|
|
Depends,
|
|
HTTPException,
|
|
Request,
|
|
)
|
|
from pydantic import (
|
|
TypeAdapter,
|
|
ValidationError,
|
|
)
|
|
from starlette.responses import JSONResponse
|
|
|
|
from dump_things_service import (
|
|
HTTP_403_FORBIDDEN,
|
|
HTTP_422_UNPROCESSABLE_CONTENT,
|
|
Format,
|
|
)
|
|
from dump_things_service.abstract_config import (
|
|
check_collection,
|
|
get_config,
|
|
get_default_token_name,
|
|
)
|
|
from dump_things_service.api_key import api_key_header_scheme
|
|
from dump_things_service.converter import FormatConverter
|
|
from dump_things_service.instance_state import get_instance_state
|
|
from dump_things_service.model import get_model_for_schema
|
|
from dump_things_service.utils import (
|
|
check_content_type,
|
|
dict_to_base_model,
|
|
get_token_store,
|
|
join_default_token_permissions,
|
|
wrap_http_exception,
|
|
)
|
|
|
|
logger = logging.getLogger('dump_things_service')
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post(
|
|
'/{collection:path}/validate/record/{class_name}',
|
|
tags=['Validate records'],
|
|
name='Validate record of class {class_name} for collection {collection}',
|
|
openapi_extra={
|
|
'requestBody': {
|
|
'required': True,
|
|
'content': {
|
|
'application/json': {'schema': {'type': 'object'}},
|
|
'text/turtle': {'schema': {'type': 'string'}},
|
|
},
|
|
},
|
|
},
|
|
)
|
|
def validate_record(
|
|
body: dict | str,
|
|
collection: str,
|
|
class_name: str,
|
|
request: Request,
|
|
format: Format | None = None,
|
|
api_key: str | None = Depends(api_key_header_scheme),
|
|
) -> JSONResponse:
|
|
format_, body = check_content_type(request, format, body)
|
|
|
|
instance_state = get_instance_state()
|
|
abstract_config = get_config()
|
|
|
|
check_collection(abstract_config, collection)
|
|
|
|
token = (
|
|
get_default_token_name(abstract_config, collection)
|
|
if api_key is None
|
|
else api_key
|
|
)
|
|
|
|
_store, token_permissions, _user_id = get_token_store(
|
|
abstract_config,
|
|
instance_state,
|
|
collection,
|
|
token,
|
|
)
|
|
final_permissions = join_default_token_permissions(
|
|
abstract_config,
|
|
token_permissions,
|
|
collection,
|
|
)
|
|
if not final_permissions.incoming_write:
|
|
raise HTTPException(
|
|
status_code=HTTP_403_FORBIDDEN,
|
|
detail=f"Not authorized to validate records for collection '{collection}'.",
|
|
)
|
|
|
|
if format_ == Format.ttl:
|
|
with wrap_http_exception(
|
|
ValueError,
|
|
status_code=HTTP_422_UNPROCESSABLE_CONTENT,
|
|
header='Conversion error',
|
|
):
|
|
json_object = FormatConverter(
|
|
abstract_config.collections[collection].schema_location,
|
|
input_format=Format.ttl,
|
|
output_format=Format.json,
|
|
).convert(body, class_name)
|
|
|
|
model = get_model_for_schema(
|
|
abstract_config.collections[collection].schema_location,
|
|
)[0]
|
|
|
|
with wrap_http_exception(
|
|
ValidationError,
|
|
status_code=HTTP_422_UNPROCESSABLE_CONTENT,
|
|
header='Validation error',
|
|
):
|
|
TypeAdapter(getattr(model, class_name)).validate_python(json_object)
|
|
else:
|
|
# Try to convert it into TTL to detect potential errors before storing
|
|
# the record
|
|
with wrap_http_exception(
|
|
ValueError,
|
|
status_code=HTTP_422_UNPROCESSABLE_CONTENT,
|
|
header='Validation error',
|
|
):
|
|
pydantic_object = dict_to_base_model(body, class_name, collection)
|
|
instance_state.validators[collection].validate(pydantic_object)
|
|
|
|
return JSONResponse(True)
|