1
1
from fastapi import APIRouter , Cookie , Depends , status , HTTPException , Query , Path
2
2
from fastapi .responses import JSONResponse
3
+ from auth .get_user_info import get_user_id
3
4
from petercat_utils import get_client
4
5
from bot .builder import bot_builder , bot_info_generator
5
6
from type_class .bot import BotUpdateRequest , BotCreateRequest
6
- from typing import Optional
7
+ from typing import Annotated , Optional
7
8
8
- def verify_user_id (user_id : Optional [str ] = Cookie (None )):
9
- if not user_id :
10
- raise HTTPException (
11
- status_code = status .HTTP_401_UNAUTHORIZED ,
12
- detail = "Auth failed"
13
- )
14
- return user_id
15
9
router = APIRouter (
16
10
prefix = "/api/bot" ,
17
11
tags = ["bot" ],
@@ -56,7 +50,7 @@ def get_bot_detail(id: Optional[str] = Query(None, description="Filter bots by p
56
50
return JSONResponse (content = {"success" : False , "errorMessage" : str (e )}, status_code = 500 )
57
51
58
52
@router .get ("/config" )
59
- def get_bot_config (id : Optional [str ] = Query (None , description = "Filter bots by personal category" ), user_id : str = Depends (verify_user_id ) ):
53
+ def get_bot_config (id : Optional [str ] = Query (None , description = "Filter bots by personal category" ), user_id : Annotated [ str | None , Depends (get_user_id )] = None ):
60
54
try :
61
55
supabase = get_client ()
62
56
data = supabase .table ("bots" ).select ('*' ).eq ('id' , id ).eq ('uid' , user_id ).execute ()
@@ -65,7 +59,7 @@ def get_bot_config(id: Optional[str] = Query(None, description="Filter bots by p
65
59
return JSONResponse (content = {"success" : False , "errorMessage" : e })
66
60
67
61
@router .post ("/create" , status_code = 200 )
68
- async def create_bot (bot_data : BotCreateRequest , user_id : str = Depends (verify_user_id ) ):
62
+ async def create_bot (bot_data : BotCreateRequest , user_id : Annotated [ str | None , Depends (get_user_id )] = None ):
69
63
try :
70
64
res = await bot_builder (user_id , ** bot_data .model_dump ())
71
65
if not res :
@@ -75,7 +69,7 @@ async def create_bot(bot_data: BotCreateRequest, user_id: str = Depends(verify_u
75
69
return JSONResponse (content = {"success" : False , "errorMessage" : str (e )}, status_code = 500 )
76
70
77
71
@router .post ("/config/generator" , status_code = 200 )
78
- async def bot_generator (bot_data : BotCreateRequest , user_id : str = Depends (verify_user_id ) ):
72
+ async def bot_generator (bot_data : BotCreateRequest , user_id : Annotated [ str | None , Depends (get_user_id )] = None ):
79
73
try :
80
74
res = await bot_info_generator (user_id , ** bot_data .model_dump ())
81
75
if not res :
@@ -85,7 +79,7 @@ async def bot_generator(bot_data: BotCreateRequest, user_id: str = Depends(verif
85
79
return JSONResponse (content = {"success" : False , "errorMessage" : str (e )}, status_code = 500 )
86
80
87
81
@router .put ("/update/{id}" , status_code = 200 )
88
- def update_bot (id : str , bot_data : BotUpdateRequest , user_id : str = Depends (verify_user_id ) ):
82
+ def update_bot (id : str , bot_data : BotUpdateRequest , user_id : Annotated [ str | None , Depends (get_user_id )] = None ):
89
83
if not id :
90
84
return {
91
85
"error" : "Incomplete parameters" ,
@@ -110,7 +104,7 @@ def update_bot(id: str, bot_data: BotUpdateRequest, user_id: str = Depends(veri
110
104
@router .delete ("/delete/{id}" , status_code = status .HTTP_200_OK )
111
105
async def delete_bot (
112
106
id : str = Path (..., description = "The ID of the bot to delete" ),
113
- user_id : str = Depends (verify_user_id )
107
+ user_id : Annotated [ str | None , Depends (get_user_id )] = None
114
108
):
115
109
try :
116
110
supabase = get_client ()
0 commit comments