-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for conditional requests #188
Support for conditional requests #188
Conversation
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## main #188 +/- ##
==========================================
- Coverage 97.21% 96.32% -0.90%
==========================================
Files 10 10
Lines 934 979 +45
Branches 171 181 +10
==========================================
+ Hits 908 943 +35
- Misses 15 23 +8
- Partials 11 13 +2
☔ View full report in Codecov by Sentry. |
Thank you for putting this together. I appreciate it! |
I still work on improving code coverage in the test, need a proper endpoint I can use during the tests. httpbin does not support that and is basically a dead project. It probably makes sense to create a fork supporting something like that. I have a pretty good idea how this could be done, e.g. a /cache/10 endpoint that updates the etag every 10s. The etag would be generated from the current time in an interval based on the parameter. There is a function available that can do that: https://gist.github.com/mscheper/9c39091e12f2ca8addb3d5eef7a40480 Then you could write a test that calls /cache/1, waits 2s and make a conditional request with the etag from before. Now the etag has changed and the server should respond with a 200 with the new content. |
@netomi I believe I handled this in requests-cache with mock responses instead of real requests + httpbin. I used the If you want to test with a real endpoint, I think it would be simpler to make a separate test app rather than forking httpbin. Here's an example with Flask that does what you describe: from datetime import datetime
from hashlib import md5
from flask import Flask, request
app = Flask(__name__)
@app.route("/cache/<int:interval>")
def cache(interval):
"""Cacheable endpoint that uses an ETag that resets every `interval` seconds"""
now = datetime.now()
server_etag = md5(str(now.second // interval).encode()).hexdigest()
request_etag = request.headers.get("If-None-Match")
if request_etag == server_etag:
return ("NOT MODIFIED", 304)
else:
return ("OK", 200, {"ETag": server_etag}) |
that is really nice will work on a PR |
These changes are now available in v0.10. Thank you again for your help! Just fyi, at some point in the future I may merge some of this logic into the |
great, so I can update my app to use that version and remove my workaround. Putting the logic into the CacheActions class certainly makes sense! btw. could you also make a release from the 0.10.0 tag? The latest release is still 0.9. |
Yes, I'll add that shortly. |
This PR adds support for conditional requests.
I extracted the relevant code in a separate method so that I can check in a test whether it was called with mocking.
However, it would be great if httpbin would support an endpoint to changes its etag over time. Will look into that and provide a patch there so we can use it here as well if accepted.
This fixes #79 .
Code cov needs to be improved though.