Skip to content

Commit 6985132

Browse files
committed
Remove unused code
Signed-off-by: Jin Hai <haijin.chn@gmail.com>
1 parent 378bdfc commit 6985132

File tree

1 file changed

+1
-76
lines changed

1 file changed

+1
-76
lines changed

api/utils/api_utils.py

Lines changed: 1 addition & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@
1919
import logging
2020
import os
2121
import queue
22-
import random
2322
import threading
2423
import time
2524
from copy import deepcopy
2625
from functools import wraps
27-
from io import BytesIO
2826
from typing import Any, Callable, Coroutine, Optional, Type, Union
2927

3028
import requests
@@ -33,20 +31,17 @@
3331
Response,
3432
jsonify,
3533
make_response,
36-
send_file,
3734
)
3835
from flask_login import current_user
3936
from flask import (
4037
request as flask_request,
4138
)
4239
from peewee import OperationalError
43-
from werkzeug.http import HTTP_STATUS_CODES
4440

4541
from api import settings
46-
from api.constants import REQUEST_MAX_WAIT_SEC, REQUEST_WAIT_SEC
4742
from api.db import ActiveEnum
4843
from api.db.db_models import APIToken
49-
from api.utils.json_encode import CustomJSONEncoder, json_dumps
44+
from api.utils.json_encode import CustomJSONEncoder
5045
from rag.utils.mcp_tool_call_conn import MCPToolCallSession, close_multiple_mcp_toolcall_sessions
5146

5247
requests.models.complexjson.dumps = functools.partial(json.dumps, cls=CustomJSONEncoder)
@@ -77,19 +72,6 @@ def serialize_for_json(obj):
7772
# Fallback: convert to string representation
7873
return str(obj)
7974

80-
81-
def get_exponential_backoff_interval(retries, full_jitter=False):
82-
"""Calculate the exponential backoff wait time."""
83-
# Will be zero if factor equals 0
84-
countdown = min(REQUEST_MAX_WAIT_SEC, REQUEST_WAIT_SEC * (2 ** retries))
85-
# Full jitter according to
86-
# https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
87-
if full_jitter:
88-
countdown = random.randrange(countdown + 1)
89-
# Adjust according to maximum wait time and account for negative values.
90-
return max(0, countdown)
91-
92-
9375
def get_data_error_result(code=settings.RetCode.DATA_ERROR, message="Sorry! Data missing!"):
9476
logging.exception(Exception(message))
9577
result_dict = {"code": code, "message": message}
@@ -124,22 +106,6 @@ def server_error_response(e):
124106
return get_json_result(code=settings.RetCode.EXCEPTION_ERROR, message=repr(e))
125107

126108

127-
def error_response(response_code, message=None):
128-
if message is None:
129-
message = HTTP_STATUS_CODES.get(response_code, "Unknown Error")
130-
131-
return Response(
132-
json.dumps(
133-
{
134-
"message": message,
135-
"code": response_code,
136-
}
137-
),
138-
status=response_code,
139-
mimetype="application/json",
140-
)
141-
142-
143109
def validate_request(*args, **kwargs):
144110
def wrapper(func):
145111
@wraps(func)
@@ -203,23 +169,6 @@ def wrapper(*args, **kwargs):
203169
return wrapper
204170

205171

206-
def is_localhost(ip):
207-
return ip in {"127.0.0.1", "::1", "[::1]", "localhost"}
208-
209-
210-
def send_file_in_mem(data, filename):
211-
if not isinstance(data, (str, bytes)):
212-
data = json_dumps(data)
213-
if isinstance(data, str):
214-
data = data.encode("utf-8")
215-
216-
f = BytesIO()
217-
f.write(data)
218-
f.seek(0)
219-
220-
return send_file(f, as_attachment=True, attachment_filename=filename)
221-
222-
223172
def get_json_result(code: settings.RetCode = settings.RetCode.SUCCESS, message="success", data=None):
224173
response = {"code": code, "message": message, "data": data}
225174
return jsonify(response)
@@ -264,36 +213,12 @@ def construct_response(code=settings.RetCode.SUCCESS, message="success", data=No
264213
return response
265214

266215

267-
def construct_result(code=settings.RetCode.DATA_ERROR, message="data is missing"):
268-
result_dict = {"code": code, "message": message}
269-
response = {}
270-
for key, value in result_dict.items():
271-
if value is None and key != "code":
272-
continue
273-
else:
274-
response[key] = value
275-
return jsonify(response)
276-
277-
278216
def construct_json_result(code: settings.RetCode = settings.RetCode.SUCCESS, message="success", data=None):
279217
if data is None:
280218
return jsonify({"code": code, "message": message})
281219
else:
282220
return jsonify({"code": code, "message": message, "data": data})
283221

284-
285-
def construct_error_response(e):
286-
logging.exception(e)
287-
try:
288-
if e.code == 401:
289-
return construct_json_result(code=settings.RetCode.UNAUTHORIZED, message=repr(e))
290-
except BaseException:
291-
pass
292-
if len(e.args) > 1:
293-
return construct_json_result(code=settings.RetCode.EXCEPTION_ERROR, message=repr(e.args[0]), data=e.args[1])
294-
return construct_json_result(code=settings.RetCode.EXCEPTION_ERROR, message=repr(e))
295-
296-
297222
def token_required(func):
298223
@wraps(func)
299224
def decorated_function(*args, **kwargs):

0 commit comments

Comments
 (0)