Skip to content
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

Add support for media types #102

Merged
merged 3 commits into from
Jul 23, 2022

Conversation

lfdebrux
Copy link
Contributor

@lfdebrux lfdebrux commented Nov 9, 2021

For some endpoints GitHub lets you request different response format using the Accept header [1]. However, by default if the response is not in JSON format GhApi.__call__ will raise an error.

This commit makes it possible by adding a bit of logic to look at the Accept header in the request and tell fastcore.core.urlsend not to return JSON if it doesn't look like the user is requesting a JSON media type.

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

For some endpoints GitHub lets you request different response format
using the `Accept` header [[1]]. However, by default if the response is
not in JSON format `GhApi.__call__` will raise an error.

This commit makes it possible by adding a bit of logic to look at the
`Accept` header in the request and tell `fastcore.core.urlsend` not to
return JSON if it doesn't look like the user is requesting a JSON media
type.

[1]: https://docs.github.com/en/rest/overview/media-types
@lfdebrux lfdebrux force-pushed the lfdebrux-add-media-types-support branch from 25a9182 to 263fc04 Compare November 9, 2021 12:05
@rshk
Copy link
Contributor

rshk commented Nov 22, 2021

I was looking into mimetype support as well.

I wonder if adding an extra argument to __call__() to control this behaviour would be better, as it makes more explicit what's the desired return type?

(Eg. there is a slight chance that a non-json mimetype contains the "json" string -- or that a json-based format doesn't).

So eg. change the signature to:

def __call__(
    self,
    path:str,
    verb:str=None,
    headers:dict=None,
    route:dict=None,
    query:dict=None,
    data=None,
    return_json:bool=True,
):
    # ...

@lfdebrux
Copy link
Contributor Author

lfdebrux commented Nov 26, 2021

@rshk I considered this initially, but it seemed a bit unnecessary because a) I think the developer's intent is already made clear by changing the accept header b) with a return_json param, if you changed the accept header but forgot to change the param, you'd get an exception from the JSON decoder, so there's no value to having the two settings separated and c) the GitHub media types seem to be well thought out and logical, a scenario where the media type contains json but is not JSON (or vice versa) seems unlikely.

@jph00 jph00 added the enhancement New feature or request label Jul 23, 2022
@jph00 jph00 merged commit a100707 into AnswerDotAI:master Jul 23, 2022
@jph00
Copy link
Contributor

jph00 commented Jul 23, 2022

Many thanks! FYI you forgot to run nbdev_build_lib - I'll do that and push.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants