Compare commits

..

2 Commits

Author SHA1 Message Date
Miyamizu-MitsuhaSang e068850e4c 记录单词搜索次数 2025-09-15 17:40:13 +08:00
Miyamizu-MitsuhaSang 2faebef7f2 更新翻译接口传入参数方式:POST+JSON请求体 2025-09-15 17:39:45 +08:00
3 changed files with 25 additions and 20 deletions

View File

@ -35,6 +35,12 @@ async def search(request: Request, body: SearchRequest, user=Depends(get_current
)
if not word_contents:
raise HTTPException(status_code=404, detail="Word not found")
# 修改freq
first_word = word_contents[0].word
current_freq = first_word.freq
await first_word.update(freq=current_freq+1)
pos_seen = set()
pos_contents = []
contents: List[SearchItemFr] = []

View File

@ -10,7 +10,7 @@ import requests
from fastapi import APIRouter, Depends, HTTPException
from app.models import User
from app.schemas.trans_schemas import TransResponse
from app.schemas.trans_schemas import TransResponse, TransRequest
from app.utils.security import is_admin_user, get_current_user
from scripts.md5 import make_md5
from settings import settings
@ -60,18 +60,18 @@ async def baidu_translation(query: str, from_lang: str, to_lang: str):
"sign": sign,
}
print(payload)
request = httpx.Request(
"POST",
url,
data=payload,
headers={"Content-Type": "application/x-www-form-urlencoded"}
)
print("完整请求内容:")
print("URL:", request.url)
print("Headers:", request.headers)
print("Body:", request.content.decode("utf-8"))
# print(payload)
#
# request = httpx.Request(
# "POST",
# url,
# data=payload,
# headers={"Content-Type": "application/x-www-form-urlencoded"}
# )
# print("完整请求内容:")
# print("URL:", request.url)
# print("Headers:", request.headers)
# print("Body:", request.content.decode("utf-8"))
async with httpx.AsyncClient(timeout=10) as client:
response = await client.post(
@ -94,15 +94,13 @@ async def baidu_translation(query: str, from_lang: str, to_lang: str):
@translator_router.post('/translate', response_model=TransResponse)
async def translate(
query: str,
from_lang: str = 'auto',
to_lang: str = 'zh',
translate_request: TransRequest,
user=Depends(get_current_user)
):
text = await baidu_translation(
query=query,
from_lang=from_lang,
to_lang=to_lang
query=translate_request.query,
from_lang=translate_request.from_lang,
to_lang=translate_request.to_lang,
)
return TransResponse(translated_text=text)

View File

@ -4,7 +4,8 @@ from pydantic import BaseModel, field_validator, model_validator
class TransRequest(BaseModel):
from_lang: Literal['fr', 'jp', 'zh'] = 'fr'
query: str
from_lang: Literal['auto', 'fr', 'jp', 'zh'] = 'auto'
to_lang: Literal['fr', 'jp', 'zh'] = 'zh'
@field_validator('from_lang', 'to_lang')