def create(payload: CreateRequestModel, current_user: User = Depends(get_current_active_user)): """ Create a new record. Age parameter can be from 18 to 150. Additional description shown in swagger docs. (Markdown format allowed.) - **name**: Limted to 100 characters - **age**: Age from 18 to 150 """ try: name = payload.name age = payload.age body_data = { 'record_id': 1003, 'message': f'Name: {name} and Age: {age}' } if age < 21: # raise HTTPException(status_code=status.HTTP_406_NOT_ACCEPTABLE, detail='Age less than 21 not allowed') return responses.JSONResponse( status_code=status.HTTP_406_NOT_ACCEPTABLE, content=error_response('VALUE_NOT_ALLOWED', 'Age less than 21 is not allowed')) return body_data except Exception: logger.exception('Exception: ') return error_response('SERVER_ERROR', traceback.format_exc())
def validation_exception(request: Request, exc: exceptions.RequestValidationError): return responses.JSONResponse( status_code=status.HTTP_400_BAD_REQUEST, content=encoders.jsonable_encoder({ "detail": exc.errors(), "body": exc.body }), )
def w_session(request: Request, *args): if request.cookies.get("session_token") != hashlib.sha256(f"4dm1nNotSoSecurePa$$secret".encode()).hexdigest(): raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED) else: if args == "json": return responses.JSONResponse({"message": "Welcome!"}) if args == "html": return HTMLResponse("Welcome!") # """ # <html> # <head> # <title></title> # </head> # <body> # <h1>Welcome!</h1> # </body> # </html> # """ else: return responses.PlainTextResponse("Welcome!")
async def unicorn_exception_handler(request: Request, exc: errors.ValidationError): return responses.JSONResponse( status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, content={"detail": str(exc)})
async def index(): return responses.JSONResponse(content={'message': 'THIS' }) # .RedirectResponse(url='/docs')
async def exception(request: Request, exc: Exception): log.exception(exc) return responses.JSONResponse( status_code=500, content={"message": "An error occurred. Please try again later."} )
async def wallet_storage_exception_handler( request: fastapi.Request, exc: crud.exceptions.BaseWalletError) -> responses.JSONResponse: del request return responses.JSONResponse(status_code=exc.code, content={"detail": str(exc)})
def story_exception_handler(request: Request, exc: StoryException): return responses.JSONResponse(status_code=status.HTTP_418_IM_A_TEAPOT, content={"detail": exc.name})
async def view(link: Link): try: links = loop(link.link) return responses.JSONResponse(links, status_code=status.HTTP_200_OK) except Exception: raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f'Invalid url {link}')