Skip to content
This repository was archived by the owner on Sep 21, 2023. It is now read-only.

Commit ac6a403

Browse files
feat: add api key support (#127)
- [ ] Regenerate this pull request now. PiperOrigin-RevId: 423842556 Source-Link: googleapis/googleapis@a616ca0 Source-Link: https://github.com/googleapis/googleapis-gen/commit/29b938c58c1e51d019f2ee539d55dc0a3c86a905 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiMjliOTM4YzU4YzFlNTFkMDE5ZjJlZTUzOWQ1NWRjMGEzYzg2YTkwNSJ9 Fixes: #140 fix(deps): require proto-plus >= 1.20.3
1 parent c14695b commit ac6a403

File tree

8 files changed

+506
-94
lines changed

8 files changed

+506
-94
lines changed

google/cloud/video/transcoder_v1/services/transcoder_service/async_client.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from collections import OrderedDict
1717
import functools
1818
import re
19-
from typing import Dict, Sequence, Tuple, Type, Union
19+
from typing import Dict, Optional, Sequence, Tuple, Type, Union
2020
import pkg_resources
2121

2222
from google.api_core.client_options import ClientOptions
@@ -120,6 +120,42 @@ def from_service_account_file(cls, filename: str, *args, **kwargs):
120120

121121
from_service_account_json = from_service_account_file
122122

123+
@classmethod
124+
def get_mtls_endpoint_and_cert_source(
125+
cls, client_options: Optional[ClientOptions] = None
126+
):
127+
"""Return the API endpoint and client cert source for mutual TLS.
128+
129+
The client cert source is determined in the following order:
130+
(1) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the
131+
client cert source is None.
132+
(2) if `client_options.client_cert_source` is provided, use the provided one; if the
133+
default client cert source exists, use the default one; otherwise the client cert
134+
source is None.
135+
136+
The API endpoint is determined in the following order:
137+
(1) if `client_options.api_endpoint` if provided, use the provided one.
138+
(2) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the
139+
default mTLS endpoint; if the environment variabel is "never", use the default API
140+
endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise
141+
use the default API endpoint.
142+
143+
More details can be found at https://google.aip.dev/auth/4114.
144+
145+
Args:
146+
client_options (google.api_core.client_options.ClientOptions): Custom options for the
147+
client. Only the `api_endpoint` and `client_cert_source` properties may be used
148+
in this method.
149+
150+
Returns:
151+
Tuple[str, Callable[[], Tuple[bytes, bytes]]]: returns the API endpoint and the
152+
client cert source to use.
153+
154+
Raises:
155+
google.auth.exceptions.MutualTLSChannelError: If any errors happen.
156+
"""
157+
return TranscoderServiceClient.get_mtls_endpoint_and_cert_source(client_options) # type: ignore
158+
123159
@property
124160
def transport(self) -> TranscoderServiceTransport:
125161
"""Returns the transport used by the client instance.

google/cloud/video/transcoder_v1/services/transcoder_service/client.py

Lines changed: 84 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,73 @@ def parse_common_location_path(path: str) -> Dict[str, str]:
262262
m = re.match(r"^projects/(?P<project>.+?)/locations/(?P<location>.+?)$", path)
263263
return m.groupdict() if m else {}
264264

265+
@classmethod
266+
def get_mtls_endpoint_and_cert_source(
267+
cls, client_options: Optional[client_options_lib.ClientOptions] = None
268+
):
269+
"""Return the API endpoint and client cert source for mutual TLS.
270+
271+
The client cert source is determined in the following order:
272+
(1) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the
273+
client cert source is None.
274+
(2) if `client_options.client_cert_source` is provided, use the provided one; if the
275+
default client cert source exists, use the default one; otherwise the client cert
276+
source is None.
277+
278+
The API endpoint is determined in the following order:
279+
(1) if `client_options.api_endpoint` if provided, use the provided one.
280+
(2) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the
281+
default mTLS endpoint; if the environment variabel is "never", use the default API
282+
endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise
283+
use the default API endpoint.
284+
285+
More details can be found at https://google.aip.dev/auth/4114.
286+
287+
Args:
288+
client_options (google.api_core.client_options.ClientOptions): Custom options for the
289+
client. Only the `api_endpoint` and `client_cert_source` properties may be used
290+
in this method.
291+
292+
Returns:
293+
Tuple[str, Callable[[], Tuple[bytes, bytes]]]: returns the API endpoint and the
294+
client cert source to use.
295+
296+
Raises:
297+
google.auth.exceptions.MutualTLSChannelError: If any errors happen.
298+
"""
299+
if client_options is None:
300+
client_options = client_options_lib.ClientOptions()
301+
use_client_cert = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")
302+
use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto")
303+
if use_client_cert not in ("true", "false"):
304+
raise ValueError(
305+
"Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
306+
)
307+
if use_mtls_endpoint not in ("auto", "never", "always"):
308+
raise MutualTLSChannelError(
309+
"Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`"
310+
)
311+
312+
# Figure out the client cert source to use.
313+
client_cert_source = None
314+
if use_client_cert == "true":
315+
if client_options.client_cert_source:
316+
client_cert_source = client_options.client_cert_source
317+
elif mtls.has_default_client_cert_source():
318+
client_cert_source = mtls.default_client_cert_source()
319+
320+
# Figure out which api endpoint to use.
321+
if client_options.api_endpoint is not None:
322+
api_endpoint = client_options.api_endpoint
323+
elif use_mtls_endpoint == "always" or (
324+
use_mtls_endpoint == "auto" and client_cert_source
325+
):
326+
api_endpoint = cls.DEFAULT_MTLS_ENDPOINT
327+
else:
328+
api_endpoint = cls.DEFAULT_ENDPOINT
329+
330+
return api_endpoint, client_cert_source
331+
265332
def __init__(
266333
self,
267334
*,
@@ -312,57 +379,22 @@ def __init__(
312379
if client_options is None:
313380
client_options = client_options_lib.ClientOptions()
314381

315-
# Create SSL credentials for mutual TLS if needed.
316-
if os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") not in (
317-
"true",
318-
"false",
319-
):
320-
raise ValueError(
321-
"Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
322-
)
323-
use_client_cert = (
324-
os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") == "true"
382+
api_endpoint, client_cert_source_func = self.get_mtls_endpoint_and_cert_source(
383+
client_options
325384
)
326385

327-
client_cert_source_func = None
328-
is_mtls = False
329-
if use_client_cert:
330-
if client_options.client_cert_source:
331-
is_mtls = True
332-
client_cert_source_func = client_options.client_cert_source
333-
else:
334-
is_mtls = mtls.has_default_client_cert_source()
335-
if is_mtls:
336-
client_cert_source_func = mtls.default_client_cert_source()
337-
else:
338-
client_cert_source_func = None
339-
340-
# Figure out which api endpoint to use.
341-
if client_options.api_endpoint is not None:
342-
api_endpoint = client_options.api_endpoint
343-
else:
344-
use_mtls_env = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto")
345-
if use_mtls_env == "never":
346-
api_endpoint = self.DEFAULT_ENDPOINT
347-
elif use_mtls_env == "always":
348-
api_endpoint = self.DEFAULT_MTLS_ENDPOINT
349-
elif use_mtls_env == "auto":
350-
if is_mtls:
351-
api_endpoint = self.DEFAULT_MTLS_ENDPOINT
352-
else:
353-
api_endpoint = self.DEFAULT_ENDPOINT
354-
else:
355-
raise MutualTLSChannelError(
356-
"Unsupported GOOGLE_API_USE_MTLS_ENDPOINT value. Accepted "
357-
"values: never, auto, always"
358-
)
386+
api_key_value = getattr(client_options, "api_key", None)
387+
if api_key_value and credentials:
388+
raise ValueError(
389+
"client_options.api_key and credentials are mutually exclusive"
390+
)
359391

360392
# Save or instantiate the transport.
361393
# Ordinarily, we provide the transport, but allowing a custom transport
362394
# instance provides an extensibility point for unusual situations.
363395
if isinstance(transport, TranscoderServiceTransport):
364396
# transport is a TranscoderServiceTransport instance.
365-
if credentials or client_options.credentials_file:
397+
if credentials or client_options.credentials_file or api_key_value:
366398
raise ValueError(
367399
"When providing a transport instance, "
368400
"provide its credentials directly."
@@ -374,6 +406,15 @@ def __init__(
374406
)
375407
self._transport = transport
376408
else:
409+
import google.auth._default # type: ignore
410+
411+
if api_key_value and hasattr(
412+
google.auth._default, "get_api_key_credentials"
413+
):
414+
credentials = google.auth._default.get_api_key_credentials(
415+
api_key_value
416+
)
417+
377418
Transport = type(self).get_transport_class(transport)
378419
self._transport = Transport(
379420
credentials=credentials,

google/cloud/video/transcoder_v1beta1/services/transcoder_service/async_client.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from collections import OrderedDict
1717
import functools
1818
import re
19-
from typing import Dict, Sequence, Tuple, Type, Union
19+
from typing import Dict, Optional, Sequence, Tuple, Type, Union
2020
import pkg_resources
2121

2222
from google.api_core.client_options import ClientOptions
@@ -119,6 +119,42 @@ def from_service_account_file(cls, filename: str, *args, **kwargs):
119119

120120
from_service_account_json = from_service_account_file
121121

122+
@classmethod
123+
def get_mtls_endpoint_and_cert_source(
124+
cls, client_options: Optional[ClientOptions] = None
125+
):
126+
"""Return the API endpoint and client cert source for mutual TLS.
127+
128+
The client cert source is determined in the following order:
129+
(1) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the
130+
client cert source is None.
131+
(2) if `client_options.client_cert_source` is provided, use the provided one; if the
132+
default client cert source exists, use the default one; otherwise the client cert
133+
source is None.
134+
135+
The API endpoint is determined in the following order:
136+
(1) if `client_options.api_endpoint` if provided, use the provided one.
137+
(2) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the
138+
default mTLS endpoint; if the environment variabel is "never", use the default API
139+
endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise
140+
use the default API endpoint.
141+
142+
More details can be found at https://google.aip.dev/auth/4114.
143+
144+
Args:
145+
client_options (google.api_core.client_options.ClientOptions): Custom options for the
146+
client. Only the `api_endpoint` and `client_cert_source` properties may be used
147+
in this method.
148+
149+
Returns:
150+
Tuple[str, Callable[[], Tuple[bytes, bytes]]]: returns the API endpoint and the
151+
client cert source to use.
152+
153+
Raises:
154+
google.auth.exceptions.MutualTLSChannelError: If any errors happen.
155+
"""
156+
return TranscoderServiceClient.get_mtls_endpoint_and_cert_source(client_options) # type: ignore
157+
122158
@property
123159
def transport(self) -> TranscoderServiceTransport:
124160
"""Returns the transport used by the client instance.

0 commit comments

Comments
 (0)