More specifically, your use case would look like this:
@app.get('/my/path/prefix/<mac_address:ext>')
async def myhandler(request, mac_address, ext):
return text("hi there!")
Alternatively, if you want to have your regex match
@app.get(r"/<address:[a-f0-9]{12}\..*>")
async def mac_address_matcher(request: Request, address: str):
address, ext = address.split(".", 1)
return json({"address": address, "ext": ext})
Or, you could register your own param type:
import re
MAC_ADDRESS_PATTERN = re.compile(r"([a-f0-9]{12})(.*)")
def mac_address(param: str) -> tuple[str, str]:
if match := MAC_ADDRESS_PATTERN.match(param):
return match.groups()
raise ValueError(f"Value {param} is not a valid mac address")
app = Sanic("TestApp")
app.router.register_pattern("mac_address", mac_address, MAC_ADDRESS_PATTERN)
@app.get("/<address:mac_address>")
async def mac_address_handler(request: Request, address: tuple[str, str]):
mac, filename = address
return json(
{
"mac": mac,
"filename": filename,
}
)