fastapi-basic 0.0.999992__tar.gz → 0.1.1__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.

Files changed (23) hide show
  1. {fastapi_basic-0.0.999992 → fastapi_basic-0.1.1}/PKG-INFO +10 -2
  2. fastapi_basic-0.1.1/fastapi_basic/base_config.py +17 -0
  3. fastapi_basic-0.1.1/fastapi_basic/base_factory.py +94 -0
  4. fastapi_basic-0.1.1/fastapi_basic/const.py +2 -0
  5. fastapi_basic-0.1.1/fastapi_basic/database/mongodb.py +29 -0
  6. fastapi_basic-0.1.1/fastapi_basic/exception/__init__.py +0 -0
  7. fastapi_basic-0.1.1/fastapi_basic/exception/base_exception.py +13 -0
  8. fastapi_basic-0.1.1/fastapi_basic/ext/__init__.py +0 -0
  9. fastapi_basic-0.1.1/fastapi_basic/ext/aws/__init__.py +24 -0
  10. fastapi_basic-0.1.1/fastapi_basic/ext/aws/const.py +1 -0
  11. {fastapi_basic-0.0.999992 → fastapi_basic-0.1.1}/fastapi_basic.egg-info/PKG-INFO +10 -2
  12. fastapi_basic-0.1.1/fastapi_basic.egg-info/SOURCES.txt +19 -0
  13. {fastapi_basic-0.0.999992 → fastapi_basic-0.1.1}/fastapi_basic.egg-info/requires.txt +8 -0
  14. {fastapi_basic-0.0.999992 → fastapi_basic-0.1.1}/setup.py +1 -4
  15. fastapi_basic-0.0.999992/fastapi_basic/base_factory.py +0 -29
  16. fastapi_basic-0.0.999992/fastapi_basic.egg-info/SOURCES.txt +0 -11
  17. {fastapi_basic-0.0.999992 → fastapi_basic-0.1.1}/README.md +0 -0
  18. {fastapi_basic-0.0.999992 → fastapi_basic-0.1.1}/fastapi_basic/__init__.py +0 -0
  19. /fastapi_basic-0.0.999992/fastapi_basic/base_config.py → /fastapi_basic-0.1.1/fastapi_basic/database/__init__.py +0 -0
  20. {fastapi_basic-0.0.999992 → fastapi_basic-0.1.1}/fastapi_basic/utils.py +0 -0
  21. {fastapi_basic-0.0.999992 → fastapi_basic-0.1.1}/fastapi_basic.egg-info/dependency_links.txt +0 -0
  22. {fastapi_basic-0.0.999992 → fastapi_basic-0.1.1}/fastapi_basic.egg-info/top_level.txt +0 -0
  23. {fastapi_basic-0.0.999992 → fastapi_basic-0.1.1}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: fastapi_basic
3
- Version: 0.0.999992
3
+ Version: 0.1.1
4
4
  Summary: A short description of your module
5
5
  Home-page: https://github.com/szx21023/fastapi-base
6
6
  Author: szx21023
@@ -12,6 +12,8 @@ Requires-Python: >=3.6
12
12
  Description-Content-Type: text/markdown
13
13
  Requires-Dist: annotated-types==0.7.0
14
14
  Requires-Dist: anyio==4.9.0
15
+ Requires-Dist: boto3==1.37.36
16
+ Requires-Dist: botocore==1.37.36
15
17
  Requires-Dist: certifi==2025.1.31
16
18
  Requires-Dist: charset-normalizer==3.4.1
17
19
  Requires-Dist: click==8.1.8
@@ -19,16 +21,22 @@ Requires-Dist: dotenv==0.9.9
19
21
  Requires-Dist: fastapi==0.115.12
20
22
  Requires-Dist: h11==0.14.0
21
23
  Requires-Dist: idna==3.10
24
+ Requires-Dist: jmespath==1.0.1
22
25
  Requires-Dist: pydantic==2.11.3
23
26
  Requires-Dist: pydantic_core==2.33.1
27
+ Requires-Dist: python-dateutil==2.9.0.post0
24
28
  Requires-Dist: python-dotenv==1.1.0
29
+ Requires-Dist: python-multipart==0.0.20
25
30
  Requires-Dist: requests==2.32.3
31
+ Requires-Dist: s3transfer==0.11.5
32
+ Requires-Dist: six==1.17.0
26
33
  Requires-Dist: sniffio==1.3.1
27
34
  Requires-Dist: starlette==0.46.1
28
35
  Requires-Dist: typing-inspection==0.4.0
29
36
  Requires-Dist: typing_extensions==4.13.1
30
37
  Requires-Dist: urllib3==2.3.0
31
38
  Requires-Dist: uvicorn==0.34.0
39
+ Requires-Dist: watchtower==3.4.0
32
40
  Dynamic: author
33
41
  Dynamic: author-email
34
42
  Dynamic: classifier
@@ -0,0 +1,17 @@
1
+ from pydantic_settings import BaseSettings
2
+
3
+ class BaseConfig(BaseSettings):
4
+ # Swagger docs config
5
+ DOCS_URL: str|None = '/docs'
6
+ REDOC_URL: str|None = '/redoc'
7
+ OPENAPI_URL: str|None = '/openapi.json'
8
+
9
+ # Logging config
10
+ LOGGER_NAME: str|None = 'uvicorn'
11
+
12
+ # AWS
13
+ AWS_ACCESS_KEY_ID: str = ""
14
+ AWS_SECRET_KEY: str = ""
15
+ AWS_REGION: str = ""
16
+ AWS_PARAMETER_PATH_PREFIX: str = ""
17
+ AWS_LOGGROUP_NAME: str = ""
@@ -0,0 +1,94 @@
1
+ from abc import ABCMeta, abstractmethod
2
+ from functools import lru_cache
3
+ import os, dotenv
4
+
5
+ import watchtower
6
+ from fastapi import FastAPI, HTTPException, Request
7
+ from fastapi.responses import JSONResponse
8
+ from fastapi.middleware.cors import CORSMiddleware
9
+ from starlette.concurrency import iterate_in_threadpool
10
+ import logging
11
+
12
+ from .const import LOG_DEFAULT_LOGGER_NAME, LOG_FMT
13
+ from .ext.aws import init_app as init_aws_app
14
+ from .exception.base_exception import InternalBaseException
15
+ from .utils import update_dict_with_cast
16
+
17
+ class BaseFactory(metaclass=ABCMeta):
18
+ @abstractmethod
19
+ @lru_cache()
20
+ def get_app_config(self):
21
+ """
22
+ Each factory should define what config it wants.
23
+ """
24
+
25
+ def __load_local_config(self):
26
+ dotenv.load_dotenv(dotenv_path='.env', override=True)
27
+ update_dict_with_cast(self.get_app_config(), os.environ)
28
+
29
+ def __setup_main_logger(self, app, logger_name=LOG_DEFAULT_LOGGER_NAME, level=logging.DEBUG):
30
+ logger = self.__setup_logger(app, logger_name, level)
31
+ app.logger = logger
32
+
33
+ def __setup_logger(self, app, logger_name, level=logging.INFO):
34
+ logger = logging.getLogger(logger_name)
35
+ logger.setLevel(level)
36
+
37
+ stream_handler = logging.StreamHandler()
38
+ stream_handler.setFormatter(logging.Formatter(LOG_FMT))
39
+ logger.addHandler(stream_handler)
40
+ return logger
41
+
42
+ def __setup_aws_cloud_log(self, app):
43
+ if app.state.aws_session and app.state.config.get("AWS_LOGGROUP_NAME"):
44
+ logs_client = app.state.aws_session.client("logs")
45
+ watchtower_handler = watchtower.CloudWatchLogHandler(
46
+ log_group_name=app.state.config.get("AWS_LOGGROUP_NAME"),
47
+ boto3_client=logs_client, create_log_group=True)
48
+ watchtower_handler.setFormatter(logging.Formatter(LOG_FMT))
49
+ app.logger.addHandler(watchtower_handler)
50
+
51
+ def create_app(self):
52
+ """
53
+ Create an application instance.
54
+ """
55
+ self.__load_local_config()
56
+ app_config = self.get_app_config()
57
+ app = FastAPI(docs_url=app_config.get('DOCS_URL'), redoc_url=app_config.get('REDOC_URL'), openapi_url=app_config.get('OPENAPI_URL'))
58
+ app.state.config = app_config
59
+
60
+ app.add_middleware(
61
+ CORSMiddleware,
62
+ allow_origins=['*'],
63
+ allow_credentials=True,
64
+ allow_methods=['*'],
65
+ allow_headers=['*'],
66
+ )
67
+ self.__setup_main_logger(app, logger_name=app.state.config.get('LOGGER_NAME', LOG_DEFAULT_LOGGER_NAME), level=logging.DEBUG)
68
+ app.state.aws_session = init_aws_app(app)
69
+ self.__setup_aws_cloud_log(app)
70
+
71
+ @app.exception_handler(InternalBaseException)
72
+ async def http_exception_handler(request: Request, exc: InternalBaseException):
73
+ return JSONResponse(
74
+ status_code=exc.status_code,
75
+ content={
76
+ "code": exc.detail["code"],
77
+ "message": exc.detail["message"],
78
+ "data": exc.detail["data"]
79
+ }
80
+ )
81
+
82
+ @app.middleware("http")
83
+ async def handle_request_headers(request: Request, call_next):
84
+ body = await request.body()
85
+ form = await request.form()
86
+ app.logger.info(f"request.url: {request.url}, method: {request.method}, headers: {request.headers}, body: {body}, form: {form}")
87
+ response = await call_next(request)
88
+ response_body = [section async for section in response.body_iterator]
89
+ response.body_iterator = iterate_in_threadpool(iter(response_body))
90
+ if response_body:
91
+ app.logger.info(f"response_body: {response_body[0].decode()}")
92
+ return response
93
+
94
+ return app
@@ -0,0 +1,2 @@
1
+ LOG_DEFAULT_LOGGER_NAME = "uvicorn"
2
+ LOG_FMT = '%(asctime)s %(filename)s %(levelname)s: %(message)s'
@@ -0,0 +1,29 @@
1
+ from beanie import init_beanie
2
+ from motor.motor_asyncio import AsyncIOMotorClient
3
+
4
+ class MongoDB:
5
+ def __init__(self, username: str, password: str, host: str, port: int, db_name: str, ssl: str=None, ssl_ca_certs: str=None):
6
+ self.username = username
7
+ self.password = password
8
+ self.host = host
9
+ self.port = port
10
+ self.db_name = db_name
11
+ self.ssl = ssl
12
+ self.ssl_ca_certs = ssl_ca_certs
13
+
14
+ async def connect(self, document_models: list):
15
+ db_settings = dict(
16
+ username=self.username,
17
+ password=self.password,
18
+ host=self.host,
19
+ port=self.port,
20
+ retryWrites=False
21
+ )
22
+ if self.ssl:
23
+ db_settings["ssl"] = self.ssl
24
+
25
+ if self.ssl_ca_certs and self.ssl_ca_certs != "":
26
+ db_settings["tlsCAFile"] = self.ssl_ca_certs
27
+
28
+ self.client = AsyncIOMotorClient(**db_settings)
29
+ await init_beanie(database=self.client[self.db_name], document_models=document_models)
@@ -0,0 +1,13 @@
1
+ from fastapi import HTTPException
2
+ from fastapi import status
3
+
4
+
5
+ class InternalBaseException(HTTPException):
6
+ def __init__(self, status_code: int = status.HTTP_500_INTERNAL_SERVER_ERROR, code: str = "internal_server_error",
7
+ message: str = "Internal server error", **kwargs):
8
+ detail = {
9
+ "code": code,
10
+ "message": message,
11
+ "data": kwargs,
12
+ }
13
+ super().__init__(status_code=status_code, detail=detail)
File without changes
@@ -0,0 +1,24 @@
1
+ # -*- coding: UTF-8 -*-
2
+ import boto3
3
+
4
+ from .const import AWS_CONF_KEY
5
+
6
+
7
+ def init_app(app):
8
+ if not all([app.state.config.get(key) for key in AWS_CONF_KEY]):
9
+ # pylint: disable=no-member
10
+ app.logger.info("Lack AWS credential keys, ignore connect to AWS")
11
+ return None
12
+
13
+ aws_session = boto3.session.Session(
14
+ aws_access_key_id=app.state.config.AWS_ACCESS_KEY_ID,
15
+ aws_secret_access_key=app.state.config.AWS_SECRET_KEY,
16
+ region_name=app.state.config.AWS_REGION
17
+ )
18
+
19
+ # This should be logging when create Logging handlers,
20
+ # But we have too many CloudWatchLogHandler, only print once here.
21
+ if not getattr(app.state.config, "AWS_LOGGROUP_NAME"):
22
+ app.logger.info("Lack AWS configuration keys, ignore AWS CloudWatch log handlers")
23
+
24
+ return aws_session
@@ -0,0 +1 @@
1
+ AWS_CONF_KEY = ["AWS_ACCESS_KEY_ID", "AWS_SECRET_KEY", "AWS_REGION"]
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: fastapi_basic
3
- Version: 0.0.999992
3
+ Version: 0.1.1
4
4
  Summary: A short description of your module
5
5
  Home-page: https://github.com/szx21023/fastapi-base
6
6
  Author: szx21023
@@ -12,6 +12,8 @@ Requires-Python: >=3.6
12
12
  Description-Content-Type: text/markdown
13
13
  Requires-Dist: annotated-types==0.7.0
14
14
  Requires-Dist: anyio==4.9.0
15
+ Requires-Dist: boto3==1.37.36
16
+ Requires-Dist: botocore==1.37.36
15
17
  Requires-Dist: certifi==2025.1.31
16
18
  Requires-Dist: charset-normalizer==3.4.1
17
19
  Requires-Dist: click==8.1.8
@@ -19,16 +21,22 @@ Requires-Dist: dotenv==0.9.9
19
21
  Requires-Dist: fastapi==0.115.12
20
22
  Requires-Dist: h11==0.14.0
21
23
  Requires-Dist: idna==3.10
24
+ Requires-Dist: jmespath==1.0.1
22
25
  Requires-Dist: pydantic==2.11.3
23
26
  Requires-Dist: pydantic_core==2.33.1
27
+ Requires-Dist: python-dateutil==2.9.0.post0
24
28
  Requires-Dist: python-dotenv==1.1.0
29
+ Requires-Dist: python-multipart==0.0.20
25
30
  Requires-Dist: requests==2.32.3
31
+ Requires-Dist: s3transfer==0.11.5
32
+ Requires-Dist: six==1.17.0
26
33
  Requires-Dist: sniffio==1.3.1
27
34
  Requires-Dist: starlette==0.46.1
28
35
  Requires-Dist: typing-inspection==0.4.0
29
36
  Requires-Dist: typing_extensions==4.13.1
30
37
  Requires-Dist: urllib3==2.3.0
31
38
  Requires-Dist: uvicorn==0.34.0
39
+ Requires-Dist: watchtower==3.4.0
32
40
  Dynamic: author
33
41
  Dynamic: author-email
34
42
  Dynamic: classifier
@@ -0,0 +1,19 @@
1
+ README.md
2
+ setup.py
3
+ fastapi_basic/__init__.py
4
+ fastapi_basic/base_config.py
5
+ fastapi_basic/base_factory.py
6
+ fastapi_basic/const.py
7
+ fastapi_basic/utils.py
8
+ fastapi_basic.egg-info/PKG-INFO
9
+ fastapi_basic.egg-info/SOURCES.txt
10
+ fastapi_basic.egg-info/dependency_links.txt
11
+ fastapi_basic.egg-info/requires.txt
12
+ fastapi_basic.egg-info/top_level.txt
13
+ fastapi_basic/database/__init__.py
14
+ fastapi_basic/database/mongodb.py
15
+ fastapi_basic/exception/__init__.py
16
+ fastapi_basic/exception/base_exception.py
17
+ fastapi_basic/ext/__init__.py
18
+ fastapi_basic/ext/aws/__init__.py
19
+ fastapi_basic/ext/aws/const.py
@@ -1,5 +1,7 @@
1
1
  annotated-types==0.7.0
2
2
  anyio==4.9.0
3
+ boto3==1.37.36
4
+ botocore==1.37.36
3
5
  certifi==2025.1.31
4
6
  charset-normalizer==3.4.1
5
7
  click==8.1.8
@@ -7,13 +9,19 @@ dotenv==0.9.9
7
9
  fastapi==0.115.12
8
10
  h11==0.14.0
9
11
  idna==3.10
12
+ jmespath==1.0.1
10
13
  pydantic==2.11.3
11
14
  pydantic_core==2.33.1
15
+ python-dateutil==2.9.0.post0
12
16
  python-dotenv==1.1.0
17
+ python-multipart==0.0.20
13
18
  requests==2.32.3
19
+ s3transfer==0.11.5
20
+ six==1.17.0
14
21
  sniffio==1.3.1
15
22
  starlette==0.46.1
16
23
  typing-inspection==0.4.0
17
24
  typing_extensions==4.13.1
18
25
  urllib3==2.3.0
19
26
  uvicorn==0.34.0
27
+ watchtower==3.4.0
@@ -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.999992', # 版號版號
8
+ version='0.1.1', # 版號版號
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 格式
@@ -18,9 +18,6 @@ setup(
18
18
  'License :: OSI Approved :: MIT License',
19
19
  'Operating System :: OS Independent',
20
20
  ],
21
- # install_requires=[ # 依賴庫
22
- # 'requests',
23
- # ],
24
21
  install_requires = [req.strip() for req in requirements],
25
22
  python_requires='>=3.6', # 支持的 Python 版本
26
23
  )
@@ -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(override=True)
29
- update_dict_with_cast(self.get_app_config(), os.environ)
@@ -1,11 +0,0 @@
1
- README.md
2
- setup.py
3
- fastapi_basic/__init__.py
4
- fastapi_basic/base_config.py
5
- fastapi_basic/base_factory.py
6
- fastapi_basic/utils.py
7
- fastapi_basic.egg-info/PKG-INFO
8
- fastapi_basic.egg-info/SOURCES.txt
9
- fastapi_basic.egg-info/dependency_links.txt
10
- fastapi_basic.egg-info/requires.txt
11
- fastapi_basic.egg-info/top_level.txt