fastapi-basic 0.1.2__py3-none-any.whl → 0.1.11__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-basic might be problematic. Click here for more details.
- fastapi_basic/base_config.py +6 -1
- fastapi_basic/base_factory.py +16 -1
- fastapi_basic/const.py +3 -1
- fastapi_basic/exception/base_exception.py +0 -10
- fastapi_basic/ext/line/__init__.py +36 -9
- fastapi_basic/ext/line/exception.py +12 -0
- {fastapi_basic-0.1.2.dist-info → fastapi_basic-0.1.11.dist-info}/METADATA +34 -2
- fastapi_basic-0.1.11.dist-info/RECORD +18 -0
- {fastapi_basic-0.1.2.dist-info → fastapi_basic-0.1.11.dist-info}/WHEEL +1 -1
- fastapi_basic-0.1.2.dist-info/RECORD +0 -17
- {fastapi_basic-0.1.2.dist-info → fastapi_basic-0.1.11.dist-info}/top_level.txt +0 -0
fastapi_basic/base_config.py
CHANGED
|
@@ -14,4 +14,9 @@ class BaseConfig(BaseSettings):
|
|
|
14
14
|
AWS_SECRET_KEY: str = ""
|
|
15
15
|
AWS_REGION: str = ""
|
|
16
16
|
AWS_PARAMETER_PATH_PREFIX: str = ""
|
|
17
|
-
AWS_LOGGROUP_NAME: str = ""
|
|
17
|
+
AWS_LOGGROUP_NAME: str = ""
|
|
18
|
+
|
|
19
|
+
# Celery config
|
|
20
|
+
CELERY_APP: str|None = ""
|
|
21
|
+
CELERY_BROKER_URL: str|None = ""
|
|
22
|
+
CELERY_BACKEND_URL: str|None = ""
|
fastapi_basic/base_factory.py
CHANGED
|
@@ -3,13 +3,14 @@ from functools import lru_cache
|
|
|
3
3
|
import os, dotenv
|
|
4
4
|
|
|
5
5
|
import watchtower
|
|
6
|
+
from celery import Celery
|
|
6
7
|
from fastapi import FastAPI, HTTPException, Request
|
|
7
8
|
from fastapi.responses import JSONResponse
|
|
8
9
|
from fastapi.middleware.cors import CORSMiddleware
|
|
9
10
|
from starlette.concurrency import iterate_in_threadpool
|
|
10
11
|
import logging
|
|
11
12
|
|
|
12
|
-
from .const import LOG_DEFAULT_LOGGER_NAME, LOG_FMT
|
|
13
|
+
from .const import LOG_DEFAULT_LOGGER_NAME, LOG_FMT, CELERY_CONFIG
|
|
13
14
|
from .ext.aws import init_app as init_aws_app
|
|
14
15
|
from .exception.base_exception import InternalBaseException
|
|
15
16
|
from .utils import update_dict_with_cast
|
|
@@ -48,6 +49,17 @@ class BaseFactory(metaclass=ABCMeta):
|
|
|
48
49
|
watchtower_handler.setFormatter(logging.Formatter(LOG_FMT))
|
|
49
50
|
app.logger.addHandler(watchtower_handler)
|
|
50
51
|
|
|
52
|
+
def __setup_celery_app(self, app):
|
|
53
|
+
if not all([app.state.config.get(key) for key in CELERY_CONFIG]):
|
|
54
|
+
app.logger.info("Lack Celery config keys, ignore get Celery_app")
|
|
55
|
+
return None
|
|
56
|
+
|
|
57
|
+
app.state.celery_app = Celery(
|
|
58
|
+
app.state.config.get("CELERY_APP"),
|
|
59
|
+
broker=app.state.config.get("CELERY_BROKER_URL"),
|
|
60
|
+
backend=app.state.config.get("CELERY_BACKEND_URL")
|
|
61
|
+
)
|
|
62
|
+
|
|
51
63
|
def create_app(self):
|
|
52
64
|
"""
|
|
53
65
|
Create an application instance.
|
|
@@ -68,8 +80,11 @@ class BaseFactory(metaclass=ABCMeta):
|
|
|
68
80
|
app.state.aws_session = init_aws_app(app)
|
|
69
81
|
self.__setup_aws_cloud_log(app)
|
|
70
82
|
|
|
83
|
+
self.__setup_celery_app(app)
|
|
84
|
+
|
|
71
85
|
@app.exception_handler(InternalBaseException)
|
|
72
86
|
async def http_exception_handler(request: Request, exc: InternalBaseException):
|
|
87
|
+
app.logger.warning(f'message: {exc.detail["message"]}')
|
|
73
88
|
return JSONResponse(
|
|
74
89
|
status_code=exc.status_code,
|
|
75
90
|
content={
|
fastapi_basic/const.py
CHANGED
|
@@ -11,13 +11,3 @@ class InternalBaseException(HTTPException):
|
|
|
11
11
|
"data": kwargs,
|
|
12
12
|
}
|
|
13
13
|
super().__init__(status_code=status_code, detail=detail)
|
|
14
|
-
|
|
15
|
-
class LineBotException(HTTPException):
|
|
16
|
-
def __init__(self, status_code: int = status.HTTP_502_BAD_GATEWAY, code: str = "line_bot_error",
|
|
17
|
-
message: str = "Line bot error", **kwargs):
|
|
18
|
-
detail = {
|
|
19
|
-
"code": code,
|
|
20
|
-
"message": message,
|
|
21
|
-
"data": kwargs,
|
|
22
|
-
}
|
|
23
|
-
super().__init__(status_code=status_code, detail=detail)
|
|
@@ -1,29 +1,56 @@
|
|
|
1
|
+
from abc import ABCMeta, abstractmethod
|
|
2
|
+
import asyncio
|
|
3
|
+
|
|
1
4
|
from linebot import LineBotApi, WebhookHandler
|
|
2
5
|
from linebot.exceptions import LineBotApiError
|
|
3
|
-
from linebot.models import TextMessage, TextSendMessage
|
|
6
|
+
from linebot.models import TextMessage, TextSendMessage, MessageEvent
|
|
4
7
|
|
|
5
|
-
from
|
|
8
|
+
from .exception import LineBotException
|
|
6
9
|
|
|
7
|
-
class LineBot:
|
|
8
|
-
def __init__(self, channel_access_token: str, channel_secret: str):
|
|
10
|
+
class LineBot(metaclass=ABCMeta):
|
|
11
|
+
def __init__(self, channel_access_token: str, channel_secret: str, logger=None):
|
|
9
12
|
self.channel_access_token = channel_access_token
|
|
10
13
|
self.channel_secret = channel_secret
|
|
11
14
|
self.line_bot_api = LineBotApi(self.channel_access_token)
|
|
12
15
|
self.handler = WebhookHandler(self.channel_secret)
|
|
16
|
+
self.logger = logger
|
|
17
|
+
|
|
18
|
+
self.register_handlers()
|
|
13
19
|
|
|
14
|
-
async def reply_message(self, event,
|
|
20
|
+
async def reply_message(self, event, text: str):
|
|
15
21
|
try:
|
|
16
|
-
self.line_bot_api.reply_message(event.reply_token, TextMessage(text=
|
|
22
|
+
self.line_bot_api.reply_message(event.reply_token, TextMessage(text=text))
|
|
23
|
+
message = f"Send line message successful, event: {event}, text: {text}"
|
|
24
|
+
self.log_message(message)
|
|
17
25
|
|
|
18
26
|
except LineBotApiError as err:
|
|
19
|
-
exception = LineBotException(str(err))
|
|
27
|
+
exception = LineBotException(message=str(err))
|
|
20
28
|
raise exception
|
|
21
29
|
|
|
22
30
|
async def push_message(self, line_uid: str, text: str):
|
|
23
31
|
try:
|
|
24
32
|
self.line_bot_api.push_message(line_uid, TextSendMessage(text=text))
|
|
33
|
+
message = f"Send line message successful, line_uid: {line_uid}, text: {text}"
|
|
34
|
+
self.log_message(message)
|
|
25
35
|
|
|
26
36
|
except LineBotApiError as err:
|
|
27
37
|
message = f"Send line message fail, line_uid: {line_uid}, text: {text}, err: {str(err)}"
|
|
28
|
-
exception = LineBotException(message)
|
|
29
|
-
raise exception
|
|
38
|
+
exception = LineBotException(message=message)
|
|
39
|
+
raise exception
|
|
40
|
+
|
|
41
|
+
def log_message(self, log_message: str):
|
|
42
|
+
if self.logger:
|
|
43
|
+
self.logger.info(log_message)
|
|
44
|
+
|
|
45
|
+
@abstractmethod
|
|
46
|
+
async def handle_message(self):
|
|
47
|
+
"""
|
|
48
|
+
Each bot should define what handle_message it wants.
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
def register_handlers(self):
|
|
52
|
+
@self.handler.add(MessageEvent, message=TextMessage)
|
|
53
|
+
def handle_message(event: MessageEvent):
|
|
54
|
+
message = f'handle_message: {event}'
|
|
55
|
+
self.log_message(message)
|
|
56
|
+
asyncio.create_task(self.handle_message(event))
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from fastapi import status
|
|
2
|
+
from ...exception.base_exception import InternalBaseException
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class LineBotException(InternalBaseException):
|
|
6
|
+
def __init__(self, message: str = "", **kwargs):
|
|
7
|
+
status_code: int = status.HTTP_502_BAD_GATEWAY
|
|
8
|
+
code: str = "line_bot_error"
|
|
9
|
+
log_message: str = "Line bot error"
|
|
10
|
+
|
|
11
|
+
message = f"{log_message}, {message}" if message else log_message
|
|
12
|
+
super().__init__(status_code=status_code, code=code, message=message, **kwargs)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: fastapi_basic
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.11
|
|
4
4
|
Summary: A short description of your module
|
|
5
5
|
Home-page: https://github.com/szx21023/fastapi-base
|
|
6
6
|
Author: szx21023
|
|
@@ -10,33 +10,65 @@ Classifier: License :: OSI Approved :: MIT License
|
|
|
10
10
|
Classifier: Operating System :: OS Independent
|
|
11
11
|
Requires-Python: >=3.6
|
|
12
12
|
Description-Content-Type: text/markdown
|
|
13
|
+
Requires-Dist: aenum==3.1.16
|
|
14
|
+
Requires-Dist: aiohappyeyeballs==2.6.1
|
|
15
|
+
Requires-Dist: aiohttp==3.10.5
|
|
16
|
+
Requires-Dist: aiosignal==1.3.2
|
|
17
|
+
Requires-Dist: amqp==5.3.1
|
|
13
18
|
Requires-Dist: annotated-types==0.7.0
|
|
14
19
|
Requires-Dist: anyio==4.9.0
|
|
20
|
+
Requires-Dist: attrs==25.3.0
|
|
21
|
+
Requires-Dist: beanie==1.29.0
|
|
22
|
+
Requires-Dist: billiard==4.2.1
|
|
15
23
|
Requires-Dist: boto3==1.37.36
|
|
16
24
|
Requires-Dist: botocore==1.37.36
|
|
25
|
+
Requires-Dist: celery==5.5.2
|
|
17
26
|
Requires-Dist: certifi==2025.1.31
|
|
18
27
|
Requires-Dist: charset-normalizer==3.4.1
|
|
19
28
|
Requires-Dist: click==8.1.8
|
|
29
|
+
Requires-Dist: click-didyoumean==0.3.1
|
|
30
|
+
Requires-Dist: click-plugins==1.1.1
|
|
31
|
+
Requires-Dist: click-repl==0.3.0
|
|
32
|
+
Requires-Dist: Deprecated==1.2.18
|
|
33
|
+
Requires-Dist: dnspython==2.7.0
|
|
20
34
|
Requires-Dist: dotenv==0.9.9
|
|
21
35
|
Requires-Dist: fastapi==0.115.12
|
|
22
|
-
Requires-Dist:
|
|
36
|
+
Requires-Dist: frozenlist==1.6.0
|
|
37
|
+
Requires-Dist: future==1.0.0
|
|
38
|
+
Requires-Dist: h11==0.16.0
|
|
23
39
|
Requires-Dist: idna==3.10
|
|
24
40
|
Requires-Dist: jmespath==1.0.1
|
|
41
|
+
Requires-Dist: kombu==5.5.3
|
|
42
|
+
Requires-Dist: lazy-model==0.2.0
|
|
43
|
+
Requires-Dist: line-bot-sdk==3.12.0
|
|
44
|
+
Requires-Dist: motor==3.7.1
|
|
45
|
+
Requires-Dist: multidict==6.4.3
|
|
46
|
+
Requires-Dist: prompt_toolkit==3.0.51
|
|
47
|
+
Requires-Dist: propcache==0.3.1
|
|
25
48
|
Requires-Dist: pydantic==2.11.3
|
|
49
|
+
Requires-Dist: pydantic-settings==2.9.1
|
|
26
50
|
Requires-Dist: pydantic_core==2.33.1
|
|
51
|
+
Requires-Dist: pymongo==4.13.0
|
|
27
52
|
Requires-Dist: python-dateutil==2.9.0.post0
|
|
28
53
|
Requires-Dist: python-dotenv==1.1.0
|
|
29
54
|
Requires-Dist: python-multipart==0.0.20
|
|
55
|
+
Requires-Dist: redis==6.1.0
|
|
30
56
|
Requires-Dist: requests==2.32.3
|
|
31
57
|
Requires-Dist: s3transfer==0.11.5
|
|
32
58
|
Requires-Dist: six==1.17.0
|
|
33
59
|
Requires-Dist: sniffio==1.3.1
|
|
34
60
|
Requires-Dist: starlette==0.46.1
|
|
61
|
+
Requires-Dist: toml==0.10.2
|
|
35
62
|
Requires-Dist: typing-inspection==0.4.0
|
|
36
63
|
Requires-Dist: typing_extensions==4.13.1
|
|
64
|
+
Requires-Dist: tzdata==2025.2
|
|
37
65
|
Requires-Dist: urllib3==2.3.0
|
|
38
66
|
Requires-Dist: uvicorn==0.34.0
|
|
67
|
+
Requires-Dist: vine==5.1.0
|
|
39
68
|
Requires-Dist: watchtower==3.4.0
|
|
69
|
+
Requires-Dist: wcwidth==0.2.13
|
|
70
|
+
Requires-Dist: wrapt==1.17.2
|
|
71
|
+
Requires-Dist: yarl==1.20.0
|
|
40
72
|
Dynamic: author
|
|
41
73
|
Dynamic: author-email
|
|
42
74
|
Dynamic: classifier
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
fastapi_basic/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
fastapi_basic/base_config.py,sha256=WtYA7nI7yTw_bmMMIH1JdKiGOl81Vjekf2OpmwtnCww,568
|
|
3
|
+
fastapi_basic/base_factory.py,sha256=a8AtByr7zp0OQhWzaTkh51KQ_Q_b7XZEF2hcl1rRYCk,4387
|
|
4
|
+
fastapi_basic/const.py,sha256=jjNg7nutqtfzrqEKlU-zbzkDo3ndf3aDc603LbGcB_k,174
|
|
5
|
+
fastapi_basic/utils.py,sha256=8ympyQIXsKkxLILTI_7ug85vmKWYKqX0mpADjgHekgU,306
|
|
6
|
+
fastapi_basic/database/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
+
fastapi_basic/database/mongodb.py,sha256=XW7ApqYkR1Jf7LM1HaZ6jzYgajzuu1whKw5cpsjSO-M,1017
|
|
8
|
+
fastapi_basic/exception/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
9
|
+
fastapi_basic/exception/base_exception.py,sha256=xkpvAI8G96sqXqHgQC_rs2ZPL1liwKVXbXBIPF_8HPQ,473
|
|
10
|
+
fastapi_basic/ext/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
11
|
+
fastapi_basic/ext/aws/__init__.py,sha256=N_cwnbv1N-F1SY6NenBOTZe20zGaKFu3UXAu17hFAXY,816
|
|
12
|
+
fastapi_basic/ext/aws/const.py,sha256=Mmb6lo11aZZDAVy-nK-v_JxtVPPKYUo9GB4ihH7XYuM,68
|
|
13
|
+
fastapi_basic/ext/line/__init__.py,sha256=XkFYkV3Mx6w4TRim4ziETJrgOy9qJ_4pPrnqonBE_QU,2128
|
|
14
|
+
fastapi_basic/ext/line/exception.py,sha256=AUJdg15yDUxQe7kys_NiOBqfEgmolj8-y5c2wNgfp2U,490
|
|
15
|
+
fastapi_basic-0.1.11.dist-info/METADATA,sha256=DAmDHtBYTmy-rE_3JIisu_kr0CHeAhdDg_eLaGCpiRM,2753
|
|
16
|
+
fastapi_basic-0.1.11.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
17
|
+
fastapi_basic-0.1.11.dist-info/top_level.txt,sha256=Q9PdwWxaB4dy135MQHiroRYOqArdUSaIeEkzYinN6W0,14
|
|
18
|
+
fastapi_basic-0.1.11.dist-info/RECORD,,
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
fastapi_basic/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
-
fastapi_basic/base_config.py,sha256=nimiiK5LRY9kmx3dBDbN7teO4R8E423-c2iyq9WMcCg,442
|
|
3
|
-
fastapi_basic/base_factory.py,sha256=xrM4tbKjrhtqpzP2hupGJxiNeGsDnXfMYe8urOcq-ss,3801
|
|
4
|
-
fastapi_basic/const.py,sha256=UA7-Eefu_dbWpbFn09Ei_BPb903SExnCgVbnm8_3ALE,99
|
|
5
|
-
fastapi_basic/utils.py,sha256=8ympyQIXsKkxLILTI_7ug85vmKWYKqX0mpADjgHekgU,306
|
|
6
|
-
fastapi_basic/database/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
-
fastapi_basic/database/mongodb.py,sha256=XW7ApqYkR1Jf7LM1HaZ6jzYgajzuu1whKw5cpsjSO-M,1017
|
|
8
|
-
fastapi_basic/exception/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
9
|
-
fastapi_basic/exception/base_exception.py,sha256=VwMqn85rBeJCF6RIOYUR1eLxbP2aSDhhtAyyhYmBI84,855
|
|
10
|
-
fastapi_basic/ext/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
11
|
-
fastapi_basic/ext/aws/__init__.py,sha256=N_cwnbv1N-F1SY6NenBOTZe20zGaKFu3UXAu17hFAXY,816
|
|
12
|
-
fastapi_basic/ext/aws/const.py,sha256=Mmb6lo11aZZDAVy-nK-v_JxtVPPKYUo9GB4ihH7XYuM,68
|
|
13
|
-
fastapi_basic/ext/line/__init__.py,sha256=ZyCTuvW9P_iaMr2lFXp0JEEQMK773qOTnWiUdxIwAS8,1170
|
|
14
|
-
fastapi_basic-0.1.2.dist-info/METADATA,sha256=AVeideHs4DOieR-m9ec0KSbnJ_2sFxG_X6btu8PCbyw,1740
|
|
15
|
-
fastapi_basic-0.1.2.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
|
|
16
|
-
fastapi_basic-0.1.2.dist-info/top_level.txt,sha256=Q9PdwWxaB4dy135MQHiroRYOqArdUSaIeEkzYinN6W0,14
|
|
17
|
-
fastapi_basic-0.1.2.dist-info/RECORD,,
|
|
File without changes
|