fix refactor management endpoint utils

This commit is contained in:
Ishaan Jaff
2024-06-11 16:16:10 -07:00
parent 13cb49688a
commit f2abac748e
4 changed files with 38 additions and 35 deletions
+3 -2
View File
@@ -103,7 +103,6 @@ from litellm.proxy.utils import (
hash_token,
html_form,
missing_keys_html_form,
_read_request_body,
_is_valid_team_configs,
_is_user_proxy_admin,
_get_user_role,
@@ -115,6 +114,8 @@ from litellm.proxy.utils import (
_to_ns,
get_error_message_str,
)
from litellm.proxy.utils.http_parsing_utils import _read_request_body
from litellm import (
CreateBatchRequest,
RetrieveBatchRequest,
@@ -163,7 +164,7 @@ from litellm.proxy.auth.auth_checks import (
get_actual_routes,
log_to_opentelemetry,
)
from litellm.proxy.management_endpoint_utils import management_endpoint_wrapper
from litellm.proxy.utils.management_endpoint_utils import management_endpoint_wrapper
from litellm.llms.custom_httpx.httpx_handler import HTTPHandler
from litellm.exceptions import RejectedRequestError
from litellm.integrations.slack_alerting import SlackAlertingArgs, SlackAlerting
-30
View File
@@ -2596,36 +2596,6 @@ async def update_spend(
raise e
async def _read_request_body(request: Optional[Request]) -> dict:
"""
Asynchronous function to read the request body and parse it as JSON or literal data.
Parameters:
- request: The request object to read the body from
Returns:
- dict: Parsed request data as a dictionary
"""
import ast, json
try:
request_data: dict = {}
if request is None:
return request_data
body = await request.body()
if body == b"" or body is None:
return request_data
body_str = body.decode()
try:
request_data = ast.literal_eval(body_str)
except:
request_data = json.loads(body_str)
return request_data
except:
return {}
def _is_projected_spend_over_limit(
current_spend: float, soft_budget_limit: Optional[float]
):
+31
View File
@@ -0,0 +1,31 @@
from typing import Optional
from fastapi import Request
import ast, json
async def _read_request_body(request: Optional[Request]) -> dict:
"""
Asynchronous function to read the request body and parse it as JSON or literal data.
Parameters:
- request: The request object to read the body from
Returns:
- dict: Parsed request data as a dictionary
"""
try:
request_data: dict = {}
if request is None:
return request_data
body = await request.body()
if body == b"" or body is None:
return request_data
body_str = body.decode()
try:
request_data = ast.literal_eval(body_str)
except:
request_data = json.loads(body_str)
return request_data
except:
return {}
@@ -1,7 +1,7 @@
from datetime import datetime
from functools import wraps
from litellm.proxy._types import UserAPIKeyAuth, ManagementEndpointLoggingPayload
from litellm.proxy.utils import _read_request_body
from http_parsing_utils import _read_request_body
from fastapi import Request
@@ -20,8 +20,9 @@ def management_endpoint_wrapper(func):
try:
result = await func(*args, **kwargs)
end_time = datetime.now()
user_api_key_dict: UserAPIKeyAuth = kwargs["user_api_key_dict"]
if kwargs is None:
kwargs = {}
user_api_key_dict: UserAPIKeyAuth = kwargs.get("user_api_key_dict")
parent_otel_span = user_api_key_dict.parent_otel_span
if parent_otel_span is not None:
from litellm.proxy.proxy_server import open_telemetry_logger