当前位置:网站首页>Support multiple API versions in flask

Support multiple API versions in flask

2022-07-06 22:04:00 javail

problem :

I started to design a RESTful webservice with Flask and Python and I'm wondering how one would support multiple API versions in the same project. I started using it Flask and Python To design a RESTful web service , I want to know how to support multiple in the same project API edition .I'm thinking of putting the requested API version in the URL like this: I'm considering the request API The version is in URL in , As shown below :

/myapp/v1/Users

After some time I want to add another endpoint in Version 1.1 of the API and keep everything from v1 which did not change: After a while , I want to API Of 1.1 Add another endpoint in version , And keep v1 Everything unchanged in :

/myapp/v1.1/Users   <= Same as in v1/myapp/v1.1/Books

In v2 the "Users"-endpoint is changed: stay v2 in ,“ user ” Endpoint changed :

/myapp/v2/Users   <= Changed in v2/myapp/v2/Books   <= Same as in v1.1

and so on... wait ...

Looking at this question the easiest way probably would be something like this: see This The simplest way to solve the problem may be like this :

@app.route('/<version>/users')def users(version):    # do something    return jsonify(response)

But I can imagine that this will get harder to maintain with each new API version. But I can imagine , With every new API edition , This will become more difficult to maintain .Therefore I was wondering if there's any better (= easier to maintain and better structured) way to achieve this with Flask? So I want to know if there is a better (= Easier to maintain and better structured ) Method to use Flask To achieve this goal ?


Solution :

Reference resources : https://stackoom.com/en/question/1wp2X
原网站

版权声明
本文为[javail]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131108055110.html