0
0
Rest APIprogramming~15 mins

Hierarchical resource paths in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
Hierarchical Resource Paths in REST API
📖 Scenario: You are building a simple REST API for a library system. The system has books and each book has chapters. You want to create hierarchical resource paths to access chapters of specific books.
🎯 Goal: Build a REST API with hierarchical resource paths to get chapters of a specific book using URL paths like /books/{book_id}/chapters/{chapter_id}.
📋 What You'll Learn
Create a dictionary called library with books and their chapters
Create a variable called book_id to select a specific book
Use a hierarchical path string to access a chapter of the selected book
Print the chapter title using the hierarchical resource path
💡 Why This Matters
🌍 Real World
Hierarchical resource paths are used in REST APIs to organize and access nested resources like books and chapters.
💼 Career
Understanding hierarchical paths helps in designing clear and scalable APIs for web services.
Progress0 / 4 steps
1
Create the library data structure
Create a dictionary called library with these exact entries: "book1" with chapters {"chapter1": "Introduction", "chapter2": "Basics"} and "book2" with chapters {"chapter1": "Getting Started", "chapter2": "Advanced Topics"}.
Rest API
Need a hint?
Think of library as a dictionary where each book ID maps to another dictionary of chapters.
2
Select a book using a variable
Create a variable called book_id and set it to the string "book1" to select the first book.
Rest API
Need a hint?
Use a simple string variable to hold the book ID.
3
Access a chapter using hierarchical resource path
Create a variable called chapter_title that accesses the chapter with key "chapter2" inside the book selected by book_id from the library dictionary.
Rest API
Need a hint?
Use two square brackets to access nested dictionaries: first for book, second for chapter.
4
Print the chapter title
Write a print statement to display the value of chapter_title.
Rest API
Need a hint?
Use print() to show the chapter title on the screen.