2020from google .cloud import exceptions
2121from google .cloud .datastore_v1 .proto import datastore_pb2 as _datastore_pb2
2222
23- from google .cloud .datastore import __version__
24-
2523
2624DATASTORE_API_HOST = "datastore.googleapis.com"
2725"""Datastore API request host."""
3230API_URL_TEMPLATE = "{api_base}/{api_version}/projects" "/{project}:{method}"
3331"""A template for the URL of a particular API call."""
3432
35- _CLIENT_INFO = connection_module .CLIENT_INFO_TEMPLATE .format (__version__ )
36-
3733
38- def _request (http , project , method , data , base_url ):
34+ def _request (http , project , method , data , base_url , client_info ):
3935 """Make a request over the Http transport to the Cloud Datastore API.
4036
4137 :type http: :class:`requests.Session`
@@ -55,15 +51,19 @@ def _request(http, project, method, data, base_url):
5551 :type base_url: str
5652 :param base_url: The base URL where the API lives.
5753
54+ :type client_info: :class:`google.api_core.client_info.ClientInfo`
55+ :param client_info: used to generate user agent.
56+
5857 :rtype: str
5958 :returns: The string response content from the API call.
6059 :raises: :class:`google.cloud.exceptions.GoogleCloudError` if the
6160 response code is not 200 OK.
6261 """
62+ user_agent = client_info .to_user_agent ()
6363 headers = {
6464 "Content-Type" : "application/x-protobuf" ,
65- "User-Agent" : connection_module . DEFAULT_USER_AGENT ,
66- connection_module .CLIENT_INFO_HEADER : _CLIENT_INFO ,
65+ "User-Agent" : user_agent ,
66+ connection_module .CLIENT_INFO_HEADER : user_agent ,
6767 }
6868 api_url = build_api_url (project , method , base_url )
6969
@@ -78,7 +78,7 @@ def _request(http, project, method, data, base_url):
7878 return response .content
7979
8080
81- def _rpc (http , project , method , base_url , request_pb , response_pb_cls ):
81+ def _rpc (http , project , method , base_url , client_info , request_pb , response_pb_cls ):
8282 """Make a protobuf RPC request.
8383
8484 :type http: :class:`requests.Session`
@@ -94,6 +94,9 @@ def _rpc(http, project, method, base_url, request_pb, response_pb_cls):
9494 :type base_url: str
9595 :param base_url: The base URL where the API lives.
9696
97+ :type client_info: :class:`google.api_core.client_info.ClientInfo`
98+ :param client_info: used to generate user agent.
99+
97100 :type request_pb: :class:`google.protobuf.message.Message` instance
98101 :param request_pb: the protobuf instance representing the request.
99102
@@ -106,7 +109,7 @@ def _rpc(http, project, method, base_url, request_pb, response_pb_cls):
106109 :returns: The RPC message parsed from the response.
107110 """
108111 req_data = request_pb .SerializeToString ()
109- response = _request (http , project , method , req_data , base_url )
112+ response = _request (http , project , method , req_data , base_url , client_info )
110113 return response_pb_cls .FromString (response )
111114
112115
@@ -172,6 +175,7 @@ def lookup(self, project_id, keys, read_options=None):
172175 project_id ,
173176 "lookup" ,
174177 self .client ._base_url ,
178+ self .client ._client_info ,
175179 request_pb ,
176180 _datastore_pb2 .LookupResponse ,
177181 )
@@ -217,6 +221,7 @@ def run_query(
217221 project_id ,
218222 "runQuery" ,
219223 self .client ._base_url ,
224+ self .client ._client_info ,
220225 request_pb ,
221226 _datastore_pb2 .RunQueryResponse ,
222227 )
@@ -240,6 +245,7 @@ def begin_transaction(self, project_id, transaction_options=None):
240245 project_id ,
241246 "beginTransaction" ,
242247 self .client ._base_url ,
248+ self .client ._client_info ,
243249 request_pb ,
244250 _datastore_pb2 .BeginTransactionResponse ,
245251 )
@@ -278,6 +284,7 @@ def commit(self, project_id, mode, mutations, transaction=None):
278284 project_id ,
279285 "commit" ,
280286 self .client ._base_url ,
287+ self .client ._client_info ,
281288 request_pb ,
282289 _datastore_pb2 .CommitResponse ,
283290 )
@@ -304,6 +311,7 @@ def rollback(self, project_id, transaction):
304311 project_id ,
305312 "rollback" ,
306313 self .client ._base_url ,
314+ self .client ._client_info ,
307315 request_pb ,
308316 _datastore_pb2 .RollbackResponse ,
309317 )
@@ -327,6 +335,7 @@ def allocate_ids(self, project_id, keys):
327335 project_id ,
328336 "allocateIds" ,
329337 self .client ._base_url ,
338+ self .client ._client_info ,
330339 request_pb ,
331340 _datastore_pb2 .AllocateIdsResponse ,
332341 )
0 commit comments