refactoring (#26)

This commit is contained in:
Dmitry Afanasyev
2023-10-03 23:30:19 +03:00
committed by GitHub
parent 482e1fdda1
commit c401e1006c
22 changed files with 423 additions and 395 deletions

View File

@@ -8,7 +8,7 @@ router = APIRouter()
@router.post(
f"/{settings.TELEGRAM_API_TOKEN}",
f"/{settings.token_part}",
name="bot:process_bot_updates",
response_class=Response,
status_code=status.HTTP_202_ACCEPTED,

View File

@@ -0,0 +1,13 @@
from fastapi import Depends
from starlette.requests import Request
from core.bot.services import ChatGptService
from settings.config import AppSettings
def get_settings(request: Request) -> AppSettings:
return request.app.state.settings
def get_chat_gpt_service(settings: AppSettings = Depends(get_settings)) -> ChatGptService:
return ChatGptService(settings.GPT_MODEL)

View File

@@ -0,0 +1,2 @@
class BaseAPIException(Exception):
pass

View File

@@ -1,11 +1,12 @@
from fastapi import APIRouter
from fastapi import APIRouter, Depends
from fastapi.responses import ORJSONResponse
from starlette import status
from starlette.responses import Response
from api.bot.deps import get_chat_gpt_service
from api.exceptions import BaseAPIException
from constants import INVALID_GPT_REQUEST_MESSAGES
from core.utils import ChatGptService
from settings.config import settings
from core.bot.services import ChatGptService
router = APIRouter()
@@ -30,8 +31,10 @@ async def healthcheck() -> ORJSONResponse:
status.HTTP_200_OK: {"description": "Successful Response"},
},
)
async def gpt_healthcheck(response: Response) -> Response:
chatgpt_service = ChatGptService(chat_gpt_model=settings.GPT_MODEL)
async def gpt_healthcheck(
response: Response,
chatgpt_service: ChatGptService = Depends(get_chat_gpt_service),
) -> Response:
data = chatgpt_service.build_request_data("Привет!")
response.status_code = status.HTTP_200_OK
try:
@@ -41,7 +44,7 @@ async def gpt_healthcheck(response: Response) -> Response:
for message in INVALID_GPT_REQUEST_MESSAGES:
if message in chatgpt_response.text:
response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
except Exception:
except BaseAPIException:
response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
return Response(status_code=response.status_code, content=None)