Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user agent prefix google-cloud-ndb + version #299

Merged
merged 4 commits into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions google/cloud/ndb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions google/cloud/ndb/_datastore_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions google/cloud/ndb/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions tests/unit/test__datastore_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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():
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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