Practice/Netflix/Implement CRUD REST APIs
CodingMust
You need to implement a simplified REST API handler that processes HTTP requests for managing user resources. Your system should support standard CRUD operations through appropriate HTTP methods and maintain an in-memory data store.
The API handler receives a sequence of operations, where each operation consists of an HTTP method, a path, and optional request data. You must process these operations in order and return the result of the final operation.
Your implementation should handle the following endpoints:
Example 1:
Input: [ ("POST", "/users", {"name": "Alice", "email": "alice@example.com"}), ("GET", "/users/1", {}) ] Output: {"id": 1, "name": "Alice", "email": "alice@example.com"} Explanation: First operation creates a user with ID 1. Second operation retrieves that user, which is returned as the final result.
Example 2:
Input: [ ("POST", "/users", {"name": "Bob", "email": "bob@test.com"}), ("PUT", "/users/1", {"email": "bob.updated@test.com"}), ("GET", "/users/1", {}) ] Output: {"id": 1, "name": "Bob", "email": "bob.updated@test.com"} Explanation: Create user, update email field, then retrieve the updated user.
Example 3:
Input: [ ("POST", "/users", {"name": "Alice", "email": "alice@test.com"}), ("POST", "/users", {"name": "Bob", "email": "bob@test.com"}), ("GET", "/users", {}) ] Output: [{"id": 1, "name": "Alice", "email": "alice@test.com"}, {"id": 2, "name": "Bob", "email": "bob@test.com"}] Explanation: Create two users, then retrieve all users as an array.