1// Copyright 2020 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//     https://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// Code generated by protoc-gen-go_gapic. DO NOT EDIT.
16
17package texttospeech
18
19import (
20	"context"
21	"math"
22	"time"
23
24	gax "github.com/googleapis/gax-go/v2"
25	"google.golang.org/api/option"
26	gtransport "google.golang.org/api/transport/grpc"
27	texttospeechpb "google.golang.org/genproto/googleapis/cloud/texttospeech/v1"
28	"google.golang.org/grpc"
29	"google.golang.org/grpc/codes"
30	"google.golang.org/grpc/metadata"
31)
32
33var newClientHook clientHook
34
35// CallOptions contains the retry settings for each method of Client.
36type CallOptions struct {
37	ListVoices       []gax.CallOption
38	SynthesizeSpeech []gax.CallOption
39}
40
41func defaultClientOptions() []option.ClientOption {
42	return []option.ClientOption{
43		option.WithEndpoint("texttospeech.googleapis.com:443"),
44		option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
45		option.WithScopes(DefaultAuthScopes()...),
46		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
47			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
48	}
49}
50
51func defaultCallOptions() *CallOptions {
52	return &CallOptions{
53		ListVoices: []gax.CallOption{
54			gax.WithRetry(func() gax.Retryer {
55				return gax.OnCodes([]codes.Code{
56					codes.Unavailable,
57					codes.DeadlineExceeded,
58				}, gax.Backoff{
59					Initial:    100 * time.Millisecond,
60					Max:        60000 * time.Millisecond,
61					Multiplier: 1.30,
62				})
63			}),
64		},
65		SynthesizeSpeech: []gax.CallOption{
66			gax.WithRetry(func() gax.Retryer {
67				return gax.OnCodes([]codes.Code{
68					codes.Unavailable,
69					codes.DeadlineExceeded,
70				}, gax.Backoff{
71					Initial:    100 * time.Millisecond,
72					Max:        60000 * time.Millisecond,
73					Multiplier: 1.30,
74				})
75			}),
76		},
77	}
78}
79
80// Client is a client for interacting with Cloud Text-to-Speech API.
81//
82// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
83type Client struct {
84	// Connection pool of gRPC connections to the service.
85	connPool gtransport.ConnPool
86
87	// The gRPC API client.
88	client texttospeechpb.TextToSpeechClient
89
90	// The call options for this service.
91	CallOptions *CallOptions
92
93	// The x-goog-* metadata to be sent with each request.
94	xGoogMetadata metadata.MD
95}
96
97// NewClient creates a new text to speech client.
98//
99// Service that implements Google Cloud Text-to-Speech API.
100func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
101	clientOpts := defaultClientOptions()
102
103	if newClientHook != nil {
104		hookOpts, err := newClientHook(ctx, clientHookParams{})
105		if err != nil {
106			return nil, err
107		}
108		clientOpts = append(clientOpts, hookOpts...)
109	}
110
111	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
112	if err != nil {
113		return nil, err
114	}
115	c := &Client{
116		connPool:    connPool,
117		CallOptions: defaultCallOptions(),
118
119		client: texttospeechpb.NewTextToSpeechClient(connPool),
120	}
121	c.setGoogleClientInfo()
122
123	return c, nil
124}
125
126// Connection returns a connection to the API service.
127//
128// Deprecated.
129func (c *Client) Connection() *grpc.ClientConn {
130	return c.connPool.Conn()
131}
132
133// Close closes the connection to the API service. The user should invoke this when
134// the client is no longer required.
135func (c *Client) Close() error {
136	return c.connPool.Close()
137}
138
139// setGoogleClientInfo sets the name and version of the application in
140// the `x-goog-api-client` header passed on each request. Intended for
141// use by Google-written clients.
142func (c *Client) setGoogleClientInfo(keyval ...string) {
143	kv := append([]string{"gl-go", versionGo()}, keyval...)
144	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
145	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
146}
147
148// ListVoices returns a list of Voice supported for synthesis.
149func (c *Client) ListVoices(ctx context.Context, req *texttospeechpb.ListVoicesRequest, opts ...gax.CallOption) (*texttospeechpb.ListVoicesResponse, error) {
150	ctx = insertMetadata(ctx, c.xGoogMetadata)
151	opts = append(c.CallOptions.ListVoices[0:len(c.CallOptions.ListVoices):len(c.CallOptions.ListVoices)], opts...)
152	var resp *texttospeechpb.ListVoicesResponse
153	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
154		var err error
155		resp, err = c.client.ListVoices(ctx, req, settings.GRPC...)
156		return err
157	}, opts...)
158	if err != nil {
159		return nil, err
160	}
161	return resp, nil
162}
163
164// SynthesizeSpeech synthesizes speech synchronously: receive results after all text input
165// has been processed.
166func (c *Client) SynthesizeSpeech(ctx context.Context, req *texttospeechpb.SynthesizeSpeechRequest, opts ...gax.CallOption) (*texttospeechpb.SynthesizeSpeechResponse, error) {
167	ctx = insertMetadata(ctx, c.xGoogMetadata)
168	opts = append(c.CallOptions.SynthesizeSpeech[0:len(c.CallOptions.SynthesizeSpeech):len(c.CallOptions.SynthesizeSpeech)], opts...)
169	var resp *texttospeechpb.SynthesizeSpeechResponse
170	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
171		var err error
172		resp, err = c.client.SynthesizeSpeech(ctx, req, settings.GRPC...)
173		return err
174	}, opts...)
175	if err != nil {
176		return nil, err
177	}
178	return resp, nil
179}
180