简体   繁体   English

速率限制 REST API 与连接和 swagger

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

I'm building a REST API using Flask and connexion.我正在使用 Flask 和连接构建 REST API。 (Python) (Python)

I'm adding the api to the connexion app using a swagger.yml file that contains the definitions of all the endpoints, methods, etc...我正在使用包含所有端点、方法等定义的 swagger.yml 文件将 api 添加到连接应用程序...

The question is, how can I add a rate limit on a specific resource/route/call?问题是,如何为特定资源/路由/调用添加速率限制?

I can't seem to find it in the documentation.我似乎无法在文档中找到它。

Thanks.谢谢。

You could use the X-Rate-Limit-* HTTP headers along with the http 429 status code.您可以使用 X-Rate-Limit-* HTTP 标头以及 http 429 状态代码。

This practically looks like in openapi:这实际上看起来像在 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 is a flask application so many techniques that work with flask work with connexion. Connexion 是 flask 应用程序,与 flask 一起使用的许多技术都与 connexion 一起使用。

We successfully used Flask-Limter to do rate limiting.我们成功地使用了Flask-Limter来进行速率限制。

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

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