restfx


Namerestfx JSON
Version 0.14.1 PyPI version JSON
download
home_pagehttps://gitee.com/hyjiacan/restfx
SummaryAuto RESTful framework for Python3.
upload_time2021-04-15 06:51:12
maintainerhyjiacan
docs_urlNone
authorhyjiacan
requires_python>=3.5
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # restfx

Python3 的 restful 多应用自动路由框架。

> 底层基于 [werkzeug](https://werkzeug.palletsprojects.com/)

## 为什么要使用此框架

开发此框架的目标是 **提升开发效率**。

我也曾使用过 **Django**,**Flask**。
但其繁琐的路由注册,以及参数声明,让人难以接受,简单的功能,却要写一大段代码。

此框架的前身是 [restful-dj](https://gitee.com/hyjiacan/restful-dj),
这是一个为 Django 开发的框架。

在使用中慢慢发现,在 restful 接口上,Django 给了我太多我用不到的东西,
臃肿不堪,于是才决定基于 `werkzeug` 开发。

此框架解决了以下问题:

- 没有繁锁的路由配置,免去路由注册。仅仅需要对模块根进行注册,模块下的所有路由都会自动被收集调用
- 不需要对路由 url 进行显示配置,完全自动解析 
- 自动解析/校验请求参数,并填充到路由函数,省去繁锁的参数获取/类型校验。需要做的仅仅是编写一个函数,并添加函数参数的类型声明 
- 提供 **接口列表页面** 以及接口测试支持,让接口随码更新,不用手动维护API文档。 见[截图](#截图)
- 提供 [路由注入][2] 支持,以通过参数方式向路由指定请求参数外的数据/函数,从而避免一些频繁的 `import` 和重复代码

**此框架的弊端: 不支持将参数作为 url 路径的一部分**

## 安装

```shell script
pip install restfx
```

`Since 0.7.1` 安装后,可以通过 CLI 工具 `restfx` 命令创建基本项目结构:

```shell script
restfx create projectname
```

> 使用此命令,可能需要将 `restfx` 安装到全局环境中。

## 文档

使用文档见 [Gitee Wiki][1]

## 创建应用

```python
import os

import restfx

if __name__ == '__main__':
    root = os.path.dirname(__file__)
    app = restfx.App(root, api_prefix='any/prefix', debug=True)
    app.map_routes({
        'x': 'test'
    })
    app.map_static(static_map={})
    app.startup(host='127.0.0.1', port=9127)
```

### 编写路由

*test/api/demo.py*

```python
from restfx import route
from restfx.http import HttpRequest, HttpFile


@route(module='测试名称-模块', name='测试名称-GET')
def get(request, param1, param2=None, param3: int = 5):
    # request 会是 HttpRequest
    return {
        'param1': param1,
        'param2': param2,
        'param3': param3,
    }


@route(module='测试名称-模块', name='测试名称-POST_PARAM')
def get_param(param1, req: HttpRequest, from_=None, param3=5):
    # req 会是 HttpRequest
    return {
        'param1': param1,
        'from': from_,
        'param3': param3,
    }


@route(module='测试名称-模块', name='测试名称-PUT_PARAM')
def put(request: str, param1, file: HttpFile, param3=5):
    # request 会是请求参数,参数列表中没有 HttpRequest
    return {
        'request': request,
        'param1': param1,
        'param3': param3,
    }


@route(module='测试名称-模块', name='测试名称-DELETE_PARAM')
def delete(request, param1, from_=None, param3=5, **kwargs):
    # 未在函数的参数列表中声明的请求参数,会出现在 kwargs 中
    return {
        'param1': param1,
        'from': from_,
        'param3': param3,
        'variable_args': kwargs
    }

```

## 截图

以下截图为接口列表,对应的路由声明源码见

- [test/test/api/__init__.py][11]
- [test/test/api/demo.py][12]


以下截图仅在 [Gitee仓库目录][3] 可见

![list](assets/1.png)

![test](assets/2.png)


[1]: https://gitee.com/hyjiacan/restfx/wikis
[2]: https://gitee.com/hyjiacan/restfx/wikis/0B.%20%E8%B7%AF%E7%94%B1%E6%B3%A8%E5%85%A5?sort_id=3519061
[3]: https://gitee.com/hyjiacan/restfx#%E6%88%AA%E5%9B%BE
[11]: https://gitee.com/hyjiacan/restfx/blob/master/test/test/api/__init__.py
[12]: https://gitee.com/hyjiacan/restfx/blob/master/test/test/api/demo.py

## TODOLIST

- [ ] 添加 自定义接口页面JS资源加载,以支持对请求和响应的数据进行额外处理
  - 类似于勾子函数
- [ ] Use `LocalStack` from werkzeug instead the `MemorySessionProvider` storage.
- [ ] Add lock onto `MemorySessionProvider` & `FileSessionProvider`
- [ ] 添加 接口页面上,自定义请求头以及渲染响应头支持


            

Raw data

            {
    "_id": null,
    "home_page": "https://gitee.com/hyjiacan/restfx",
    "name": "restfx",
    "maintainer": "hyjiacan",
    "docs_url": null,
    "requires_python": ">=3.5",
    "maintainer_email": "hyjiacan@163.com",
    "keywords": "",
    "author": "hyjiacan",
    "author_email": "hyjiacan@163.com",
    "download_url": "https://files.pythonhosted.org/packages/39/2e/0f16c3e0a8d7ee6578a278db6a754409a6c6e1883b4bbdcce79421d0cec0/restfx-0.14.1.tar.gz",
    "platform": "",
    "description": "# restfx\n\nPython3 \u7684 restful \u591a\u5e94\u7528\u81ea\u52a8\u8def\u7531\u6846\u67b6\u3002\n\n> \u5e95\u5c42\u57fa\u4e8e [werkzeug](https://werkzeug.palletsprojects.com/)\n\n## \u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u6b64\u6846\u67b6\n\n\u5f00\u53d1\u6b64\u6846\u67b6\u7684\u76ee\u6807\u662f **\u63d0\u5347\u5f00\u53d1\u6548\u7387**\u3002\n\n\u6211\u4e5f\u66fe\u4f7f\u7528\u8fc7 **Django**\uff0c**Flask**\u3002\n\u4f46\u5176\u7e41\u7410\u7684\u8def\u7531\u6ce8\u518c\uff0c\u4ee5\u53ca\u53c2\u6570\u58f0\u660e\uff0c\u8ba9\u4eba\u96be\u4ee5\u63a5\u53d7\uff0c\u7b80\u5355\u7684\u529f\u80fd\uff0c\u5374\u8981\u5199\u4e00\u5927\u6bb5\u4ee3\u7801\u3002\n\n\u6b64\u6846\u67b6\u7684\u524d\u8eab\u662f [restful-dj](https://gitee.com/hyjiacan/restful-dj)\uff0c\n\u8fd9\u662f\u4e00\u4e2a\u4e3a Django \u5f00\u53d1\u7684\u6846\u67b6\u3002\n\n\u5728\u4f7f\u7528\u4e2d\u6162\u6162\u53d1\u73b0\uff0c\u5728 restful \u63a5\u53e3\u4e0a\uff0cDjango \u7ed9\u4e86\u6211\u592a\u591a\u6211\u7528\u4e0d\u5230\u7684\u4e1c\u897f\uff0c\n\u81c3\u80bf\u4e0d\u582a\uff0c\u4e8e\u662f\u624d\u51b3\u5b9a\u57fa\u4e8e `werkzeug` \u5f00\u53d1\u3002\n\n\u6b64\u6846\u67b6\u89e3\u51b3\u4e86\u4ee5\u4e0b\u95ee\u9898\uff1a\n\n- \u6ca1\u6709\u7e41\u9501\u7684\u8def\u7531\u914d\u7f6e\uff0c\u514d\u53bb\u8def\u7531\u6ce8\u518c\u3002\u4ec5\u4ec5\u9700\u8981\u5bf9\u6a21\u5757\u6839\u8fdb\u884c\u6ce8\u518c\uff0c\u6a21\u5757\u4e0b\u7684\u6240\u6709\u8def\u7531\u90fd\u4f1a\u81ea\u52a8\u88ab\u6536\u96c6\u8c03\u7528\n- \u4e0d\u9700\u8981\u5bf9\u8def\u7531 url \u8fdb\u884c\u663e\u793a\u914d\u7f6e\uff0c\u5b8c\u5168\u81ea\u52a8\u89e3\u6790 \n- \u81ea\u52a8\u89e3\u6790/\u6821\u9a8c\u8bf7\u6c42\u53c2\u6570\uff0c\u5e76\u586b\u5145\u5230\u8def\u7531\u51fd\u6570\uff0c\u7701\u53bb\u7e41\u9501\u7684\u53c2\u6570\u83b7\u53d6/\u7c7b\u578b\u6821\u9a8c\u3002\u9700\u8981\u505a\u7684\u4ec5\u4ec5\u662f\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u6dfb\u52a0\u51fd\u6570\u53c2\u6570\u7684\u7c7b\u578b\u58f0\u660e \n- \u63d0\u4f9b **\u63a5\u53e3\u5217\u8868\u9875\u9762** \u4ee5\u53ca\u63a5\u53e3\u6d4b\u8bd5\u652f\u6301\uff0c\u8ba9\u63a5\u53e3\u968f\u7801\u66f4\u65b0\uff0c\u4e0d\u7528\u624b\u52a8\u7ef4\u62a4API\u6587\u6863\u3002 \u89c1[\u622a\u56fe](#\u622a\u56fe)\n- \u63d0\u4f9b [\u8def\u7531\u6ce8\u5165][2] \u652f\u6301\uff0c\u4ee5\u901a\u8fc7\u53c2\u6570\u65b9\u5f0f\u5411\u8def\u7531\u6307\u5b9a\u8bf7\u6c42\u53c2\u6570\u5916\u7684\u6570\u636e/\u51fd\u6570\uff0c\u4ece\u800c\u907f\u514d\u4e00\u4e9b\u9891\u7e41\u7684 `import` \u548c\u91cd\u590d\u4ee3\u7801\n\n**\u6b64\u6846\u67b6\u7684\u5f0a\u7aef: \u4e0d\u652f\u6301\u5c06\u53c2\u6570\u4f5c\u4e3a url \u8def\u5f84\u7684\u4e00\u90e8\u5206**\n\n## \u5b89\u88c5\n\n```shell script\npip install restfx\n```\n\n`Since 0.7.1` \u5b89\u88c5\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7 CLI \u5de5\u5177 `restfx` \u547d\u4ee4\u521b\u5efa\u57fa\u672c\u9879\u76ee\u7ed3\u6784:\n\n```shell script\nrestfx create projectname\n```\n\n> \u4f7f\u7528\u6b64\u547d\u4ee4\uff0c\u53ef\u80fd\u9700\u8981\u5c06 `restfx` \u5b89\u88c5\u5230\u5168\u5c40\u73af\u5883\u4e2d\u3002\n\n## \u6587\u6863\n\n\u4f7f\u7528\u6587\u6863\u89c1 [Gitee Wiki][1]\n\n## \u521b\u5efa\u5e94\u7528\n\n```python\nimport os\n\nimport restfx\n\nif __name__ == '__main__':\n    root = os.path.dirname(__file__)\n    app = restfx.App(root, api_prefix='any/prefix', debug=True)\n    app.map_routes({\n        'x': 'test'\n    })\n    app.map_static(static_map={})\n    app.startup(host='127.0.0.1', port=9127)\n```\n\n### \u7f16\u5199\u8def\u7531\n\n*test/api/demo.py*\n\n```python\nfrom restfx import route\nfrom restfx.http import HttpRequest, HttpFile\n\n\n@route(module='\u6d4b\u8bd5\u540d\u79f0-\u6a21\u5757', name='\u6d4b\u8bd5\u540d\u79f0-GET')\ndef get(request, param1, param2=None, param3: int = 5):\n    # request \u4f1a\u662f HttpRequest\n    return {\n        'param1': param1,\n        'param2': param2,\n        'param3': param3,\n    }\n\n\n@route(module='\u6d4b\u8bd5\u540d\u79f0-\u6a21\u5757', name='\u6d4b\u8bd5\u540d\u79f0-POST_PARAM')\ndef get_param(param1, req: HttpRequest, from_=None, param3=5):\n    # req \u4f1a\u662f HttpRequest\n    return {\n        'param1': param1,\n        'from': from_,\n        'param3': param3,\n    }\n\n\n@route(module='\u6d4b\u8bd5\u540d\u79f0-\u6a21\u5757', name='\u6d4b\u8bd5\u540d\u79f0-PUT_PARAM')\ndef put(request: str, param1, file: HttpFile, param3=5):\n    # request \u4f1a\u662f\u8bf7\u6c42\u53c2\u6570\uff0c\u53c2\u6570\u5217\u8868\u4e2d\u6ca1\u6709 HttpRequest\n    return {\n        'request': request,\n        'param1': param1,\n        'param3': param3,\n    }\n\n\n@route(module='\u6d4b\u8bd5\u540d\u79f0-\u6a21\u5757', name='\u6d4b\u8bd5\u540d\u79f0-DELETE_PARAM')\ndef delete(request, param1, from_=None, param3=5, **kwargs):\n    # \u672a\u5728\u51fd\u6570\u7684\u53c2\u6570\u5217\u8868\u4e2d\u58f0\u660e\u7684\u8bf7\u6c42\u53c2\u6570\uff0c\u4f1a\u51fa\u73b0\u5728 kwargs \u4e2d\n    return {\n        'param1': param1,\n        'from': from_,\n        'param3': param3,\n        'variable_args': kwargs\n    }\n\n```\n\n## \u622a\u56fe\n\n\u4ee5\u4e0b\u622a\u56fe\u4e3a\u63a5\u53e3\u5217\u8868\uff0c\u5bf9\u5e94\u7684\u8def\u7531\u58f0\u660e\u6e90\u7801\u89c1\n\n- [test/test/api/__init__.py][11]\n- [test/test/api/demo.py][12]\n\n\n\u4ee5\u4e0b\u622a\u56fe\u4ec5\u5728 [Gitee\u4ed3\u5e93\u76ee\u5f55][3] \u53ef\u89c1\n\n![list](assets/1.png)\n\n![test](assets/2.png)\n\n\n[1]: https://gitee.com/hyjiacan/restfx/wikis\n[2]: https://gitee.com/hyjiacan/restfx/wikis/0B.%20%E8%B7%AF%E7%94%B1%E6%B3%A8%E5%85%A5?sort_id=3519061\n[3]: https://gitee.com/hyjiacan/restfx#%E6%88%AA%E5%9B%BE\n[11]: https://gitee.com/hyjiacan/restfx/blob/master/test/test/api/__init__.py\n[12]: https://gitee.com/hyjiacan/restfx/blob/master/test/test/api/demo.py\n\n## TODOLIST\n\n- [ ] \u6dfb\u52a0 \u81ea\u5b9a\u4e49\u63a5\u53e3\u9875\u9762JS\u8d44\u6e90\u52a0\u8f7d\uff0c\u4ee5\u652f\u6301\u5bf9\u8bf7\u6c42\u548c\u54cd\u5e94\u7684\u6570\u636e\u8fdb\u884c\u989d\u5916\u5904\u7406\n  - \u7c7b\u4f3c\u4e8e\u52fe\u5b50\u51fd\u6570\n- [ ] Use `LocalStack` from werkzeug instead the `MemorySessionProvider` storage.\n- [ ] Add lock onto `MemorySessionProvider` & `FileSessionProvider`\n- [ ] \u6dfb\u52a0 \u63a5\u53e3\u9875\u9762\u4e0a\uff0c\u81ea\u5b9a\u4e49\u8bf7\u6c42\u5934\u4ee5\u53ca\u6e32\u67d3\u54cd\u5e94\u5934\u652f\u6301\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Auto RESTful framework for Python3.",
    "version": "0.14.1",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "eb25e428590cde4e1c237d8a2d3303a5",
                "sha256": "c0f5aede7f5a0e1e1ad0a2168cbe9c7652af67212b465a148ab596faa8e4b6d9"
            },
            "downloads": -1,
            "filename": "restfx-0.14.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "eb25e428590cde4e1c237d8a2d3303a5",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.5",
            "size": 83861,
            "upload_time": "2021-04-15T06:51:09",
            "upload_time_iso_8601": "2021-04-15T06:51:09.110160Z",
            "url": "https://files.pythonhosted.org/packages/7e/63/5e61c0e83b310c100496a7839c22fa404031cf48608c6a72a7e025689ac6/restfx-0.14.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "463306f1bfd87a0c3728ae3c18c1d414",
                "sha256": "cbaed57f5b1ae9224f60f6586aee48404397affc4364fbb5b69332701a62fecf"
            },
            "downloads": -1,
            "filename": "restfx-0.14.1.tar.gz",
            "has_sig": false,
            "md5_digest": "463306f1bfd87a0c3728ae3c18c1d414",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.5",
            "size": 69565,
            "upload_time": "2021-04-15T06:51:12",
            "upload_time_iso_8601": "2021-04-15T06:51:12.828221Z",
            "url": "https://files.pythonhosted.org/packages/39/2e/0f16c3e0a8d7ee6578a278db6a754409a6c6e1883b4bbdcce79421d0cec0/restfx-0.14.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2021-04-15 06:51:12",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "restfx"
}
        
Elapsed time: 0.28371s