Skip to content

API responses accessible like objects with type hints #1695

Closed
@sethmlarson

Description

@sethmlarson

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
"..."

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions