0
0
Rest APIprogramming~30 mins

Related resource links in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
Related Resource Links in REST API
📖 Scenario: You are building a simple REST API that returns information about books. Each book should include links to related resources, such as the author details and reviews.
🎯 Goal: Create a Python dictionary representing a book with related resource links. Then, add a configuration for the base URL. Use dictionary comprehension to build full URLs for related resources. Finally, print the complete book data with related links.
📋 What You'll Learn
Create a dictionary called book with keys id, title, and related_links.
Create a variable called base_url with the value "https://api.example.com".
Use dictionary comprehension to update related_links with full URLs using base_url.
Print the book dictionary.
💡 Why This Matters
🌍 Real World
APIs often provide related resource links so clients can easily find connected information like authors or reviews.
💼 Career
Understanding how to structure and build related resource links is important for backend developers working with REST APIs.
Progress0 / 4 steps
1
Create the initial book dictionary
Create a dictionary called book with these exact keys and values: 'id': 101, 'title': 'Learn REST APIs', and 'related_links' which is another dictionary with keys 'author' and 'reviews' having values '/authors/5' and '/books/101/reviews' respectively.
Rest API
Need a hint?

Remember to create a dictionary named book with the exact keys and values given.

2
Add the base URL configuration
Create a variable called base_url and set it to the string "https://api.example.com".
Rest API
Need a hint?

Use a simple assignment to create base_url with the exact string.

3
Build full URLs for related links
Use dictionary comprehension to update book['related_links'] so that each path is prefixed with base_url. Use {key: base_url + path for key, path in book['related_links'].items()} and assign it back to book['related_links'].
Rest API
Need a hint?

Use dictionary comprehension to combine base_url with each path in related_links.

4
Print the complete book dictionary
Write a print statement to display the book dictionary.
Rest API
Need a hint?

Use print(book) to show the final dictionary with full URLs.