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

API responses accessible like objects with type hints #1695

Closed
sethmlarson opened this issue Aug 25, 2021 · 1 comment
Closed

API responses accessible like objects with type hints #1695

sethmlarson opened this issue Aug 25, 2021 · 1 comment

Comments

@sethmlarson
Copy link
Contributor

sethmlarson commented Aug 25, 2021

Currently APIs return dictionaries for JSON response bodies however this makes it impossible to determine transport-level information per-response and makes adding type hints not possible for more complex responses.

from elasticsearch import Elasticsearch

client = Elasticsearch()
resp = client.search(
    index="index",
    query={"match_all": {}}
)

# Returns an API response instead of a dict
>>> resp
{"hits": {"hits": [...], ...}, ...}

# Still access the response like a dict
>>> resp["hits"]
{"hits": [...], ...}

# But also can access like an object
>>> resp.body.hits
HitsMetadata(hits=[...], ...)

# Can access transport information from the response
>>> resp.meta
ApiResponseMeta(
  status=200,
  headers={
    ...
  }
)

Non-JSON Responses

Responses which aren't JSON will still be wrapped as objects:

resp = client.cat.tasks()

>>> resp
"..."

>>> resp.raw
"..."
@sethmlarson sethmlarson changed the title Make API JSON responses accessible like objects with type hints API responses accessible like objects with type hints Aug 26, 2021
@sethmlarson sethmlarson mentioned this issue Aug 26, 2021
13 tasks
@pquentin
Copy link
Member

This was done as part of #386. Closing.

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

2 participants