1// Copyright 2021 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 speech 18 19import ( 20 "context" 21 "math" 22 "time" 23 24 "cloud.google.com/go/longrunning" 25 lroauto "cloud.google.com/go/longrunning/autogen" 26 gax "github.com/googleapis/gax-go/v2" 27 "google.golang.org/api/option" 28 "google.golang.org/api/option/internaloption" 29 gtransport "google.golang.org/api/transport/grpc" 30 speechpb "google.golang.org/genproto/googleapis/cloud/speech/v1" 31 longrunningpb "google.golang.org/genproto/googleapis/longrunning" 32 "google.golang.org/grpc" 33 "google.golang.org/grpc/codes" 34 "google.golang.org/grpc/metadata" 35) 36 37var newClientHook clientHook 38 39// CallOptions contains the retry settings for each method of Client. 40type CallOptions struct { 41 Recognize []gax.CallOption 42 LongRunningRecognize []gax.CallOption 43 StreamingRecognize []gax.CallOption 44} 45 46func defaultGRPCClientOptions() []option.ClientOption { 47 return []option.ClientOption{ 48 internaloption.WithDefaultEndpoint("speech.googleapis.com:443"), 49 internaloption.WithDefaultMTLSEndpoint("speech.mtls.googleapis.com:443"), 50 internaloption.WithDefaultAudience("https://speech.googleapis.com/"), 51 internaloption.WithDefaultScopes(DefaultAuthScopes()...), 52 option.WithGRPCDialOption(grpc.WithDisableServiceConfig()), 53 option.WithGRPCDialOption(grpc.WithDefaultCallOptions( 54 grpc.MaxCallRecvMsgSize(math.MaxInt32))), 55 } 56} 57 58func defaultCallOptions() *CallOptions { 59 return &CallOptions{ 60 Recognize: []gax.CallOption{ 61 gax.WithRetry(func() gax.Retryer { 62 return gax.OnCodes([]codes.Code{ 63 codes.Unavailable, 64 codes.DeadlineExceeded, 65 }, gax.Backoff{ 66 Initial: 100 * time.Millisecond, 67 Max: 60000 * time.Millisecond, 68 Multiplier: 1.30, 69 }) 70 }), 71 }, 72 LongRunningRecognize: []gax.CallOption{}, 73 StreamingRecognize: []gax.CallOption{ 74 gax.WithRetry(func() gax.Retryer { 75 return gax.OnCodes([]codes.Code{ 76 codes.Unavailable, 77 codes.DeadlineExceeded, 78 }, gax.Backoff{ 79 Initial: 100 * time.Millisecond, 80 Max: 60000 * time.Millisecond, 81 Multiplier: 1.30, 82 }) 83 }), 84 }, 85 } 86} 87 88// internalClient is an interface that defines the methods availaible from Cloud Speech-to-Text API. 89type internalClient interface { 90 Close() error 91 setGoogleClientInfo(...string) 92 Connection() *grpc.ClientConn 93 Recognize(context.Context, *speechpb.RecognizeRequest, ...gax.CallOption) (*speechpb.RecognizeResponse, error) 94 LongRunningRecognize(context.Context, *speechpb.LongRunningRecognizeRequest, ...gax.CallOption) (*LongRunningRecognizeOperation, error) 95 LongRunningRecognizeOperation(name string) *LongRunningRecognizeOperation 96 StreamingRecognize(context.Context, ...gax.CallOption) (speechpb.Speech_StreamingRecognizeClient, error) 97} 98 99// Client is a client for interacting with Cloud Speech-to-Text API. 100// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 101// 102// Service that implements Google Cloud Speech API. 103type Client struct { 104 // The internal transport-dependent client. 105 internalClient internalClient 106 107 // The call options for this service. 108 CallOptions *CallOptions 109 110 // LROClient is used internally to handle long-running operations. 111 // It is exposed so that its CallOptions can be modified if required. 112 // Users should not Close this client. 113 LROClient *lroauto.OperationsClient 114} 115 116// Wrapper methods routed to the internal client. 117 118// Close closes the connection to the API service. The user should invoke this when 119// the client is no longer required. 120func (c *Client) Close() error { 121 return c.internalClient.Close() 122} 123 124// setGoogleClientInfo sets the name and version of the application in 125// the `x-goog-api-client` header passed on each request. Intended for 126// use by Google-written clients. 127func (c *Client) setGoogleClientInfo(keyval ...string) { 128 c.internalClient.setGoogleClientInfo(keyval...) 129} 130 131// Connection returns a connection to the API service. 132// 133// Deprecated. 134func (c *Client) Connection() *grpc.ClientConn { 135 return c.internalClient.Connection() 136} 137 138// Recognize performs synchronous speech recognition: receive results after all audio 139// has been sent and processed. 140func (c *Client) Recognize(ctx context.Context, req *speechpb.RecognizeRequest, opts ...gax.CallOption) (*speechpb.RecognizeResponse, error) { 141 return c.internalClient.Recognize(ctx, req, opts...) 142} 143 144// LongRunningRecognize performs asynchronous speech recognition: receive results via the 145// google.longrunning.Operations interface. Returns either an 146// Operation.error or an Operation.response which contains 147// a LongRunningRecognizeResponse message. 148// For more information on asynchronous speech recognition, see the 149// how-to (at https://cloud.google.com/speech-to-text/docs/async-recognize). 150func (c *Client) LongRunningRecognize(ctx context.Context, req *speechpb.LongRunningRecognizeRequest, opts ...gax.CallOption) (*LongRunningRecognizeOperation, error) { 151 return c.internalClient.LongRunningRecognize(ctx, req, opts...) 152} 153 154// LongRunningRecognizeOperation returns a new LongRunningRecognizeOperation from a given name. 155// The name must be that of a previously created LongRunningRecognizeOperation, possibly from a different process. 156func (c *Client) LongRunningRecognizeOperation(name string) *LongRunningRecognizeOperation { 157 return c.internalClient.LongRunningRecognizeOperation(name) 158} 159 160// StreamingRecognize performs bidirectional streaming speech recognition: receive results while 161// sending audio. This method is only available via the gRPC API (not REST). 162func (c *Client) StreamingRecognize(ctx context.Context, opts ...gax.CallOption) (speechpb.Speech_StreamingRecognizeClient, error) { 163 return c.internalClient.StreamingRecognize(ctx, opts...) 164} 165 166// gRPCClient is a client for interacting with Cloud Speech-to-Text API over gRPC transport. 167// 168// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 169type gRPCClient struct { 170 // Connection pool of gRPC connections to the service. 171 connPool gtransport.ConnPool 172 173 // flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE 174 disableDeadlines bool 175 176 // Points back to the CallOptions field of the containing Client 177 CallOptions **CallOptions 178 179 // The gRPC API client. 180 client speechpb.SpeechClient 181 182 // LROClient is used internally to handle long-running operations. 183 // It is exposed so that its CallOptions can be modified if required. 184 // Users should not Close this client. 185 LROClient **lroauto.OperationsClient 186 187 // The x-goog-* metadata to be sent with each request. 188 xGoogMetadata metadata.MD 189} 190 191// NewClient creates a new speech client based on gRPC. 192// The returned client must be Closed when it is done being used to clean up its underlying connections. 193// 194// Service that implements Google Cloud Speech API. 195func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) { 196 clientOpts := defaultGRPCClientOptions() 197 if newClientHook != nil { 198 hookOpts, err := newClientHook(ctx, clientHookParams{}) 199 if err != nil { 200 return nil, err 201 } 202 clientOpts = append(clientOpts, hookOpts...) 203 } 204 205 disableDeadlines, err := checkDisableDeadlines() 206 if err != nil { 207 return nil, err 208 } 209 210 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) 211 if err != nil { 212 return nil, err 213 } 214 client := Client{CallOptions: defaultCallOptions()} 215 216 c := &gRPCClient{ 217 connPool: connPool, 218 disableDeadlines: disableDeadlines, 219 client: speechpb.NewSpeechClient(connPool), 220 CallOptions: &client.CallOptions, 221 } 222 c.setGoogleClientInfo() 223 224 client.internalClient = c 225 226 client.LROClient, err = lroauto.NewOperationsClient(ctx, gtransport.WithConnPool(connPool)) 227 if err != nil { 228 // This error "should not happen", since we are just reusing old connection pool 229 // and never actually need to dial. 230 // If this does happen, we could leak connp. However, we cannot close conn: 231 // If the user invoked the constructor with option.WithGRPCConn, 232 // we would close a connection that's still in use. 233 // TODO: investigate error conditions. 234 return nil, err 235 } 236 c.LROClient = &client.LROClient 237 return &client, nil 238} 239 240// Connection returns a connection to the API service. 241// 242// Deprecated. 243func (c *gRPCClient) Connection() *grpc.ClientConn { 244 return c.connPool.Conn() 245} 246 247// setGoogleClientInfo sets the name and version of the application in 248// the `x-goog-api-client` header passed on each request. Intended for 249// use by Google-written clients. 250func (c *gRPCClient) setGoogleClientInfo(keyval ...string) { 251 kv := append([]string{"gl-go", versionGo()}, keyval...) 252 kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) 253 c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) 254} 255 256// Close closes the connection to the API service. The user should invoke this when 257// the client is no longer required. 258func (c *gRPCClient) Close() error { 259 return c.connPool.Close() 260} 261 262func (c *gRPCClient) Recognize(ctx context.Context, req *speechpb.RecognizeRequest, opts ...gax.CallOption) (*speechpb.RecognizeResponse, error) { 263 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 264 cctx, cancel := context.WithTimeout(ctx, 5000000*time.Millisecond) 265 defer cancel() 266 ctx = cctx 267 } 268 ctx = insertMetadata(ctx, c.xGoogMetadata) 269 opts = append((*c.CallOptions).Recognize[0:len((*c.CallOptions).Recognize):len((*c.CallOptions).Recognize)], opts...) 270 var resp *speechpb.RecognizeResponse 271 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 272 var err error 273 resp, err = c.client.Recognize(ctx, req, settings.GRPC...) 274 return err 275 }, opts...) 276 if err != nil { 277 return nil, err 278 } 279 return resp, nil 280} 281 282func (c *gRPCClient) LongRunningRecognize(ctx context.Context, req *speechpb.LongRunningRecognizeRequest, opts ...gax.CallOption) (*LongRunningRecognizeOperation, error) { 283 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 284 cctx, cancel := context.WithTimeout(ctx, 5000000*time.Millisecond) 285 defer cancel() 286 ctx = cctx 287 } 288 ctx = insertMetadata(ctx, c.xGoogMetadata) 289 opts = append((*c.CallOptions).LongRunningRecognize[0:len((*c.CallOptions).LongRunningRecognize):len((*c.CallOptions).LongRunningRecognize)], opts...) 290 var resp *longrunningpb.Operation 291 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 292 var err error 293 resp, err = c.client.LongRunningRecognize(ctx, req, settings.GRPC...) 294 return err 295 }, opts...) 296 if err != nil { 297 return nil, err 298 } 299 return &LongRunningRecognizeOperation{ 300 lro: longrunning.InternalNewOperation(*c.LROClient, resp), 301 }, nil 302} 303 304func (c *gRPCClient) StreamingRecognize(ctx context.Context, opts ...gax.CallOption) (speechpb.Speech_StreamingRecognizeClient, error) { 305 ctx = insertMetadata(ctx, c.xGoogMetadata) 306 var resp speechpb.Speech_StreamingRecognizeClient 307 opts = append((*c.CallOptions).StreamingRecognize[0:len((*c.CallOptions).StreamingRecognize):len((*c.CallOptions).StreamingRecognize)], opts...) 308 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 309 var err error 310 resp, err = c.client.StreamingRecognize(ctx, settings.GRPC...) 311 return err 312 }, opts...) 313 if err != nil { 314 return nil, err 315 } 316 return resp, nil 317} 318 319// LongRunningRecognizeOperation manages a long-running operation from LongRunningRecognize. 320type LongRunningRecognizeOperation struct { 321 lro *longrunning.Operation 322} 323 324// LongRunningRecognizeOperation returns a new LongRunningRecognizeOperation from a given name. 325// The name must be that of a previously created LongRunningRecognizeOperation, possibly from a different process. 326func (c *gRPCClient) LongRunningRecognizeOperation(name string) *LongRunningRecognizeOperation { 327 return &LongRunningRecognizeOperation{ 328 lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}), 329 } 330} 331 332// Wait blocks until the long-running operation is completed, returning the response and any errors encountered. 333// 334// See documentation of Poll for error-handling information. 335func (op *LongRunningRecognizeOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*speechpb.LongRunningRecognizeResponse, error) { 336 var resp speechpb.LongRunningRecognizeResponse 337 if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil { 338 return nil, err 339 } 340 return &resp, nil 341} 342 343// Poll fetches the latest state of the long-running operation. 344// 345// Poll also fetches the latest metadata, which can be retrieved by Metadata. 346// 347// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and 348// the operation has completed with failure, the error is returned and op.Done will return true. 349// If Poll succeeds and the operation has completed successfully, 350// op.Done will return true, and the response of the operation is returned. 351// If Poll succeeds and the operation has not completed, the returned response and error are both nil. 352func (op *LongRunningRecognizeOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*speechpb.LongRunningRecognizeResponse, error) { 353 var resp speechpb.LongRunningRecognizeResponse 354 if err := op.lro.Poll(ctx, &resp, opts...); err != nil { 355 return nil, err 356 } 357 if !op.Done() { 358 return nil, nil 359 } 360 return &resp, nil 361} 362 363// Metadata returns metadata associated with the long-running operation. 364// Metadata itself does not contact the server, but Poll does. 365// To get the latest metadata, call this method after a successful call to Poll. 366// If the metadata is not available, the returned metadata and error are both nil. 367func (op *LongRunningRecognizeOperation) Metadata() (*speechpb.LongRunningRecognizeMetadata, error) { 368 var meta speechpb.LongRunningRecognizeMetadata 369 if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata { 370 return nil, nil 371 } else if err != nil { 372 return nil, err 373 } 374 return &meta, nil 375} 376 377// Done reports whether the long-running operation has completed. 378func (op *LongRunningRecognizeOperation) Done() bool { 379 return op.lro.Done() 380} 381 382// Name returns the name of the long-running operation. 383// The name is assigned by the server and is unique within the service from which the operation is created. 384func (op *LongRunningRecognizeOperation) Name() string { 385 return op.lro.Name() 386} 387