0
0
Rest APIprogramming~30 mins

Media type versioning in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
Media Type Versioning in REST API
📖 Scenario: You are building a simple REST API for a book store. You want to support versioning of your API using media type versioning. This means clients specify the API version in the Accept header using custom media types.For example, application/vnd.bookstore.v1+json for version 1 and application/vnd.bookstore.v2+json for version 2.
🎯 Goal: Create a basic REST API endpoint that returns book data. Implement media type versioning so the API returns different responses depending on the version specified in the Accept header.
📋 What You'll Learn
Create a dictionary called books with two entries: "id": 1, "title": "The Great Gatsby" and "id": 2, "title": "1984".
Create a variable called supported_versions that lists the supported media types for version 1 and version 2.
Write a function called get_books that takes a media_type string and returns the book data formatted differently depending on the version in the media type.
Print the output of get_books when called with "application/vnd.bookstore.v1+json" and "application/vnd.bookstore.v2+json".
💡 Why This Matters
🌍 Real World
Media type versioning helps APIs evolve without breaking existing clients by letting them request specific versions.
💼 Career
Understanding API versioning is important for backend developers and API designers to maintain and improve web services safely.
Progress0 / 4 steps
1
DATA SETUP: Create the books dictionary
Create a dictionary called books with these exact entries: {1: {"id": 1, "title": "The Great Gatsby"}, 2: {"id": 2, "title": "1984"}}.
Rest API
Need a hint?

Use a dictionary with keys 1 and 2. Each key maps to another dictionary with keys id and title.

2
CONFIGURATION: Define supported media type versions
Create a list called supported_versions containing the strings "application/vnd.bookstore.v1+json" and "application/vnd.bookstore.v2+json".
Rest API
Need a hint?

Use a list with the two exact media type strings for version 1 and version 2.

3
CORE LOGIC: Write the get_books function with media type versioning
Write a function called get_books that takes a parameter media_type. If media_type is "application/vnd.bookstore.v1+json", return a list of book titles. If it is "application/vnd.bookstore.v2+json", return the full book dictionaries as a list. Use the books dictionary defined earlier.
Rest API
Need a hint?

Use an if statement to check the media_type. Return a list of titles for v1, and full book dictionaries for v2.

4
OUTPUT: Print the results for both media types
Print the result of calling get_books with "application/vnd.bookstore.v1+json" and then with "application/vnd.bookstore.v2+json".
Rest API
Need a hint?

Use two print statements to show the output for each media type version.