繁体   English   中英

速率限制 REST API 与连接和 swagger

[英]Rate limit REST API made with connexion and swagger

我正在使用 Flask 和连接构建 REST API。 (Python)

我正在使用包含所有端点、方法等定义的 swagger.yml 文件将 api 添加到连接应用程序...

问题是,如何为特定资源/路由/调用添加速率限制?

我似乎无法在文档中找到它。

谢谢。

您可以使用 X-Rate-Limit-* HTTP 标头以及 http 429 状态代码。

这实际上看起来像在 openapi 中:

  ....
  responses:
    "200":
      description: Success response
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/YourResponseModel"
      headers:
        "X-Rate-Limit-Limit": {
          "description": "The number of allowed requests in the current period",
          "schema": {
            "type": "integer"
          }
        } ,
        "X-Rate-Limit-Remaining": {
          "description": "The number of remaining requests in the current period",
          "schema": {
            "type": "integer"
          }
        },
        "X-Rate-Limit-Reset": {
         "description": "The number of seconds left in the current period",
         "schema": {
           "type": "integer"
         }
      }
    "429": 
      description: Too many requests
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/ErrorMessageResponse"

  ....

Connexion 是 flask 应用程序,与 flask 一起使用的许多技术都与 connexion 一起使用。

我们成功地使用了Flask-Limter来进行速率限制。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM