amos-api


Nameamos-api JSON
Version 1.0.8 PyPI version JSON
download
home_pageNone
SummaryAMOS common api
upload_time2024-10-11 03:28:48
maintainerNone
docs_urlNone
authorNone
requires_python>=3
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # amos api

主要用于定义离线分析中的 API 规范以及通用的 Response 等

## 快速开始

```py
import logging
from fastapi import (FastAPI, Body)
from fastapi.responses import JSONResponse
from pydantic import ValidationError

from amos_api import (
    ResponseBase,
    ResponseOK,
    ResponseValidationError,
    ResponseAlgorithmError,
    CheckAssertionError,
    CheckValueError
)

from starlette.responses import RedirectResponse

logger = logging.getLogger()

async def document():
    # return RedirectResponse(url="/docs")
    return RedirectResponse(url="/redoc")

def mount_app_routes(app: FastAPI):
    app.get("/",
            response_model=ResponseBase,
            summary="swagger 文档")(document)
    # tag items
    app.post("/user",
             tags=["User"],
             summary="用户信息",
             )(userInfo)

async def userInfo(name: str=Body(..., description="用户名称", examples=["ray"])) -> ResponseBase:
    return ResponseBase(data={"seq":"1", "name": name,})

async def userList(name: str=Body(..., description="用户名称", examples=["ray"])) -> ResponseBase:
    try:
        dblist()
        return ResponseOK(data=[])
    except Exception as e:
        if isinstance(e, (ValidationError, CheckAssertionError, CheckValueError)):
            ret = ResponseValidationError(data='校验错误的详细信息')
        else:
            ret = ResponseAlgorithmError(data='算法错误的详细信息')
        logger.error() #错误日志
        return JSONResponse(status_code=ret.code, content=ret.model_dump())
```

- `HttpCode._200` 请求成功,正常 code 值,ResponseBase 如果不设置code时,默认值。
- `HttpCode._400` 错误的请求,比如参数错误
- `HttpCode._403` 禁止访问的资源
- `HttpCode._404` 未找到
- `HttpCode._422` 常用于参数错误
- `HttpCode._500` 服务器错误,内部错误
- `HttpCode._522` 服务器错误,内部错误
- `HttpCode._523` 服务器错误,内部错误,已知
  

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "amos-api",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3",
    "maintainer_email": null,
    "keywords": null,
    "author": null,
    "author_email": "\"ilex.h\" <390132625@qq.com>, leavor <lint.hit@163.com>",
    "download_url": "https://files.pythonhosted.org/packages/4a/98/4eff152ae933de115d8d3ecbb88fc2079f5f65a65800599aa08a2587b1ab/amos_api-1.0.8.tar.gz",
    "platform": null,
    "description": "# amos api\r\n\r\n\u4e3b\u8981\u7528\u4e8e\u5b9a\u4e49\u79bb\u7ebf\u5206\u6790\u4e2d\u7684 API \u89c4\u8303\u4ee5\u53ca\u901a\u7528\u7684 Response \u7b49\r\n\r\n## \u5feb\u901f\u5f00\u59cb\r\n\r\n```py\r\nimport logging\r\nfrom fastapi import (FastAPI, Body)\r\nfrom fastapi.responses import JSONResponse\r\nfrom pydantic import ValidationError\r\n\r\nfrom amos_api import (\r\n    ResponseBase,\r\n    ResponseOK,\r\n    ResponseValidationError,\r\n    ResponseAlgorithmError,\r\n    CheckAssertionError,\r\n    CheckValueError\r\n)\r\n\r\nfrom starlette.responses import RedirectResponse\r\n\r\nlogger = logging.getLogger()\r\n\r\nasync def document():\r\n    # return RedirectResponse(url=\"/docs\")\r\n    return RedirectResponse(url=\"/redoc\")\r\n\r\ndef mount_app_routes(app: FastAPI):\r\n    app.get(\"/\",\r\n            response_model=ResponseBase,\r\n            summary=\"swagger \u6587\u6863\")(document)\r\n    # tag items\r\n    app.post(\"/user\",\r\n             tags=[\"User\"],\r\n             summary=\"\u7528\u6237\u4fe1\u606f\",\r\n             )(userInfo)\r\n\r\nasync def userInfo(name: str=Body(..., description=\"\u7528\u6237\u540d\u79f0\", examples=[\"ray\"])) -> ResponseBase:\r\n    return ResponseBase(data={\"seq\":\"1\", \"name\": name,})\r\n\r\nasync def userList(name: str=Body(..., description=\"\u7528\u6237\u540d\u79f0\", examples=[\"ray\"])) -> ResponseBase:\r\n    try:\r\n        dblist()\r\n        return ResponseOK(data=[])\r\n    except Exception as e:\r\n        if isinstance(e, (ValidationError, CheckAssertionError, CheckValueError)):\r\n            ret = ResponseValidationError(data='\u6821\u9a8c\u9519\u8bef\u7684\u8be6\u7ec6\u4fe1\u606f')\r\n        else:\r\n            ret = ResponseAlgorithmError(data='\u7b97\u6cd5\u9519\u8bef\u7684\u8be6\u7ec6\u4fe1\u606f')\r\n        logger.error() #\u9519\u8bef\u65e5\u5fd7\r\n        return JSONResponse(status_code=ret.code, content=ret.model_dump())\r\n```\r\n\r\n- `HttpCode._200` \u8bf7\u6c42\u6210\u529f,\u6b63\u5e38 code \u503c\uff0cResponseBase \u5982\u679c\u4e0d\u8bbe\u7f6ecode\u65f6\uff0c\u9ed8\u8ba4\u503c\u3002\r\n- `HttpCode._400` \u9519\u8bef\u7684\u8bf7\u6c42\uff0c\u6bd4\u5982\u53c2\u6570\u9519\u8bef\r\n- `HttpCode._403` \u7981\u6b62\u8bbf\u95ee\u7684\u8d44\u6e90\r\n- `HttpCode._404` \u672a\u627e\u5230\r\n- `HttpCode._422` \u5e38\u7528\u4e8e\u53c2\u6570\u9519\u8bef\r\n- `HttpCode._500` \u670d\u52a1\u5668\u9519\u8bef\uff0c\u5185\u90e8\u9519\u8bef\r\n- `HttpCode._522` \u670d\u52a1\u5668\u9519\u8bef\uff0c\u5185\u90e8\u9519\u8bef\r\n- `HttpCode._523` \u670d\u52a1\u5668\u9519\u8bef\uff0c\u5185\u90e8\u9519\u8bef,\u5df2\u77e5\r\n  \r\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "AMOS common api",
    "version": "1.0.8",
    "project_urls": {
        "Homepage": "https://github.com/aptray/python-pkg"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c3838d6ae852087b7698e959db2a27eea0f8ef1549f1a788baeb8cb4c09d1405",
                "md5": "49563f987e4285b9b5f5c0aa1abc4237",
                "sha256": "28d778bc307994707eb63473c32e09c27b9e38f0d3e2ba91b967020563c04d56"
            },
            "downloads": -1,
            "filename": "amos_api-1.0.8-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "49563f987e4285b9b5f5c0aa1abc4237",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3",
            "size": 3297,
            "upload_time": "2024-10-11T03:28:46",
            "upload_time_iso_8601": "2024-10-11T03:28:46.366368Z",
            "url": "https://files.pythonhosted.org/packages/c3/83/8d6ae852087b7698e959db2a27eea0f8ef1549f1a788baeb8cb4c09d1405/amos_api-1.0.8-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4a984eff152ae933de115d8d3ecbb88fc2079f5f65a65800599aa08a2587b1ab",
                "md5": "bfb63573702d306c904088e3d40a1cf7",
                "sha256": "ab370c094d6ca4194c28b91dfe25d1d9afc20339353f9aa776c1a076bf8e4ebe"
            },
            "downloads": -1,
            "filename": "amos_api-1.0.8.tar.gz",
            "has_sig": false,
            "md5_digest": "bfb63573702d306c904088e3d40a1cf7",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3",
            "size": 3189,
            "upload_time": "2024-10-11T03:28:48",
            "upload_time_iso_8601": "2024-10-11T03:28:48.206128Z",
            "url": "https://files.pythonhosted.org/packages/4a/98/4eff152ae933de115d8d3ecbb88fc2079f5f65a65800599aa08a2587b1ab/amos_api-1.0.8.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-11 03:28:48",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "aptray",
    "github_project": "python-pkg",
    "github_not_found": true,
    "lcname": "amos-api"
}
        
Elapsed time: 0.52856s