abhinavk99/jikanpy

Allow user to pass headers to jikan requests

Opened this issue ยท 3 comments

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.

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

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)

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.