Skip to content

Conversation

@simorenoh
Copy link
Member

@simorenoh simorenoh commented May 28, 2024

We do not have a good way for users to access response headers. Currently, we use a last_response_headers dictionary (map) that gets cached in the client instance, and recommend users look at headers like such:
image

Not only is this design not intuitive, it is not thread safe, so using it directly has no real guarantees of validity depending on what the user is trying to do. Separately, we also allow users to pass in a function/method/callable response_hook, and have recently made that functionality thread safe, but having to require users to send in a function/method/callable in order to access these headers is also less than ideal from a UX perspective. Since in Cosmos headers can be highly relevant to user cases, this PR attempts to improve the user experience.

This PR adds the ability to fetch response headers for a given operation directly from its response through a newly introduced method. The new return type can do this, while still maintaining its ability to work as previously, with CosmosDict behaving like a dict and CosmosList behaving like a list.

This means an operation response like this:
image

Would now be able to have direct knowledge into the response headers associated with it by doing this, and would be able to use that information directly:
image
image

Follow-up work
For query operations, since each request is made to one partition at once, aggregating the results of an entire query result for cross-partition scenarios is going to be a separate issue. For now, much like in the Java SDK we will collect headers on a per-page basis for pagination scenarios - and in this case the headers will be updated in the pageable object as the user goes through each of them:
image
image
and then the next page:
image
image

Issue for this item: #37932

Items in this PR:

  • changes for sync client
  • changes for async client
  • refactoring of tests previously using last_response_headers
  • refactoring of samples previously using last_response_headers
  • CHANGELOG
  • adds additional tests for new response headers packaging

@azure-sdk
Copy link
Collaborator

azure-sdk commented May 28, 2024

API change check

APIView has identified API level changes in this PR and created following API reviews.

azure-cosmos

@simorenoh
Copy link
Member Author

/azp run python - cosmos - tests

@annatisch
Copy link
Member

Adding a reference to underlying Azure SDK feature issue: #32571

@simorenoh simorenoh marked this pull request as ready for review June 19, 2024 16:42
@simorenoh simorenoh changed the title [Cosmos] item response includes response headers [Cosmos] item operation response includes response headers Jul 20, 2024
@simorenoh simorenoh requested a review from a team as a code owner August 1, 2024 18:55
@simorenoh
Copy link
Member Author

/azp run python - cosmos - tests

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@simorenoh
Copy link
Member Author

/azp run python - cosmos - tests

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@simorenoh
Copy link
Member Author

/azp run python - cosmos - tests

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@simorenoh simorenoh merged commit 738d9da into Azure:main Oct 16, 2024
@simorenoh simorenoh deleted the package-response-headers branch October 16, 2024 18:50
l0lawrence pushed a commit to l0lawrence/azure-sdk-for-python that referenced this pull request Feb 19, 2025
* adds new response to sync point operations

* Update _cosmos_response.py

* addresses comments, adds query changes

* added logic to deal with unpacking of actual values

* pylint

* async changes

* Update CHANGELOG.md

* samples using last_response_headers

* update use of last_response_headers, sanity check on responses

* add header tests

* readme and oopsies

* test fixes

* Update sdk/cosmos/azure-cosmos/azure/cosmos/_cosmos_responses.py

Co-authored-by: Anna Tisch <[email protected]>

* Update sdk/cosmos/azure-cosmos/azure/cosmos/_cosmos_responses.py

Co-authored-by: Anna Tisch <[email protected]>

* typing updates

* pylint

* mypy part1

* tests

* fixes

* tests

* update typehint for mypy

* Revert "update typehint for mypy"

This reverts commit adbab3a.

* mypy

* Update _cosmos_client_connection.py

* Update _cosmos_client_connection.py

* Update _cosmos_client_connection.py

* Update _cosmos_client_connection.py

* Update _cosmos_client_connection_async.py

* Update _cosmos_client_connection_async.py

* public surface area changes

* review updates

* Update README.md

* Update README.md

* Update container.py

* rename responses and update inhertiance typing

* revert subtyping

* missed empty dict responses

* Update test_globaldb.py

* simplify query_iterable header logic

* revert

* add subscripts to class definition

* try re-adding typehint

* revert

* specify rtypes

* Update _cosmos_client_connection.py

* remove query changes

* Update _cosmos_client_connection_async.py

* update rtypes

* pylint

* revert more query stuff

---------

Co-authored-by: Anna Tisch <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants