Restful Routing - A Flask API Example

WBOY
Release: 2024-08-23 06:02:31
Original
601 people have browsed it

Restful Routing - A Flask API Example

Restful routing is the push to make routing consistent though all different applications. REST is Representational State Transfer. It uses HTTP in a consistent, human-readable, machine-readable way.

The standards are GET, POST, PATCH, PUT, and DELETE.

Below will give an example of a couple of the restful routes in a flask API database to get/give the information from/to the front and perform the required action.

An example of a GET for Users in the server side using flask is using the below code.

First you will also need to import these items. The db import will be used later for the DELETE example.

from flask import request (*Used for POST and PATCH*)
from config import db, api, app 

from flask_restful import Resource
from models.user import User
Copy after login

Your config file should be set up like below for the imports to work.

 db = SQLAlchemy(app=app, metadata=metadata)
 api = Api(app=app)
Copy after login

The GET code in the User route:

 class UsersResource(Resource):
     def get(self):
        users = [user.to_dict() for user in User.query.all()]
        return users, 200

 api.add_resource(UsersResource, "/api/users", endpoint="users")
Copy after login

To break down this code.

You will need to create a class for the Users Resource and put in the Resource as an argument.

Next create a function for the get. The function will query the User table then create a list of all the users converting them to dictionaries for transfer so that they can be visible on the webpage as JSON. Then you return the list as well as a status code of 200 that the request was successful.

Lastly you will need to create the resource. Name the resource you are using as well as the path and endpoint. /api is added in front of the path so that the hosting website can discern from the front and backend route.

For DELETE you will have to create another resource for a single user delete. See the code below:

 class UserResource(Resource):
     def delete(self, id):
         user= User.query.get(id)
         db.session.delete(user)
         db.session.commit()
         return {}, 204

 api.add_resource(UserResource, "/api/user/<int:id>", 
 endpoint="user")
Copy after login

To further explain what is happening in the delete that is different from the get is a few things. First it is just deleting one user so you will need to create a new resource. It is set up similarly but not plural.

Then you create the delete function. It will need two arguments as you need to pass in the id that was sent from the front end to determine which user to delete. Then you will use db session delete and commit to update the database. After that you return an empty response as there is nothing to send back and the status for a delete (204).

Lastly you need to create the resource. This time using the UserResource. The path will be different to be singular and have the id that was passed into the front end and the endpoint is also singular.

RESTful routing makes it so when switching between different applications there is a standard that everyone can follow for backend routing and know the paths that are standardized. Additionally it makes the paths cleaner and easier to read.

The above is the detailed content of Restful Routing - A Flask API Example. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!