Skip to content

Added Response Headers to Control Plane Operations #41742

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions sdk/cosmos/azure-cosmos/azure/cosmos/aio/_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def __init__(
self.container_link = "{}/colls/{}".format(database_link, self.id)
self._is_system_key: Optional[bool] = None
self._scripts: Optional[ScriptsProxy] = None
self._response_headers = properties
Copy link
Preview

Copilot AI Jun 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If 'properties' is passed as None, calling .copy() in get_response_headers() will result in an error. Consider initializing _response_headers to {} when properties is None.

Suggested change
self._response_headers = properties
self._response_headers = properties if properties is not None else {}

Copilot uses AI. Check for mistakes.

if properties:
self.client_connection._set_container_properties_cache(self.container_link,
_build_properties_cache(properties,
Expand All @@ -112,6 +113,14 @@ async def _get_properties(self, **kwargs: Any) -> Dict[str, Any]:
await self.read(**kwargs)
return self.client_connection._container_properties_cache[self.container_link]

def get_response_headers(self) -> dict[str, Any]:
"""Returns a copy of the response headers associated to this response

:return: Dict of response headers
:rtype: dict[str, Any]
"""
return self._response_headers.copy()

@property
async def is_system_key(self) -> bool:
if self._is_system_key is None:
Expand Down
9 changes: 9 additions & 0 deletions sdk/cosmos/azure-cosmos/azure/cosmos/aio/_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def __init__(
self.id = id
self.database_link = "dbs/{}".format(self.id)
self._properties = properties
self._response_headers = properties
Copy link
Preview

Copilot AI Jun 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that _response_headers is always a dictionary. If properties is None, using .copy() in get_response_headers() will fail; consider initializing with {} as a fallback.

Suggested change
self._response_headers = properties
self._response_headers = properties if properties is not None else {}

Copilot uses AI. Check for mistakes.


def __repr__(self) -> str:
return "<DatabaseProxy [{}]>".format(self.database_link)[:1024]
Expand All @@ -126,6 +127,14 @@ async def _get_properties(self) -> Dict[str, Any]:
self._properties = await self.read()
return self._properties

def get_response_headers(self) -> dict[str, Any]:
"""Returns a copy of the response headers associated to this response

:return: Dict of response headers
:rtype: dict[str, Any]
"""
return self._response_headers.copy()

@distributed_trace_async
async def read(
self,
Expand Down
9 changes: 9 additions & 0 deletions sdk/cosmos/azure-cosmos/azure/cosmos/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def __init__(
self.client_connection = client_connection
self._is_system_key: Optional[bool] = None
self._scripts: Optional[ScriptsProxy] = None
self._response_headers = properties
Copy link
Preview

Copilot AI Jun 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If 'properties' is None, calling get_response_headers() could lead to an error since .copy() would not be available. Consider defaulting _response_headers to {} when properties is None.

Suggested change
self._response_headers = properties
self._response_headers = properties if properties is not None else {}

Copilot uses AI. Check for mistakes.

if properties:
self.client_connection._set_container_properties_cache(self.container_link,
_build_properties_cache(properties,
Expand All @@ -123,6 +124,14 @@ def _get_properties(self, **kwargs: Any) -> Dict[str, Any]:
self.read(**kwargs)
return self.__get_client_container_caches()[self.container_link]

def get_response_headers(self) -> dict[str, Any]:
"""Returns a copy of the response headers associated to this response

:return: Dict of response headers
:rtype: dict[str, Any]
"""
return self._response_headers.copy()

@property
def is_system_key(self) -> bool:
if self._is_system_key is None:
Expand Down
9 changes: 9 additions & 0 deletions sdk/cosmos/azure-cosmos/azure/cosmos/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def __init__(
self.id = id
self.database_link: str = "dbs/{}".format(self.id)
self._properties: Optional[Dict[str, Any]] = properties
self._response_headers = properties
Copy link
Preview

Copilot AI Jun 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If 'properties' is None, calling get_response_headers() may raise an AttributeError when attempting to use .copy(). Consider initializing _response_headers to an empty dictionary (e.g., properties or {} if properties is None).

Suggested change
self._response_headers = properties
self._response_headers = properties if properties is not None else {}

Copilot uses AI. Check for mistakes.


def __repr__(self) -> str:
return "<DatabaseProxy [{}]>".format(self.database_link)[:1024]
Expand All @@ -120,6 +121,14 @@ def _get_properties(self) -> Dict[str, Any]:
self._properties = self.read()
return self._properties

def get_response_headers(self) -> dict[str, Any]:
"""Returns a copy of the response headers associated to this response

:return: Dict of response headers
:rtype: dict[str, Any]
"""
return self._response_headers.copy()

@distributed_trace
def read( # pylint:disable=docstring-missing-param
self,
Expand Down
24 changes: 24 additions & 0 deletions sdk/cosmos/azure-cosmos/tests/test_cosmos_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,30 @@ def test_point_operation_headers(self):
assert len(batch_response.get_response_headers()) > 0
assert int(lsn) + 1 < int(batch_response.get_response_headers()['lsn'])

def test_create_database_headers(self):
first_response = self.client.create_database(id="responses_test" + str(uuid.uuid4()))
assert len(first_response.get_response_headers()) > 0

def test_create_database_if_not_exists_headers(self):
first_response = self.client.create_database_if_not_exists(id="responses_test" + str(uuid.uuid4()))
assert len(first_response.get_response_headers()) > 0

def test_create_container_headers(self):
first_response = self.test_database.create_container(id="responses_test" + str(uuid.uuid4()),
partition_key=PartitionKey(path="/company"))
assert len(first_response.get_response_headers()) > 0

def test_create_container_if_not_exists_headers(self):
first_response = self.test_database.create_container_if_not_exists(id="responses_test" + str(uuid.uuid4()),
partition_key=PartitionKey(path="/company"))
assert len(first_response.get_response_headers()) > 0

def test_replace_container_headers(self):
first_response = self.test_database.create_container_if_not_exists(id="responses_test" + str(uuid.uuid4()),
partition_key=PartitionKey(path="/company"))
second_response = self.test_database.replace_container(first_response.id,
partition_key=PartitionKey(path="/company"))
assert len(second_response.get_response_headers()) > 0

if __name__ == '__main__':
unittest.main()
24 changes: 24 additions & 0 deletions sdk/cosmos/azure-cosmos/tests/test_cosmos_responses_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,30 @@ async def test_point_operation_headers_async(self):
assert len(batch_response.get_response_headers()) > 0
assert int(lsn) + 1 < int(batch_response.get_response_headers()['lsn'])

async def test_create_database_headers(self):
first_response = await self.client.create_database(id="responses_test" + str(uuid.uuid4()))
assert len(first_response.get_response_headers()) > 0

async def test_create_database_if_not_exists_headers(self):
first_response = await self.client.create_database_if_not_exists(id="responses_test" + str(uuid.uuid4()))
assert len(first_response.get_response_headers()) > 0

async def test_create_container_headers(self):
first_response = await self.test_database.create_container(id="responses_test" + str(uuid.uuid4()),
partition_key=PartitionKey(path="/company"))
assert len(first_response.get_response_headers()) > 0

async def test_create_container_if_not_exists_headers(self):
first_response = await self.test_database.create_container_if_not_exists(id="responses_test" + str(uuid.uuid4()),
partition_key=PartitionKey(path="/company"))
assert len(first_response.get_response_headers()) > 0

async def test_replace_container_headers(self):
first_response = await self.test_database.create_container_if_not_exists(id="responses_test" + str(uuid.uuid4()),
partition_key=PartitionKey(path="/company"))
second_response = await self.test_database.replace_container(first_response.id,
partition_key=PartitionKey(path="/company"))
assert len(second_response.get_response_headers()) > 0

if __name__ == '__main__':
unittest.main()
Loading