📖 Scenario: You are building a REST API for a bookstore. The API needs to handle different types of requests efficiently and cleanly. Using advanced design patterns helps organize your code, making it easier to maintain and extend as the bookstore grows.
🎯 Goal: Build a simple REST API simulation using advanced patterns like the Command pattern to handle different API actions. This will show how advanced patterns solve real problems by making the code modular and scalable.
📋 What You'll Learn
Create a dictionary called commands to map action names to functions
Define separate functions for list_books, add_book, and remove_book
Use a variable action to select which command to run
Use the command pattern to call the correct function based on action
Print the result of the command execution
💡 Why This Matters
🌍 Real World
Advanced patterns like the Command pattern help developers build APIs that are easy to update and add new features without breaking existing code.
💼 Career
Understanding and applying design patterns is a key skill for software developers working on scalable and maintainable backend systems.
Progress0 / 4 steps
1
DATA SETUP: Create initial book list
Create a list called books with these exact strings: '1984', 'Brave New World', 'Fahrenheit 451'.
Rest API
Hint
Use square brackets to create a list with the exact book titles.
2
CONFIGURATION: Define the action variable
Create a variable called action and set it to the string 'list'.
Rest API
Hint
Assign the string 'list' to the variable action.
3
CORE LOGIC: Define command functions and map them
Define three functions: list_books() that returns the books list, add_book() that adds 'The Handmaid's Tale' to books and returns the updated list, and remove_book() that removes '1984' from books and returns the updated list. Then create a dictionary called commands mapping 'list' to list_books, 'add' to add_book, and 'remove' to remove_book.
Rest API
Hint
Define each function exactly as described and map them in the commands dictionary.
4
OUTPUT: Execute the command and print the result
Use the action variable to get the correct function from commands and call it. Store the result in result. Then print result.
Rest API
Hint
Use commands[action]() to call the function and print the returned list.
Practice
(1/5)
1. Why do advanced REST API patterns like pagination and consistent error handling matter in real-world applications?
easy
A. They slow down the API response time significantly.
B. They add unnecessary complexity to simple APIs.
C. They make APIs easier to use, maintain, and scale.
D. They are only useful for small projects.
Solution
Step 1: Understand the purpose of advanced patterns
Advanced patterns like pagination and consistent error handling help organize API responses and interactions.
Step 2: Connect patterns to real-world benefits
These patterns improve usability, maintainability, and scalability of APIs in practical applications.
Final Answer:
They make APIs easier to use, maintain, and scale. -> Option C
Quick Check:
Advanced patterns improve API usability = They make APIs easier to use, maintain, and scale. [OK]
Hint: Think about how patterns help users and developers [OK]
Common Mistakes:
Assuming patterns only add complexity
Believing patterns slow down APIs
Thinking patterns are for small projects only
2. Which of the following is the correct way to indicate pagination in a REST API response header?
easy
A. X-Pagination-Page: 1
B. Pagination-Page: 1
C. Page-Number: 1
D. Page: 1
Solution
Step 1: Recall standard pagination header naming
Common practice uses custom headers starting with 'X-' like 'X-Pagination-Page' to indicate page number.
Step 2: Compare options to standard naming
Only 'X-Pagination-Page: 1' follows the common convention for pagination headers.
Final Answer:
X-Pagination-Page: 1 -> Option A
Quick Check:
Standard pagination header = X-Pagination-Page: 1 [OK]
Hint: Look for headers starting with 'X-' for custom metadata [OK]
Common Mistakes:
Using generic header names without 'X-' prefix
Confusing query parameters with headers
Assuming 'Page' alone is a header
3. Given this REST API response snippet for pagination:
Divide total by per_page: 5 / 2 = 2.5, round up to 3 pages.
Final Answer:
3 -> Option D
Quick Check:
5 items with 2 per page = 3 pages [OK]
Hint: Divide total items by per_page and round up [OK]
Common Mistakes:
Using integer division without rounding up
Confusing current page with total pages
Ignoring total count and using data length
4. You have this error response from a REST API:
{
"error": "Invalid ID",
"code": 400
}
But your client expects error details under the key "message" instead of "error". How can you fix this to follow consistent error patterns?
medium
A. Change the API to return {"message": "Invalid ID", "code": 400}
B. Change the client to look for "error" key instead of "message"
C. Ignore the error and retry the request
D. Remove the "code" key from the response
Solution
Step 1: Identify the inconsistency
The API returns error details under "error" but client expects "message" key.
Step 2: Fix the API response to match client expectation
Change the API to use "message" key for error details to keep consistent error format.
Final Answer:
Change the API to return {"message": "Invalid ID", "code": 400} -> Option A
Quick Check:
Consistent error keys = Use "message" key in API response [OK]
Hint: Match error keys between API and client [OK]
Common Mistakes:
Ignoring client expectations
Retrying without fixing error format
Removing important error code info
5. You want to design a REST API that returns a list of users with optional filtering by age and supports pagination. Which advanced pattern combination best solves this real problem?
hard
A. Use POST method to send filters in the body and ignore pagination.
B. Use query parameters for filters and pagination, and return consistent error messages.
C. Return all users in one response without filters or pagination.
D. Send filters as headers and paginate by splitting responses manually.
Solution
Step 1: Identify best practices for filtering and pagination
Filters are commonly passed as query parameters; pagination also uses query parameters for page and size.
Use query parameters for filters and pagination, and return consistent error messages. -> Option B
Quick Check:
Filters + pagination via query params + consistent errors = Use query parameters for filters and pagination, and return consistent error messages. [OK]
Hint: Use query params for filters and pagination [OK]
Common Mistakes:
Ignoring pagination causing large responses
Using POST instead of GET for filtering
Sending filters in headers which is non-standard
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.