From cdb94ffe16fad234492c944bd204851dc8588fd1 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Fri, 25 Oct 2024 10:31:21 +0400 Subject: [PATCH] use helper for _route_matches_pattern --- litellm/proxy/auth/route_checks.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/litellm/proxy/auth/route_checks.py b/litellm/proxy/auth/route_checks.py index 6687cf05256..a237b0bdd60 100644 --- a/litellm/proxy/auth/route_checks.py +++ b/litellm/proxy/auth/route_checks.py @@ -174,10 +174,9 @@ class RouteChecks: # Replace placeholders with regex pattern # placeholders are written as "/threads/{thread_id}" if "{" in openai_route: - pattern = re.sub(r"\{[^}]+\}", r"[^/]+", openai_route) - # Anchor the pattern to match the entire string - pattern = f"^{pattern}$" - if re.match(pattern, route): + if RouteChecks._route_matches_pattern( + route=route, pattern=openai_route + ): return True # Pass through Bedrock, VertexAI, and Cohere Routes @@ -192,3 +191,25 @@ class RouteChecks: if "/langfuse/" in route: return True return False + + @staticmethod + def _route_matches_pattern(route: str, pattern: str) -> bool: + """ + Check if route matches the pattern placed in proxy/_types.py + + Example: + - pattern: "/threads/{thread_id}" + - route: "/threads/thread_49EIN5QF32s4mH20M7GFKdlZ" + - returns: True + + + - pattern: "/key/{token_id}/regenerate" + - route: "/key/regenerate/82akk800000000jjsk" + - returns: False, pattern is "/key/{token_id}/regenerate" + """ + pattern = re.sub(r"\{[^}]+\}", r"[^/]+", pattern) + # Anchor the pattern to match the entire string + pattern = f"^{pattern}$" + if re.match(pattern, route): + return True + return False