It is still very important to write a friendly API in Python
Xiaoshuai b The correct posture for learning python
Xiaoshuai wants to talk to you about RESTful API and how to use Python to quickly create your API for others to call.
You should call the API frequently, and you can quickly use the services provided by others through certain rules. For example, if you want to use face ID comparison, there is a corresponding API for you to use:
Then you can quickly implement it as long as you call this interface:
Return result:
The advantage of this is that we only need to provide an interface at the back end, and then whether it is a mobile phone, a PC, a WeChat applet, etc., the front end can implement specific functions through this interface, so that the front and back ends can not interfere with each other. ", separate deployment is fine.
So how do we use Python to implement API interfaces like this and provide services for others to use? What the hell is RESTful API?
Then the next step is:
Correct posture for learning python
What is RESTful API?
Although you can customize the API, you can't write it like this:
Often, conflicts between front-end and back-end programmers arise...
front end:
rear end:
Therefore, it is still very important to write a friendly API, and RESTful API is a popular and friendly style. It is mainly used to restrict the writing of API, so that others can feel clear, concise, clear at a glance, and easy to use.
like this:
The URI we define needs to be clear and easy to understand, for example:
https://api.fxxkpython.com/shop/products/list
so that we can understand at a glance that this is a list of requested product data.
Try to pass data objects and attributes through JSON or XML.
Use HTTP methods explicitly (for example, get, post, put, and delete).
Stateless, at the time of request, the server does not store the context state of the client.
How does Python create a RESTful API?
I believe you have a certain understanding of the Python web framework:
(From the video number: Learn the correct posture of python)
What I want to introduce to you today is a lightweight rest API framework written based on the Flask framework: FlaskRESTful, which is easy to use and similar in writing to Flask. You can also use it directly in the ORM layer of your project.
Let's take a look at the official examples and briefly demonstrate its use.
Import the corresponding library, define the class resources, in which you can use the get and put methods of http to define the interface, and then add resources through the api to easily implement an API interface:
Up and running:
access:
As I just said, you only need to define the resource class, and you can write multiple http get, put, post and other methods in it. For example, here you can use put to add data to the dictionary:
Here you can use the todo_id of the request path as a variable, and after running it, you can put the corresponding data in it:
Use the get method to obtain:
In add_resource, you can define multiple routes to point to the same resource:
access:
To verify the data type of the form, you can use reqparse to define the type:
In this way, when the user submits a non-int type, it will directly return an error message:
When your API wants to return an object, you may need to define the type of the parameters in the object, you can use fields and marshal_with to decorate, like this:
ok, the above is the common operation of FlaskRESTful, more parameter information can be viewed in the following link:
https://flask-restful.readthedocs.io/en/latest/
You can also try to develop a [personal plan todolist] in Python that we said last time and rewrite it with FlaskRESTful to experience the difference.
See you next time, peace!