Complete the code to import the FastAPI class.
from fastapi import [1] app = [1]()
The FastAPI class is imported from the fastapi module to create the app instance.
Complete the code to add query parameters for pagination: skip and limit.
@app.get('/items/') async def read_items(skip: int = [1], limit: int = 10): return {'skip': skip, 'limit': limit}
Setting skip default to 0 means start from the first item.
Fix the error in the code to correctly use the Query function for limit with a default and max value.
from fastapi import Query @app.get('/items/') async def read_items(limit: int = Query([1], le=100)): return {'limit': limit}
The default value for limit should be an integer, here 10, and le=100 limits max to 100.
Fill both blanks to create a paginated response with skip and limit parameters and return sliced items.
items = ['apple', 'banana', 'cherry', 'date', 'elderberry'] @app.get('/fruits/') async def get_fruits(skip: int = [1], limit: int = [2]): return items[skip:skip + limit]
Skip starts at 0 to begin from the first item, limit is 1 to return one item per page.
Fill all three blanks to implement cursor-based pagination using a query parameter 'cursor' and slicing the items list.
items = ['a', 'b', 'c', 'd', 'e', 'f'] @app.get('/letters/') async def get_letters(cursor: int = [1], limit: int = [2]): start = cursor if cursor >= 0 else 0 end = start + limit return {'items': items[start:end], 'next_cursor': [3] if end < len(items) else None}
Cursor starts at 0, limit is 3 items per page, next_cursor is cursor + limit for next page.