0
0
Rest APIprogramming~20 mins

Action links for state transitions in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
Action Links for State Transitions in a REST API
📖 Scenario: You are building a simple REST API for managing orders in an online store. Each order can be in different states like pending, processing, or shipped. To help clients know what actions they can take next, you want to include action links in the API response that show possible state transitions.
🎯 Goal: Create a REST API response that includes an order's current state and a dictionary of action_links showing URLs for allowed state transitions.
📋 What You'll Learn
Create a dictionary called order with keys id and state
Create a dictionary called state_transitions mapping states to possible next states
Generate a dictionary called action_links with URLs for each allowed next state
Print the order dictionary including the action_links dictionary
💡 Why This Matters
🌍 Real World
APIs often need to tell clients what actions are possible next. Action links help clients navigate state changes safely and clearly.
💼 Career
Understanding how to represent state transitions and action links is important for backend developers building REST APIs that are easy to use and maintain.
Progress0 / 4 steps
1
Create the initial order data
Create a dictionary called order with id set to 123 and state set to 'pending'.
Rest API
Need a hint?

Use curly braces to create a dictionary with keys 'id' and 'state'.

2
Define possible state transitions
Create a dictionary called state_transitions with these exact entries: 'pending' maps to ['processing'], 'processing' maps to ['shipped'], and 'shipped' maps to an empty list [].
Rest API
Need a hint?

Use a dictionary where each key is a state and the value is a list of next possible states.

3
Generate action links for allowed transitions
Create a dictionary called action_links where each key is a next state from state_transitions[order['state']] and each value is a URL string in the format f"/orders/{order['id']}/state/{next_state}".
Rest API
Need a hint?

Use a dictionary comprehension to build URLs for each next state.

4
Add action links to order and print the result
Add the action_links dictionary to the order dictionary under the key 'action_links'. Then print the order dictionary.
Rest API
Need a hint?

Use order['action_links'] = action_links and then print(order).