Repository navigation
feat: implement cache fallback for resilient API responses - #4
Merged
Merged
Conversation
…Divers 2 API is offline
Contributor
There was a problem hiding this comment.
Pull Request Overview
Adds cache-fallback logic to serve the most recent database snapshots when the upstream Hell Divers 2 API is unavailable, improving resiliency of several endpoints.
- Introduces four new snapshot helpers in the database layer.
- Updates five API endpoints to try live fetch first and fall back to cached data; returns 503 when both are unavailable.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| src/database.py | Adds snapshot retrieval methods for planets, campaigns, factions, and biomes to support cache fallback. |
| src/app.py | Implements cache-first-with-fallback on campaigns, planets, planet status by index, factions, and biomes endpoints; updates error codes to 503 when no cache is present. |
Comments suppressed due to low confidence (1)
src/app.py:1
- The same truthiness issue applies here; empty but valid objects would incorrectly fall through to the fallback and potentially to a 503. Replace with None checks:
if data is None:for fallback andif data is not None:before returning.
import logging
… statistics from initial war_data fetch
Contributor
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 8 comments.
Comments suppressed due to low confidence (1)
src/app.py:1
- This endpoint calls collector.collect_planet_data(planet_index), which (per its design) collects and persists, but does not synchronously return the planet status payload for response. This will likely return None and always trigger the fallback path. Use scraper.get_planet_status(planet_index) for the live fetch, and align the fallback check to 'is None' for consistency with other endpoints.
import logging
…handling in data collection
Collaborator
Author
✅ Resolution SummaryAll review comments have been addressed and resolved: Fixed Issues:
Current Status:
The feature branch is ready for merge once GitHub Actions CI confirms all tests pass across all matrix jobs (ubuntu/macos/windows × Python 3.13/3.14). |
…in data collector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Implements cache fallback mechanism for API endpoints so they gracefully degrade when the Hell Divers 2 community API becomes unavailable. Previously, 5 endpoints would fail with 404 errors if the upstream API went offline. Now they fall back to the most recent cached data in the database.
Problem
When Hell Divers 2 community API (api.helldivers2.dev) is offline:
/api/planets- Returns 404 instead of cached data/api/campaigns- Returns 404 instead of cached data/api/planets/{index}- Returns 404 instead of cached data/api/factions- Returns 404 instead of cached data/api/biomes- Returns 404 instead of cached dataSolution
Implemented cache-first-with-fallback pattern on all 5 endpoints:
Changes
Database (src/database.py)
Added 4 new snapshot methods for cache fallback:
get_latest_planets_snapshot()- All planets from most recent collectionget_latest_campaigns_snapshot()- All campaigns from most recent collectionget_latest_factions_snapshot()- Extracted from war status cacheget_latest_biomes_snapshot()- Extracted from planet cacheAPI (src/app.py)
Updated 5 endpoints with cache fallback:
/api/planets- Try live, fallback to cache/api/campaigns- Try live, fallback to cache/api/planets/{index}- Try live, fallback to cache/api/factions- Try live, fallback to cache/api/biomes- Try live, fallback to cacheTesting
HTTP Status Codes
200- Success (fresh data from API or cached data)503- Service unavailable (API down and no cached data)Checklist