You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.2 KiB

5 years ago
  1. # -*- coding: utf-8 -*-
  2. import functools
  3. from fastapi import FastAPI
  4. from fastapi.openapi.utils import get_openapi
  5. def custom_openapi():
  6. if oapi.openapi_schema:
  7. return oapi.openapi_schema
  8. openapi_schema = get_openapi(
  9. title="Custom title",
  10. version="2.5.0",
  11. description="This is a very custom OpenAPI schema",
  12. routes=oapi.routes,
  13. )
  14. openapi_schema["info"]["x-logo"] = {
  15. "url": "https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png"
  16. }
  17. oapi.openapi_schema = openapi_schema
  18. return oapi.openapi_schema
  19. oapi = FastAPI()
  20. oapi.openapi = custom_openapi
  21. def apiroute(route=None, **kw):
  22. routing = kw.copy()
  23. def apidecorator(f):
  24. if route:
  25. if isinstance(route, list):
  26. routes = route
  27. else:
  28. routes = [route]
  29. routing['routes'] = routes
  30. @functools.wraps(f)
  31. def response_wrap(*args, **kw):
  32. response = f(*args, **kw)
  33. return response
  34. oapi.add_api_route(routes[0], f, include_in_schema=True)
  35. response_wrap.routing = routing
  36. response_wrap.original_func = f
  37. return response_wrap
  38. return apidecorator
  39. #