-
Couldn't load subscription status.
- Fork 0
[refactor] 重构模块化组件,添加调用实例 #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lanbinshijie
merged 1 commit into
OpenTeens:feat-moduleize
from
AUXStar:feat-moduleize-njzy
Mar 2, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| ''' | ||
| Date: 2024-02-18 15:57:15 | ||
| LastEditors: 宁静致远 468835121@qq.com | ||
| LastEditTime: 2024-02-18 15:59:59 | ||
| ''' | ||
| def createAPIRouter(): | ||
| from fastapi import APIRouter | ||
| router = APIRouter() | ||
| from . import example_service2,example_service3 | ||
| router.include_router(example_service2.createAPIRouter()) | ||
| router.include_router(example_service3.createAPIRouter()) | ||
| return router | ||
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,13 @@ | ||
| ''' | ||
| Date: 2024-02-18 15:41:17 | ||
| LastEditors: 宁静致远 468835121@qq.com | ||
| LastEditTime: 2024-02-18 15:51:41 | ||
| ''' | ||
| ''' | ||
| Date: 2024-02-18 15:41:17 | ||
| LastEditors: 宁静致远 468835121@qq.com | ||
| LastEditTime: 2024-02-18 15:44:36 | ||
| ''' | ||
|
Comment on lines
+1
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 能不能去掉或者只显示一个。。。 我觉得git就能自动追踪这些 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 嗷,我编辑器的问题 |
||
| """ | ||
| [该文件为必须文件] | ||
| router.py用于定义对外公开的api路由, 并提供一个router对象供__init__.py调用。 | ||
|
|
@@ -26,7 +36,7 @@ | |
| router = fastapi.APIRouter(prefix="/example") | ||
|
|
||
| @router.get("/add") | ||
| def add(): | ||
| def add(a:int,b:int): | ||
| """ | ||
| 对func.add进行简单封装。 | ||
|
|
||
|
|
@@ -39,9 +49,9 @@ def add(): | |
| :return: int | ||
| """ | ||
| # Get parameter | ||
| request = fastapi.Request() | ||
| a = request.args.get("a") | ||
| b = request.args.get("b") | ||
| # request = fastapi.Request() | ||
| # a = request.args.get("a") | ||
| # b = request.args.get("b") | ||
|
|
||
| # Type Check | ||
| if not isinstance(a, int) or not isinstance(b, int): | ||
|
|
@@ -66,7 +76,7 @@ def add(): | |
| } | ||
|
|
||
| @router.get("/wrong") | ||
| def wrong_example(): | ||
| def wrong_example(a:int,b:int,c:int): | ||
| """ | ||
| 一个 **错误** 的例子。 | ||
|
|
||
|
|
@@ -75,10 +85,10 @@ def wrong_example(): | |
| :return: int | ||
| """ | ||
| # Get parameter | ||
| request = fastapi.Request() | ||
| a = request.args.get("a") | ||
| b = request.args.get("b") | ||
| c = request.args.get("c") | ||
| # request = fastapi.Request() | ||
| # a = request.args.get("a") | ||
| # b = request.args.get("b") | ||
| # c = request.args.get("c") | ||
|
|
||
| # [Warning!] Call two business logic function | ||
| ret = api.add(a, b) + api.add(b, c) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| ''' | ||
| Date: 2024-02-18 15:54:04 | ||
| LastEditors: 宁静致远 468835121@qq.com | ||
| LastEditTime: 2024-02-18 16:02:04 | ||
| ''' | ||
|
|
||
| def createAPIRouter(): | ||
| from . import router | ||
| return router.router |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| ''' | ||
| Date: 2024-02-18 15:54:04 | ||
| LastEditors: 宁静致远 468835121@qq.com | ||
| LastEditTime: 2024-02-18 16:02:42 | ||
| ''' | ||
| """ | ||
| 这个文件是必须的。 | ||
|
|
||
| 这个文件定义了对模块提供的API。 | ||
|
|
||
| 提供方式 **只能** 是对业务逻辑的 import, 不能进行其他处理。 | ||
|
|
||
| ---------------------------------------------- | ||
| 别的模块通过 | ||
|
|
||
| ```py | ||
| from services.example_service1.api imoprt API1, API2, ... | ||
| ``` | ||
|
|
||
| 来使用这个模块提供的API。 | ||
| 这些对模块暴露的api在这里被定义。 | ||
| """ | ||
|
|
||
| def me(a): | ||
| return f"I'm e2. say {a}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| ''' | ||
| Date: 2024-02-18 15:41:17 | ||
| LastEditors: 宁静致远 468835121@qq.com | ||
| LastEditTime: 2024-02-18 16:10:16 | ||
| ''' | ||
| ''' | ||
| Date: 2024-02-18 15:41:17 | ||
| LastEditors: 宁静致远 468835121@qq.com | ||
| LastEditTime: 2024-02-18 15:44:36 | ||
| ''' | ||
| """ | ||
| [该文件为必须文件] | ||
| router.py用于定义对外公开的api路由, 并提供一个router对象供__init__.py调用。 | ||
|
|
||
| 需要提供的东西: | ||
| - router: fastapi.APIRouter | ||
|
|
||
| ---------------------------------------------------------- | ||
| 这里的函数应当能直接处理fastapi的调用。 | ||
|
|
||
| 一般该函数的内容: | ||
| 1. Get Parameter 从http请求中取获取参数 | ||
| 2. Type Check 进行类型校验 | ||
| 3. Type Convert 进行类型转化 | ||
| 4. Process 调用api.py内的api函数。(提供给外部的api显然也得提供给内部) | ||
| 5. Return 处理业务函数的返回值, reformat为json或其他格式, 返回给用户。 | ||
|
|
||
| 【注意】:为了保证代码简洁,这里的函数 **只能** 调用提供给业务逻辑函数。 | ||
| """ | ||
|
|
||
| import fastapi | ||
|
|
||
| from . import api | ||
|
|
||
|
|
||
| router = fastapi.APIRouter(prefix="/example2") | ||
|
|
||
| @router.get("/add") | ||
| def add(a:int,b:int): | ||
| """ | ||
| 对func.add进行简单封装。 | ||
|
|
||
| 从参数中获取a和b, 做类型转化, 然后调用api.add。 | ||
| **(为了代码整齐, 只能做这些)** | ||
|
|
||
| :param a: int | ||
| :param b: int | ||
|
|
||
| :return: int | ||
| """ | ||
| # Get parameter | ||
| # request = fastapi.Request() | ||
| # a = request.args.get("a") | ||
| # b = request.args.get("b") | ||
|
|
||
|
|
||
| # Process | ||
| ret = a + b | ||
|
|
||
| # Return | ||
| return { | ||
| "code": 0, | ||
| "msg": "Success", | ||
| "data": ret | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
|
|
||
| def createAPIRouter(): | ||
| from . import router | ||
| return router.router |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| """ | ||
| 这个文件是必须的。 | ||
|
|
||
| 这个文件定义了对模块提供的API。 | ||
|
|
||
| 提供方式 **只能** 是对业务逻辑的 import, 不能进行其他处理。 | ||
|
|
||
| ---------------------------------------------- | ||
| 别的模块通过 | ||
|
|
||
| ```py | ||
| from services.example_service1.api imoprt API1, API2, ... | ||
| ``` | ||
|
|
||
| 来使用这个模块提供的API。 | ||
| 这些对模块暴露的api在这里被定义。 | ||
| """ | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| ''' | ||
| Date: 2024-02-18 15:41:17 | ||
| LastEditors: 宁静致远 468835121@qq.com | ||
| LastEditTime: 2024-02-18 16:10:28 | ||
| ''' | ||
| ''' | ||
| Date: 2024-02-18 15:41:17 | ||
| LastEditors: 宁静致远 468835121@qq.com | ||
| LastEditTime: 2024-02-18 15:44:36 | ||
| ''' | ||
| """ | ||
| [该文件为必须文件] | ||
| router.py用于定义对外公开的api路由, 并提供一个router对象供__init__.py调用。 | ||
|
|
||
| 需要提供的东西: | ||
| - router: fastapi.APIRouter | ||
|
|
||
| ---------------------------------------------------------- | ||
| 这里的函数应当能直接处理fastapi的调用。 | ||
|
|
||
| 一般该函数的内容: | ||
| 1. Get Parameter 从http请求中取获取参数 | ||
| 2. Type Check 进行类型校验 | ||
| 3. Type Convert 进行类型转化 | ||
| 4. Process 调用api.py内的api函数。(提供给外部的api显然也得提供给内部) | ||
| 5. Return 处理业务函数的返回值, reformat为json或其他格式, 返回给用户。 | ||
|
|
||
| 【注意】:为了保证代码简洁,这里的函数 **只能** 调用提供给业务逻辑函数。 | ||
| """ | ||
|
|
||
| import fastapi | ||
|
|
||
| from . import api | ||
|
|
||
| # 模块间调用 | ||
| from ..example_service2.api import me | ||
|
|
||
|
|
||
| router = fastapi.APIRouter(prefix="/example3") | ||
|
|
||
| @router.get("/say") | ||
| def say(a:str): | ||
| return me(a) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我觉得这些可以合并到app.py里面,没必要单独列一个文件。
还有能不能自动扫描整个services目录,这样就不用每次都往里面手动加了