From 3907159af95c5daf027f5fecf267830c15a8ff14 Mon Sep 17 00:00:00 2001 From: Emmanuel T Odeke Date: Sat, 7 Dec 2019 22:32:15 -0800 Subject: [PATCH] spanner/dbapi: pass user_agent+python_version in ClientInfo `user_agent` being passed directly a spanner.Client(**) has been deprecated in favor of passing in a field `client_info` of type google.api_core.gapic_v1.client_info.ClientInfo in which we can specify: * user_agent * python_version and that results in the following header being sent over the wire: 'x-goog-api-client': 'spanner-django/0.0.1 gl-python/3.7.3 grpc/1.21.1 gax/1.14.3' Fixes #139 --- spanner/dbapi/__init__.py | 4 ++-- spanner/dbapi/version.py | 19 +++++++++++++++++- tests/spanner/dbapi/test_version.py | 30 +++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 tests/spanner/dbapi/test_version.py diff --git a/spanner/dbapi/__init__.py b/spanner/dbapi/__init__.py index 926c6b7c1f..45563a7b33 100644 --- a/spanner/dbapi/__init__.py +++ b/spanner/dbapi/__init__.py @@ -29,7 +29,7 @@ BINARY, DATETIME, NUMBER, ROWID, STRING, Date, DateFromTicks, Time, TimeFromTicks, Timestamp, TimestampFromTicks, ) -from .version import USER_AGENT +from .version import google_client_info # Globals that MUST be defined ### apilevel = "2.0" # Implements the Python Database API specification 2.0 version. @@ -74,7 +74,7 @@ def connect(spanner_url, credentials_uri=None): kwargs = dict( project=conn_params.get('project_id'), - user_agent=USER_AGENT, + client_info=google_client_info(), ) # Pre-requisite are the database and instance names. diff --git a/spanner/dbapi/version.py b/spanner/dbapi/version.py index 1eb1f380f7..7152a0830a 100644 --- a/spanner/dbapi/version.py +++ b/spanner/dbapi/version.py @@ -12,6 +12,23 @@ # See the License for the specific language governing permissions and # limitations under the License. +import sys -VERSION = 'v0.0.1' +from google.api_core.gapic_v1.client_info import ClientInfo + +VERSION = '0.0.1' USER_AGENT = 'spanner-django/' + VERSION + +vers = sys.version_info + + +def google_client_info(): + """ + Return a google.api_core.gapic_v1.client_info.ClientInfo + containg the user_agent and python_version for this library + """ + + return ClientInfo( + user_agent=USER_AGENT, + python_version='%d.%d.%d' % (vers.major, vers.minor, vers.micro or 0), + ) diff --git a/tests/spanner/dbapi/test_version.py b/tests/spanner/dbapi/test_version.py new file mode 100644 index 0000000000..04f8ff1db3 --- /dev/null +++ b/tests/spanner/dbapi/test_version.py @@ -0,0 +1,30 @@ +# Copyright 2019 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import sys +from unittest import TestCase + +from google.api_core.gapic_v1.client_info import ClientInfo +from spanner.dbapi.version import USER_AGENT, google_client_info + + +class VersionUtils(TestCase): + def test_google_client_info(self): + vers = sys.version_info + got = google_client_info().to_grpc_metadata() + want = ClientInfo( + user_agent=USER_AGENT, + python_version='%d.%d.%d' % (vers.major, vers.minor, vers.micro or 0), + ).to_grpc_metadata() + self.assertEqual(got, want)