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