Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Graceful Degradation in REST API
📖 Scenario: You are building a simple REST API for a weather service. Sometimes, the detailed weather data might not be available due to external service issues. You want your API to still respond with basic information instead of failing completely.
🎯 Goal: Create a REST API endpoint that tries to return detailed weather data. If detailed data is unavailable, it should gracefully degrade and return basic weather data instead.
📋 What You'll Learn
Create a dictionary called weather_data with keys 'temperature', 'humidity', and 'detailed_forecast' with exact values.
Create a boolean variable called is_detailed_available to simulate availability of detailed data.
Write a function called get_weather() that returns detailed data if available, otherwise returns basic data.
Print the result of calling get_weather().
💡 Why This Matters
🌍 Real World
APIs often depend on external services that may fail or be slow. Graceful degradation helps keep the API responsive by providing simpler data when full data is unavailable.
💼 Career
Understanding graceful degradation is important for backend developers and API designers to build reliable and user-friendly services.
Progress0 / 4 steps
1
DATA SETUP: Create the weather data dictionary
Create a dictionary called weather_data with these exact entries: 'temperature': 22, 'humidity': 60, and 'detailed_forecast': 'Sunny with light clouds'.
Rest API
Hint
Use curly braces to create a dictionary and separate keys and values with colons.
2
CONFIGURATION: Add a flag for detailed data availability
Create a boolean variable called is_detailed_available and set it to False to simulate that detailed data is not available.
Rest API
Hint
Use False to indicate the detailed data is not available.
3
CORE LOGIC: Write the function to return weather data with graceful degradation
Write a function called get_weather() that returns a dictionary with 'temperature' and 'humidity' always. If is_detailed_available is True, include 'detailed_forecast' from weather_data in the returned dictionary. Otherwise, return only the basic data.
Rest API
Hint
Use an if statement inside the function to check is_detailed_available and add the detailed forecast if true.
4
OUTPUT: Print the weather data returned by the function
Write a print statement to display the result of calling get_weather().
Rest API
Hint
Call get_weather() inside print() to show the output.
Practice
(1/5)
1. What is the main goal of graceful degradation in REST APIs?
easy
A. To keep the API working even if some parts fail
B. To stop the API immediately when an error occurs
C. To ignore all errors and continue without response
D. To make the API faster by skipping error checks
Solution
Step 1: Understand graceful degradation purpose
Graceful degradation means the system still works even if some parts fail.
Step 2: Compare options with this meaning
Only To keep the API working even if some parts fail matches this idea by keeping the API working despite failures.
Final Answer:
To keep the API working even if some parts fail -> Option A
Quick Check:
Graceful degradation = keep working despite failure [OK]
Hint: Graceful degradation means continue working despite errors [OK]
Common Mistakes:
Thinking it stops the API on error
Assuming errors are ignored without response
Confusing with performance optimization
2. Which of the following is the correct way to handle errors for graceful degradation in a REST API response (in pseudocode)?
easy
A. ignore errors and return nothing
B. return data; if error then stop
C. try { return data } catch { return fallbackData }
D. throw error without handling
Solution
Step 1: Identify error handling syntax
Graceful degradation uses try-catch to handle errors and provide fallback data.
Step 2: Match options to this pattern
try { return data } catch { return fallbackData } shows try-catch with fallback, others either stop or ignore errors.
Final Answer:
try { return data } catch { return fallbackData } -> Option C
Quick Check:
Use try-catch with fallback for graceful degradation [OK]
Hint: Use try-catch to return fallback on error [OK]
Common Mistakes:
Not catching errors properly
Stopping API on first error
Ignoring fallback responses
3. Consider this pseudocode for a REST API endpoint: