0
0
Flaskframework~20 mins

API versioning with blueprints in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
API Versioning Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Flask route versioning setup?

Given two blueprints registered with different URL prefixes for API versions, what URL will respond with 'Hello from v1'?

Flask
from flask import Flask, Blueprint

app = Flask(__name__)

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

@v1.route('/hello')
def hello_v1():
    return 'Hello from v1'

v2 = Blueprint('v2', __name__, url_prefix='/api/v2')

@v2.route('/hello')
def hello_v2():
    return 'Hello from v2'

app.register_blueprint(v1)
app.register_blueprint(v2)
AGET request to /hello returns 'Hello from v1'
BGET request to /api/v1/hello returns 'Hello from v1'
CGET request to /api/hello returns 'Hello from v1'
DGET request to /api/v2/hello returns 'Hello from v1'
Attempts:
2 left
💡 Hint

Think about how the url_prefix affects the route URL.

📝 Syntax
intermediate
2:00remaining
Which option correctly registers two versioned blueprints in Flask?

Choose the code snippet that correctly registers two blueprints v1 and v2 with URL prefixes /api/v1 and /api/v2 respectively.

A
app.register_blueprint(v1)
app.register_blueprint(v2)

v1.url_prefix = '/api/v1'
v2.url_prefix = '/api/v2'
B
app.register_blueprint(v1, url_prefix='/api/v1')
app.register_blueprint(v2, url_prefix='/api/v2')
C
v1 = Blueprint('v1', __name__)
v2 = Blueprint('v2', __name__)
app.register_blueprint(v1, url_prefix='/api/v1')
app.register_blueprint(v2, url_prefix='/api/v2')
D
v1 = Blueprint('v1', __name__, url_prefix='/api/v1')
v2 = Blueprint('v2', __name__, url_prefix='/api/v2')
app.register_blueprint(v1)
app.register_blueprint(v2)
Attempts:
2 left
💡 Hint

Remember where the url_prefix should be set: at blueprint creation or registration.

state_output
advanced
2:00remaining
What is the response when accessing an unregistered API version route?

Given blueprints registered for /api/v1 and /api/v2, what happens when a client requests /api/v3/hello?

Flask
from flask import Flask, Blueprint

app = Flask(__name__)

v1 = Blueprint('v1', __name__, url_prefix='/api/v1')
@v1.route('/hello')
def hello_v1():
    return 'Hello from v1'

v2 = Blueprint('v2', __name__, url_prefix='/api/v2')
@v2.route('/hello')
def hello_v2():
    return 'Hello from v2'

app.register_blueprint(v1)
app.register_blueprint(v2)
A404 Not Found error
B500 Internal Server Error
C'Hello from v1' response
D'Hello from v2' response
Attempts:
2 left
💡 Hint

Think about what happens if no route matches the requested URL.

🔧 Debug
advanced
2:00remaining
Why does this Flask app return 404 for /api/v1/hello despite blueprint registration?

Consider this code snippet. The route /hello is defined inside blueprint v1 with prefix /api/v1. But accessing /api/v1/hello returns 404. What is the likely cause?

Flask
from flask import Flask, Blueprint

app = Flask(__name__)

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

@app.route('/hello')
def hello():
    return 'Hello from v1'

app.register_blueprint(v1)
AThe route is registered on app, not on blueprint v1
BThe blueprint url_prefix is incorrect
CThe blueprint was not registered
DThe route function name conflicts with blueprint name
Attempts:
2 left
💡 Hint

Check where the route decorator is applied.

🧠 Conceptual
expert
3:00remaining
How does Flask blueprint versioning help in API maintenance?

Why is using blueprints with URL prefixes for API versioning a good practice in Flask applications?

AIt merges all API versions into a single route for simplicity
BIt automatically upgrades old API versions to the latest without code changes
CIt allows separating code for different API versions cleanly and enables running multiple versions simultaneously
DIt forces all clients to use the newest API version only
Attempts:
2 left
💡 Hint

Think about how versioning affects code organization and client support.