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

Cursusinhoud

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
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.

# 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.

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.

@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

@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

@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.

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 4. Hoofdstuk 6

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

course content

Cursusinhoud

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
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.

# 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.

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.

@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

@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

@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.

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 4. Hoofdstuk 6
some-alt