Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions sdk/cosmos/azure-cosmos/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#### Bugs Fixed

#### Other Changes
* Changed to include client id in headers for all requests. See [PR 42104](https://github.com/Azure/azure-sdk-for-python/pull/42104).

### 4.14.0b1 (2025-07-14)

Expand Down
2 changes: 2 additions & 0 deletions sdk/cosmos/azure-cosmos/azure/cosmos/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ def GetHeaders( # pylint: disable=too-many-statements,too-many-branches

if client_id is not None:
headers[http_constants.HttpHeaders.ClientId] = client_id
elif cosmos_client_connection and cosmos_client_connection.client_id:
headers[http_constants.HttpHeaders.ClientId] = cosmos_client_connection.client_id

if options.get("enableScriptLogging"):
headers[http_constants.HttpHeaders.EnableScriptLogging] = options["enableScriptLogging"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2825,7 +2825,8 @@ def Read(
options = {}

initial_headers = initial_headers or self.default_headers
headers = base.GetHeaders(self, initial_headers, "get", path, id, typ, documents._OperationType.Read, options)
headers = base.GetHeaders(self, initial_headers, "get", path, id, typ, documents._OperationType.Read,
options)
# Read will use ReadEndpoint since it uses GET operation
request_params = RequestObject(typ, documents._OperationType.Read, headers)
request_params.set_excluded_location_from_options(options)
Expand Down
19 changes: 19 additions & 0 deletions sdk/cosmos/azure-cosmos/tests/test_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ def side_effect_correlated_activity_id(self, *args, **kwargs):
assert args[3]["x-ms-cosmos-correlated-activityid"] # cspell:disable-line
raise StopIteration

def side_effect_client_id(self, *args, **kwargs):
# Extract request headers from args
assert args[2][http_constants.HttpHeaders.ClientId]
raise StopIteration

def test_correlated_activity_id(self):
query = 'SELECT * from c ORDER BY c._ts'

Expand Down Expand Up @@ -98,6 +103,20 @@ def test_negative_max_integrated_cache_staleness(self):
except Exception as exception:
assert isinstance(exception, ValueError)

def test_client_id(self):
# Client ID should be sent on every request, Verify it is sent on a read_item request
cosmos_client_connection = self.container.client_connection
original_connection_get = cosmos_client_connection._CosmosClientConnection__Get
cosmos_client_connection._CosmosClientConnection__Get = MagicMock(
side_effect=self.side_effect_client_id)
try:
self.container.read_item(item="id-1", partition_key="pk-1")
except StopIteration:
pass
finally:
cosmos_client_connection._CosmosClientConnection__Get = original_connection_get


def test_client_level_throughput_bucket(self):
cosmos_client.CosmosClient(self.host, self.masterKey,
throughput_bucket=client_throughput_bucket_number,
Expand Down
27 changes: 26 additions & 1 deletion sdk/cosmos/azure-cosmos/tests/test_headers_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
# Copyright (c) Microsoft Corporation. All rights reserved.

import unittest
from unittest.mock import MagicMock

import pytest
import uuid


import test_config
from azure.cosmos import http_constants
import azure.cosmos.exceptions as exceptions
from azure.cosmos.aio import CosmosClient, _retry_utility_async, DatabaseProxy
from azure.cosmos.partition_key import PartitionKey

Expand All @@ -23,6 +23,12 @@ async def request_raw_response_hook(response):
assert (response.http_request.headers[http_constants.HttpHeaders.ThroughputBucket]
== str(request_throughput_bucket_number))


class ClientIDVerificationError(Exception):
"""Custom exception for client ID verification errors."""
pass


@pytest.mark.cosmosEmulator
class TestHeadersAsync(unittest.IsolatedAsyncioTestCase):
client: CosmosClient = None
Expand Down Expand Up @@ -206,5 +212,24 @@ async def test_container_read_item_negative_throughput_bucket_async(self):
assert "specified for the header 'x-ms-cosmos-throughput-bucket' is invalid." in e.http_error_message
"""

async def side_effect_client_id(self, *args, **kwargs):
# This is a side effect to verify that the client ID is sent in the request headers
assert args[2].get(http_constants.HttpHeaders.ClientId) is not None
raise ClientIDVerificationError("Client ID verification complete")

async def test_client_id(self):
# Client ID should be sent on every request, Verify it is sent on a read_item request
cosmos_client_connection = self.container.client_connection
original_connection_get = cosmos_client_connection._CosmosClientConnection__Get
cosmos_client_connection._CosmosClientConnection__Get = MagicMock(
side_effect=self.side_effect_client_id)
try:
await self.container.read_item(item="id-1", partition_key="pk-1")
except ClientIDVerificationError:
pass
finally:
cosmos_client_connection._CosmosClientConnection__Get = original_connection_get


if __name__ == "__main__":
unittest.main()