0
0
Flaskframework~10 mins

API versioning with blueprints in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a Flask blueprint named 'v1'.

Flask
from flask import Blueprint

api_v1 = Blueprint('v1', __name__, url_prefix='/api/v1')

@api_v1.route('/hello')
def hello():
    return 'Hello from version 1!'

app.register_blueprint([1])
Drag options to blanks, or click blank then click option'
Aapi_v1
Bapi_v2
Capp
Dblueprint
Attempts:
3 left
💡 Hint
Common Mistakes
Using the app object instead of the blueprint in register_blueprint.
Confusing blueprint names like api_v2 instead of api_v1.
2fill in blank
medium

Complete the code to create a second blueprint for API version 2 with prefix '/api/v2'.

Flask
api_v2 = Blueprint('v2', __name__, url_prefix=[1])

@api_v2.route('/hello')
def hello_v2():
    return 'Hello from version 2!'

app.register_blueprint(api_v2)
Drag options to blanks, or click blank then click option'
A'/api/v2'
B'/v2/api'
C'/api/version2'
D'/api/v1'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong URL prefix that does not match the version.
Forgetting the quotes around the string prefix.
3fill in blank
hard

Fix the error in the route decorator to correctly define a GET endpoint in the blueprint.

Flask
@api_v1.route('/status', methods=[1])
def status():
    return {'status': 'ok'}
Drag options to blanks, or click blank then click option'
A'GET'
B['POST']
C['GET']
DGET
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string without brackets like 'GET' instead of ['GET'].
Using the wrong HTTP method like 'POST' when GET is needed.
4fill in blank
hard

Fill both blanks to register blueprints for v1 and v2 with the Flask app.

Flask
app = Flask(__name__)

app.[1]_blueprint(api_v1)
app.[2]_blueprint(api_v2)
Drag options to blanks, or click blank then click option'
Aregister
Badd
Dattach
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like add_blueprint or attach_blueprint.
Using different methods for each blueprint.
5fill in blank
hard

Fill all three blanks to create a versioned API blueprint with a route that returns JSON.

Flask
from flask import Blueprint, jsonify

api_[1] = Blueprint('v[2]', __name__, url_prefix=[3])

@api_[1].route('/info')
def info():
    return jsonify({'version': 'v[2]', 'status': 'running'})
Drag options to blanks, or click blank then click option'
A1
B'/api/v1'
C2
D'/api/v2'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing version numbers between variable names and URL prefixes.
Forgetting quotes around the URL prefix string.