Custom Routes In Flask Using Flask Restful

chukwunazaekpere

Chukwunazaekpere

Posted on July 25, 2022

Custom Routes In Flask Using Flask Restful

In this short tutorial, I'll illustrate how we can use flask to write an API; with the following goals:
a) Defining custom routes
b) Separating routes, controllers, models, validators etc into their respective folders

I'll at a later time put up a post to aid us in understanding my style of architecture, when it comes to Flask.
Defining Custom Route
Let's take a hypothetical blog-site for instance. I'm assuming in your project you've got a users, comments and posts folder. The reason for this, bothers on the fact that you'd want to separate the models, routes, controllers etc for posts and users. Your users controllers would definitely concern, but not limited to; registration, login, forgot-password, change-email, etc. While those for posts would include edit-post, delete-post, create-post etc. You would agree with me that clustering this transactions in one file, would definitely wear you out, if not possibly, get you insane. FurhterMore, since Flask-Restful only supports the known HTTP-verbs, "GET", "POST" etc it'd as well not sound manageable, to define new classes for each post, delete etc operations respectively. One would think that to achieve a login and register transaction would require different classes as below (in your routes.py file);

routes.py
class LoginView(Resource):
    logging.basicConfig(level=logging.INFO)
    logging.info("Entered the LoginView...")

    def login(self):
        print("\n\t Login route....")

class RegisterView(Resource):
    logging.basicConfig(level=logging.INFO)
    logging.info("Entered the RegisterView...")

    def register(self):
        print("\n\t Register route....")
Enter fullscreen mode Exit fullscreen mode

This problem can simply be solved, pythonically, as below;

from flask_restful import (
    Resource,
    request
)
import logging

from ..models.models import Users
class AuthViews(Resource):
    logging.basicConfig(level=logging.INFO)
    logging.info("Entered the auth views...")

    def login(self):
        print("\n\t Login route....")


    def register(self):
        print("\n\t Register route....")


    def post(self):
        print("\n\t Login-Data: ", request.data)
        print("\n\t Login-Data: ", request.url)
        url = request.url
        if "login" in url:
            login = self.login()
        elif "register" in url:
            register = self.register()
        elif "forgot-password" in url:
            register = self.forgot_password()
        else:
            etc....

auth_routes = ["/auth/register", "/auth/login"]
Enter fullscreen mode Exit fullscreen mode

Then in your app.py file, you'd register your resource like so;

...
from routes.routes import AuthViews, auth_routes
app = Flask(__name__)
api = Api(app)
api.add_resource(AuthViews, *auth_routes)
Enter fullscreen mode Exit fullscreen mode

Thanks. Next, I'll show you how to define models, using PyMongo.

💖 💪 🙅 🚩
chukwunazaekpere
Chukwunazaekpere

Posted on July 25, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related