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

Runtime Config: add client_info support to client / connection. #7871

Merged
merged 4 commits into from
May 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
14 changes: 9 additions & 5 deletions runtimeconfig/google/cloud/runtimeconfig/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,22 @@
from google.cloud.runtimeconfig import __version__


_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__)


class Connection(_http.JSONConnection):
"""A connection to Google Cloud RuntimeConfig via the JSON REST API.

:type client: :class:`~google.cloud.runtimeconfig.client.Client`
:param client: The client that owns the current connection.

:type client_info: :class:`~google.api_core.client_info.ClientInfo`
:param client_info: (Optional) instance used to generate user agent.
"""

def __init__(self, client, client_info=None):
super(Connection, self).__init__(client, client_info)

self._client_info.gapic_version = __version__
self._client_info.client_library_version = __version__

API_BASE_URL = "https://runtimeconfig.googleapis.com"
"""The base of the API call URL."""

Expand All @@ -39,5 +45,3 @@ class Connection(_http.JSONConnection):

API_URL_TEMPLATE = "{api_base_url}/{api_version}{path}"
"""A template for the URL of a particular API call."""

_EXTRA_HEADERS = {_http.CLIENT_INFO_HEADER: _CLIENT_INFO}
11 changes: 9 additions & 2 deletions runtimeconfig/google/cloud/runtimeconfig/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,23 @@ class Client(ClientWithProject):
``credentials`` for the current object.
This parameter should be considered private, and could
change in the future.

:type client_info: :class:`~google.api_core.client_info.ClientInfo`
:param client_info:
The client info used to send a user-agent string along with API
requests. If ``None``, then default info will be used. Generally,
you only need to set this if you're developing your own library
or partner tool.
"""

SCOPE = ("https://www.googleapis.com/auth/cloudruntimeconfig",)
"""The scopes required for authenticating as a RuntimeConfig consumer."""

def __init__(self, project=None, credentials=None, _http=None):
def __init__(self, project=None, credentials=None, _http=None, client_info=None):
super(Client, self).__init__(
project=project, credentials=credentials, _http=_http
)
self._connection = Connection(self)
self._connection = Connection(self, client_info=client_info)

def config(self, config_name):
"""Factory constructor for config object.
Expand Down
6 changes: 4 additions & 2 deletions runtimeconfig/noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ def lint_setup_py(session):
def default(session):
"""Default unit test session.
"""
# Install all test dependencies, then install this package in-place.
session.install('mock', 'pytest', 'pytest-cov', *LOCAL_DEPS)
# Install all test dependencies, then install local packages in-place.
session.install('mock', 'pytest', 'pytest-cov')
for local_dep in LOCAL_DEPS:
session.install('-e', local_dep)
session.install('-e', '.')

# Run py.test against the unit tests.
Expand Down
6 changes: 2 additions & 4 deletions runtimeconfig/tests/unit/test__http.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ def test_default_url(self):

def test_extra_headers(self):
import requests

from google.cloud import _http as base_http
from google.cloud.runtimeconfig import _http as MUT

http = mock.create_autospec(requests.Session, instance=True)
response = requests.Response()
Expand All @@ -53,8 +51,8 @@ def test_extra_headers(self):

expected_headers = {
"Accept-Encoding": "gzip",
base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO,
"User-Agent": conn.USER_AGENT,
base_http.CLIENT_INFO_HEADER: conn.user_agent,
"User-Agent": conn.user_agent,
}
expected_uri = conn.build_api_url("/rainbow")
http.request.assert_called_once_with(
Expand Down
37 changes: 34 additions & 3 deletions runtimeconfig/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,46 @@ def _get_target_class():
def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)

def test_ctor_wo_client_info(self):
from google.cloud._http import ClientInfo
from google.cloud.runtimeconfig._http import Connection

PROJECT = "PROJECT"
http = object()
creds = _make_credentials()

client = self._make_one(project=PROJECT, credentials=creds, _http=http)
self.assertIsInstance(client._connection, Connection)
self.assertIs(client._credentials, creds)
self.assertIs(client._http_internal, http)
self.assertIsInstance(client._connection._client_info, ClientInfo)

def test_ctor_w_client_info(self):
from google.cloud._http import ClientInfo
from google.cloud.runtimeconfig._http import Connection

PROJECT = "PROJECT"
http = object()
creds = _make_credentials()
client_info = ClientInfo()

client = self._make_one(
project=PROJECT, credentials=creds, _http=http, client_info=client_info
)
self.assertIsInstance(client._connection, Connection)
self.assertIs(client._credentials, creds)
self.assertIs(client._http_internal, http)
self.assertIs(client._connection._client_info, client_info)

def test_config(self):
PROJECT = "PROJECT"
CONFIG_NAME = "config_name"
creds = _make_credentials()

client_obj = self._make_one(project=PROJECT, credentials=creds)
new_config = client_obj.config(CONFIG_NAME)
client = self._make_one(project=PROJECT, credentials=creds)
new_config = client.config(CONFIG_NAME)
self.assertEqual(new_config.name, CONFIG_NAME)
self.assertIs(new_config._client, client_obj)
self.assertIs(new_config._client, client)
self.assertEqual(new_config.project, PROJECT)
self.assertEqual(
new_config.full_name, "projects/%s/configs/%s" % (PROJECT, CONFIG_NAME)
Expand Down