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

spanner/dbapi: pass user_agent+python_version in ClientInfo #140

Merged
merged 1 commit into from
Dec 8, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
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)