Composite Operations with Multiple Resources in REST API
📖 Scenario: You are building a simple REST API that manages a library system. The system has two resources: books and authors. Each book has a title and an author ID. Each author has a name.Sometimes, you want to perform multiple operations together, like adding a new author and then adding a book for that author in one go. This is called a composite operation.
🎯 Goal: Build a simple REST API that supports composite operations to create an author and a book together in one request.
📋 What You'll Learn
Create an in-memory data store for authors and books.
Add a configuration variable to track the next available ID for authors and books.
Implement a composite operation endpoint that creates an author and a book in one request.
Print the final state of authors and books after the composite operation.
💡 Why This Matters
🌍 Real World
Composite operations are common in real-world APIs where multiple related resources need to be created or updated together to keep data consistent.
💼 Career
Understanding composite operations helps in backend development, API design, and working with microservices where multiple resources interact.
Progress0 / 4 steps
1
DATA SETUP: Create in-memory data stores for authors and books
Create two empty dictionaries called authors and books to store authors and books respectively.
Rest API
Hint
Use empty curly braces {} to create empty dictionaries.
2
CONFIGURATION: Add ID counters for authors and books
Create two variables called next_author_id and next_book_id and set both to 1 to track the next available IDs.
Rest API
Hint
Use simple integer variables to keep track of IDs.
3
CORE LOGIC: Implement composite operation to add author and book
Write a function called add_author_and_book that takes author_name and book_title as parameters. Inside the function, use the global variables next_author_id and next_book_id. Add the author to authors with the next author ID, then add the book to books with the next book ID and link it to the author ID. Increment both ID counters by 1. Return a tuple with the new author ID and book ID.
Rest API
Hint
Remember to use global to modify the ID counters inside the function.
4
OUTPUT: Call the composite operation and print authors and books
Call add_author_and_book with "Jane Austen" as author_name and "Pride and Prejudice" as book_title. Then print the authors and books dictionaries.
Rest API
Hint
Call the function with the exact strings and then print both dictionaries.
Practice
(1/5)
1. What is the main benefit of using composite operations in REST APIs?
easy
A. They combine multiple API calls into a single request to save time and keep data consistent.
B. They allow you to write SQL queries inside REST endpoints.
C. They automatically generate API documentation.
D. They encrypt all API responses for security.
Solution
Step 1: Understand what composite operations do
Composite operations bundle several API calls into one request to reduce network overhead.
Step 2: Identify the main benefit
This bundling saves time and ensures related data changes happen together, keeping consistency.
Final Answer:
They combine multiple API calls into a single request to save time and keep data consistent. -> Option A
Hint: Composite means combining multiple calls into one [OK]
Common Mistakes:
Confusing composite operations with documentation tools
Thinking composite operations encrypt data
Assuming composite operations run SQL queries
2. Which of the following is the correct way to structure a composite operation request in JSON?
easy
A. [{"method": "DELETE", "path": "/items/1"}]
B. {"operation": "GET", "url": "/users"}
C. {"requests": {"method": "POST", "endpoint": "/orders"}}
D. {"operations": [{"method": "POST", "path": "/users"}, {"method": "PUT", "path": "/accounts/123"}]}
Solution
Step 1: Identify the correct JSON structure for composite operations
Composite operations usually use an "operations" array with each operation having "method" and "path" keys.
Step 2: Compare options
{"operations": [{"method": "POST", "path": "/users"}, {"method": "PUT", "path": "/accounts/123"}]} matches this structure exactly, while others use incorrect keys or formats.
But the API returns an error saying "Invalid reference ${1.id}". What is the likely cause?
medium
A. The syntax for referencing previous results should be ${0.id}, not ${1.id}.
B. The first operation did not return an ID, so ${1.id} is invalid.
C. PUT method cannot be used in composite operations.
D. The inventory path should not include dynamic IDs.
Solution
Step 1: Check the reference usage
The error "Invalid reference ${1.id}" means the first operation's response does not contain an "id" field.
Step 2: Understand why this causes error
If the first operation's response lacks "id", the reference ${1.id} is invalid and causes failure.
Final Answer:
The first operation did not return an ID, so ${1.id} is invalid. -> Option B
Quick Check:
Reference error = missing id in first operation response [OK]
Hint: Check if referenced operation returns the expected field [OK]
Common Mistakes:
Assuming index starts at 0 instead of 1
Thinking PUT is disallowed in composite operations
Ignoring that referenced fields must exist in response
5. You want to create a composite operation that creates a customer, then creates two orders for that customer, and finally updates the customer's status to "active". Which JSON structure correctly represents this multi-resource composite operation?