Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cdb2486

Browse filesBrowse files
authored
feat(grpc_user_agent): send user_agent to grpc channel (#1712)
* feat(async_grpc_client): send `user_agent` to grpc channel by providing it as channel arg - "grpc.primary_user_agent". * This will send "user_agent" to all RPC calls.
1 parent 2e1a1eb commit cdb2486
Copy full SHA for cdb2486

File tree

Expand file treeCollapse file tree

2 files changed

+68
-14
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+68
-14
lines changed
Open diff view settings
Collapse file

‎google/cloud/storage/_experimental/asyncio/async_grpc_client.py‎

Copy file name to clipboardExpand all lines: google/cloud/storage/_experimental/asyncio/async_grpc_client.py
+12-3Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
"""An async client for interacting with Google Cloud Storage using the gRPC API."""
1616

1717
from google.cloud import _storage_v2 as storage_v2
18+
from google.cloud._storage_v2.services.storage.transports.base import (
19+
DEFAULT_CLIENT_INFO,
20+
)
1821

1922

2023
class AsyncGrpcClient:
@@ -30,7 +33,7 @@ class AsyncGrpcClient:
3033
The client info used to send a user-agent string along with API
3134
requests. If ``None``, then default info will be used.
3235
33-
:type client_options: :class:`~google.api_core.client_options.ClientOptions` or :class:`dict`
36+
:type client_options: :class:`~google.api_core.client_options.ClientOptions`
3437
:param client_options: (Optional) Client options used to set user options
3538
on the client.
3639
@@ -39,7 +42,6 @@ class AsyncGrpcClient:
3942
(Optional) Whether to attempt to use DirectPath for gRPC connections.
4043
Defaults to ``True``.
4144
"""
42-
4345
def __init__(
4446
self,
4547
credentials=None,
@@ -65,8 +67,15 @@ def _create_async_grpc_client(
6567
transport_cls = storage_v2.StorageAsyncClient.get_transport_class(
6668
"grpc_asyncio"
6769
)
70+
71+
if client_info is None:
72+
client_info = DEFAULT_CLIENT_INFO
73+
primary_user_agent = client_info.to_user_agent()
74+
6875
channel = transport_cls.create_channel(
69-
attempt_direct_path=attempt_direct_path, credentials=credentials
76+
attempt_direct_path=attempt_direct_path,
77+
credentials=credentials,
78+
options=(("grpc.primary_user_agent", primary_user_agent),),
7079
)
7180
transport = transport_cls(channel=channel)
7281

Collapse file

‎tests/unit/asyncio/test_async_grpc_client.py‎

Copy file name to clipboardExpand all lines: tests/unit/asyncio/test_async_grpc_client.py
+56-11Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
from unittest import mock
1717
from google.auth import credentials as auth_credentials
1818
from google.auth.credentials import AnonymousCredentials
19+
from google.api_core import client_info as client_info_lib
20+
from google.cloud.storage._experimental.asyncio import async_grpc_client
21+
from google.cloud.storage._experimental.asyncio.async_grpc_client import (
22+
DEFAULT_CLIENT_INFO,
23+
)
1924

2025

2126
def _make_credentials(spec=None):
@@ -27,32 +32,60 @@ def _make_credentials(spec=None):
2732
class TestAsyncGrpcClient(unittest.TestCase):
2833
@mock.patch("google.cloud._storage_v2.StorageAsyncClient")
2934
def test_constructor_default_options(self, mock_async_storage_client):
30-
from google.cloud.storage._experimental.asyncio import async_grpc_client
31-
35+
# Arrange
3236
mock_transport_cls = mock.MagicMock()
3337
mock_async_storage_client.get_transport_class.return_value = mock_transport_cls
3438
mock_creds = _make_credentials()
3539

40+
primary_user_agent = DEFAULT_CLIENT_INFO.to_user_agent()
41+
expected_options = (("grpc.primary_user_agent", primary_user_agent),)
42+
43+
# Act
3644
async_grpc_client.AsyncGrpcClient(credentials=mock_creds)
3745

46+
# Assert
3847
mock_async_storage_client.get_transport_class.assert_called_once_with(
3948
"grpc_asyncio"
4049
)
4150
mock_transport_cls.create_channel.assert_called_once_with(
42-
attempt_direct_path=True, credentials=mock_creds
51+
attempt_direct_path=True,
52+
credentials=mock_creds,
53+
options=expected_options,
4354
)
4455
mock_channel = mock_transport_cls.create_channel.return_value
4556
mock_transport_cls.assert_called_once_with(channel=mock_channel)
4657
mock_transport = mock_transport_cls.return_value
4758
mock_async_storage_client.assert_called_once_with(
4859
transport=mock_transport,
4960
client_options=None,
50-
client_info=None,
61+
client_info=DEFAULT_CLIENT_INFO,
62+
)
63+
64+
@mock.patch("google.cloud._storage_v2.StorageAsyncClient")
65+
def test_constructor_with_client_info(self, mock_async_storage_client):
66+
67+
mock_transport_cls = mock.MagicMock()
68+
mock_async_storage_client.get_transport_class.return_value = mock_transport_cls
69+
mock_creds = _make_credentials()
70+
client_info = client_info_lib.ClientInfo(
71+
client_library_version="1.2.3",
72+
)
73+
74+
async_grpc_client.AsyncGrpcClient(
75+
credentials=mock_creds, client_info=client_info
76+
)
77+
78+
primary_user_agent = client_info.to_user_agent()
79+
expected_options = (("grpc.primary_user_agent", primary_user_agent),)
80+
81+
mock_transport_cls.create_channel.assert_called_once_with(
82+
attempt_direct_path=True,
83+
credentials=mock_creds,
84+
options=expected_options,
5185
)
5286

5387
@mock.patch("google.cloud._storage_v2.StorageAsyncClient")
5488
def test_constructor_disables_directpath(self, mock_async_storage_client):
55-
from google.cloud.storage._experimental.asyncio import async_grpc_client
5689

5790
mock_transport_cls = mock.MagicMock()
5891
mock_async_storage_client.get_transport_class.return_value = mock_transport_cls
@@ -62,15 +95,19 @@ def test_constructor_disables_directpath(self, mock_async_storage_client):
6295
credentials=mock_creds, attempt_direct_path=False
6396
)
6497

98+
primary_user_agent = DEFAULT_CLIENT_INFO.to_user_agent()
99+
expected_options = (("grpc.primary_user_agent", primary_user_agent),)
100+
65101
mock_transport_cls.create_channel.assert_called_once_with(
66-
attempt_direct_path=False, credentials=mock_creds
102+
attempt_direct_path=False,
103+
credentials=mock_creds,
104+
options=expected_options,
67105
)
68106
mock_channel = mock_transport_cls.create_channel.return_value
69107
mock_transport_cls.assert_called_once_with(channel=mock_channel)
70108

71109
@mock.patch("google.cloud._storage_v2.StorageAsyncClient")
72110
def test_grpc_client_property(self, mock_grpc_gapic_client):
73-
from google.cloud.storage._experimental.asyncio import async_grpc_client
74111

75112
# Arrange
76113
mock_transport_cls = mock.MagicMock()
@@ -81,7 +118,8 @@ def test_grpc_client_property(self, mock_grpc_gapic_client):
81118
mock_transport_cls.return_value = mock.sentinel.transport
82119

83120
mock_creds = _make_credentials()
84-
mock_client_info = mock.sentinel.client_info
121+
mock_client_info = mock.MagicMock(spec=client_info_lib.ClientInfo)
122+
mock_client_info.to_user_agent.return_value = "test-user-agent"
85123
mock_client_options = mock.sentinel.client_options
86124
mock_attempt_direct_path = mock.sentinel.attempt_direct_path
87125

@@ -102,8 +140,11 @@ def test_grpc_client_property(self, mock_grpc_gapic_client):
102140
retrieved_client = client.grpc_client
103141

104142
# Assert
143+
expected_options = (("grpc.primary_user_agent", "test-user-agent"),)
105144
mock_transport_cls.create_channel.assert_called_once_with(
106-
attempt_direct_path=mock_attempt_direct_path, credentials=mock_creds
145+
attempt_direct_path=mock_attempt_direct_path,
146+
credentials=mock_creds,
147+
options=expected_options,
107148
)
108149
mock_transport_cls.assere_with(channel=channel_sentinel)
109150
mock_grpc_gapic_client.assert_called_once_with(
@@ -115,7 +156,6 @@ def test_grpc_client_property(self, mock_grpc_gapic_client):
115156

116157
@mock.patch("google.cloud._storage_v2.StorageAsyncClient")
117158
def test_grpc_client_with_anon_creds(self, mock_grpc_gapic_client):
118-
from google.cloud.storage._experimental.asyncio import async_grpc_client
119159

120160
# Arrange
121161
mock_transport_cls = mock.MagicMock()
@@ -133,7 +173,12 @@ def test_grpc_client_with_anon_creds(self, mock_grpc_gapic_client):
133173
# Assert
134174
self.assertIs(retrieved_client, mock_grpc_gapic_client.return_value)
135175

176+
primary_user_agent = DEFAULT_CLIENT_INFO.to_user_agent()
177+
expected_options = (("grpc.primary_user_agent", primary_user_agent),)
178+
136179
mock_transport_cls.create_channel.assert_called_once_with(
137-
attempt_direct_path=True, credentials=anonymous_creds
180+
attempt_direct_path=True,
181+
credentials=anonymous_creds,
182+
options=expected_options,
138183
)
139184
mock_transport_cls.assert_called_once_with(channel=channel_sentinel)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.