|
| 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 |
0 commit comments