fastapi-scaff 0.3.0__py3-none-any.whl → 0.3.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of fastapi-scaff might be problematic. Click here for more details.

fastapi_scaff/__init__.py CHANGED
@@ -7,4 +7,4 @@
7
7
  @history
8
8
  """
9
9
 
10
- __version__ = "0.3.0"
10
+ __version__ = "0.3.1"
@@ -9,7 +9,7 @@
9
9
  "app/main.py": "\"\"\"\n@author axiner\n@version v1.0.0\n@created 2024/07/29 22:22\n@abstract main\n@description\n@history\n\"\"\"\nfrom contextlib import asynccontextmanager\nfrom fastapi import FastAPI\nfrom fastapi.responses import ORJSONResponse\n\nfrom app import (\n api,\n middleware,\n)\nfrom app.initializer import g\n\ng.setup()\n# #\nopenapi_url = \"/openapi.json\"\ndocs_url = \"/docs\"\nredoc_url = \"/redoc\"\nif g.config.app_disable_docs is True:\n openapi_url, docs_url, redoc_url = None, None, None\n\n\n@asynccontextmanager\nasync def lifespan(app_: FastAPI):\n g.logger.info(f\"Application env '{g.config.app_env}'\")\n g.logger.info(f\"Application yaml '{g.config.app_yaml}'\")\n g.logger.info(f\"Application title '{g.config.app_title}'\")\n g.logger.info(f\"Application version '{g.config.app_version}'\")\n # #\n g.logger.info(\"Application server running\")\n yield\n g.logger.info(\"Application server shutdown\")\n\n\napp = FastAPI(\n title=g.config.app_title,\n summary=g.config.app_summary,\n description=g.config.app_description,\n version=g.config.app_version,\n debug=g.config.app_debug,\n openapi_url=openapi_url,\n docs_url=docs_url,\n redoc_url=redoc_url,\n lifespan=lifespan,\n default_response_class=ORJSONResponse,\n)\n# #\napi.register_routers(app)\nmiddleware.register_middlewares(app)\n",
10
10
  "app/__init__.py": "\"\"\"\n@author axiner\n@version v1.0.0\n@created 2024/07/29 22:22\n@abstract app\n@description\n@history\n\"\"\"\nfrom pathlib import Path\n\nAPP_DIR = Path(__file__).absolute().parent\n",
11
11
  "app/api/exceptions.py": "from typing import Any\n\nfrom app.api.status import Status\n\n\nclass CustomException(Exception):\n\n def __init__(\n self,\n msg: str = None,\n code: int = None,\n data: Any = None,\n status: Status = Status.FAILURE,\n ):\n self.msg = msg or status.msg\n self.code = code or status.code\n self.data = data\n self.status = status\n\n def __str__(self) -> str:\n return f\"{self.code} {self.msg}\"\n\n def __repr__(self) -> str:\n return f\"<{self.__class__.__name__}: ({self.code!r}, {self.msg!r})>\"\n",
12
- "app/api/responses.py": "import json\nfrom typing import Mapping, get_type_hints, Any\n\nfrom fastapi.encoders import jsonable_encoder\nfrom starlette.background import BackgroundTask\nfrom starlette.requests import Request\nfrom starlette.responses import JSONResponse, StreamingResponse, ContentStream\nfrom toollib.utils import map_jsontype\n\nfrom app.api.status import Status\n\n\nclass Responses:\n\n @staticmethod\n def success(\n data: dict | list | str | None = None,\n msg: str = None,\n code: int = None,\n status: Status = Status.SUCCESS,\n is_encode_data: bool = False,\n request: Request = None,\n status_code: int = 200,\n headers: Mapping[str, str] | None = None,\n media_type: str | None = None,\n background: BackgroundTask | None = None,\n ) -> JSONResponse:\n content = {\n \"msg\": msg or status.msg,\n \"code\": code or status.code,\n \"data\": Responses.encode_data(data) if is_encode_data else data,\n }\n if request:\n if request_id := getattr(request.state, 'request_id', None):\n content[\"request_id\"] = request_id\n return JSONResponse(\n content=content,\n status_code=status_code,\n headers=headers,\n media_type=media_type,\n background=background,\n )\n\n @staticmethod\n def failure(\n msg: str = None,\n code: int = None,\n error: str | Exception | None = None,\n data: dict | list | str | None = None,\n status: Status = Status.FAILURE,\n is_encode_data: bool = False,\n request: Request = None,\n status_code: int = 200,\n headers: Mapping[str, str] | None = None,\n media_type: str | None = None,\n background: BackgroundTask | None = None,\n ) -> JSONResponse:\n content = {\n \"msg\": msg or status.msg,\n \"code\": code or status.code,\n \"error\": str(error) if error else None,\n \"data\": Responses.encode_data(data) if is_encode_data else data,\n }\n if request:\n if request_id := getattr(request.state, 'request_id', None):\n content[\"request_id\"] = request_id\n return JSONResponse(\n content=content,\n status_code=status_code,\n headers=headers,\n media_type=media_type,\n background=background,\n )\n\n @staticmethod\n def encode_data(data: Any) -> Any:\n if data is None or isinstance(data, (str, int, float, bool)):\n return data\n if isinstance(data, (dict, list)):\n try:\n json.dumps(data)\n return data\n except (TypeError, OverflowError):\n pass\n return jsonable_encoder(data)\n\n @staticmethod\n def stream(\n content: ContentStream,\n status_code: int = 200,\n headers: Mapping[str, str] | None = None,\n media_type: str | None = None,\n background: BackgroundTask | None = None,\n ) -> StreamingResponse:\n return StreamingResponse(\n content=content,\n status_code=status_code,\n headers=headers,\n media_type=media_type,\n background=background,\n )\n\n\ndef response_docs(\n model=None, # \u6a21\u578b(BaseModel): \u81ea\u52a8\u4ece\u6a21\u578b\u4e2d\u89e3\u6790\u5b57\u6bb5\u4e0e\u7c7b\u578b\n data: dict | str = None, # \u6570\u636e(dict/str): \u76f4\u63a5\u7ed9\u5b9a\u5b57\u6bb5\u4e0e\u7c7b\u578b/\u7c7b\u578b\n is_listwrap: bool = False,\n listwrap_key: str = None,\n listwrap_key_extra: dict = None,\n docs_extra: dict = None,\n):\n \"\"\"\u54cd\u5e94\u6587\u6863\"\"\"\n\n def _data_from_model(model_, default: str = \"\u672a\u77e5\") -> dict:\n \"\"\"\u6570\u636e\u6a21\u677f\"\"\"\n data_ = {}\n if hasattr(model_, \"response_fields\"):\n all_fields = set(model_.response_fields())\n else:\n all_fields = set(model_.model_fields.keys())\n type_hints = get_type_hints(model_)\n for field_name in all_fields:\n try:\n t = type_hints.get(field_name)\n t = str(t).replace(\"<class '\", \"\").replace(\"'>\", \"\") if t else default\n except Exception:\n t = default\n data_[field_name] = t\n return data_\n\n final_data = {}\n if model:\n final_data = _data_from_model(model)\n if data:\n if isinstance(data, dict):\n final_data.update(data)\n else:\n final_data = data\n if is_listwrap:\n final_data = [final_data] if not isinstance(final_data, list) else final_data\n if listwrap_key:\n final_data = {listwrap_key: final_data}\n if listwrap_key_extra:\n final_data.update(listwrap_key_extra)\n\n def _format_value(value):\n if isinstance(value, str):\n _value = value.split(\"|\")\n if len(_value) > 1:\n return \" | \".join([map_jsontype(_v.strip(), is_keep_integer=True) for _v in _value])\n return map_jsontype(value, is_keep_integer=True)\n elif isinstance(value, dict):\n return {k: _format_value(v) for k, v in value.items()}\n elif isinstance(value, (list, tuple)):\n return [_format_value(item) for item in value]\n else:\n return str(value)\n\n format_data = _format_value(final_data)\n\n docs = {\n 200: {\n \"description\": \"\u64cd\u4f5c\u6210\u529f\u3010code\u4e3a0 & http\u72b6\u6001\u7801200\u3011\",\n \"content\": {\n \"application/json\": {\n \"example\": {\n \"msg\": \"string\",\n \"code\": \"integer\",\n \"data\": format_data,\n \"request_id\": \"string\",\n }\n }\n }\n },\n 422: {\n \"description\": \"\u64cd\u4f5c\u5931\u8d25\u3010code\u975e0 & http\u72b6\u6001\u7801200\u3011\",\n \"content\": {\n \"application/json\": {\n \"example\": {\n \"msg\": \"string\",\n \"code\": \"integer\",\n \"error\": \"string\",\n \"data\": \"object | array | ...\",\n \"request_id\": \"string\",\n }\n }\n }\n },\n }\n if docs_extra:\n docs.update(docs_extra)\n return docs\n",
12
+ "app/api/responses.py": "import json\nfrom typing import Mapping, get_type_hints, Any\n\nfrom fastapi.encoders import jsonable_encoder\nfrom starlette.background import BackgroundTask\nfrom starlette.requests import Request\nfrom starlette.responses import JSONResponse, StreamingResponse, ContentStream\nfrom toollib.utils import map_jsontype\n\nfrom app.api.status import Status\n\n\nclass Responses:\n\n @staticmethod\n def success(\n data: dict | list | str | None = None,\n msg: str = None,\n code: int = None,\n status: Status = Status.SUCCESS,\n is_encode_data: bool = False,\n request: Request = None,\n status_code: int = 200,\n headers: Mapping[str, str] | None = None,\n media_type: str | None = None,\n background: BackgroundTask | None = None,\n ) -> JSONResponse:\n content = {\n \"msg\": msg or status.msg,\n \"code\": code or status.code,\n \"data\": Responses.encode_data(data) if is_encode_data else data,\n }\n if request:\n if request_id := getattr(request.state, 'request_id', None):\n content[\"request_id\"] = request_id\n return JSONResponse(\n content=content,\n status_code=status_code,\n headers=headers,\n media_type=media_type,\n background=background,\n )\n\n @staticmethod\n def failure(\n msg: str = None,\n code: int = None,\n error: str | Exception | None = None,\n data: dict | list | str | None = None,\n status: Status = Status.FAILURE,\n is_encode_data: bool = False,\n request: Request = None,\n status_code: int = 200,\n headers: Mapping[str, str] | None = None,\n media_type: str | None = None,\n background: BackgroundTask | None = None,\n ) -> JSONResponse:\n content = {\n \"msg\": msg or status.msg,\n \"code\": code or status.code,\n \"error\": str(error) if error else None,\n \"data\": Responses.encode_data(data) if is_encode_data else data,\n }\n if request:\n if request_id := getattr(request.state, 'request_id', None):\n content[\"request_id\"] = request_id\n return JSONResponse(\n content=content,\n status_code=status_code,\n headers=headers,\n media_type=media_type,\n background=background,\n )\n\n @staticmethod\n def encode_data(data: Any) -> Any:\n if data is None or isinstance(data, (str, int, float, bool)):\n return data\n if isinstance(data, (dict, list)):\n try:\n json.dumps(data)\n return data\n except (TypeError, OverflowError):\n pass\n return jsonable_encoder(data)\n\n @staticmethod\n def stream(\n content: ContentStream,\n status_code: int = 200,\n headers: Mapping[str, str] | None = None,\n media_type: str | None = None,\n background: BackgroundTask | None = None,\n ) -> StreamingResponse:\n return StreamingResponse(\n content=content,\n status_code=status_code,\n headers=headers,\n media_type=media_type,\n background=background,\n )\n\n\ndef response_docs(\n model=None, # \u6a21\u578b(BaseModel): \u81ea\u52a8\u4ece\u6a21\u578b\u4e2d\u89e3\u6790\u5b57\u6bb5\u4e0e\u7c7b\u578b\n data: dict | str = None, # \u6570\u636e(dict/str): \u76f4\u63a5\u7ed9\u5b9a\u5b57\u6bb5\u4e0e\u7c7b\u578b/\u7c7b\u578b\n is_listwrap: bool = False,\n listwrap_key: str = None,\n listwrap_key_extra: dict = None,\n docs_extra: dict = None,\n):\n \"\"\"\u54cd\u5e94\u6587\u6863\"\"\"\n\n def _data_from_model(model_, default: str = \"\u672a\u77e5\") -> dict:\n \"\"\"\u6570\u636e\u6a21\u677f\"\"\"\n data_ = {}\n if hasattr(model_, \"response_fields\"):\n all_fields = set(model_.response_fields())\n else:\n all_fields = set(model_.model_fields.keys())\n type_hints = get_type_hints(model_)\n for field_name in all_fields:\n try:\n t = type_hints.get(field_name)\n t = str(t).replace(\"<class '\", \"\").replace(\"'>\", \"\") if t else default\n except Exception:\n t = default\n data_[field_name] = t\n return data_\n\n final_data = {}\n if model:\n final_data = _data_from_model(model)\n if data:\n if isinstance(data, dict):\n final_data.update(data)\n else:\n final_data = data\n if is_listwrap:\n final_data = [final_data] if not isinstance(final_data, list) else final_data\n if listwrap_key:\n final_data = {listwrap_key: final_data}\n if listwrap_key_extra:\n final_data.update(listwrap_key_extra)\n\n def _format_value(value):\n if isinstance(value, str):\n _value = value.split(\"|\")\n if len(_value) > 1:\n return \" | \".join([map_jsontype(_v.strip(), is_keep_integer=True) for _v in _value])\n return map_jsontype(value, is_keep_integer=True)\n elif isinstance(value, dict):\n return {k: _format_value(v) for k, v in value.items()}\n elif isinstance(value, (list, tuple)):\n return [_format_value(item) for item in value]\n else:\n return str(value)\n\n format_data = _format_value(final_data)\n\n docs = {\n 200: {\n \"description\": \"\u64cd\u4f5c\u6210\u529f\u3010code\u4e3a0 & http\u72b6\u6001\u7801200\u3011\",\n \"content\": {\n \"application/json\": {\n \"example\": {\n \"msg\": \"string\",\n \"code\": \"integer\",\n \"data\": format_data or \"object | array | ...\",\n \"request_id\": \"string\",\n }\n }\n }\n },\n 422: {\n \"description\": \"\u64cd\u4f5c\u5931\u8d25\u3010code\u975e0 & http\u72b6\u6001\u7801200\u3011\",\n \"content\": {\n \"application/json\": {\n \"example\": {\n \"msg\": \"string\",\n \"code\": \"integer\",\n \"error\": \"string\",\n \"data\": \"object | array | ...\",\n \"request_id\": \"string\",\n }\n }\n }\n },\n }\n if docs_extra:\n docs.update(docs_extra)\n return docs\n",
13
13
  "app/api/status.py": "from enum import Enum\n\n\nclass Status(Enum):\n SUCCESS = (0, '\u64cd\u4f5c\u6210\u529f')\n FAILURE = (1, '\u64cd\u4f5c\u5931\u8d25')\n\n PARAMS_ERROR = (400, '\u53c2\u6570\u9519\u8bef')\n UNAUTHORIZED_ERROR = (401, '\u8ba4\u8bc1\u5931\u8d25')\n FORBIDDEN_ERROR = (403, '\u65e0\u6743\u9650')\n # \u5efa\u8bae\uff1a\u4e1a\u52a1\u6a21\u5757\u9519\u8bef\u7801\u4ece10000\u5f00\u59cb\n RECORD_NOT_EXIST_ERROR = (10000, '\u8bb0\u5f55\u4e0d\u5b58\u5728')\n RECORD_EXISTS_ERROR = (10001, '\u8bb0\u5f55\u5df2\u5b58\u5728')\n USER_OR_PASSWORD_ERROR = (10002, '\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef')\n\n @property\n def code(self):\n return self.value[0]\n\n @property\n def msg(self):\n return self.value[1]\n\n @classmethod\n def collect_status(cls):\n text = \"\"\n for s in cls:\n text += f\"{s.code} {s.msg}\\n\"\n return text\n",
14
14
  "app/api/__init__.py": "\"\"\"\napi\n\"\"\"\nimport importlib\nimport sys\nfrom pathlib import Path\n\nfrom fastapi import FastAPI, APIRouter\nfrom loguru import logger\n\nfrom app import APP_DIR\n\n_API_MOD_DIR = APP_DIR.joinpath(\"api\")\n_API_MOD_BASE = \"app.api\"\n\n\ndef register_routers(\n app: FastAPI,\n mod_dir: Path = _API_MOD_DIR,\n mod_base: str = _API_MOD_BASE,\n name: str = \"router\",\n prefix: str = \"\",\n depth: int = 0,\n max_depth: int = 2\n):\n \"\"\"\n \u6ce8\u518c\u8def\u7531\n \u8981\u6c42\uff1a\n \u8def\u7531\u6a21\u5757\uff1a\u975e'__'\u5f00\u5934\u7684\u6a21\u5757\n \u8def\u7531\u540d\u79f0\uff1a{name}\n :param app: FastAPI\u5e94\u7528\n :param mod_dir: api\u6a21\u5757\u76ee\u5f55\n :param mod_base: api\u6a21\u5757\u57fa\u7840\n :param name: \u8def\u7531\u540d\u79f0\n :param prefix: url\u524d\u7f00\n :param depth: \u5f53\u524d\u9012\u5f52\u6df1\u5ea6\n :param max_depth: \u6700\u5927\u9012\u5f52\u6df1\u5ea6\n \"\"\"\n if depth > max_depth:\n return\n for item in mod_dir.iterdir():\n if item.name.startswith(\"__\"):\n continue\n if item.is_dir():\n new_mod_dir = item\n new_mod_base = f\"{mod_base}.{item.name}\"\n new_prefix = prefix\n try:\n mod = importlib.import_module(new_mod_base)\n _prefix = getattr(mod, \"_prefix\", None)\n if _prefix:\n new_prefix = f\"{new_prefix}/{_prefix}\"\n except ImportError:\n logger.error(f\"Register router failed to import module: {new_mod_base}\")\n continue\n register_routers(\n app=app,\n mod_dir=new_mod_dir,\n mod_base=new_mod_base,\n prefix=new_prefix,\n name=name,\n depth=depth + 1,\n max_depth=max_depth\n )\n elif item.is_file() and item.suffix == \".py\" and depth > 0:\n mod_name = item.stem\n final_mod = f\"{mod_base}.{mod_name}\"\n try:\n mod = importlib.import_module(final_mod)\n if not getattr(mod, \"_active\", True):\n logger.info(f\"Register router skipping inactive module: {final_mod}\")\n sys.modules.pop(final_mod)\n continue\n if router := getattr(mod, name, None):\n if isinstance(router, APIRouter):\n tag = getattr(mod, \"_tag\", None)\n if not tag:\n tag = item.parent.stem if depth > 1 else mod_name\n app.include_router(\n router=router,\n prefix=prefix.replace(\"//\", \"/\").rstrip(\"/\"),\n tags=[tag]\n )\n except ImportError:\n logger.error(f\"Register router failed to import module: {final_mod}\")\n continue\n",
15
15
  "app/api/default/aping.py": "from fastapi import APIRouter\n\nfrom app_celery.producer.publisher import publish\n\nrouter = APIRouter()\n\n\n@router.get(\n path=\"/aping\",\n summary=\"aping\",\n)\ndef ping():\n task_id = publish(\"ping\")\n return f\"pong > {task_id}\"\n",
@@ -24,7 +24,7 @@
24
24
  "app/initializer/_redis.py": "from toollib.rediser import RedisCli\n\n\ndef init_redis_cli(\n host: str,\n port: int,\n db: int,\n password: str = None,\n max_connections: int = None,\n **kwargs,\n) -> RedisCli:\n if not host:\n return RedisCli()\n return RedisCli(\n host=host,\n port=port,\n db=db,\n password=password,\n max_connections=max_connections,\n **kwargs,\n )\n",
25
25
  "app/initializer/_snow.py": "import os\n\nfrom loguru import logger\nfrom toollib.guid import SnowFlake\nfrom toollib.rediser import RedisCli\nfrom toollib.utils import localip\n\n_CACHE_KEY_SNOW_WORKER_ID_INCR = \"config:snow_worker_id_incr\"\n_CACHE_KEY_SNOW_DATACENTER_ID_INCR = \"config:snow_datacenter_id_incr\"\n_CACHE_EXPIRE_SNOW = 120\n\n\ndef init_snow_cli(\n redis_cli: RedisCli,\n datacenter_id: int = None,\n to_str: bool = True,\n) -> SnowFlake: # \u5efa\u8bae\uff1a\u91c7\u7528\u670d\u52a1\u7684\u65b9\u5f0f\u8c03\u7528api\u83b7\u53d6\n if datacenter_id is None:\n datacenter_id = _snow_incr(redis_cli, _CACHE_KEY_SNOW_DATACENTER_ID_INCR, _CACHE_EXPIRE_SNOW)\n if datacenter_id is None:\n local_ip = localip()\n if local_ip:\n ip_parts = list(map(int, local_ip.split('.')))\n ip_int = (ip_parts[0] << 24) + (ip_parts[1] << 16) + (ip_parts[2] << 8) + ip_parts[3]\n datacenter_id = ip_int % 32\n worker_id = _snow_incr(redis_cli, _CACHE_KEY_SNOW_WORKER_ID_INCR, _CACHE_EXPIRE_SNOW)\n if worker_id is None:\n worker_id = os.getpid() % 32\n return SnowFlake(worker_id=worker_id, datacenter_id=datacenter_id, to_str=to_str)\n\n\ndef _snow_incr(redis_cli, cache_key: str, cache_expire: int):\n incr = None\n try:\n with redis_cli.connection() as r:\n resp = r.ping()\n if resp:\n lua_script = \"\"\"\n if redis.call('exists', KEYS[1]) == 1 then\n redis.call('expire', KEYS[1], ARGV[1])\n return redis.call('incr', KEYS[1])\n else\n redis.call('set', KEYS[1], 0)\n redis.call('expire', KEYS[1], ARGV[1])\n return 0\n end\n \"\"\"\n incr = r.eval(lua_script, 1, cache_key, cache_expire)\n except Exception as e:\n logger.warning(f\"snow\u521d\u59cb\u5316id\u5c06\u91c7\u7528\u672c\u5730\u65b9\u5f0f\uff0c\u7531\u4e8e\uff08{e}\uff09\")\n return incr\n",
26
26
  "app/initializer/__init__.py": "\"\"\"\n\u521d\u59cb\u5316\n\"\"\"\nfrom loguru._logger import Logger # noqa\nfrom sqlalchemy.orm import sessionmaker, scoped_session\nfrom toollib.guid import SnowFlake\nfrom toollib.rediser import RedisCli\nfrom toollib.utils import Singleton\n\nfrom app.initializer._conf import init_config\nfrom app.initializer._db import init_db_session, init_db_async_session\nfrom app.initializer._log import init_logger\nfrom app.initializer._redis import init_redis_cli\nfrom app.initializer._snow import init_snow_cli\n\n\nclass G(metaclass=Singleton):\n \"\"\"\n \u5168\u5c40\u53d8\u91cf\n \"\"\"\n config = None\n logger: Logger = None\n redis_cli: RedisCli = None\n snow_cli: SnowFlake = None\n db_session: scoped_session = None\n db_async_session: sessionmaker = None\n\n def __getattribute__(self, name):\n try:\n value = super().__getattribute__(name)\n except AttributeError:\n value = None\n if value is None:\n getter_name = f\"_get_{name}\"\n getter_method = getattr(self.__class__, getter_name, None)\n if callable(getter_method):\n value = getter_method()\n setattr(self, name, value)\n return value\n\n @classmethod\n def _get_config(cls):\n if not cls.config:\n cls.config = init_config()\n return cls.config\n\n @classmethod\n def _get_logger(cls):\n if not cls.logger:\n cls.logger = init_logger(\n debug=cls.config.app_debug,\n log_dir=cls.config.app_log_dir,\n )\n return cls.logger\n\n @classmethod\n def _get_redis_cli(cls):\n if not cls.redis_cli:\n cls.redis_cli = init_redis_cli(\n host=cls.config.redis_host,\n port=cls.config.redis_port,\n db=cls.config.redis_db,\n password=cls.config.redis_password,\n max_connections=cls.config.redis_max_connections,\n )\n return cls.redis_cli\n\n @classmethod\n def _get_snow_cli(cls):\n if not cls.snow_cli:\n cls.snow_cli = init_snow_cli(\n redis_cli=cls.redis_cli,\n datacenter_id=cls.config.snow_datacenter_id,\n )\n return cls.snow_cli\n\n @classmethod\n def _get_db_session(cls):\n if not cls.db_session:\n cls.db_session = init_db_session(\n db_url=cls.config.db_url,\n db_echo=cls.config.app_debug,\n is_create_tables=True,\n )\n return cls.db_session\n\n @classmethod\n def _get_db_async_session(cls):\n if not cls.db_async_session:\n cls.db_async_session = init_db_async_session(\n db_url=cls.config.db_async_url,\n db_echo=cls.config.app_debug,\n is_create_tables=True,\n )\n return cls.db_async_session\n\n @classmethod\n def setup(cls):\n \"\"\"\n \u521d\u59cb\u5316\n \"\"\"\n cls._get_config()\n cls._get_logger()\n cls._get_redis_cli()\n cls._get_snow_cli()\n # cls._get_db_session()\n cls._get_db_async_session()\n\n\ng = G()\n",
27
- "app/middleware/auths.py": "from fastapi import Depends, Security\nfrom fastapi.security import HTTPBearer, HTTPAuthorizationCredentials, APIKeyHeader\nfrom typing import Optional\n\nfrom fastapi.security.utils import get_authorization_scheme_param\nfrom pydantic import BaseModel\nfrom starlette.requests import Request\n\nfrom app.api.exceptions import CustomException\nfrom app.api.status import Status\nfrom app.initializer import g\nfrom app.utils import db_async_util\nfrom app.utils.auths_util import verify_jwt\n\n\n# ======= jwt =======\n\nclass JWTUser(BaseModel):\n # \u4e0e\u5b9e\u9645`user`\u5bf9\u9f50\n id: str = None\n phone: str = None\n name: str = None\n age: int = None\n gender: int = None\n\n @staticmethod\n async def get_jwt_key(user_id: str):\n # \u5efa\u8bae\uff1ajwt_key\u8fdb\u884credis\u7f13\u5b58\n async with g.db_async_session() as session:\n data = await db_async_util.sqlfetch_one(\n session=session,\n sql='SELECT jwt_key FROM \"user\" WHERE id = :id', # noqa\n params={\"id\": user_id},\n )\n return data.get(\"jwt_key\")\n\n\nclass JWTAuthorizationCredentials(HTTPAuthorizationCredentials):\n jwt_user: JWTUser\n\n\nclass JWTBearer(HTTPBearer):\n\n async def __call__(\n self, request: Request\n ) -> Optional[JWTAuthorizationCredentials]:\n authorization = request.headers.get(\"Authorization\")\n scheme, credentials = get_authorization_scheme_param(authorization)\n if not (authorization and scheme and credentials):\n if self.auto_error:\n raise CustomException(\n msg=\"Not authenticated\",\n status=Status.UNAUTHORIZED_ERROR,\n )\n else:\n return None\n if scheme.lower() != \"bearer\":\n if self.auto_error:\n raise CustomException(\n msg=\"Invalid authentication credentials\",\n status=Status.UNAUTHORIZED_ERROR,\n )\n else:\n return None\n jwt_user = await self.verify_credentials(credentials)\n return JWTAuthorizationCredentials(scheme=scheme, credentials=credentials, jwt_user=jwt_user)\n\n async def verify_credentials(self, credentials: str) -> JWTUser:\n playload = await self._verify_jwt(credentials)\n if playload is None:\n raise CustomException(status=Status.UNAUTHORIZED_ERROR)\n jwt_key = await JWTUser.get_jwt_key(playload.get(\"id\"))\n if not jwt_key:\n raise CustomException(status=Status.UNAUTHORIZED_ERROR)\n await self._verify_jwt(credentials, jwt_key=jwt_key)\n return JWTUser(\n id=playload.get(\"id\"),\n phone=playload.get(\"phone\"),\n name=playload.get(\"name\"),\n age=playload.get(\"age\"),\n gender=playload.get(\"gender\"),\n )\n\n @staticmethod\n async def _verify_jwt(token: str, jwt_key: str = None) -> dict:\n try:\n return verify_jwt(token=token, jwt_key=jwt_key)\n except Exception as e:\n raise CustomException(status=Status.UNAUTHORIZED_ERROR, msg=str(e))\n\n\ndef get_current_user(\n credentials: Optional[JWTAuthorizationCredentials] = Depends(JWTBearer(auto_error=True))\n) -> JWTUser:\n if not credentials:\n return JWTUser()\n return credentials.jwt_user\n\n\n# ======= api key =======\n\n_API_KEY_HEADER = APIKeyHeader(name=\"X-API-Key\", auto_error=False)\n\n\nasync def get_api_key(api_key: str = Security(_API_KEY_HEADER)):\n if not api_key:\n raise CustomException(status=Status.FORBIDDEN_ERROR)\n if api_key not in g.config.api_keys:\n raise CustomException(status=Status.UNAUTHORIZED_ERROR)\n return api_key\n",
27
+ "app/middleware/auths.py": "from fastapi import Depends, Security\nfrom fastapi.security import HTTPBearer, HTTPAuthorizationCredentials, APIKeyHeader\nfrom typing import Optional\n\nfrom fastapi.security.utils import get_authorization_scheme_param\nfrom pydantic import BaseModel\nfrom starlette.requests import Request\n\nfrom app.api.exceptions import CustomException\nfrom app.api.status import Status\nfrom app.initializer import g\nfrom app.utils import db_async_util\nfrom app.utils.auths_util import verify_jwt\n\n\n# ======= jwt =======\n\nclass JWTUser(BaseModel):\n # \u4e0e\u5b9e\u9645`user`\u5bf9\u9f50\n id: str = None\n phone: str = None\n name: str = None\n age: int = None\n gender: int = None\n\n @staticmethod\n async def get_user_jwt_key(user_id: str) -> str:\n # \u5efa\u8bae\uff1ajwt_key\u8fdb\u884credis\u7f13\u5b58\n async with g.db_async_session() as session:\n data = await db_async_util.sqlfetch_one(\n session=session,\n sql='SELECT jwt_key FROM \"user\" WHERE id = :id', # noqa\n params={\"id\": user_id},\n )\n return data.get(\"jwt_key\")\n\n\nclass JWTAuthorizationCredentials(HTTPAuthorizationCredentials):\n jwt_user: JWTUser\n\n\nclass JWTBearer(HTTPBearer):\n\n async def __call__(\n self, request: Request\n ) -> Optional[JWTAuthorizationCredentials]:\n authorization = request.headers.get(\"Authorization\")\n scheme, credentials = get_authorization_scheme_param(authorization)\n if not (authorization and scheme and credentials):\n if self.auto_error:\n raise CustomException(\n msg=\"Not authenticated\",\n status=Status.UNAUTHORIZED_ERROR,\n )\n else:\n return None\n if scheme.lower() != \"bearer\":\n if self.auto_error:\n raise CustomException(\n msg=\"Invalid authentication credentials\",\n status=Status.UNAUTHORIZED_ERROR,\n )\n else:\n return None\n jwt_user = await self.verify_credentials(credentials)\n return JWTAuthorizationCredentials(scheme=scheme, credentials=credentials, jwt_user=jwt_user)\n\n async def verify_credentials(self, credentials: str) -> JWTUser:\n playload = await self._verify_jwt(credentials)\n if playload is None:\n raise CustomException(status=Status.UNAUTHORIZED_ERROR)\n user_jwt_key = await JWTUser.get_user_jwt_key(playload.get(\"id\"))\n if not user_jwt_key:\n raise CustomException(status=Status.UNAUTHORIZED_ERROR)\n await self._verify_jwt(credentials, jwt_key=user_jwt_key)\n return JWTUser(\n id=playload.get(\"id\"),\n phone=playload.get(\"phone\"),\n name=playload.get(\"name\"),\n age=playload.get(\"age\"),\n gender=playload.get(\"gender\"),\n )\n\n @staticmethod\n async def _verify_jwt(token: str, jwt_key: str = None) -> dict:\n try:\n return verify_jwt(token=token, jwt_key=jwt_key)\n except Exception as e:\n raise CustomException(status=Status.UNAUTHORIZED_ERROR, msg=str(e))\n\n\ndef get_current_user(\n credentials: Optional[JWTAuthorizationCredentials] = Depends(JWTBearer(auto_error=True))\n) -> JWTUser:\n if not credentials:\n return JWTUser()\n return credentials.jwt_user\n\n\n# ======= api key =======\n\n_API_KEY_HEADER = APIKeyHeader(name=\"X-API-Key\", auto_error=False)\n\n\nclass ApiKeyUser(BaseModel):\n\n @staticmethod\n def get_user_api_key(user_id: str = None) -> list:\n if user_id:\n return g.config.api_keys.get(user_id)\n return g.config.api_keys\n\n\nasync def get_current_api_key(api_key: str = Security(_API_KEY_HEADER)):\n if not api_key:\n raise CustomException(status=Status.FORBIDDEN_ERROR)\n user_api_key = ApiKeyUser.get_user_api_key()\n if not user_api_key:\n raise CustomException(status=Status.UNAUTHORIZED_ERROR)\n if api_key not in user_api_key:\n raise CustomException(status=Status.UNAUTHORIZED_ERROR)\n return api_key\n",
28
28
  "app/middleware/cors.py": "from fastapi.middleware.cors import CORSMiddleware\n\nfrom app.initializer import g\n\n\nclass Cors:\n middleware_class = CORSMiddleware\n allow_origins = g.config.app_allow_origins\n allow_credentials = True\n allow_methods = [\"*\"]\n allow_headers = [\"*\"]\n",
29
29
  "app/middleware/exceptions.py": "import traceback\n\nfrom fastapi.exceptions import RequestValidationError\nfrom starlette.exceptions import HTTPException\nfrom starlette.requests import Request\nfrom starlette.responses import JSONResponse\n\nfrom app.api.exceptions import CustomException\nfrom app.api.responses import Responses\nfrom app.api.status import Status\nfrom app.initializer import g\n\n\nclass ExceptionsHandler:\n\n @staticmethod\n async def custom_exception_handler(\n request: Request,\n exc: CustomException,\n is_traceback: bool = False,\n ) -> JSONResponse:\n lmsg = f'- \"{request.method} {request.url.path}\" {exc.code} {exc.msg}'\n if is_traceback:\n lmsg = traceback.format_exc()\n g.logger.error(lmsg)\n return Responses.failure(\n msg=exc.msg,\n code=exc.code,\n data=exc.data,\n request=request,\n )\n\n @staticmethod\n async def http_exception_handler(\n request: Request,\n exc: HTTPException,\n is_traceback: bool = False,\n ) -> JSONResponse:\n lmsg = f'- \"{request.method} {request.url.path}\" {exc.status_code} {exc.detail}'\n if is_traceback:\n lmsg = traceback.format_exc()\n g.logger.error(lmsg)\n return Responses.failure(\n msg=exc.detail,\n code=exc.status_code,\n request=request,\n )\n\n @staticmethod\n async def validation_exception_handler(\n request: Request,\n exc: RequestValidationError,\n is_display_all: bool = False,\n is_traceback: bool = False,\n ) -> JSONResponse:\n if is_display_all:\n msg = \", \".join([f\"'{item['loc'][1] if len(item['loc']) > 1 else item['loc'][0]}' {item['msg'].lower()}\" for item in exc.errors()]) # noqa: E501\n else:\n _first_error = exc.errors()[0]\n msg = f\"'{_first_error['loc'][1] if len(_first_error['loc']) > 1 else _first_error['loc'][0]}' {_first_error['msg'].lower()}\" # noqa: E501\n lmsg = f'- \"{request.method} {request.url.path}\" {Status.PARAMS_ERROR.code} {msg}'\n if is_traceback:\n lmsg = traceback.format_exc()\n g.logger.error(lmsg)\n return Responses.failure(\n msg=msg,\n status=Status.PARAMS_ERROR,\n request=request,\n )\n",
30
30
  "app/middleware/headers.py": "import uuid\nfrom starlette.middleware.base import BaseHTTPMiddleware\nfrom starlette.requests import Request\n\nfrom app.initializer.context import request_id_ctx_var\n\n\nclass HeadersMiddleware(BaseHTTPMiddleware):\n \"\"\"\u5934\u5904\u7406\u4e2d\u95f4\u4ef6\"\"\"\n _HEADERS = {\n # \u53ef\u6dfb\u52a0\u76f8\u5173\u5934\n }\n\n async def dispatch(self, request: Request, call_next):\n request_id = self._get_or_create_request_id(request)\n request.state.request_id = request_id\n ctx_token = request_id_ctx_var.set(request_id)\n try:\n response = await call_next(request)\n response.headers[\"X-Request-ID\"] = request_id\n for key, value in self._HEADERS.items():\n if key not in response.headers:\n response.headers[key] = value\n return response\n finally:\n request_id_ctx_var.reset(ctx_token)\n\n @staticmethod\n def _get_or_create_request_id(request: Request) -> str:\n request_id = request.headers.get(\"X-Request-ID\")\n if not request_id:\n request_id = f\"req-{uuid.uuid4()}\"\n return request_id\n",
@@ -45,7 +45,7 @@
45
45
  "app_celery/consumer/__init__.py": "\"\"\"\n\u6d88\u8d39\u8005\n\"\"\"\nimport re\nfrom pathlib import Path\n\nfrom app_celery import make_celery\n\n\ndef autodiscover_task_modules(\n task_name: str = \"tasks\",\n task_module: str = \"app_celery.consumer.tasks\",\n) -> list:\n \"\"\"\n \u81ea\u52a8\u53d1\u73b0\u4efb\u52a1\u6a21\u5757\n - \u53ef\u5728\u6a21\u5757\u4e2d\u52a0\u5165`_active = False`\u6765\u53d6\u6d88\u6fc0\u6d3b\n \"\"\"\n task_modules = []\n active_pat = re.compile(r\"^_active\\s*=\\s*False\\s*(?:#.*)?$\", re.MULTILINE)\n for p in Path(__file__).parent.joinpath(task_name).rglob(\"*.py\"):\n if p.stem == \"__init__\":\n continue\n if active_pat.search(p.read_text(encoding=\"utf-8\")):\n continue\n task_modules.append(f\"{task_module}.{p.stem}\")\n return task_modules\n\n\ncelery_app = make_celery(\n include=autodiscover_task_modules()\n)\n",
46
46
  "app_celery/consumer/tasks/beat_ping.py": "import logging\n\nfrom celery.schedules import crontab\n\nfrom app_celery.consumer import celery_app\n\nlogger = logging.getLogger(__name__)\n\ncelery_app.conf.beat_schedule.setdefault(\n 'beat_ping', {\n 'task': 'app_celery.consumer.tasks.beat_ping.ping',\n 'schedule': crontab(minute='*/2'), # \u6bcfx\u5206\u949f\u6267\u884c\u4e00\u6b21\n 'options': {'queue': 'beat_ping'}\n }\n)\n\n\n@celery_app.task(\n bind=True,\n autoretry_for=(Exception,),\n max_retries=3,\n retry_backoff=True,\n retry_backoff_max=300,\n retry_jitter=True,\n time_limit=360,\n soft_time_limit=300,\n acks_late=True,\n)\ndef ping(self, text: str = \"\u8fd9\u662f\u4e00\u4e2a\u5b9a\u65f6\u4efb\u52a1\u6d4b\u8bd5\"):\n logger.info(f\"pong: {text}\")\n",
47
47
  "app_celery/consumer/tasks/ping.py": "import logging\n\nfrom app_celery.consumer import celery_app\n\nlogger = logging.getLogger(__name__)\n\n\n@celery_app.task(\n bind=True,\n autoretry_for=(Exception,),\n max_retries=3,\n retry_backoff=True,\n retry_backoff_max=300,\n retry_jitter=True,\n time_limit=360,\n soft_time_limit=300,\n acks_late=True,\n)\ndef ping(self, text: str = \"\u8fd9\u662f\u4e00\u4e2a\u5f02\u6b65\u4efb\u52a1\u6d4b\u8bd5\"):\n logger.info(f\"pong: {text}\")\n",
48
- "app_celery/consumer/tasks/__init__.py": "\"\"\"\n\u5f02\u6b65\u4efb\u52a1\n\"\"\"\n",
48
+ "app_celery/consumer/tasks/__init__.py": "\"\"\"\n\u4efb\u52a1\uff08\u5b9a\u65f6&\u5f02\u6b65\uff09\n\"\"\"\n",
49
49
  "app_celery/consumer/workers/beat_ping.py": "from app_celery.consumer import celery_app\n\ncelery_app.conf.update(\n task_queues={\n \"beat_ping\": {\n \"exchange_type\": \"direct\",\n \"exchange\": \"beat_ping\",\n \"routing_key\": \"beat_ping\",\n },\n },\n task_routes={\n \"app_celery.consumer.tasks.beat_ping.ping\": {\"queue\": \"beat_ping\"},\n }\n)\n",
50
50
  "app_celery/consumer/workers/ping.py": "from app_celery.consumer import celery_app\n\ncelery_app.conf.update(\n task_queues={\n \"ping\": {\n \"exchange_type\": \"direct\",\n \"exchange\": \"ping\",\n \"routing_key\": \"ping\",\n },\n },\n task_routes={\n \"app_celery.consumer.tasks.ping.ping\": {\"queue\": \"ping\"},\n }\n)\n",
51
51
  "app_celery/consumer/workers/__init__.py": "\"\"\"\n\u5de5\u4f5c\u8005\n\"\"\"",
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fastapi-scaff
3
- Version: 0.3.0
3
+ Version: 0.3.1
4
4
  Summary: This is a fastapi scaff.
5
5
  Author-email: axiner <atpuxiner@163.com>
6
6
  Project-URL: Homepage, https://github.com/atpuxiner/fastapi-scaff
@@ -0,0 +1,10 @@
1
+ fastapi_scaff/__init__.py,sha256=ytCH_krcYT8riZWEw_zK8DvKL3Cbc2OPjA4sZ0tPtIw,120
2
+ fastapi_scaff/__main__.py,sha256=-aiQDTbnoFG2S_Z2zybI-yhSYhovOyeDAmFdE9f72UY,18337
3
+ fastapi_scaff/_api_tpl.json,sha256=dwFNehzYWjxBkRtXzeuyfEjllFDDJonpKpO2MH3WD4Y,7713
4
+ fastapi_scaff/_project_tpl.json,sha256=M6mXc0Z148Vrj2LmP-yJt5n-pS0wolyKii5Jd1IwAYw,92776
5
+ fastapi_scaff-0.3.1.dist-info/licenses/LICENSE,sha256=A5H6q7zd1QrL3iVs1KLsBOG0ImV-t9PpPspM4x-4Ea8,1069
6
+ fastapi_scaff-0.3.1.dist-info/METADATA,sha256=Jwo38u7402RkF_Gac7VOGrGt0iTvoU-vhtbRRn0u8Vw,3792
7
+ fastapi_scaff-0.3.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
8
+ fastapi_scaff-0.3.1.dist-info/entry_points.txt,sha256=kzs28nmpRWVCmWmZav3X7u7YOIOEir3sCkLnvQKTJbY,62
9
+ fastapi_scaff-0.3.1.dist-info/top_level.txt,sha256=LeyfUxMRhdbRHcYoH37ftfdspyZ8V3Uut2YBaTCzq2k,14
10
+ fastapi_scaff-0.3.1.dist-info/RECORD,,
@@ -1,10 +0,0 @@
1
- fastapi_scaff/__init__.py,sha256=6WzlB-Tb-THGDCAr7KSQjRu4HPM4PQ7_FREqNnWVMoY,120
2
- fastapi_scaff/__main__.py,sha256=-aiQDTbnoFG2S_Z2zybI-yhSYhovOyeDAmFdE9f72UY,18337
3
- fastapi_scaff/_api_tpl.json,sha256=dwFNehzYWjxBkRtXzeuyfEjllFDDJonpKpO2MH3WD4Y,7713
4
- fastapi_scaff/_project_tpl.json,sha256=wuStxBsogfTc4nNyqNZ-vfyTJnuUNUgDoR4X0X8oXuk,92330
5
- fastapi_scaff-0.3.0.dist-info/licenses/LICENSE,sha256=A5H6q7zd1QrL3iVs1KLsBOG0ImV-t9PpPspM4x-4Ea8,1069
6
- fastapi_scaff-0.3.0.dist-info/METADATA,sha256=uUDVmM4J-WFjfm6qmFP-2JvRhpk0_wtq51y2PxN_jSU,3792
7
- fastapi_scaff-0.3.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
8
- fastapi_scaff-0.3.0.dist-info/entry_points.txt,sha256=kzs28nmpRWVCmWmZav3X7u7YOIOEir3sCkLnvQKTJbY,62
9
- fastapi_scaff-0.3.0.dist-info/top_level.txt,sha256=LeyfUxMRhdbRHcYoH37ftfdspyZ8V3Uut2YBaTCzq2k,14
10
- fastapi_scaff-0.3.0.dist-info/RECORD,,