跳转到主要内容

Sticker是一个强大且无需模板的替代方案,用于编写您的Web API。

项目描述

Sticker

https://img.shields.io/pypi/v/sticker.svg https://img.shields.io/pypi/l/sticker.svg https://img.shields.io/pypi/pyversions/sticker.svg https://img.shields.io/github/contributors/rafaelcaricio/sticker.svg

编写无需模板的Python函数,并将它们用作您的API处理程序。Sticker允许您选择Flask、bottle.py、Sanic或Tornado作为应用程序运行时。

亮点:

编写起来很容易

您只需要一点Python。

def say_hello(params):
    return {"contents": "Hello World!"}

再加上一些API描述。

openapi: "3.0.0"
paths:
  /:
    get:
      operationId: hello.say_hello

现在是最有趣的部分,您可以选择您想要使用的Web框架。

使用Flask运行

from sticker import FlaskAPI
api = FlaskAPI('hello.yml')
api.get_app(__name__).run()

使用Bottle.py运行

from sticker import BottleAPI
api = BottleAPI('hello.yml')
api.run()

使用Sanic运行

from sticker import SanicAPI
api = SanicAPI('hello.yml')
api.get_app(__name__).run()

使用Tornado运行

from sticker import TornadoAPI
import tornado.ioloop
api = TornadoAPI('hello.yml')
api.get_app().listen(8888)
tornado.ioloop.IOLoop.current().start()

框架设置、验证、类型转换和模拟由Sticker在运行时处理。

安装

Sticker发布在PyPI上,因此您可以使用pip进行安装

$ pip install sticker

要求

Sticker 是为 Python >=3.6OpenAPI 3.0 开发的。本项目不支持 Python 2.7,也不计划添加。

文档

Sticker 是一个灵活的元框架,用于 Web API 的开发和执行。OpenAPI 3.0 标准用作 Sticker 驱动的 API 的描述格式。您提供 API 规范并选择 Sticker 的一个运行时,即可启动 Web 服务器。

在本文档中,我们将描述几种与 Sticker 一起编写代码的不同方法。

纯 Python 处理器

Sticker 支持使用纯 Python 函数作为处理器。您的代码将没有任何框架特定的模板代码,包括 Sticker 本身。这允许您根据需要在不同框架之间进行切换。Sticker 将负责组装您的代码、您的 API 和您选择的框架。

def myhandler(params):
    return {
        "content": f"Hello {params.get("name", "World")}!",
        "status": 200
    }

为纯 Python 处理器编写测试既简单又无需模板代码。

def test_myhandler():
    params = {
        "name": "John Doe"
    }
    response = myhandler(params)
    assert response["content"] == "Hello John Doe!"

如上面的示例所示,定义 API 处理函数不需要从 Sticker 导入。这仅仅是因为 Sticker 预期您的处理程序遵循代码约定。

API 处理函数的结构

请在此处编写。

响应

API 处理程序预期返回一个 Python 字典(dict)对象。返回的字典定义了响应的外观。字典中的所有键都是可选的。预期的键在下表中描述。

类型

描述

content

str

HTTP 请求的主体。不对此值进行处理/解析。值直接传递给所选框架。

json

Union[dict, List[dict]]

用于请求主体的 JSON 值。这是一个快捷方式,用于具有“Content-Type: application/json”头并使用所选框架最常见的方式序列化此值。

file

Union[IO[AnyStr], str]

要作为字节流返回的数据。这是一个快捷方式,用于具有“Content-Type: application/octet-stream”头。使用所选框架最常见的方式来流式传输文件。

redirect

str

要重定向的路径或完整 URL。这是一个快捷方式,用于具有“Location:”头和 HTTP 状态 301

status

int

要用于响应的 HTTP 状态码。此值将覆盖使用快捷键(“json”、“file”和“redirect”)时设置的任何默认状态码。

headers

Dict[str, str]

要用于响应的 HTTP 头。此值与快捷值合并,并具有优先级。

我们在此处提供了一些使用我们上面定义的 dict 的不同配置的示例,用于描述 API 处理程序的 HTTP 响应。实际生成的 HTTP 响应值将取决于所选的运行时框架。这些示例只是对预期的 HTTP 响应的最小说明。

当希望返回状态码 200 的“Hello world!”字符串时,可以使用“content”键。

def say_hello(params):
    return {"content": "Hello world!"}

生成的 HTTP 响应类似于

HTTP/1.1 200 OK
Content-Type: text/plain

Hello world!

当希望返回状态码 201 的 JSON 响应时,可以使用“json”键。

def create(params):
    data = {
        "id": "uhHuehuE",
        "value": "something"
    }
    return {"json": data, "status": 201}

生成的 HTTP 响应将类似于

HTTP/1.1 201 Created
Content-Type: application/json

{"id":"uhHuehuE","value":"something"}

使用“file”键返回文件内容。

def homepage(params):
    return {
        "file": open('templates/home.html', 'r'),
        "headers": {
            "Content-Type": "text/html"
        }
    }

HTTP 响应将类似于

HTTP/1.1 200 OK
Content-Type: text/html

<html><title>My homepage</title><body><h1>Welcome!</h1></body></html>

当需要重定向请求时,可以使用“redirect”键。

def old_endpoint(params):
    return {'redirect': '/new-path'}

HTTP 响应将类似于

HTTP/1.1 301 Moved Permanently
Location: https://example.com/new-path

在之前的示例中展示了“status”和“headers”键的使用。当设置时,“status”和“headers”键将覆盖使用快捷键(“json”、“file”和“redirect”)时设置的默认值。

错误处理

Sticker 预期您定义 API 返回的错误格式。错误处理程序可配置,并在端点的验证失败时被调用。

def error_handler(error):
    return {
        "content": {
            "error": error["message"]
        },
        "headers": {
            "Content-Type": "application/json"
        },
        "status_code": 400
    }

贡献

该贴纸是在Apache 2.0许可证下开发的,并向公众免费提供。我们很高兴接受贡献。

如何贡献

  1. 检查开放的问题或创建一个新问题以开始围绕功能想法或错误进行讨论。对于不熟悉代码库的人,有良好入门问题标签。

  2. 在GitHub上分叉存储库,以开始对master分支进行更改(或从它分支出来)。

  3. 编写一个测试,以证明错误已被修复或功能按预期工作。

  4. 发送一个拉取请求,并持续打扰维护者,直到它被合并并发布。 :) 确保将自己添加到AUTHORS中。

项目详情


下载文件

下载适合您的平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

sticker-0.0.5.tar.gz (11.9 kB 查看哈希)

上传时间

构建分布

sticker-0.0.5-py2.py3-none-any.whl (9.9 kB 查看哈希)

上传时间 Python 2 Python 3

支持者: