![]() I’m trying to add them to my existing Flask application now. I followed the Salesforce demo code that uses tabs to create the charts I have and they work perfectly by themselves (there’s index.py, app.py, and the. I have the following folder structure for my Flask application that uses Blueprints. def create_app(config_class =Config):Īpp. Register API Blueprint With The Factory Functionįinally, to complete the setup process, we can now register the API blueprint with the application factory function:Īpp/api/_init_.py: Register API blueprint with factory function #. We will provide an alternative way for clients who are not web browsers to log in.Īpp/api/tokens.py: API authentication module def get_token(): This is where the authentication subsystem is going to be defined. ![]() ![]() To begin, let us add the following:Īpp/api/errors.py: Deal with error responses def bad_request(): We can define a few helper functions to deal with error responses. Similarly, when interacting with a post (say to update or delete one), a logged-in user can only do so to posts they have authored. For example, in the POST method (while working with posts APIs), we need to ensure that a post is made by a logged-in user identified by their id. Something about the posts APIs is that the posts have to be linked to a user. The API endpoints for posts are going to be as follows (skeleton):Īpp/api/posts.py: Posts API resources from app.api import. To begin with the app/api/users.py module, these are the endpoints we are going to create and work with: The gist of our API is going to be in the app/api/users.py and app/api/posts.py modules. (venv)app $ touch users errors tokens posts They are imported at the bottom to avoid circular dependency errors. The last line requires us to create the four files (also known as modules - they do not exist yet). Update this new file with the following initialization:Īpp/api/_init_.py: Initialize the API blueprint from flask import Blueprintįrom app.api import users, errors, tokens, posts # I am running this command from the application's root directoryĬreate a new file called _init_.py in the new directory: (venv) $ touch app /api /_init_. To create an api blueprint, let us start by creating an empty folder called api in the app/ sub-directory. Should I ever want to scale this application, a new project structure utilizing blueprints would be suitable. Why use a blueprint? Blueprints are fantastic for larger applications. For example, instead of bundling everything in routes.py, we can choose to separate the logic into auth for authentication-related features or errors for error-related features. A blueprint is a logical structure that represents a subset of an application. ![]() In the linked basic chat application ( v1.0.0-basic-app above), no blueprints have been used.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |