Skip to content

Commit

Permalink
spanner/dbapi: pass user_agent+python_version in ClientInfo
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
odeke-em committed Dec 8, 2019
1 parent 13dbb62 commit 3907159
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
4 changes: 2 additions & 2 deletions spanner/dbapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
19 changes: 18 additions & 1 deletion spanner/dbapi/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
30 changes: 30 additions & 0 deletions tests/spanner/dbapi/test_version.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 3907159

Please sign in to comment.