diff --git a/google/cloud/ndb/__init__.py b/google/cloud/ndb/__init__.py index 5fb5a3f4..d20d5d80 100644 --- a/google/cloud/ndb/__init__.py +++ b/google/cloud/ndb/__init__.py @@ -17,9 +17,13 @@ It was originally included in the Google App Engine runtime as a "new" version of the ``db`` API (hence ``ndb``). +.. autodata:: __version__ .. autodata:: __all__ """ +from pkg_resources import get_distribution + +__version__ = get_distribution("google-cloud-ndb").version from google.cloud.ndb.client import Client from google.cloud.ndb.context import AutoBatcher @@ -124,8 +128,6 @@ from google.cloud.ndb._transaction import transactional_tasklet from google.cloud.ndb._transaction import non_transactional - -"""Current ``ndb`` version.""" __all__ = [ "AutoBatcher", "Client", diff --git a/google/cloud/ndb/_datastore_api.py b/google/cloud/ndb/_datastore_api.py index 37f4bab7..cf30362f 100644 --- a/google/cloud/ndb/_datastore_api.py +++ b/google/cloud/ndb/_datastore_api.py @@ -20,7 +20,6 @@ import grpc from google.cloud import _helpers -from google.cloud import _http from google.cloud.datastore import helpers from google.cloud.datastore_v1.proto import datastore_pb2 from google.cloud.datastore_v1.proto import datastore_pb2_grpc @@ -69,8 +68,9 @@ def make_stub(client): The stub instance. """ if client.secure: + user_agent = client.client_info.to_user_agent() channel = _helpers.make_secure_channel( - client._credentials, _http.DEFAULT_USER_AGENT, client.host + client._credentials, user_agent, client.host ) else: channel = grpc.insecure_channel(client.host) diff --git a/google/cloud/ndb/client.py b/google/cloud/ndb/client.py index 2af0c3d2..56067fb2 100644 --- a/google/cloud/ndb/client.py +++ b/google/cloud/ndb/client.py @@ -18,13 +18,19 @@ import os import requests +from google.api_core import client_info from google.cloud import environment_vars from google.cloud import _helpers from google.cloud import client as google_client from google.cloud.datastore_v1.gapic import datastore_client +from google.cloud.ndb import __version__ from google.cloud.ndb import context as context_module +_CLIENT_INFO = client_info.ClientInfo( + user_agent="google-cloud-ndb/{}".format(__version__) +) + DATASTORE_API_HOST = datastore_client.DatastoreClient.SERVICE_ADDRESS.rsplit( ":", 1 )[0] @@ -85,6 +91,7 @@ def __init__(self, project=None, namespace=None, credentials=None): self.host = os.environ.get( environment_vars.GCD_HOST, DATASTORE_API_HOST ) + self.client_info = _CLIENT_INFO # Use insecure connection when using Datastore Emulator, otherwise # use secure connection diff --git a/tests/unit/test__datastore_api.py b/tests/unit/test__datastore_api.py index ccd61d09..6cd7a438 100644 --- a/tests/unit/test__datastore_api.py +++ b/tests/unit/test__datastore_api.py @@ -19,7 +19,7 @@ import pytest -from google.cloud import _http +from google.api_core import client_info from google.cloud.datastore import entity from google.cloud.datastore import helpers from google.cloud.datastore import key as ds_key_module @@ -33,6 +33,7 @@ from google.cloud.ndb import model from google.cloud.ndb import _options from google.cloud.ndb import tasklets +from google.cloud.ndb import __version__ from tests.unit import utils @@ -54,6 +55,9 @@ def test_secure_channel(datastore_pb2_grpc, _helpers): secure=True, host="thehost", spec=("_credentials", "secure", "host"), + client_info=client_info.ClientInfo( + user_agent="google-cloud-ndb/{}".format(__version__) + ), ) context = context_module.Context(client) with context.use(): @@ -62,7 +66,7 @@ def test_secure_channel(datastore_pb2_grpc, _helpers): assert stub is datastore_pb2_grpc.DatastoreStub.return_value datastore_pb2_grpc.DatastoreStub.assert_called_once_with(channel) _helpers.make_secure_channel.assert_called_once_with( - "creds", _http.DEFAULT_USER_AGENT, "thehost" + "creds", client.client_info.to_user_agent(), "thehost" ) @staticmethod diff --git a/tests/unit/test_client.py b/tests/unit/test_client.py index 91f5c0c9..e5784426 100644 --- a/tests/unit/test_client.py +++ b/tests/unit/test_client.py @@ -133,3 +133,13 @@ def finish_up(): with client.context(): _eventloop.call_soon(finish_up) + + @staticmethod + def test_client_info(): + with patch_credentials("testing"): + client = client_module.Client() + agent = client.client_info.to_user_agent() + assert "google-cloud-ndb" in agent + version = agent.split("/")[1] + assert version[0].isdigit() + assert "." in version