From b8824588d9acf283834d08458d5ff041456569cc Mon Sep 17 00:00:00 2001 From: Jullian Pepito Date: Mon, 30 Oct 2023 12:58:29 -0700 Subject: [PATCH] Replaced urllib.parse.urljoin() with a private _urljoin(). Addresses changes made in Python 3.11 urllib.parse.urlsplit() which is implicitly called by urllib.parse.urljoin(): https://github.com/python/cpython/issues/103848 CDK guarantee's a static URL is returned from the default Api stage: https://github.com/aws/aws-cdk/blob/v2.103.1/packages/@aws-cdk/aws-apigatewayv2-alpha/lib/http/stage.ts#L188-L192 So, since we're only appending some desired path to the base of the HttpApi URL, we extrapolate and format the scheme + netloc, concatenate the path, then return the result. --- components/control_broker_api.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/components/control_broker_api.py b/components/control_broker_api.py index 40a846f8..47512eb9 100644 --- a/components/control_broker_api.py +++ b/components/control_broker_api.py @@ -1,6 +1,5 @@ import json from typing import Any -from urllib.parse import urljoin import aws_cdk from aws_cdk import ( @@ -18,6 +17,7 @@ class ControlBrokerApi(aws_apigatewayv2_alpha.HttpApi): + CONTROL_BROKER_EVAL_ENGINE_INVOCATION_PATH = "/EvalEngine" def __init__( @@ -68,8 +68,18 @@ def __init__( }, ) self.urls = [] + + self.parsed_url = self.url.split('/') + self.scheme = self.parsed_url[0].rstrip(':') + self.netloc = self.parsed_url[2].rstrip('/') + self._add_control_broker_eval_engine_invocation_route() + def _urljoin(self, path: str) -> str: + """Concatenate and return a URL using the base HttpApi's scheme + netloc with the desired path.""" + + return '{}://{}/{}'.format(self.scheme, self.netloc, path) + def _add_control_broker_eval_engine_invocation_route(self): """Adds a route, which only handlers can call, that directly invokes the Control Broker Eval Engine.""" @@ -84,10 +94,9 @@ def _add_control_broker_eval_engine_invocation_route(self): integration=self.handler_invocation_integration, authorizer=aws_apigatewayv2_authorizers_alpha.HttpIamAuthorizer(), )[0] - eval_engine_url = urljoin( - self.url.rstrip("/"), - self.CONTROL_BROKER_EVAL_ENGINE_INVOCATION_PATH.strip("/"), - ) + + eval_engine_url = self._urljoin(self.CONTROL_BROKER_EVAL_ENGINE_INVOCATION_PATH.strip('/')) + self.urls.append(eval_engine_url) self.handler_invocation_url_mapping = aws_apigatewayv2_alpha.ParameterMapping() self.handler_invocation_url_mapping.overwrite_header( @@ -131,7 +140,7 @@ def add_api_handler( integration=integration, **kwargs, )[0] - handler_url = urljoin(self.url.rstrip("/"), path.strip("/")) + handler_url = self._urljoin(path.strip("/")) self.urls.append(handler_url) CfnOutput(self, f"{name}HandlerUrl", value=handler_url) # return route