Commit 6cbcc7b4 authored by Robin Knapp's avatar Robin Knapp

Upsert post request now available

parent 524c951e
......@@ -8,8 +8,11 @@ from src.api.server_utils import ValidationErrorResponse, extract_modelname_from
send_to_platform
from src.logic import ValidationException, Creator, Updater
validation_ld_entities = Blueprint('val_ld_ent', __name__, url_prefix='/validation/ld/entities')
historic_validation_ld_entities = Blueprint('hist_ld_ent', __name__, url_prefix='/historic/validation/ld/entities')
VAL_CURRENT_LD_ROUTER = 'val_ld_ent'
VAL_HISTORIC_LD_ROUTER = 'hist_ld_ent'
validation_ld_entities = Blueprint(VAL_CURRENT_LD_ROUTER, __name__, url_prefix='/validation/ld/entities')
historic_validation_ld_entities = Blueprint(VAL_HISTORIC_LD_ROUTER, __name__,
url_prefix='/historic/validation/ld/entities')
def validate_post_ld(model: dict, cache: flask_caching.Cache):
......@@ -68,7 +71,7 @@ def validate_patch_ld(model: dict, model_type: str, cache: flask_caching.Cache):
try:
if cache.get(model_type.lower()):
specs = current_app.config.cache.get(model_type.lower())
specs = cache.get(model_type.lower())
else: # load specs from resource
conn = Connector(reads_from_disk=current_app.config['READ_SPECS_FROM_DISK'])
specs = conn.load_specs(model_type=model_type)
......@@ -99,7 +102,7 @@ def validate_patch_ld(model: dict, model_type: str, cache: flask_caching.Cache):
def get_validation_v2_entities(url):
if request.data:
raise ValidationErrorResponse(msg=f'GET-Request must not contain data', log=False)
if request.blueprint == 'val_ld_ent':
if request.blueprint == VAL_CURRENT_LD_ROUTER:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT"]}{request.full_path}'
else:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT_HISTORIC"]}{request.full_path}'
......@@ -109,7 +112,7 @@ def get_validation_v2_entities(url):
@historic_validation_ld_entities.route('/<path:url>', methods=["DELETE"])
@validation_ld_entities.route('/<path:url>', methods=["DELETE"])
def delete_validation_v2_entities(url):
if request.blueprint == 'val_ld_ent':
if request.blueprint == VAL_CURRENT_LD_ROUTER:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT"]}{request.full_path}'
else:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT_HISTORIC"]}{request.full_path}'
......@@ -119,7 +122,7 @@ def delete_validation_v2_entities(url):
@historic_validation_ld_entities.route('/<path:url>', methods=["OPTIONS"])
@validation_ld_entities.route('/<path:url>', methods=["OPTIONS"])
def options_validation_v2_entities(url):
if request.blueprint == 'val_v2_ent':
if request.blueprint == VAL_CURRENT_LD_ROUTER:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT"]}{request.full_path}'
else:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT_HISTORIC"]}{request.full_path}'
......@@ -131,7 +134,7 @@ def options_validation_v2_entities(url):
def put_validation_v2_entities(url):
model = json.loads(request.data.decode('UTF-8')) # type = dict
if request.blueprint == 'val_ld_ent':
if request.blueprint == VAL_CURRENT_LD_ROUTER:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT"]}{request.full_path}'
else:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT_HISTORIC"]}{request.full_path}'
......@@ -164,7 +167,7 @@ def put_validation_v2_entities(url):
def patch_validation_v2_entities(url):
model = ast.literal_eval(request.data.decode('UTF-8')) # dict or list
if request.blueprint == 'val_ld_ent':
if request.blueprint == VAL_CURRENT_LD_ROUTER:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT"]}{request.full_path}'
else:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT_HISTORIC"]}{request.full_path}'
......@@ -187,7 +190,17 @@ def patch_validation_v2_entities(url):
def post_validation_v2_entities(url):
model = ast.literal_eval(request.data.decode('UTF-8')) # dict or list
if request.blueprint == 'val_ld_ent': # `current` api
# upsert request, id and type is missing
if not model.get('id') and not model.get('type') and extract_modelname_from_url(url):
validate_patch_ld(model=model, model_type=extract_modelname_from_url(url),
cache=current_app.config.get('APP_CACHE'))
if request.blueprint == VAL_CURRENT_LD_ROUTER:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT"]}{request.full_path}'
else:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT_HISTORIC"]}{request.full_path}'
return send_to_platform(forward_url=forward_url)
if request.blueprint == VAL_CURRENT_LD_ROUTER: # `current` api
if isinstance(model, list):
raise ValidationErrorResponse(msg=f'None-historic api does not support json-array currently.',
module=__name__)
......@@ -213,4 +226,4 @@ def post_validation_v2_entities(url):
except ValidationException as vex:
raise ValidationErrorResponse(
msg=f'Json-model failed the evaluation. Error message: {vex.extern_logging}', log=False)
return send_to_platform(forward_url=forward_url)
\ No newline at end of file
return send_to_platform(forward_url=forward_url)
......@@ -8,8 +8,10 @@ from src.api.server_utils import ValidationErrorResponse, extract_modelname_from
send_to_platform
from src.logic import ValidationException, Creator, Updater
validation_v2_entities = Blueprint('val_v2_ent', __name__, url_prefix='/validation/v2/entities')
historic_validation_v2_entities = Blueprint('hist_v2_ent', __name__, url_prefix='/historic/validation/v2/entities')
VAL_CURRENT_V2_ROUTER = 'val_v2_ent'
VAL_HISTORIC_V2_ROUTER = 'hist_v2_ent'
validation_v2_entities = Blueprint(VAL_CURRENT_V2_ROUTER, __name__, url_prefix='/validation/v2/entities')
historic_validation_v2_entities = Blueprint(VAL_HISTORIC_V2_ROUTER, __name__, url_prefix='/historic/validation/v2/entities')
def validate_post_v2(model: dict, cache: flask_caching.Cache):
......@@ -26,7 +28,7 @@ def validate_post_v2(model: dict, cache: flask_caching.Cache):
try:
model_type = model.get('type')
if not model_type:
raise ValidationException(extern=f'Model is missing the "type" attribute')
raise ValidationException(extern=f'Model is missing the "type" attribute', log=False)
if cache.get(model_type.lower()):
specs = cache.get(model_type.lower())
......@@ -68,7 +70,7 @@ def validate_patch_v2(model: dict, model_type: str, cache: flask_caching.Cache):
try:
if cache.get(model_type.lower()):
specs = current_app.config.cache.get(model_type.lower())
specs = cache.get(model_type.lower())
else: # load specs from resource
conn = Connector(reads_from_disk=current_app.config['READ_SPECS_FROM_DISK'])
specs = conn.load_specs(model_type=model_type)
......@@ -99,7 +101,7 @@ def validate_patch_v2(model: dict, model_type: str, cache: flask_caching.Cache):
def get_validation_v2_entities(url):
if request.data:
raise ValidationErrorResponse(msg=f'GET-Request must not contain data', log=False)
if request.blueprint == 'val_v2_ent':
if request.blueprint == VAL_CURRENT_V2_ROUTER:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT"]}{request.full_path}'
else:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT_HISTORIC"]}{request.full_path}'
......@@ -109,7 +111,7 @@ def get_validation_v2_entities(url):
@historic_validation_v2_entities.route('/<path:url>', methods=["DELETE"])
@validation_v2_entities.route('/<path:url>', methods=["DELETE"])
def delete_validation_v2_entities(url):
if request.blueprint == 'val_v2_ent':
if request.blueprint == VAL_CURRENT_V2_ROUTER:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT"]}{request.full_path}'
else:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT_HISTORIC"]}{request.full_path}'
......@@ -119,7 +121,7 @@ def delete_validation_v2_entities(url):
@historic_validation_v2_entities.route('/<path:url>', methods=["OPTIONS"])
@validation_v2_entities.route('/<path:url>', methods=["OPTIONS"])
def options_validation_v2_entities(url):
if request.blueprint == 'val_v2_ent':
if request.blueprint == VAL_CURRENT_V2_ROUTER:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT"]}{request.full_path}'
else:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT_HISTORIC"]}{request.full_path}'
......@@ -131,7 +133,7 @@ def options_validation_v2_entities(url):
def put_validation_v2_entities(url):
model = json.loads(request.data.decode('UTF-8')) # type = dict
if request.blueprint == 'val_v2_ent':
if request.blueprint == VAL_CURRENT_V2_ROUTER:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT"]}{request.full_path}'
else:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT_HISTORIC"]}{request.full_path}'
......@@ -164,7 +166,7 @@ def put_validation_v2_entities(url):
def patch_validation_v2_entities(url):
model = ast.literal_eval(request.data.decode('UTF-8')) # dict or list
if request.blueprint == 'val_v2_ent':
if request.blueprint == VAL_CURRENT_V2_ROUTER:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT"]}{request.full_path}'
else:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT_HISTORIC"]}{request.full_path}'
......@@ -187,11 +189,22 @@ def patch_validation_v2_entities(url):
def post_validation_v2_entities(url):
model = ast.literal_eval(request.data.decode('UTF-8')) # dict or list
if request.blueprint == 'val_v2_ent':
# upsert request, id and type is missing
if not model.get('id') and not model.get('type') and extract_modelname_from_url(url):
validate_patch_v2(model=model, model_type=extract_modelname_from_url(url),
cache=current_app.config.get('APP_CACHE'))
if request.blueprint == VAL_CURRENT_V2_ROUTER:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT"]}{request.full_path}'
else:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT_HISTORIC"]}{request.full_path}'
return send_to_platform(forward_url=forward_url)
if request.blueprint == VAL_CURRENT_V2_ROUTER:
if isinstance(model, list):
raise ValidationErrorResponse(msg=f'None-historic api does not support json-array currently.',
module=__name__)
forward_url = f'{current_app.config["PLATFORM_ENDPOINT"]}{request.full_path}'
try:
validate_post_v2(model=model, cache=current_app.config.get('APP_CACHE'))
except ValidationException as vex:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment