Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Team Endpoints | Endpoints with Blueprints and MethodView
Professional Web API with Flask
course content

Contenido del Curso

Professional Web API with Flask

Professional Web API with Flask

1. Introduction to the Course
2. Database and Models
3. Flask-Smorest and Schemas
4. Endpoints with Blueprints and MethodView
5. Authentication with JWT
6. Deployment

book
Team Endpoints

From the previous chapter, we already know how to define GET views and use Blueprint decorators to link endpoints with specific schemas.

GET Method Implementation

The code becomes very concise, which has an advantage over function-based views. We return the TeamModel.query.get_or_404(team_id) command, passing the team_id. If there is no team with such an ID, a 404 error will be returned. Don't forget to import TeamModel and TeamSchema from the respective modules.

DELETE Method Implementation

Now, let's define the DELETE method. In this case, we don't need to link with schemas. We simply find an instance by a specific ID, delete it from the database, and save the new state of the database. We return only a message about the successful deletion of the team. Also, import db from our db module.

GET All and POST Methods

Next, let's define another GET method to return the list of all club teams and link it with TeamSchema using the Blueprint decorator. Then, move on to the POST method. First, attempt to create a new team instance from the information stored in the team_data dictionary, which our method receives via the @bpl.arguments() decorator with TeamSchema. This schema will validate and deserialize the received JSON information. If the information doesn't match the schema, or required fields are missing, or the type of information is incorrect, an error will occur.

PUT Method

Remember that we also defined a schema for editing or updating a team. Let's apply this schema in the new PUT request. We also apply the Blueprint decorator to the response with status code 200 and TeamSchema. Arguments for updating will be taken from TeamUpdateSchemaand imported from schemas.

Now, let's test the new functionality in Postman or Insomnia. We've covered the full CRUD functionality for our API. Ensure the requests are organized in a folder named Team. Test each request and ensure the functionality works as expected.

Now, we move on to the next endpoints for the User and Player models.

What happens if a team with a specific ID is not found in the PUT method?

What happens if a team with a specific ID is not found in the PUT method?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 5
We're sorry to hear that something went wrong. What happened?
some-alt