Skip to content

Commit

Permalink
feat: support custom client info (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshi-automation authored Aug 24, 2020
1 parent 3f68197 commit 9b52f2b
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

from google.cloud.texttospeech_v1.types import cloud_tts

from .transports.base import TextToSpeechTransport
from .transports.base import TextToSpeechTransport, DEFAULT_CLIENT_INFO
from .transports.grpc_asyncio import TextToSpeechGrpcAsyncIOTransport
from .client import TextToSpeechClient

Expand All @@ -56,6 +56,7 @@ def __init__(
credentials: credentials.Credentials = None,
transport: Union[str, TextToSpeechTransport] = "grpc_asyncio",
client_options: ClientOptions = None,
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
) -> None:
"""Instantiate the text to speech client.
Expand Down Expand Up @@ -88,7 +89,10 @@ def __init__(
"""

self._client = TextToSpeechClient(
credentials=credentials, transport=transport, client_options=client_options,
credentials=credentials,
transport=transport,
client_options=client_options,
client_info=client_info,
)

async def list_voices(
Expand Down Expand Up @@ -155,7 +159,7 @@ async def list_voices(
rpc = gapic_v1.method_async.wrap_method(
self._client._transport.list_voices,
default_timeout=None,
client_info=_client_info,
client_info=DEFAULT_CLIENT_INFO,
)

# Send the request.
Expand Down Expand Up @@ -239,7 +243,7 @@ async def synthesize_speech(
rpc = gapic_v1.method_async.wrap_method(
self._client._transport.synthesize_speech,
default_timeout=None,
client_info=_client_info,
client_info=DEFAULT_CLIENT_INFO,
)

# Send the request.
Expand All @@ -250,13 +254,13 @@ async def synthesize_speech(


try:
_client_info = gapic_v1.client_info.ClientInfo(
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
gapic_version=pkg_resources.get_distribution(
"google-cloud-texttospeech",
).version,
)
except pkg_resources.DistributionNotFound:
_client_info = gapic_v1.client_info.ClientInfo()
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo()


__all__ = ("TextToSpeechAsyncClient",)
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

from google.cloud.texttospeech_v1.types import cloud_tts

from .transports.base import TextToSpeechTransport
from .transports.base import TextToSpeechTransport, DEFAULT_CLIENT_INFO
from .transports.grpc import TextToSpeechGrpcTransport
from .transports.grpc_asyncio import TextToSpeechGrpcAsyncIOTransport

Expand Down Expand Up @@ -131,6 +131,7 @@ def __init__(
credentials: credentials.Credentials = None,
transport: Union[str, TextToSpeechTransport] = None,
client_options: ClientOptions = None,
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
) -> None:
"""Instantiate the text to speech client.
Expand All @@ -156,6 +157,11 @@ def __init__(
(2) The ``client_cert_source`` property is used to provide client
SSL credentials for mutual TLS transport. If not provided, the
default SSL credentials will be used if present.
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
The client info used to send a user-agent string along with
API requests. If ``None``, then default info will be used.
Generally, you only need to set this if you're developing
your own client library.
Raises:
google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport
Expand Down Expand Up @@ -213,6 +219,7 @@ def __init__(
api_mtls_endpoint=client_options.api_endpoint,
client_cert_source=client_options.client_cert_source,
quota_project_id=client_options.quota_project_id,
client_info=client_info,
)

def list_voices(
Expand Down Expand Up @@ -378,13 +385,13 @@ def synthesize_speech(


try:
_client_info = gapic_v1.client_info.ClientInfo(
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
gapic_version=pkg_resources.get_distribution(
"google-cloud-texttospeech",
).version,
)
except pkg_resources.DistributionNotFound:
_client_info = gapic_v1.client_info.ClientInfo()
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo()


__all__ = ("TextToSpeechClient",)
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@


try:
_client_info = gapic_v1.client_info.ClientInfo(
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
gapic_version=pkg_resources.get_distribution(
"google-cloud-texttospeech",
).version,
)
except pkg_resources.DistributionNotFound:
_client_info = gapic_v1.client_info.ClientInfo()
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo()


class TextToSpeechTransport(abc.ABC):
Expand All @@ -51,6 +51,7 @@ def __init__(
credentials_file: typing.Optional[str] = None,
scopes: typing.Optional[typing.Sequence[str]] = AUTH_SCOPES,
quota_project_id: typing.Optional[str] = None,
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
**kwargs,
) -> None:
"""Instantiate the transport.
Expand All @@ -68,6 +69,11 @@ def __init__(
scope (Optional[Sequence[str]]): A list of scopes.
quota_project_id (Optional[str]): An optional project to use for billing
and quota.
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
The client info used to send a user-agent string along with
API requests. If ``None``, then default info will be used.
Generally, you only need to set this if you're developing
your own client library.
"""
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
if ":" not in host:
Expand Down Expand Up @@ -95,16 +101,16 @@ def __init__(
self._credentials = credentials

# Lifted into its own function so it can be stubbed out during tests.
self._prep_wrapped_messages()
self._prep_wrapped_messages(client_info)

def _prep_wrapped_messages(self):
def _prep_wrapped_messages(self, client_info):
# Precompute the wrapped methods.
self._wrapped_methods = {
self.list_voices: gapic_v1.method.wrap_method(
self.list_voices, default_timeout=None, client_info=_client_info,
self.list_voices, default_timeout=None, client_info=client_info,
),
self.synthesize_speech: gapic_v1.method.wrap_method(
self.synthesize_speech, default_timeout=None, client_info=_client_info,
self.synthesize_speech, default_timeout=None, client_info=client_info,
),
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from typing import Callable, Dict, Optional, Sequence, Tuple

from google.api_core import grpc_helpers # type: ignore
from google.api_core import gapic_v1 # type: ignore
from google import auth # type: ignore
from google.auth import credentials # type: ignore
from google.auth.transport.grpc import SslCredentials # type: ignore
Expand All @@ -27,7 +28,7 @@

from google.cloud.texttospeech_v1.types import cloud_tts

from .base import TextToSpeechTransport
from .base import TextToSpeechTransport, DEFAULT_CLIENT_INFO


class TextToSpeechGrpcTransport(TextToSpeechTransport):
Expand Down Expand Up @@ -55,7 +56,8 @@ def __init__(
channel: grpc.Channel = None,
api_mtls_endpoint: str = None,
client_cert_source: Callable[[], Tuple[bytes, bytes]] = None,
quota_project_id: Optional[str] = None
quota_project_id: Optional[str] = None,
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
) -> None:
"""Instantiate the transport.
Expand Down Expand Up @@ -84,6 +86,11 @@ def __init__(
is None.
quota_project_id (Optional[str]): An optional project to use for billing
and quota.
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
The client info used to send a user-agent string along with
API requests. If ``None``, then default info will be used.
Generally, you only need to set this if you're developing
your own client library.
Raises:
google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport
Expand Down Expand Up @@ -139,6 +146,7 @@ def __init__(
credentials_file=credentials_file,
scopes=scopes or self.AUTH_SCOPES,
quota_project_id=quota_project_id,
client_info=client_info,
)

@classmethod
Expand All @@ -149,7 +157,7 @@ def create_channel(
credentials_file: str = None,
scopes: Optional[Sequence[str]] = None,
quota_project_id: Optional[str] = None,
**kwargs
**kwargs,
) -> grpc.Channel:
"""Create and return a gRPC channel object.
Args:
Expand Down Expand Up @@ -183,7 +191,7 @@ def create_channel(
credentials_file=credentials_file,
scopes=scopes,
quota_project_id=quota_project_id,
**kwargs
**kwargs,
)

@property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple

from google.api_core import gapic_v1 # type: ignore
from google.api_core import grpc_helpers_async # type: ignore
from google.auth import credentials # type: ignore
from google.auth.transport.grpc import SslCredentials # type: ignore
Expand All @@ -26,7 +27,7 @@

from google.cloud.texttospeech_v1.types import cloud_tts

from .base import TextToSpeechTransport
from .base import TextToSpeechTransport, DEFAULT_CLIENT_INFO
from .grpc import TextToSpeechGrpcTransport


Expand Down Expand Up @@ -98,6 +99,7 @@ def __init__(
api_mtls_endpoint: str = None,
client_cert_source: Callable[[], Tuple[bytes, bytes]] = None,
quota_project_id=None,
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
) -> None:
"""Instantiate the transport.
Expand Down Expand Up @@ -127,6 +129,11 @@ def __init__(
is None.
quota_project_id (Optional[str]): An optional project to use for billing
and quota.
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
The client info used to send a user-agent string along with
API requests. If ``None``, then default info will be used.
Generally, you only need to set this if you're developing
your own client library.
Raises:
google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport
Expand Down Expand Up @@ -175,6 +182,7 @@ def __init__(
credentials_file=credentials_file,
scopes=scopes or self.AUTH_SCOPES,
quota_project_id=quota_project_id,
client_info=client_info,
)

self._stubs = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

from google.cloud.texttospeech_v1beta1.types import cloud_tts

from .transports.base import TextToSpeechTransport
from .transports.base import TextToSpeechTransport, DEFAULT_CLIENT_INFO
from .transports.grpc_asyncio import TextToSpeechGrpcAsyncIOTransport
from .client import TextToSpeechClient

Expand All @@ -56,6 +56,7 @@ def __init__(
credentials: credentials.Credentials = None,
transport: Union[str, TextToSpeechTransport] = "grpc_asyncio",
client_options: ClientOptions = None,
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
) -> None:
"""Instantiate the text to speech client.
Expand Down Expand Up @@ -88,7 +89,10 @@ def __init__(
"""

self._client = TextToSpeechClient(
credentials=credentials, transport=transport, client_options=client_options,
credentials=credentials,
transport=transport,
client_options=client_options,
client_info=client_info,
)

async def list_voices(
Expand Down Expand Up @@ -155,7 +159,7 @@ async def list_voices(
rpc = gapic_v1.method_async.wrap_method(
self._client._transport.list_voices,
default_timeout=None,
client_info=_client_info,
client_info=DEFAULT_CLIENT_INFO,
)

# Send the request.
Expand Down Expand Up @@ -239,7 +243,7 @@ async def synthesize_speech(
rpc = gapic_v1.method_async.wrap_method(
self._client._transport.synthesize_speech,
default_timeout=None,
client_info=_client_info,
client_info=DEFAULT_CLIENT_INFO,
)

# Send the request.
Expand All @@ -250,13 +254,13 @@ async def synthesize_speech(


try:
_client_info = gapic_v1.client_info.ClientInfo(
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
gapic_version=pkg_resources.get_distribution(
"google-cloud-texttospeech",
).version,
)
except pkg_resources.DistributionNotFound:
_client_info = gapic_v1.client_info.ClientInfo()
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo()


__all__ = ("TextToSpeechAsyncClient",)
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

from google.cloud.texttospeech_v1beta1.types import cloud_tts

from .transports.base import TextToSpeechTransport
from .transports.base import TextToSpeechTransport, DEFAULT_CLIENT_INFO
from .transports.grpc import TextToSpeechGrpcTransport
from .transports.grpc_asyncio import TextToSpeechGrpcAsyncIOTransport

Expand Down Expand Up @@ -131,6 +131,7 @@ def __init__(
credentials: credentials.Credentials = None,
transport: Union[str, TextToSpeechTransport] = None,
client_options: ClientOptions = None,
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
) -> None:
"""Instantiate the text to speech client.
Expand All @@ -156,6 +157,11 @@ def __init__(
(2) The ``client_cert_source`` property is used to provide client
SSL credentials for mutual TLS transport. If not provided, the
default SSL credentials will be used if present.
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
The client info used to send a user-agent string along with
API requests. If ``None``, then default info will be used.
Generally, you only need to set this if you're developing
your own client library.
Raises:
google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport
Expand Down Expand Up @@ -213,6 +219,7 @@ def __init__(
api_mtls_endpoint=client_options.api_endpoint,
client_cert_source=client_options.client_cert_source,
quota_project_id=client_options.quota_project_id,
client_info=client_info,
)

def list_voices(
Expand Down Expand Up @@ -378,13 +385,13 @@ def synthesize_speech(


try:
_client_info = gapic_v1.client_info.ClientInfo(
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
gapic_version=pkg_resources.get_distribution(
"google-cloud-texttospeech",
).version,
)
except pkg_resources.DistributionNotFound:
_client_info = gapic_v1.client_info.ClientInfo()
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo()


__all__ = ("TextToSpeechClient",)
Loading

0 comments on commit 9b52f2b

Please sign in to comment.