Skip to content

Commit

Permalink
Replaced urllib.parse.urljoin() with a private _urljoin(). Addresses …
Browse files Browse the repository at this point in the history
…changes made in Python 3.11 urllib.parse.urlsplit() which is implicitly called by urllib.parse.urljoin(): python/cpython#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.
  • Loading branch information
j-pepito committed Oct 30, 2023
1 parent 59c59ee commit b882458
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions components/control_broker_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
from typing import Any
from urllib.parse import urljoin

import aws_cdk
from aws_cdk import (
Expand All @@ -18,6 +17,7 @@


class ControlBrokerApi(aws_apigatewayv2_alpha.HttpApi):

CONTROL_BROKER_EVAL_ENGINE_INVOCATION_PATH = "/EvalEngine"

def __init__(
Expand Down Expand Up @@ -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."""

Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b882458

Please sign in to comment.