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

Allow user to pass headers to jikan requests #57

Open
seanbreckenridge opened this issue Aug 30, 2019 · 3 comments
Open

Allow user to pass headers to jikan requests #57

seanbreckenridge opened this issue Aug 30, 2019 · 3 comments

Comments

@seanbreckenridge
Copy link
Collaborator

seanbreckenridge commented Aug 30, 2019

As stated in #54, adding header support would be good to allow users to bypass caching, and compare ETag values

Current thoughts/issues:

  • I believe, coincidentally, the way I laid out _wrap_response already works for ETag checking (304s). It defaults to {}, does nothing in except block and, doesnt get modified in _check_response, and then has the headers/url added to it. So the 304 would just return a dict with the jikan_url and response headers (which I think is what we want).
json_response: Dict = {}
try:
    json_response = response.json()
except json.decoder.JSONDecodeError:
    # json failed to be parsed
    # this could happen, for example, when someone has been IP banned
    # and it returns the typical nginx 403 forbidden page
    pass
self._check_response(response_dict=json_response, response_status_code=response.status_code, **kwargs)
return self._add_jikan_metadata(response, json_response, url)
  • Add default_headers to abstractjikan? That way if someone wants to set a User-Agent to identify themselves to api.jikan.moe. Then headers passed per request can override with dict.update

Not totally sure how to implement a 304 pytest, but I'll give it a try.

No Estimate currently on when I'll have this done.

@irfan-dahir
Copy link

irfan-dahir commented Sep 1, 2019

304 can be tested in the following way

  1. Get value ETag from a request.
  2. Next time that request is made, pass an If-None-Match header with the ETag value as it's value.
  3. If data is unchanged, response code will be 304 - Not Modified. The standard output for 304 is empty body. So only a header check for 304 is required to determine that the data is unchanged.
    If data has changed; response will be how it usually is. 200 - OK with the response (updated/changed data) in the body.

Better explained with a diagram: https://jikan.docs.apiary.io/#introduction/cache-validation

@seanbreckenridge
Copy link
Collaborator Author

seanbreckenridge commented Sep 2, 2019

As per the discussion in discord:

  • Allow users to override session objects, document how to set default headers for jikan/aiojikan
  • Let users pass additional kwargs which get passed to session.get

For jikan:

https://2.python-requests.org/en/master/api/#requests.request

Accept a subset of the kwargs that are relevant (to avoid confusion with params/parameters):

headers
cookies
timeout
allow_redirects
proxies
verify
stream
cert

The other ones we can ignore since we're the ones creating data for them:
url
method
params (get fields)

Or theyre just not relevant:

data (for post requests)
json (for post requests)
files

Similarly, accept a subset of kwargs from aiohttp.Session.get

https://aiohttp.readthedocs.io/en/stable/client_reference.html#aiohttp.ClientSession.request

Document which kwargs are allows to be passed.

Add a function: _check_request that gets called before the request is sent, that checks (jikan/aiojikan specific) if the kwargs are allowed (by defining a list of them in parameters.py)

@seanbreckenridge
Copy link
Collaborator Author

Could probably just wait till v4 since some subset of the API will have to be rewritten to handle passing the JWT, and the parameter checks are being removed anyways.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants