- Python API Development Fundamentals
- Jack Chan Ray Chung Jack Huang
- 401字
- 2021-06-11 12:56:26
Configuring Endpoints
Now that we have defined all our resources, we will set up some endpoints so that users can send requests to them. These endpoints can be accessed by the users and are connected to specific resources. We will be using the add_resource method on the API object to specify the URL for these endpoints and route the client HTTP request to our resources.
For example, the api.add_resource(RecipeListResource, '/recipes') syntax is used to link the route (relative URL path) to RecipeListResource so that HTTP requests will be directed to this resource. Depending on the HTTP verb (for example, GET, and POST), the request will be handled by the corresponding methods in the resource accordingly.
Exercise 10: Creating the Main Application File
In this exercise, we will create our app.py file, which will be our main application file. We will set up Flask and initialize our flask_restful.API there. Finally, we will set up the endpoints so that users can send requests to our backend services. Let's get started:
- Create the app.py file under the project folder.
- Import the necessary classes using the following code:
from flask import Flask
from flask_restful import Api
from resources.recipe import RecipeListResource, RecipeResource, RecipePublishResource
- Set up Flask and initialize flask_restful.API with our Flask app:
app = Flask(__name__)
api = Api(app)
- Add resource routing by passing in the URL so that it will route to our resources. Each resource will have its own HTTP method defined:
api.add_resource(RecipeListResource, '/recipes')
api.add_resource(RecipeResource, '/recipes/<int:recipe_id>')
api.add_resource(RecipePublishResource, '/recipes/<int:recipe_id>/publish')
if __name__ == '__main__':
app.run(port=5000, debug=True)
Note
In RecipeListResource, we have defined the get and post methods. So, when there is a GET HTTP request to the "/recipes" URL route, it will invoke the get method under RecipeListResource and get back all the published recipes.
In the preceding code, you will notice that we have used <int: recipe_id > in the code. It is there as a placeholder for the recipe ID. When a GET HTTP request has been sent to the route "/recipes/2" URL, this will invoke the get method under RecipeResource with a parameter, that is, recipe_id = 2.
- Save app.py and right-click on it to run the application. Flask will then start up and run on the localhost (127.0.0.1) at port 5000:

Figure 2.9: Flask started and running on localhost
Congratulations! You have completed the API endpoint. Now, let's move on to testing. You can either test it in curl/httpie or Postman.
- 數(shù)據(jù)通信網(wǎng)絡實踐:基礎知識與交換機技術
- FreeSWITCH 1.2
- 物聯(lián)網(wǎng)短距離無線通信技術應用與開發(fā)
- 物聯(lián)網(wǎng)安全:理論、實踐與創(chuàng)新
- Web Application Development with R Using Shiny
- 面向云平臺的物聯(lián)網(wǎng)多源異構信息融合方法
- Building RESTful Web services with Go
- 網(wǎng)絡環(huán)境中基于用戶視角的信息質量評價研究
- 從實踐中學習手機抓包與數(shù)據(jù)分析
- INSTANT LinkedIn Customization How-to
- 現(xiàn)場總線與工業(yè)以太網(wǎng)及其應用技術(第2版)
- 網(wǎng)絡攻防技術與實踐
- 5G時代邊緣計算:LF Edge生態(tài)與EdgeGallery技術詳解
- CCNP TSHOOT(642-832)學習指南
- 一本書讀懂24種互聯(lián)網(wǎng)思維