自动导入APISTAR子应用程序
apistar-autoapp的Python项目详细描述
API Star自动应用程序
轻松管理和编写api之星项目
使用
^基于{
功能
- 根据项目的结构自动生成URL。
- 自动应用程序Includes routes
从项目中的应用创建基于
来自项目根文件
app.py
的文件系统路径。
- 自动应用程序Includes routes
从项目中的应用创建基于
来自项目根文件
- 自动从应用程序收集event_hooks和component列表并将其合并 一起构建App/ASyncApp。
- 轻松添加支持
app.py
布局的外部包。
例如:AutoApp(apps=['apistar_websocket'])
- 允许按路径字符串排序的应用程序优先级列表来控制项的顺序 在event_hooks和component列表中以及控制导入顺序。
快速启动
只需分别使用AutoApp
或AutoASyncApp
代替App和ASyncApp即可
fromapistar-autoappimportAutoAppasyncdefwelcome()->dict:return{'msg':'hello'}routes=[Route('/','GET',handler=welcome,name='welcome'),]app=AutoApp(routes=routes,)if__name__=='__main__':app.serve('127.0.0.1',8000,debug=True)
使用apistar-websocket
包的异步应用程序。简单地使用外部库
在apps
参数中列出它。
注意:在apps
中列出的包必须支持app.py
布局。
fromapistar-autoappimportAutoASyncAppasyncdefwelcome()->dict:return{'msg':'hello'}routes=[Route('/','GET',handler=welcome,name='welcome'),]app=AutoASyncApp(apps=['apistar_websocket'],routes=routes,)if__name__=='__main__':app.serve('127.0.0.1',8000,debug=True)
安装
pip install apistar-autoapp
或者对于Pipenv用户
pipenv install apistar-autoapp
解剖学
要将包视为“app”,它必须在其顶部目录中包含名为app.py
的文件。这个
app.py
文件可以为空。它在app.py
文件中,您可以在其中公开您的应用程序
提供给API Star配置。自动应用程序将显示
对于app.py
模块上的三个属性,如果找到它们,请将它们添加到API Star
启动时的配置。
属性必须是列表并命名:
routes
components
event_hooks
例如,一个只公开其路由的简单应用程序可以是:
app.py
:
from .handlers import routes
或者一个应用程序公开它的路由、事件挂钩和组件:
app.py
:
from .handlers import routes
from .components import components
from .event_hooks import event_hooks
当然,如果你有一个简单的应用程序,你可以在app.py
和
然后由应用程序代码定义模块变量。
一个简单的应用程序:
fromapistarimportApp,Routedefhomepage()->str:return'<html><body><h1>Homepage</h1></body></html>'routes=[Route('/',method='GET',handler=homepage),]
示例项目结构
project/
app.py
v1/
app.py
...
endpointOne/
app.py
...
endpointTwo/
app.py
...
如果v1/app.py
文件为空并且每个endpoint*
应用程序都公开一个根url,/
,则路由
通过Includes路由为项目创建的URL
将是:
/v1/endpointOne
/v1/endpointTwo
如果endpointOne
有另一个url的路由/users
,那么您将拥有:
/v1/endpointOne
/v1/endpointOne/users
/v1/endpointTwo
文档
自动应用程序
AutoApp(project_dir: str = None,
priority_apps: list = None,
print_results: bool = False,
**kwargs) -> App
参数:
(Optional)
apps: A list of packages to add to your App. Use this for packages outside or your App/project.
Packages listed in the apps list must be on your PYTHON_PATH.
(Optional)
project_dir: The directory from which apistar-autoapp will look for a project root.
This is autodetected if not used and you normally won't use this parameter.
(Optional)
priority_apps: A list of apps, by their import path, that will be imported before all
other apps found by apistar-autoapp and imported in the order they are given.
(Optional)
print_results: Print the results of the configuration created by apistar-autoapp to
the console.
kwargs: These are the arguments you'd normally pass to App or ASyncApp. If you pass
any of the arguments: routes, components or event_hooks, they will be given precedence
and listed before any of the corresponding values created by autoapp.
自动异步应用程序
AutoASyncApp(project_dir: str = None,
priority_apps: list = None,
print_results: bool = False,
**kwargs) -> ASyncApp
应用程序参数
app_args(project_dir: str = None,
priority_apps: list = None,
print_results: bool = False,
**kwargs) -> dict:
app_args
与autoapp和autoasyncapp相同,只是它返回一个参数字典
供App或ASyncApp使用的。
实际上AutoApp只是:
defAutoApp(**kwargs)->App:returnApp(**app_args(**kwargs))
因此,如果您想在创建应用程序之前对来自autoapp的数据执行某些操作,则很容易:
kwargs=app_args(...)# Do something with kwargs# ...app=App(**kwargs)
打印助手
有一些打印帮助程序供APISTAR AutoApp内部使用,但已公开供其他人使用
模块。有一个Printer
类默认使用print
,但可以使用
在它的位置接受一个字符串。
fromapistar_autoapp.printerimportPrinterpr=Printer()# Or if you want to your own print function, like a logger:pr=Printer(printer=logger.info)...# to print out a list of routes:pr.routes(routes)
包括
将一个Include实例以及它的所有Routes以以下形式打印到控制台:
Include: <url> <name>
<Routes>
示例:
包括:/v2/welcome v2:welcome 路线:get/,欢迎 app.welcome()->;听写:
路线
以以下形式将Route实例打印到控制台:
Route: <method> <url>, <name>
<handler>
示例:
Route: GET /, welcome
app.welcome() -> dict:
路线
使用^{print_include
形式:
Routes:
[
<print_route> or <print_include>,
...
]
示例:
Routes:
Route: GET /, welcome
app.welcome() -> dict:
Include: /v2/home v2:home
Route: GET /v2/home/, list
v2.home.handlers.list_homes() -> list:
...
组件
以以下格式打印components列表:
Components:
<ComponentClass>
resolve(<signature>) -> <returns>:
示例:
Components:
WebSocketComponent
resolve(self, scope: ASGIScope, send: ASGISend, receive: ASGIReceive, app: App) -> WebSocket:
待办事项
- 允许任何带有可导入的
app.py
文件的包与autoapp一起使用 - 添加一个排除的应用程序列表,该列表不会由AutoApp导入
- 为
event_hooks
添加打印机