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