codekingpro/portable-devtools
114k
1# Copyright 2019 Google LLC2#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 at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# 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 and13# limitations under the License.14 15"""Client options class.16 17Client options provide a consistent interface for user options to be defined18across clients.19 20You can pass a client options object to a client.21 22.. code-block:: python23 24 from google.api_core.client_options import ClientOptions25 from google.cloud.vision_v1 import ImageAnnotatorClient26 27 def get_client_cert():28 # code to load client certificate and private key.29 return client_cert_bytes, client_private_key_bytes30 31 options = ClientOptions(api_endpoint="foo.googleapis.com",32 client_cert_source=get_client_cert)33 34 client = ImageAnnotatorClient(client_options=options)35 36You can also pass a mapping object.37 38.. code-block:: python39 40 from google.cloud.vision_v1 import ImageAnnotatorClient41 42 client = ImageAnnotatorClient(43 client_options={44 "api_endpoint": "foo.googleapis.com",45 "client_cert_source" : get_client_cert46 })47 48 49"""50 51 52class ClientOptions(object):53 """Client Options used to set options on clients.54 55 Args:56 api_endpoint (Optional[str]): The desired API endpoint, e.g.,57 compute.googleapis.com58 client_cert_source (Optional[Callable[[], (bytes, bytes)]]): A callback59 which returns client certificate bytes and private key bytes both in60 PEM format. ``client_cert_source`` and ``client_encrypted_cert_source``61 are mutually exclusive.62 client_encrypted_cert_source (Optional[Callable[[], (str, str, bytes)]]):63 A callback which returns client certificate file path, encrypted64 private key file path, and the passphrase bytes.``client_cert_source``65 and ``client_encrypted_cert_source`` are mutually exclusive.66 quota_project_id (Optional[str]): A project name that a client's67 quota belongs to.68 credentials_file (Optional[str]): A path to a file storing credentials.69 ``credentials_file` and ``api_key`` are mutually exclusive.70 scopes (Optional[Sequence[str]]): OAuth access token override scopes.71 api_key (Optional[str]): Google API key. ``credentials_file`` and72 ``api_key`` are mutually exclusive.73 api_audience (Optional[str]): The intended audience for the API calls74 to the service that will be set when using certain 3rd party75 authentication flows. Audience is typically a resource identifier.76 If not set, the service endpoint value will be used as a default.77 An example of a valid ``api_audience`` is: "https://language.googleapis.com".78 universe_domain (Optional[str]): The desired universe domain. This must match79 the one in credentials. If not set, the default universe domain is80 `googleapis.com`. If both `api_endpoint` and `universe_domain` are set,81 then `api_endpoint` is used as the service endpoint. If `api_endpoint` is82 not specified, the format will be `{service}.{universe_domain}`.83 84 Raises:85 ValueError: If both ``client_cert_source`` and ``client_encrypted_cert_source``86 are provided, or both ``credentials_file`` and ``api_key`` are provided.87 """88 89 def __init__(90 self,91 api_endpoint=None,92 client_cert_source=None,93 client_encrypted_cert_source=None,94 quota_project_id=None,95 credentials_file=None,96 scopes=None,97 api_key=None,98 api_audience=None,99 universe_domain=None,100 ):101 if client_cert_source and client_encrypted_cert_source:102 raise ValueError(103 "client_cert_source and client_encrypted_cert_source are mutually exclusive"104 )105 if api_key and credentials_file:106 raise ValueError("api_key and credentials_file are mutually exclusive")107 self.api_endpoint = api_endpoint108 self.client_cert_source = client_cert_source109 self.client_encrypted_cert_source = client_encrypted_cert_source110 self.quota_project_id = quota_project_id111 self.credentials_file = credentials_file112 self.scopes = scopes113 self.api_key = api_key114 self.api_audience = api_audience115 self.universe_domain = universe_domain116 117 def __repr__(self):118 return "ClientOptions: " + repr(self.__dict__)119 120 121def from_dict(options):122 """Construct a client options object from a mapping object.123 124 Args:125 options (collections.abc.Mapping): A mapping object with client options.126 See the docstring for ClientOptions for details on valid arguments.127 """128 129 client_options = ClientOptions()130 131 for key, value in options.items():132 if hasattr(client_options, key):133 setattr(client_options, key, value)134 else:135 raise ValueError("ClientOptions does not accept an option '" + key + "'")136 137 return client_options138 