Skip to content

Commit e459f14

Browse files
andrewmathew1Andrew Mathew
andauthored
Added User Agent Suffix (Azure#40904)
* added suffix with tests * added documentation in client functions * added changes from comments in pr * added to changelog * edited changelog --------- Co-authored-by: Andrew Mathew <[email protected]>
1 parent a1946d4 commit e459f14

File tree

7 files changed

+23
-6
lines changed

7 files changed

+23
-6
lines changed

sdk/cosmos/azure-cosmos/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
## Release History
22

33
### 4.12.0b2 (Unreleased)
4-
* Added ability to use request level `excluded_locations` on metadata calls, such as getting container properties. See [PR 40905](https://github.com/Azure/azure-sdk-for-python/pull/40905)
54

65
#### Features Added
6+
* Added ability to set a user agent suffix at the client level. See [PR 40904](https://github.com/Azure/azure-sdk-for-python/pull/40904)
7+
* Added ability to use request level `excluded_locations` on metadata calls, such as getting container properties. See [PR 40905](https://github.com/Azure/azure-sdk-for-python/pull/40905)
78

89
#### Bugs Fixed
910
* Fixed issue where Query Change Feed did not return items if the container uses legacy Hash V1 Partition Keys. This also fixes issues with not being able to change feed query for Specific Partition Key Values for HPK. See [PR 41270](https://github.com/Azure/azure-sdk-for-python/pull/41270/)

sdk/cosmos/azure-cosmos/azure/cosmos/_cosmos_client_connection.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ def __init__( # pylint: disable=too-many-statements
197197
proxy = host if url.port else host + ":" + str(self.connection_policy.ProxyConfiguration.Port)
198198
proxies.update({url.scheme: proxy})
199199

200-
self._user_agent: str = _utils.get_user_agent()
200+
suffix = kwargs.pop('user_agent_suffix', None)
201+
self._user_agent: str = _utils.get_user_agent(suffix)
201202

202203
credentials_policy = None
203204
if self.aad_credentials:

sdk/cosmos/azure-cosmos/azure/cosmos/_utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,21 @@
3131
from ._version import VERSION
3232

3333

34-
def get_user_agent() -> str:
34+
def get_user_agent(suffix: Optional[str]) -> str:
3535
os_name = safe_user_agent_header(platform.platform())
3636
python_version = safe_user_agent_header(platform.python_version())
3737
user_agent = "azsdk-python-cosmos/{} Python/{} ({})".format(VERSION, python_version, os_name)
38+
if suffix:
39+
user_agent += f" {suffix}"
3840
return user_agent
3941

4042

41-
def get_user_agent_async() -> str:
43+
def get_user_agent_async(suffix: Optional[str]) -> str:
4244
os_name = safe_user_agent_header(platform.platform())
4345
python_version = safe_user_agent_header(platform.python_version())
4446
user_agent = "azsdk-python-cosmos-async/{} Python/{} ({})".format(VERSION, python_version, os_name)
47+
if suffix:
48+
user_agent += f" {suffix}"
4549
return user_agent
4650

4751

sdk/cosmos/azure-cosmos/azure/cosmos/aio/_cosmos_client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ class CosmosClient: # pylint: disable=client-accepts-api-version-keyword
172172
:keyword bool no_response_on_write: Indicates whether service should be instructed to skip sending
173173
response payloads for write operations on items by default unless specified differently per operation.
174174
:keyword int throughput_bucket: The desired throughput bucket for the client
175+
:keyword str user_agent_suffix: Allows user agent suffix to be specified when creating client
175176
176177
.. admonition:: Example:
177178

sdk/cosmos/azure-cosmos/azure/cosmos/aio/_cosmos_client_connection_async.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ def __init__( # pylint: disable=too-many-statements
202202
proxy = host if url.port else host + ":" + str(self.connection_policy.ProxyConfiguration.Port)
203203
proxies.update({url.scheme: proxy})
204204

205-
self._user_agent = _utils.get_user_agent_async()
205+
suffix = kwargs.pop('user_agent_suffix', None)
206+
self._user_agent = _utils.get_user_agent_async(suffix)
206207

207208
credentials_policy = None
208209
if self.aad_credentials:

sdk/cosmos/azure-cosmos/azure/cosmos/cosmos_client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ class CosmosClient: # pylint: disable=client-accepts-api-version-keyword
193193
:keyword bool no_response_on_write: Indicates whether service should be instructed to skip sending
194194
response payloads on rite operations for items.
195195
:keyword int throughput_bucket: The desired throughput bucket for the client
196+
:keyword str user_agent_suffix: Allows user agent suffix to be specified when creating client
196197
197198
.. admonition:: Example:
198199

sdk/cosmos/azure-cosmos/tests/test_client_user_agent.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from test_config import TestConfig
1111

1212

13-
@pytest.mark.skip
13+
@pytest.mark.cosmosEmulator
1414
class TestClientUserAgent(unittest.IsolatedAsyncioTestCase):
1515

1616
async def test_client_user_agent(self):
@@ -21,6 +21,14 @@ async def test_client_user_agent(self):
2121
self.assertTrue(client_async.client_connection._user_agent.startswith("azsdk-python-cosmos-async/"))
2222
self.assertTrue(client_async.client_connection._user_agent != client_sync.client_connection._user_agent)
2323

24+
async def test_client_user_agent_suffix(self):
25+
async with async_client(url=TestConfig.host, credential=TestConfig.masterKey, user_agent_suffix="testAsyncSuffix") as client_async:
26+
client_sync = sync_client(url=TestConfig.host, credential=TestConfig.masterKey, user_agent_suffix="testSyncSuffix")
27+
28+
self.assertTrue(client_sync.client_connection._user_agent.endswith(" testSyncSuffix"))
29+
self.assertTrue(client_async.client_connection._user_agent.endswith(" testAsyncSuffix"))
30+
self.assertTrue(client_async.client_connection._user_agent != client_sync.client_connection._user_agent)
31+
2432

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

0 commit comments

Comments
 (0)