Skip to content

feat: If-Modified-Since for /environment-document#5283

Merged
rolodato merged 5 commits intomainfrom
feat/if-modified-since
Apr 29, 2025
Merged

feat: If-Modified-Since for /environment-document#5283
rolodato merged 5 commits intomainfrom
feat/if-modified-since

Conversation

@rolodato
Copy link
Copy Markdown
Contributor

Adds support for If-Modified-Since to GET /environment-document. This makes it so that if clients already have a recent environment document, the API can respond with an empty 304 instead of serving the whole document.

Tests are not yet passing. I understand this is because the environment document cache uses SDK keys and not environment IDs as its cache keys, which means that calling clear_environment_cache has no effect if the environment was only ever fetched using server-side SDK keys.

@rolodato rolodato requested a review from a team as a code owner March 31, 2025 21:22
@rolodato rolodato requested review from matthewelwell and removed request for a team March 31, 2025 21:22
@vercel
Copy link
Copy Markdown

vercel bot commented Mar 31, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

3 Skipped Deployments
Name Status Preview Comments Updated (UTC)
docs ⬜️ Ignored (Inspect) Visit Preview Apr 25, 2025 10:02pm
flagsmith-frontend-preview ⬜️ Ignored (Inspect) Visit Preview Apr 25, 2025 10:02pm
flagsmith-frontend-staging ⬜️ Ignored (Inspect) Visit Preview Apr 25, 2025 10:02pm

@github-actions github-actions bot added api Issue related to the REST API feature New feature or request labels Mar 31, 2025
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 31, 2025

Docker builds report

Image Build Status Security report
ghcr.io/flagsmith/flagsmith-api-test:pr-5283 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-e2e:pr-5283 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-api:pr-5283 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-frontend:pr-5283 Finished ✅ Results
ghcr.io/flagsmith/flagsmith:pr-5283 Finished ✅ Results

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 31, 2025

Uffizzi Preview deployment-62355 was deleted.

@rolodato rolodato force-pushed the feat/if-modified-since branch from 09cddef to 12af449 Compare March 31, 2025 21:25
@github-actions github-actions bot added feature New feature or request and removed feature New feature or request labels Mar 31, 2025
Copy link
Copy Markdown
Contributor

@matthewelwell matthewelwell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason that the tests are failing is because the updated_at value on the environment is updated by a hook on an audit log record getting created (it is not using the usual auto_now logic from django). Since you're updating the name of the environment manually, an audit log record isn't being created (because there's no user associated with the change).

Some options:

  1. Just manually update the updated_at value on the environment instance
  2. Convert this test into more of an integration test and actually make an update to the environment via the PUT endpoint for example

I'm happy with either option really so I'd recommend just going with option 1 as it's the easiest. As per my comment on the specific lines in the code though, we should add a test to verify that not passing the if-modified-since header still returns the whole document.

Comment thread api/environments/authentication.py Outdated
Comment on lines +52 to +55
if not self._environment:
raise AssertionError(
"Tried to access environment on an authenticated request, but was None"
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is intentionally verbose, but this can be rewritten as

Suggested change
if not self._environment:
raise AssertionError(
"Tried to access environment on an authenticated request, but was None"
)
assert self._environment, (
"Tried to access environment on an authenticated request, but was None"
)

assert response.status_code == status.HTTP_403_FORBIDDEN


def test_environment_document_caching(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd argue that this isn't really 'caching' ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 4084a46

Comment on lines +197 to +198
# Then - second request should return 304 Not Modified
assert response2.status_code == status.HTTP_304_NOT_MODIFIED
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also make a request from a client that doesn't have the If-Modified-Since header to make sure that it still returns the environment document?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 957e6ff

@github-actions github-actions bot added feature New feature or request and removed feature New feature or request labels Apr 24, 2025
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 24, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.62%. Comparing base (8dc92c3) to head (957e6ff).
Report is 14 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5283   +/-   ##
=======================================
  Coverage   97.61%   97.62%           
=======================================
  Files        1237     1237           
  Lines       42975    43015   +40     
=======================================
+ Hits        41952    41992   +40     
  Misses       1023     1023           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions github-actions bot added feature New feature or request and removed feature New feature or request labels Apr 25, 2025
@rolodato rolodato changed the title feat: (WIP) If-Modified-Since for /environment-document feat: If-Modified-Since for /environment-document Apr 25, 2025
@rolodato rolodato requested a review from matthewelwell April 25, 2025 22:04
@rolodato rolodato merged commit 8e8ff22 into main Apr 29, 2025
38 checks passed
@rolodato rolodato deleted the feat/if-modified-since branch April 29, 2025 22:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Issue related to the REST API feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants