In this video, we aim to cover how to implement the Update operation from CRUD using Flask and SQLAlchemy as a RESTful API.
- [Instructor] Hi and welcome to the third section: Implementing Update and Delete through a RESTful API. In this section, we will learn how to implement Update and delete Code operations. What Hypermedia means and we will see how to implement pagination for our API. Now, we move on to the first video of this section that deals with implementing the Update code operation. By the end of the video, you will learn what HTTP web should be used for update operation and what HTTP response codes should be sent once the Update operation has been executed.
In this video, we are going to take a look at the characteristics of the Update code operation and we are going to review the Flask code which implements the implementation of this operation. The update operation is mapped through the HTTP PUT verb. The Update and the HTTP PUT methods are not saved operations. These are always changing data on the server side and they can change the state of the application, too. When implementing the Update operation, the HTTP PUT request but it normally contains the field of which we want to update in our entity.
There are two approaches when implementing the Update operation. The first approach is to save the data in the request body if that does not exist in the database. In the second case, the response should return a HTTP 201 error code and contain the excess path for the newly created entity in the HTTP headers. The second approach is more restrictive. In case you send the whole object to the API which does not exist in the database, this simply rejects it by saying there is nothing to update. I prefer this approach because it forces the API users to use the Create operation, covered in section two, for creating new entities in the database and to use Update for real data updates.
We should be attentive to always return meaningful HTTP Response codes. In the routes.py we define a new route for the slash API slash candidate slash ID which is handled by the Update candidate middleware method and is assigned to the HTTP PUT verb. The Update candidate method receives the IDS parameter. First, we build up a new dictionary from the properties sending the request body. As you can see, our API at the moment only supports the update of four parameters in the candidate entity.
Next, we look up the candidate which has the ID we got in the request URL. If we do not find an entry in the database with that ID, then we return a 204 no content response saying that the requested resource was not found. Otherwise we return the updated object. In our data provider service class, the update candidate meddled first tries to load the candidate with the given ID. If that is found, then it updates the four fields and updates the entity in the session.
Finally, it copies the changes to the database and reloads the candidate which is returned afterwards. Awesome! We made very good progress. We covered the source code and the theoretical part. Now, let's see the API in action. In Postman, I send the PUT request to the HTTP local host port 5000 slash API slash candidates and I specify five as the ID of the candidate. In the form data I specify four fields: first name, last name, email, and phone number. These are the fields which I want to update with my API.
When I press the send button, I can see that I got the response code of 200. This means that the server successfully processed our request and in the body of the response I can see the updated candidate details. In this video, we have checked what are the characteristics of the Update code operation, how this can be implemented in Flask, and we saw how our API in action is using Postman. In the next video, we are going to take a deeper look into the Delete code operation. Thank you for watching and see you in the next video.
Regardless of platform, you will need to build APIs to serve data between different client applications and endpoints. Good APIs are a necessity for web and mobile projects, especially with the modern, mobile-first approach to development. This course delivers the fundamental knowledge required to enable highly connected interactions between applications via RESTful web APIs. Follow along with Gergo Bogdan and learn how to build up and structure an effective web API that can be used by any client application accessing it over HTTP, using Flask, the Python microframework.
Find out how to implement CRUD operations using SQLAlchemy and MySQL as the data store. Get an understanding of how REST works relative to APIs, and learn how to test APIs written in Python with the support of Flask. Explore token-based authentication and find out how to store passwords securely in your database. Along the way, Gergo introduces best practices and design guidelines when building large applications.
- Setting up Flask
- Using Flask templates and routing
- Implementing CRUD operations
- Implementing pagination
- Configuring RESTful authentication
- HTTP caching
- Securing web APIs
- Testing the application