📖 Scenario: You are building a simple REST API for a library system. The system manages books as resources. Each book has a unique ID, a title, and an author.We will create the data structure for books, configure a filter for books by author, implement the core logic to select books by that author, and finally output the filtered list.
🎯 Goal: Build a REST API endpoint that returns a list of books filtered by a given author using resource-based design thinking.
📋 What You'll Learn
Create a dictionary called books with book IDs as keys and dictionaries with title and author as values
Create a variable called filter_author to hold the author name to filter by
Use a dictionary comprehension to create filtered_books containing only books by filter_author
Print the filtered_books dictionary as the output
💡 Why This Matters
🌍 Real World
Filtering resources by attributes is a common task in REST APIs, such as listing books by author or products by category.
💼 Career
Understanding resource-based design thinking and filtering is essential for backend developers building APIs that serve dynamic data based on client requests.
Progress0 / 4 steps
1
DATA SETUP: Create the books dictionary
Create a dictionary called books with these exact entries: 1: {'title': 'The Hobbit', 'author': 'J.R.R. Tolkien'}, 2: {'title': '1984', 'author': 'George Orwell'}, 3: {'title': 'The Silmarillion', 'author': 'J.R.R. Tolkien'}
Rest API
Hint
Remember to use the exact keys and values as shown, with book IDs as keys and dictionaries as values.
2
CONFIGURATION: Set the filter author
Create a variable called filter_author and set it to the string 'J.R.R. Tolkien' to filter books by this author
Rest API
Hint
Make sure the variable name is exactly filter_author and the value is the exact string.
3
CORE LOGIC: Filter books by author
Use a dictionary comprehension to create a new dictionary called filtered_books that contains only the books from books where the author matches filter_author
Rest API
Hint
Use a dictionary comprehension with the condition to check the author matches filter_author.
4
OUTPUT: Print the filtered books
Write a print statement to display the filtered_books dictionary
Rest API
Hint
Use print(filtered_books) to show the filtered dictionary.
Practice
(1/5)
1. What is the main idea behind resource-based design thinking in REST APIs?
easy
A. Focusing only on database tables
B. Using only POST requests for all actions
C. Designing APIs without any structure
D. Organizing APIs around things like users or products
Solution
Step 1: Understand the concept of resource-based design
Resource-based design means APIs are built around resources, which are things like users, products, or orders.
Step 2: Compare options to the concept
Only Organizing APIs around things like users or products correctly describes organizing APIs around things (resources). Other options describe incorrect or unrelated ideas.
Final Answer:
Organizing APIs around things like users or products -> Option D
Quick Check:
Resource-based design = Organizing APIs around resources [OK]
Hint: Think of APIs as managing 'things' like users or products [OK]
Common Mistakes:
Confusing resource-based design with database design
Thinking all requests must be POST
Ignoring the concept of resources
2. Which HTTP method is typically used to update an existing resource in a REST API?
easy
A. PUT
B. POST
C. GET
D. DELETE
Solution
Step 1: Recall HTTP methods and their common uses
GET retrieves data, POST creates new data, PUT updates existing data, DELETE removes data.
Step 2: Identify the method for updating
PUT is the standard method used to update an existing resource in REST APIs.
Final Answer:
PUT -> Option A
Quick Check:
Update resource = PUT [OK]
Hint: PUT means replace or update a resource fully [OK]
Common Mistakes:
Using POST to update instead of create
Confusing GET with update
Using DELETE for updating
3. Given this REST API call: DELETE /products/123, what is the expected result?
medium
A. Retrieve product with ID 123
B. Create a new product with ID 123
C. Delete product with ID 123
D. Update product with ID 123
Solution
Step 1: Analyze the HTTP method and URL
DELETE method means remove. The URL targets the product resource with ID 123.
Step 2: Determine the action
DELETE /products/123 means delete the product with ID 123 from the system.
Final Answer:
Delete product with ID 123 -> Option C
Quick Check:
DELETE + resource ID = delete that resource [OK]
Hint: DELETE + resource ID means remove that resource [OK]
Common Mistakes:
Thinking DELETE retrieves data
Confusing DELETE with POST or PUT
Ignoring the resource ID in URL
4. You see this API request: POST /users/45. What is wrong with this request in resource-based design?
medium
A. The URL should be /users without ID for creating
B. POST cannot be used to create resources
C. The HTTP method should be PUT
D. POST must include a resource ID in the URL
Solution
Step 1: Understand POST usage in REST
POST is used to create new resources. The URL should point to the collection, not a specific resource ID.
Step 2: Analyze the request URL
Including an ID (like /users/45) with POST is incorrect because the server assigns IDs when creating new resources.
Final Answer:
The URL should be /users without ID for creating -> Option A
Quick Check:
POST to collection URL without ID = correct [OK]
Hint: POST creates; URL should be collection without ID [OK]
Common Mistakes:
Using POST with resource ID in URL
Thinking POST updates existing resource
Confusing POST and PUT URLs
5. You want to design an API to manage orders and their items. Which URL and HTTP method combination best follows resource-based design to update the quantity of item 7 in order 42?
hard
A. POST /orders/42/items with item ID and quantity in body
B. PUT /orders/42/items/7 with updated quantity in body
C. DELETE /orders/42/items/7 to update quantity
D. GET /orders/42/items/7 with quantity in query
Solution
Step 1: Identify the resource and action
The resource is the specific item (ID 7) inside order 42. Updating quantity means modifying this resource.
Step 2: Choose correct HTTP method and URL
PUT is used to update existing resources. The URL should point to the specific item resource: /orders/42/items/7.
Final Answer:
PUT /orders/42/items/7 with updated quantity in body -> Option B
Quick Check:
Update nested resource = PUT to specific item URL [OK]
Hint: Use PUT on specific nested resource URL to update it [OK]
Common Mistakes:
Using POST to update existing item
Using DELETE to update data
Using GET with query to change data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.