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

Logging: Add client_options to v1 #9046

Merged
merged 1 commit into from
Aug 28, 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
13 changes: 8 additions & 5 deletions logging/google/cloud/logging/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,20 @@ class Connection(_http.JSONConnection):

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

:type client_options: :class:`~google.api_core.client_options.ClientOptions`
:param client_options (Optional) Client options used to set user options
on the client. API Endpoint should be set through client_options.
"""

def __init__(self, client, client_info=None):
super(Connection, self).__init__(client, client_info)
DEFAULT_API_ENDPOINT = "https://logging.googleapis.com"

def __init__(self, client, client_info=None, api_endpoint=DEFAULT_API_ENDPOINT):
super(Connection, self).__init__(client, client_info)
self.API_BASE_URL = api_endpoint
self._client_info.gapic_version = __version__
self._client_info.client_library_version = __version__

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

API_VERSION = "v2"
"""The version of the API, used in building the API call's URL."""

Expand Down
20 changes: 19 additions & 1 deletion logging/google/cloud/logging/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
else:
_HAVE_GRPC = True

import google.api_core.client_options
from google.cloud.client import ClientWithProject
from google.cloud.environment_vars import DISABLE_GRPC
from google.cloud.logging._helpers import retrieve_metadata_server
Expand Down Expand Up @@ -95,6 +96,10 @@ class Client(ClientWithProject):
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.
:type client_options: :class:`~google.api_core.client_options.ClientOptions`
or :class:`dict`
:param client_options: (Optional) Client options used to set user options
on the client. API Endpoint should be set through client_options.
"""

_logging_api = None
Expand All @@ -116,11 +121,24 @@ def __init__(
_http=None,
_use_grpc=None,
client_info=None,
client_options=None,
):
super(Client, self).__init__(
project=project, credentials=credentials, _http=_http
)
self._connection = Connection(self, client_info=client_info)

kw_args = {"client_info": client_info}
if client_options:
if type(client_options) == dict:
client_options = google.api_core.client_options.from_dict(
client_options
)
if client_options.api_endpoint:
api_endpoint = client_options.api_endpoint
kw_args["api_endpoint"] = api_endpoint

self._connection = Connection(self, **kw_args)

self._client_info = client_info
if _use_grpc is None:
self._use_grpc = _USE_GRPC
Expand Down
6 changes: 6 additions & 0 deletions logging/tests/unit/test__http.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ def test_default_url(self):
conn = self._make_one(client)
self.assertIs(conn._client, client)

def test_build_api_url_w_custom_endpoint(self):
custom_endpoint = "https://foo-logging.googleapis.com"
conn = self._make_one(object(), api_endpoint=custom_endpoint)
URI = "/".join([custom_endpoint, conn.API_VERSION, "foo"])
self.assertEqual(conn.build_api_url("/foo"), URI)

def test_extra_headers(self):
import requests
from google.cloud import _http as base_http
Expand Down
36 changes: 36 additions & 0 deletions logging/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,42 @@ def test_ctor_explicit(self):
self.assertIsInstance(client._connection, Connection)
self.assertIs(client._connection._client_info, client_info)

def test_ctor_w_empty_client_options(self):
from google.api_core.client_options import ClientOptions

creds = _make_credentials()
client_options = ClientOptions()
client = self._make_one(
project=self.PROJECT, credentials=creds, client_options=client_options
)
self.assertEqual(
client._connection.API_BASE_URL, client._connection.DEFAULT_API_ENDPOINT
)

def test_ctor_w_client_options_object(self):
from google.api_core.client_options import ClientOptions

creds = _make_credentials()
client_options = ClientOptions(
api_endpoint="https://foo-logging.googleapis.com"
)
client = self._make_one(
project=self.PROJECT, credentials=creds, client_options=client_options
)
self.assertEqual(
client._connection.API_BASE_URL, "https://foo-logging.googleapis.com"
)

def test_ctor_w_client_options_dict(self):
creds = _make_credentials()
client_options = {"api_endpoint": "https://foo-logging.googleapis.com"}
client = self._make_one(
project=self.PROJECT, credentials=creds, client_options=client_options
)
self.assertEqual(
client._connection.API_BASE_URL, "https://foo-logging.googleapis.com"
)

def test_logging_api_wo_gapic(self):
from google.cloud.logging._http import _LoggingAPI

Expand Down