Skip to main content

AMOS common api

Project description

amos api

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

快速开始

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 服务器错误,内部错误,已知

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

amos_api-1.0.9.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

amos_api-1.0.9-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file amos_api-1.0.9.tar.gz.

File metadata

  • Download URL: amos_api-1.0.9.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for amos_api-1.0.9.tar.gz
Algorithm Hash digest
SHA256 d9fbec294bf0163799bbf79068fa197b942d0176759d6eb60b402713b6ab8d5d
MD5 404240f9a6c9ad1b7f78295fe12a2859
BLAKE2b-256 c695a112de51e96521585e784b11310a23e196885203f9544e49fd998cda07ce

See more details on using hashes here.

File details

Details for the file amos_api-1.0.9-py3-none-any.whl.

File metadata

  • Download URL: amos_api-1.0.9-py3-none-any.whl
  • Upload date:
  • Size: 3.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for amos_api-1.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 eae29b5501ce6d8c5eece27688d651d73e879d4506716bcb3d597b642d3073a4
MD5 ffd8b2e6d88c963a3faa7731b2458789
BLAKE2b-256 4d6c86539bf4839d3a92dabbad5d53f78412eb09ac75c8f95fcc448d3a09015b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page