-
Notifications
You must be signed in to change notification settings - Fork 0
Flask
Flask is the Python library used for the backend API. The Flask application is created in app.py. app.py creates the Flask app, sets the configuration values, and registers the Blueprints
Blueprints are a Flask feature that allow the API to extend across Python files. If we didn't use Blueprints, then all Python code for API methods would need to be in the app.py file. Blueprints allow for better organization of the API code. For example, the API methods that pertain to authentication can be placed in the auth.py file using Blueprints.
- Create a new Python file
- Add the following imports (only the first import is actually required for a Blueprint)
from flask import Blueprint
from flask import request
from flask import make_response
from http import HTTPStatus- Create the Blueprint with the following Python code:
name_api = Blueprint('name_api', __name__). Make sure to replacenameinname_apiwith the name of the file (without the .py). For example, the auth.py file defines the Blueprint likeauth_api = Blueprint('auth_api', __name__) - Register the Blueprint in app.py with the following Python code:
from fileName import name_api
- Add the import in app.py with the following Python code:
from fileName import name_api. ReplacefileNamewith the name of the file (without the .py) and replacenameinname_apiwith thenamefrom Step 3. - Add the line of code to register the Blueprint with Flask:
app.register_blueprint(name_api, url_prefix='/prefix'). Replacenameinname_apiwith the name of the file (without the .py) from Step 3. Theurl_prefixis optional. Theurl_prefixwill be required before the methods in the Blueprint. For example, if auth.py has a method called login and has the prefix of "/auth", then the URL must be http://localhost:5000/auth/login. If no prefix is specified, then the URL will be http://localhost:5000/login.
- Create a method for the API
- Add an annotation to the method like the following:
@name_api.route('/url', methods = ['POST']). Replacename_apiwith the name of the Blueprint (See Step 3 in Creating a Blueprint. Replace '/url' with the path for the API method. Updatemethodswith the desired HTTP methods the API call will take.
Cross-origin resource sharing (CORS) is a security HTTP Header that restricts who can access resources for a given domain. Without including this header, browsers will prevent example.com from accessing resources from service.example.com. For the API, we want any domain to be able to call the API. Thus, the CORS HTTP Header will need to be added to every response letting the browser know its ok for any domain to call our API. The flask_cors Python library does just that. It adds the appropriate HTTP CORS header to every response to allow any domain to call the API. A call to CORS(app) in app.py sets up the CORS header for every request.
Here is a link to a flask document that is still in progress. I making notes here is the link to the doc: https://docs.google.com/document/d/18_ONEKNqEBTvAebX3rGSeVx6X-EXiUpmYvFvinwNRx0/edit?usp=sharing