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

book
User and Player Endpoints

User Endpoints

We will continue implementing functionality for our endpoints. Let's start with the User. Let's quickly define the GET methods and specify the schemas in the Blueprint response decorators.

python
# resource/user.py

@blp.route("/users/<int:user_id>")
class User(MethodView):
@blp.response(200, UserSchema)
def get(self, user_id):
return UserModel.query.get_or_404(user_id)


@blp.route("/users/")
class UserList(MethodView):
@blp.response(200, UserSchema(many=True))
def get(self):
return UserModel.query.all()

Player Endpoints

Now let's move on to Player. We quickly define the GET methods and import models and schema in the resources/player.py.

python
blp = Blueprint("players", __name__, description="Operations on the players")

@blp.route("/players/<int:player_id>")
class Player(MethodView):
@blp.response(200, PlayerSchema)
def get(self, player_id):
return PlayerModel.query.get_or_404(player_id)


@blp.route("/players/")
class PlayerList(MethodView):
@blp.response(200, PlayerSchema(many=True))
def get(self):
return PlayerModel.query.all()

DELETE Method

let's handle the DELETE method. We literally repeat the functionality from the TeamView.

python
@blp.route("/players/<int:player_id>")
class Player(MethodView):
...

def delete(self, player_id):
player = PlayerModel.query.get_or_404(player_id)
db.session.delete(player)
db.session.commit()
return {"message": "Player deleted"}

CREATE Method

python
@blp.route("/players/")
class PlayerList(MethodView):
...

@blp.arguments(PlayerSchema)
@blp.response(201, PlayerSchema)
def post(self, player_data):
player = PlayerModel(**player_data)

try:
db.session.add(player)
db.session.commit()
except IntegrityError as er:
abort(400, message=er)
except SQLAlchemyError as er:
abort(500, message=er)

return player

UPDATE Method

python
@blp.route("/players/<int:player_id>")
class Player(MethodView):
...

@blp.arguments(PlayerUpdateSchema)
@blp.response(200, PlayerSchema)
def put(self, player_data, player_id):
player = PlayerModel.query.get(player_id)

if player:
player.first_name = player_data["first_name"] or player.first_name
player.last_name = player_data["last_name"] or player.last_name
player.position = player_data["position"] or player.position
player.number = player_data["number"] or player.number
else:
player = PlayerModel(**player_data)

db.session.add(player)

db.session.commit()

return player

In the next chapter, we will get acquainted with new functionality for the quick and convenient creation of documentation for our API.

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 6

Pergunte à IA

expand
ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

some-alt