From dc02e22deb303e2755060dd63f36bd64e8e3f030 Mon Sep 17 00:00:00 2001 From: Andrew Gorcester Date: Thu, 23 Jan 2020 20:00:36 -0800 Subject: [PATCH 1/3] Add user agent prefix google-cloud-python + version --- docs/conf.py | 5 +++++ google/cloud/ndb/__init__.py | 6 ++++-- google/cloud/ndb/_datastore_api.py | 3 ++- google/cloud/ndb/client.py | 7 +++++++ tests/unit/test__datastore_api.py | 7 ++++++- tests/unit/test_client.py | 10 ++++++++++ 6 files changed, 34 insertions(+), 4 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 3719bd85..27683ad8 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -25,6 +25,11 @@ copyright = "2018, Google" author = "Google APIs" +# The full version, including alpha/beta/rc tags. +release = google.cloud.ndb.__version__ +# The short X.Y version. +version = ".".join(release.split(".")[:2]) + # -- General configuration --------------------------------------------------- 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..ac0b2e84 100644 --- a/google/cloud/ndb/_datastore_api.py +++ b/google/cloud/ndb/_datastore_api.py @@ -69,8 +69,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..1574ee5c 100644 --- a/tests/unit/test__datastore_api.py +++ b/tests/unit/test__datastore_api.py @@ -19,6 +19,7 @@ import pytest +from google.api_core import client_info from google.cloud import _http from google.cloud.datastore import entity from google.cloud.datastore import helpers @@ -33,6 +34,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 +56,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 +67,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 From dfa5609eccfca9af6d09fe40a31cd9c61a937266 Mon Sep 17 00:00:00 2001 From: Andrew Gorcester Date: Fri, 24 Jan 2020 13:27:56 -0800 Subject: [PATCH 2/3] remove unused code in conf.py --- docs/conf.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 27683ad8..3719bd85 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -25,11 +25,6 @@ copyright = "2018, Google" author = "Google APIs" -# The full version, including alpha/beta/rc tags. -release = google.cloud.ndb.__version__ -# The short X.Y version. -version = ".".join(release.split(".")[:2]) - # -- General configuration --------------------------------------------------- From 6d0b3ebc5217e068f64bd96ed5b39ee4ba719fda Mon Sep 17 00:00:00 2001 From: Andrew Gorcester Date: Fri, 24 Jan 2020 13:58:13 -0800 Subject: [PATCH 3/3] remove unused import for lint --- google/cloud/ndb/_datastore_api.py | 1 - tests/unit/test__datastore_api.py | 1 - 2 files changed, 2 deletions(-) diff --git a/google/cloud/ndb/_datastore_api.py b/google/cloud/ndb/_datastore_api.py index ac0b2e84..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 diff --git a/tests/unit/test__datastore_api.py b/tests/unit/test__datastore_api.py index 1574ee5c..6cd7a438 100644 --- a/tests/unit/test__datastore_api.py +++ b/tests/unit/test__datastore_api.py @@ -20,7 +20,6 @@ import pytest from google.api_core import client_info -from google.cloud import _http from google.cloud.datastore import entity from google.cloud.datastore import helpers from google.cloud.datastore import key as ds_key_module