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

Core: Add client options. #8265

Merged
merged 9 commits into from
Jun 24, 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
48 changes: 48 additions & 0 deletions api_core/google/api_core/client_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# 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
#
# http://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.

"""Client options class.

Client options provide a consistent interface for user options to be defined
across clients.
"""


class ClientOptions(object):
"""Client Options used to set options on clients.

Args:
api_endpoint (str): The desired API endpoint, e.g., compute.googleapis.com
"""

def __init__(self, api_endpoint=None):
self.api_endpoint = api_endpoint


def from_dict(options):
"""Construct a client options object from a dictionary.

Args:
options (dict): A dictionary with client options.
"""

client_options = ClientOptions()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would allow users to do something like:

client = pubsub_v1.PublisherClient(client_options={"api_endpoint": "...", "user_project": "..."})


for key, value in options.items():
if hasattr(client_options, key):
setattr(client_options, key, value)
else:
raise ValueError("ClientOptions does not accept an option '" + key + "'")

return client_options
36 changes: 36 additions & 0 deletions api_core/tests/unit/test_client_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 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
#
# http://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 pytest

from google.api_core import client_options


def test_constructor():
options = client_options.ClientOptions(api_endpoint="foo.googleapis.com")

assert options.api_endpoint == "foo.googleapis.com"


def test_from_dict():
options = client_options.from_dict({"api_endpoint": "foo.googleapis.com"})

assert options.api_endpoint == "foo.googleapis.com"


def test_from_dict_bad_argument():
with pytest.raises(ValueError):
client_options.from_dict(
{"api_endpoint": "foo.googleapis.com", "bad_arg": "1234"}
)
1 change: 1 addition & 0 deletions iot/.coveragerc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Generated by synthtool. DO NOT EDIT!
[run]
branch = True

Expand Down
1 change: 1 addition & 0 deletions iot/.flake8
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Generated by synthtool. DO NOT EDIT!
[flake8]
ignore = E203, E266, E501, W503
exclude =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ def __init__(
}

@classmethod
def create_channel(cls, address="cloudiot.googleapis.com:443", credentials=None):
def create_channel(
cls, address="cloudiot.googleapis.com:443", credentials=None, **kwargs
):
"""Create and return a gRPC channel object.

Args:
Expand All @@ -82,12 +84,14 @@ def create_channel(cls, address="cloudiot.googleapis.com:443", credentials=None)
credentials identify this application to the service. If
none are specified, the client will attempt to ascertain
the credentials from the environment.
kwargs (dict): Keyword arguments, which are passed to the
channel creation.

Returns:
grpc.Channel: A gRPC channel object.
"""
return google.api_core.grpc_helpers.create_channel(
address, credentials=credentials, scopes=cls._OAUTH_SCOPES
address, credentials=credentials, scopes=cls._OAUTH_SCOPES, **kwargs
)

@property
Expand Down
1 change: 1 addition & 0 deletions iot/google/cloud/iot_v1/proto/device_manager_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions iot/google/cloud/iot_v1/proto/resources_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions iot/noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Generated by synthtool. DO NOT EDIT!

from __future__ import absolute_import
import os
import shutil
Expand Down Expand Up @@ -46,7 +48,7 @@ def blacken(session):
"""Run black.

Format code to uniform standard.

This currently uses Python 3.6 due to the automated Kokoro run of synthtool.
That run uses an image that doesn't have 3.6 installed. Before updating this
check the state of the `gcp_ubuntu_config` we use for that Kokoro run.
Expand Down Expand Up @@ -78,7 +80,7 @@ def default(session):
"--cov-append",
"--cov-config=.coveragerc",
"--cov-report=",
"--cov-fail-under=80",
"--cov-fail-under=0",
os.path.join("tests", "unit"),
*session.posargs,
)
Expand Down
1 change: 1 addition & 0 deletions iot/setup.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Generated by synthtool. DO NOT EDIT!
[bdist_wheel]
universal = 1
10 changes: 5 additions & 5 deletions iot/synth.metadata
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"updateTime": "2019-05-25T12:22:48.919409Z",
"updateTime": "2019-06-18T12:19:55.701594Z",
"sources": [
{
"generator": {
"name": "artman",
"version": "0.21.0",
"dockerImage": "googleapis/artman@sha256:28d4271586772b275cd3bc95cb46bd227a24d3c9048de45dccdb7f3afb0bfba9"
"version": "0.27.0",
"dockerImage": "googleapis/artman@sha256:b036a7f4278d9deb5796f065e5c7f608d47d75369985ca7ab5039998120e972d"
}
},
{
"git": {
"name": "googleapis",
"remote": "https://github.com/googleapis/googleapis.git",
"sha": "7ca19138ccebe219a67be2245200e821b3e32123",
"internalRef": "249916728"
"sha": "384aa843867c4d17756d14a01f047b6368494d32",
"internalRef": "253675319"
}
},
{
Expand Down