-
Notifications
You must be signed in to change notification settings - Fork 194
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
Allow asking for JSON using a query parameter #216
Allow asking for JSON using a query parameter #216
Conversation
…t format. As in the existing code, this doesn't try to fully parse the Accept header, but just uses simple substring matching within the header. As such, it doesn't take account of any q-values. If both text/html and application/json are present, it ignores the request's preferences and uses the configured preferred format.
It seems likely that this was the original intention.
Codecov Report
@@ Coverage Diff @@
## master #216 +/- ##
==========================================
+ Coverage 73.85% 75.52% +1.67%
==========================================
Files 32 32
Lines 348 380 +32
==========================================
+ Hits 257 287 +30
- Misses 91 93 +2
Continue to review full report at Codecov.
|
thanks for the contribution. You are not the first one, to approach us with that idea. Please read through #208 to see my concerns about this topic. It would be great to get this one the road, just give it a little more work. Best |
Hi @codingjoe, Sorry I didn't see the previous PR on a similar topic before making this. I understand wanting to keep the number of configurable options down. I'd prefer having something that doesn't need a parameter, but it's a reasonable middle ground that will still make it possible for consumers of the endpoint where I can't set arbitrary headers. Hopefully the latest changes are in line with what you're expecting. Please let me know if they need any adjustment. |
This follows @codingjoe's suggested approach, which I diverged from earlier.
bca85e1
to
b880d53
Compare
b880d53
to
317ad1c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ashokdelphia I made some changes, mainly I renamed the GET attribute back to format=json
, sorry for the back and forth.
Furthermore I change the code to actually respect the weight of the accept header media types as well as using the GET parameter as override, no matter what has been defined in the header.
Let me know if you see any problems. Otherwise, I will merge and release this feature. Thanks for all the hard work, much appreciated.
health_check/views.py
Outdated
return self.render_to_response(context, status=status_code) | ||
accept_header = request.META.get('HTTP_ACCEPT', '*/*') | ||
for media in MediaType.parse_header(accept_header): | ||
if media.mime_type in ['text/html', ' application/xhtml+xml', '*/*']: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like there's a spurious tab in the second element here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new changes generally look good to me.
The spurious tab character is the only thing that looks clearly wonky to me.
Cheers.
tests/test_views.py
Outdated
def test_from_string(self): | ||
assert MediaType.from_string('*/*') == MediaType('*/*') | ||
assert MediaType.from_string('*/*;0.9') == MediaType('*/*', 0.9) | ||
assert MediaType.from_string('*/*;0.9') == MediaType('*/*', 0.9) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the format here is following how accept headers should be formatted.
I think it should be of the form foo/bar;q=0.9
, where the quality a parameter q
, not a bare value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may also be worth having explicit tests for the various places you're allowed to have optional whitespace, starting with directly after the ;
, which I think is reasonably common.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good lord you are right. This was wrong. I fixed it and added a bunch more tests. Please double check, before I release it.
The code tries to return text/html in this case, but there's a slip in how it allows it.
I adjusted the spurious whitespace, but while adding a failing test, I noticed that passing an accept header with an unrecognised type seems to cause a problem:
Gives:
A bisect suggests it breaks in 317ad1c, but I haven't had time to dig in to why yet. I can take a look after the weekend, if it looks like a real problem and you don't have time before then. |
@ashokdelphia there was no fallback. I added it and a test. I also added another commit to support an empty accept header. |
Released in 3.10.0 Thx @ashokdelphia, really good work! |
Thanks for this useful package. It made adding a richer health-check endpoint very straightforward.
I'm using it in a context where a number of different agents are hitting the endpoint, and not all are easily configurable to set arbitrary headers for their requests. For us, the HTML response is about 1000 bytes, but the JSON response is about 50, so I would like to make the default response JSON instead of HTML.I tried to add reasonable test coverage, and simple docs, but please let me know if you'd rather handle those differently.I stopped short of fully parsing the Accept header and interpreting the q-values, as that felt like it was likely overkill for this purpose.This PR adds an alternative way to ask for a JSON response, by passing a
json
query parameter.(See the comments for the change in direction here.)