Commit 5f31645d authored by Robin's avatar Robin

forwarding now correct

parent 53defcd2
......@@ -22,6 +22,10 @@ validation_ld_entities = Blueprint(CURRENT_LD_ROUTER, __name__, url_prefix='/val
historic_validation_ld_entities = Blueprint(HISTORIC_LD_ROUTER, __name__,
url_prefix='/historic/validation/ld/entities')
# necessary to determine which part of the uri will be send to the platform as URL
router_prefixes = [validation_v2_entities.url_prefix, historic_validation_v2_entities.url_prefix,
validation_ld_entities.url_prefix, historic_validation_ld_entities.url_prefix]
def validate_post_v2(model: dict, cache: flask_caching.Cache):
"""
......@@ -239,6 +243,46 @@ def detect_upsert_request(model: Union[dict, list], url: str) -> bool:
return upsert
def left_crop_uri(uri: str) -> str:
"""
Meant to crop the part of the uri that should not be send to the platform when passing the request.
Should be used to crop the `request.full_path`.
Args:
uri: The uri to be cropped. e.g. /validation/v2/entities/urn:ngsi-ld:FlowerBed:FlowerBed-3/attrs
Returns:
uri: The uri that is suitable for passing to the platform. e.g. /v2/entities/urn:ngsi-ld:FlowerBed:FlowerBed-3/attrs
"""
def get_search_terms() -> list[str]:
"""
Based on the `seperation_terms` splits the router prefixes and adds whats on the "left hand side" of the
split prefix.
Returns:
uri: list of unique uri parts
"""
separation_terms = ['/v2/', '/ld/']
to_be_cropped = []
for prefix in router_prefixes: # e.g. '/historic/validation/ld/entities'
for term in separation_terms: # e.g. 'ld'
if term in prefix:
crop_part, irrelevant = prefix.split(term)
if crop_part not in to_be_cropped:
to_be_cropped.append(crop_part) # e.g. add '/historic/validation/'
return to_be_cropped
search_terms = get_search_terms()
for search_term in search_terms:
if uri.startswith(search_term):
return uri[len(search_term):]
@historic_validation_ld_entities.route('/<path:url>', methods=["GET"])
@validation_ld_entities.route('/<path:url>', methods=["GET"])
@historic_validation_v2_entities.route('/<path:url>', methods=["GET"])
......@@ -247,9 +291,9 @@ def get_validation_all_entities(url):
if request.data:
raise ValidationErrorResponse(msg=f'GET-Request must not contain data', log=False)
if request.blueprint == CURRENT_V2_ROUTER or request.blueprint == CURRENT_LD_ROUTER:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT"]}{request.full_path}'
forward_url = f'{current_app.config["PLATFORM_ENDPOINT"]}{left_crop_uri(uri=request.full_path)}'
else:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT_HISTORIC"]}{request.full_path}'
forward_url = f'{current_app.config["PLATFORM_ENDPOINT_HISTORIC"]}{left_crop_uri(uri=request.full_path)}'
return send_to_platform(forward_url=forward_url)
......@@ -259,9 +303,9 @@ def get_validation_all_entities(url):
@validation_v2_entities.route('/<path:url>', methods=["DELETE"])
def delete_validation_all_entities(url):
if request.blueprint == CURRENT_V2_ROUTER or request.blueprint == CURRENT_LD_ROUTER:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT"]}{request.full_path}'
forward_url = f'{current_app.config["PLATFORM_ENDPOINT"]}{left_crop_uri(uri=request.full_path)}'
else:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT_HISTORIC"]}{request.full_path}'
forward_url = f'{current_app.config["PLATFORM_ENDPOINT_HISTORIC"]}{left_crop_uri(uri=request.full_path)}'
return send_to_platform(forward_url=forward_url)
......@@ -271,9 +315,9 @@ def delete_validation_all_entities(url):
@validation_v2_entities.route('/<path:url>', methods=["OPTIONS"])
def options_validation_all_entities(url):
if request.blueprint == CURRENT_V2_ROUTER or request.blueprint == CURRENT_LD_ROUTER:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT"]}{request.full_path}'
forward_url = f'{current_app.config["PLATFORM_ENDPOINT"]}{left_crop_uri(uri=request.full_path)}'
else:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT_HISTORIC"]}{request.full_path}'
forward_url = f'{current_app.config["PLATFORM_ENDPOINT_HISTORIC"]}{left_crop_uri(uri=request.full_path)}'
return send_to_platform(forward_url=forward_url)
......@@ -285,9 +329,9 @@ def put_validation_all_entities(url):
model = json.loads(request.data.decode('UTF-8')) # type = dict
if request.blueprint == (CURRENT_V2_ROUTER or CURRENT_LD_ROUTER):
forward_url = f'{current_app.config["PLATFORM_ENDPOINT"]}{request.full_path}'
forward_url = f'{current_app.config["PLATFORM_ENDPOINT"]}{left_crop_uri(uri=request.full_path)}'
else:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT_HISTORIC"]}{request.full_path}'
forward_url = f'{current_app.config["PLATFORM_ENDPOINT_HISTORIC"]}{left_crop_uri(uri=request.full_path)}'
# detect patch-request
if extract_modelname_from_url(url) and not model.get('id') and not model.get('type'):
......@@ -326,9 +370,9 @@ def patch_validation_all_entities(url):
model = ast.literal_eval(request.data.decode('UTF-8')) # dict or list
if request.blueprint == (CURRENT_V2_ROUTER or CURRENT_LD_ROUTER):
forward_url = f'{current_app.config["PLATFORM_ENDPOINT"]}{request.full_path}'
forward_url = f'{current_app.config["PLATFORM_ENDPOINT"]}{left_crop_uri(uri=request.full_path)}'
else:
forward_url = f'{current_app.config["PLATFORM_ENDPOINT_HISTORIC"]}{request.full_path}'
forward_url = f'{current_app.config["PLATFORM_ENDPOINT_HISTORIC"]}{left_crop_uri(uri=request.full_path)}'
if not extract_modelname_from_url(url) or model.get('id') or model.get('type'):
raise ValidationErrorResponse(msg='Malformed PATCH request', module=__name__)
......@@ -354,7 +398,7 @@ def post_validation_current_entities(url):
if isinstance(model, list):
raise ValidationErrorResponse(msg=f'None-historic api does not support json-array.', module=__name__)
forward_url = f'{current_app.config["PLATFORM_ENDPOINT"]}{request.full_path}'
forward_url = f'{current_app.config["PLATFORM_ENDPOINT"]}{left_crop_uri(uri=request.full_path)}'
# upsert request: id and type is missing
if not model.get('id') and not model.get('type') and extract_modelname_from_url(url):
......@@ -390,7 +434,7 @@ def post_validation_current_entities(url):
@historic_validation_v2_entities.route('/<path:url>', methods=["POST"])
def post_validation_historic_entities(url):
model = ast.literal_eval(request.data.decode('UTF-8')) # dict or list
forward_url = f'{current_app.config["PLATFORM_ENDPOINT_HISTORIC"]}{request.full_path}'
forward_url = f'{current_app.config["PLATFORM_ENDPOINT_HISTORIC"]}{left_crop_uri(uri=request.full_path)}'
# figure type and upsert-status of the datamodel
try:
......
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