mani.py on_event->lifespan; error->return code -1
This commit is contained in:
@@ -31,8 +31,8 @@ async def test(
|
|||||||
|
|
||||||
|
|
||||||
@r.get(
|
@r.get(
|
||||||
"/apps",
|
"/apps",tags=["App"],
|
||||||
response_model=List[AppList],
|
response_model=ApiReturnPage[AppList],
|
||||||
response_model_exclude_none=True,
|
response_model_exclude_none=True,
|
||||||
)
|
)
|
||||||
async def apps_list(
|
async def apps_list(
|
||||||
@@ -44,7 +44,7 @@ async def apps_list(
|
|||||||
filtered_apps = []
|
filtered_apps = []
|
||||||
domain = dbdomain.get_default_domain(db,user.id) #get_activedomain(db, user.id)
|
domain = dbdomain.get_default_domain(db,user.id) #get_activedomain(db, user.id)
|
||||||
if not domain:
|
if not domain:
|
||||||
return filtered_apps
|
return ApiReturnPage(data = filtered_apps)
|
||||||
|
|
||||||
platformapps = get_apps(db,domain.url)
|
platformapps = get_apps(db,domain.url)
|
||||||
kintoneevn = config.KINTONE_ENV(domain)
|
kintoneevn = config.KINTONE_ENV(domain)
|
||||||
@@ -68,10 +68,11 @@ async def apps_list(
|
|||||||
if papp.appid in kintone_apps_dict:
|
if papp.appid in kintone_apps_dict:
|
||||||
papp.appname = kintone_apps_dict[papp.appid]["name"]
|
papp.appname = kintone_apps_dict[papp.appid]["name"]
|
||||||
filtered_apps.append(papp)
|
filtered_apps.append(papp)
|
||||||
return filtered_apps
|
return ApiReturnPage(data = filtered_apps)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise APIException('platform:apps',request.url._url,f"Error occurred while get apps:",e)
|
raise APIException('platform:apps',request.url._url,f"Error occurred while get apps:",e)
|
||||||
|
|
||||||
|
|
||||||
@r.post("/apps", response_model=AppList, response_model_exclude_none=True)
|
@r.post("/apps", response_model=AppList, response_model_exclude_none=True)
|
||||||
async def apps_update(
|
async def apps_update(
|
||||||
request: Request,
|
request: Request,
|
||||||
|
|||||||
@@ -13,6 +13,11 @@ class ApiReturnModel(BaseModel,Generic[T]):
|
|||||||
msg:str ="OK"
|
msg:str ="OK"
|
||||||
data:T
|
data:T
|
||||||
|
|
||||||
|
class ApiReturnError(BaseModel):
|
||||||
|
code:int = -1
|
||||||
|
msg:str =""
|
||||||
|
|
||||||
|
|
||||||
class Params(BaseModel, AbstractParams):
|
class Params(BaseModel, AbstractParams):
|
||||||
page:int = Query(1,get=1, description="Page number")
|
page:int = Query(1,get=1, description="Page number")
|
||||||
size:int = Query(20,get=0, le=100,description="Page size")
|
size:int = Query(20,get=0, le=100,description="Page size")
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ class dbdomain(crudbase):
|
|||||||
if db_domain.is_active == True and domain.is_active == False:
|
if db_domain.is_active == True and domain.is_active == False:
|
||||||
db_userdomains = dbuserdomain.get_default_domains(db,domain.id)
|
db_userdomains = dbuserdomain.get_default_domains(db,domain.id)
|
||||||
for userdomain in db_userdomains:
|
for userdomain in db_userdomains:
|
||||||
userdomain.active = False
|
userdomain.is_default = False
|
||||||
db.add(userdomain)
|
db.add(userdomain)
|
||||||
db_domain.is_active=domain.is_active
|
db_domain.is_active=domain.is_active
|
||||||
db_domain.kintoneuser=domain.kintoneuser
|
db_domain.kintoneuser=domain.kintoneuser
|
||||||
|
|||||||
@@ -15,15 +15,22 @@ from app import tasks
|
|||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
import logging
|
import logging
|
||||||
from app.core.apiexception import APIException, writedblog
|
from app.core.apiexception import APIException, writedblog
|
||||||
|
from app.core.common import ApiReturnError
|
||||||
from app.db.crud import create_log
|
from app.db.crud import create_log
|
||||||
from fastapi.responses import JSONResponse
|
from fastapi.responses import JSONResponse
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
|
|
||||||
Base.metadata.create_all(bind=engine)
|
Base.metadata.create_all(bind=engine)
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def lifespan(app: FastAPI):
|
||||||
|
startup_event()
|
||||||
|
yield
|
||||||
|
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
title=config.PROJECT_NAME, docs_url="/api/docs", openapi_url="/api"
|
title=config.PROJECT_NAME, docs_url="/api/docs", openapi_url="/api",lifespan=lifespan
|
||||||
)
|
)
|
||||||
|
|
||||||
origins = [
|
origins = [
|
||||||
@@ -47,8 +54,7 @@ add_pagination(app)
|
|||||||
# request.state.db.close()
|
# request.state.db.close()
|
||||||
# return response
|
# return response
|
||||||
|
|
||||||
@app.on_event("startup")
|
def startup_event():
|
||||||
async def startup_event():
|
|
||||||
log_dir="log"
|
log_dir="log"
|
||||||
if not os.path.exists(log_dir):
|
if not os.path.exists(log_dir):
|
||||||
os.makedirs(log_dir)
|
os.makedirs(log_dir)
|
||||||
@@ -64,7 +70,7 @@ async def api_exception_handler(request: Request, exc: APIException):
|
|||||||
loop.run_in_executor(None,writedblog,exc)
|
loop.run_in_executor(None,writedblog,exc)
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
status_code=exc.status_code,
|
status_code=exc.status_code,
|
||||||
content={"detail": f"{exc.detail}"},
|
content= ApiReturnError(msg = f"{exc.detail}").model_dump(),
|
||||||
)
|
)
|
||||||
|
|
||||||
@app.get("/api/v1")
|
@app.get("/api/v1")
|
||||||
|
|||||||
Reference in New Issue
Block a user