Skip to content

Commit a08f118

Browse files
authored
Add client options. (#8265)
1 parent 64bd7f0 commit a08f118

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

google/api_core/client_options.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Client options class.
16+
17+
Client options provide a consistent interface for user options to be defined
18+
across clients.
19+
"""
20+
21+
22+
class ClientOptions(object):
23+
"""Client Options used to set options on clients.
24+
25+
Args:
26+
api_endpoint (str): The desired API endpoint, e.g., compute.googleapis.com
27+
"""
28+
29+
def __init__(self, api_endpoint=None):
30+
self.api_endpoint = api_endpoint
31+
32+
33+
def from_dict(options):
34+
"""Construct a client options object from a dictionary.
35+
36+
Args:
37+
options (dict): A dictionary with client options.
38+
"""
39+
40+
client_options = ClientOptions()
41+
42+
for key, value in options.items():
43+
if hasattr(client_options, key):
44+
setattr(client_options, key, value)
45+
else:
46+
raise ValueError("ClientOptions does not accept an option '" + key + "'")
47+
48+
return client_options

tests/unit/test_client_options.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import pytest
16+
17+
from google.api_core import client_options
18+
19+
20+
def test_constructor():
21+
options = client_options.ClientOptions(api_endpoint="foo.googleapis.com")
22+
23+
assert options.api_endpoint == "foo.googleapis.com"
24+
25+
26+
def test_from_dict():
27+
options = client_options.from_dict({"api_endpoint": "foo.googleapis.com"})
28+
29+
assert options.api_endpoint == "foo.googleapis.com"
30+
31+
32+
def test_from_dict_bad_argument():
33+
with pytest.raises(ValueError):
34+
client_options.from_dict(
35+
{"api_endpoint": "foo.googleapis.com", "bad_arg": "1234"}
36+
)

0 commit comments

Comments
 (0)