fastapi-basic 0.0.6__tar.gz → 0.0.8__tar.gz

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-basic might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: fastapi_basic
3
- Version: 0.0.6
3
+ Version: 0.0.8
4
4
  Summary: A short description of your module
5
5
  Home-page: https://github.com/szx21023/fastapi-base
6
6
  Author: szx21023
@@ -22,6 +22,7 @@ Requires-Dist: idna==3.10
22
22
  Requires-Dist: pydantic==2.11.3
23
23
  Requires-Dist: pydantic_core==2.33.1
24
24
  Requires-Dist: python-dotenv==1.1.0
25
+ Requires-Dist: python-multipart==0.0.20
25
26
  Requires-Dist: requests==2.32.3
26
27
  Requires-Dist: sniffio==1.3.1
27
28
  Requires-Dist: starlette==0.46.1
@@ -4,4 +4,7 @@ class BaseConfig(BaseSettings):
4
4
  # Swagger docs config
5
5
  DOCS_URL: str|None = '/docs'
6
6
  REDOC_URL: str|None = '/redoc'
7
- OPENAPI_URL: str|None = '/openapi.json'
7
+ OPENAPI_URL: str|None = '/openapi.json'
8
+
9
+ # Logging config
10
+ LOGGER_NAME: str|None = 'uvicorn'
@@ -0,0 +1,60 @@
1
+ from abc import ABCMeta, abstractmethod
2
+ from functools import lru_cache
3
+ import os, dotenv
4
+
5
+ from fastapi import FastAPI, Request
6
+ from starlette.concurrency import iterate_in_threadpool
7
+ import logging
8
+
9
+ from .const import LOG_DEFAULT_LOGGER_NAME, LOG_FMT
10
+ from .utils import update_dict_with_cast
11
+
12
+ class BaseFactory(metaclass=ABCMeta):
13
+ @abstractmethod
14
+ @lru_cache()
15
+ def get_app_config(self):
16
+ """
17
+ Each factory should define what config it wants.
18
+ """
19
+
20
+ def __load_local_config(self):
21
+ dotenv.load_dotenv(dotenv_path='.env', override=True)
22
+ update_dict_with_cast(self.get_app_config(), os.environ)
23
+
24
+ def __setup_main_logger(self, app, logger_name=LOG_DEFAULT_LOGGER_NAME, level=logging.DEBUG):
25
+ logger = self.__setup_logger(app, logger_name, level)
26
+ app.logger = logger
27
+
28
+ def __setup_logger(self, app, logger_name, level=logging.INFO):
29
+ logger = logging.getLogger(logger_name)
30
+ logger.setLevel(level)
31
+
32
+ stream_handler = logging.StreamHandler()
33
+ stream_handler.setFormatter(logging.Formatter(LOG_FMT))
34
+ logger.addHandler(stream_handler)
35
+ return logger
36
+
37
+ def create_app(self):
38
+ """
39
+ Create an application instance.
40
+ """
41
+ self.__load_local_config()
42
+ app_config = self.get_app_config()
43
+ app = FastAPI(docs_url=app_config.get('DOCS_URL'), redoc_url=app_config.get('REDOC_URL'), openapi_url=app_config.get('OPENAPI_URL'))
44
+ app.state.config = app_config
45
+
46
+ self.__setup_main_logger(app, logger_name=app.state.config.get('LOGGER_NAME', LOG_DEFAULT_LOGGER_NAME), level=logging.DEBUG)
47
+
48
+ @app.middleware("http")
49
+ async def handle_request_headers(request: Request, call_next):
50
+ body = await request.body()
51
+ form = await request.form()
52
+ app.logger.info(f"request.url: {request.url}, method: {request.method}, headers: {request.headers}, body: {body}, form: {form}")
53
+ response = await call_next(request)
54
+ response_body = [section async for section in response.body_iterator]
55
+ response.body_iterator = iterate_in_threadpool(iter(response_body))
56
+ if response_body:
57
+ app.logger.info(f"response_body: {response_body[0].decode()}")
58
+ return response
59
+
60
+ return app
@@ -0,0 +1,2 @@
1
+ LOG_DEFAULT_LOGGER_NAME = "uvicorn"
2
+ LOG_FMT = '%(asctime)s %(filename)s %(levelname)s: %(message)s'
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: fastapi_basic
3
- Version: 0.0.6
3
+ Version: 0.0.8
4
4
  Summary: A short description of your module
5
5
  Home-page: https://github.com/szx21023/fastapi-base
6
6
  Author: szx21023
@@ -22,6 +22,7 @@ Requires-Dist: idna==3.10
22
22
  Requires-Dist: pydantic==2.11.3
23
23
  Requires-Dist: pydantic_core==2.33.1
24
24
  Requires-Dist: python-dotenv==1.1.0
25
+ Requires-Dist: python-multipart==0.0.20
25
26
  Requires-Dist: requests==2.32.3
26
27
  Requires-Dist: sniffio==1.3.1
27
28
  Requires-Dist: starlette==0.46.1
@@ -3,6 +3,7 @@ setup.py
3
3
  fastapi_basic/__init__.py
4
4
  fastapi_basic/base_config.py
5
5
  fastapi_basic/base_factory.py
6
+ fastapi_basic/const.py
6
7
  fastapi_basic/utils.py
7
8
  fastapi_basic.egg-info/PKG-INFO
8
9
  fastapi_basic.egg-info/SOURCES.txt
@@ -10,6 +10,7 @@ idna==3.10
10
10
  pydantic==2.11.3
11
11
  pydantic_core==2.33.1
12
12
  python-dotenv==1.1.0
13
+ python-multipart==0.0.20
13
14
  requests==2.32.3
14
15
  sniffio==1.3.1
15
16
  starlette==0.46.1
@@ -5,7 +5,7 @@ with open('requirements.txt', 'r') as f:
5
5
 
6
6
  setup(
7
7
  name='fastapi_basic', # 模組名稱
8
- version='0.0.6', # 版號版號
8
+ version='0.0.8', # 版號版號
9
9
  description='A short description of your module', # 模塊描述
10
10
  long_description=open('README.md').read(), # 詳細描述,通常是 README 文件的内容
11
11
  long_description_content_type='text/markdown', # markdown 格式
@@ -1,29 +0,0 @@
1
- from abc import ABCMeta, abstractmethod
2
- from functools import lru_cache
3
- import os, dotenv
4
-
5
- from fastapi import FastAPI
6
-
7
- from .utils import update_dict_with_cast
8
-
9
- class BaseFactory(metaclass=ABCMeta):
10
- @abstractmethod
11
- @lru_cache()
12
- def get_app_config(self):
13
- """
14
- Each factory should define what config it wants.
15
- """
16
-
17
- def create_app(self):
18
- """
19
- Create an application instance.
20
- """
21
- self.__load_local_config()
22
- app_config = self.get_app_config()
23
- app = FastAPI(docs_url=app_config.get('DOCS_URL'), redoc_url=app_config.get('REDOC_URL'), openapi_url=app_config.get('OPENAPI_URL'))
24
- app.state.config = app_config
25
- return app
26
-
27
- def __load_local_config(self):
28
- dotenv.load_dotenv(dotenv_path='.env', override=True)
29
- update_dict_with_cast(self.get_app_config(), os.environ)
File without changes
File without changes