Debian Codegraph @

mediatranslation/apiv1beta1/speech_translation_client.go

raw
1 // Copyright 2025 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
17 package mediatranslation
18
19 import (
20 "context"
21 "log/slog"
22 "math"
23
24 mediatranslationpb "cloud.google.com/go/mediatranslation/apiv1beta1/mediatranslationpb"
25 gax "github.com/googleapis/gax-go/v2"
26 "google.golang.org/api/option"
27 "google.golang.org/api/option/internaloption"
28 gtransport "google.golang.org/api/transport/grpc"
29 "google.golang.org/grpc"
30 )
31
32 var newSpeechTranslationClientHook clientHook
33
34 // SpeechTranslationCallOptions contains the retry settings for each method of SpeechTranslationClient.
35 type SpeechTranslationCallOptions struct {
36 StreamingTranslateSpeech []gax.CallOption
37 }
38
39 func defaultSpeechTranslationGRPCClientOptions() []option.ClientOption {
40 return []option.ClientOption{
41 internaloption.WithDefaultEndpoint("mediatranslation.googleapis.com:443"),
42 internaloption.WithDefaultEndpointTemplate("mediatranslation.UNIVERSE_DOMAIN:443"),
43 internaloption.WithDefaultMTLSEndpoint("mediatranslation.mtls.googleapis.com:443"),
44 internaloption.WithDefaultUniverseDomain("googleapis.com"),
45 internaloption.WithDefaultAudience("https://mediatranslation.googleapis.com/"),
46 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
47 internaloption.EnableJwtWithScope(),
48 internaloption.EnableNewAuthLibrary(),
49 option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
50 grpc.MaxCallRecvMsgSize(math.MaxInt32))),
51 }
52 }
53
54 func defaultSpeechTranslationCallOptions() *SpeechTranslationCallOptions {
55 return &SpeechTranslationCallOptions{
56 StreamingTranslateSpeech: []gax.CallOption{},
57 }
58 }
59
60 // internalSpeechTranslationClient is an interface that defines the methods available from Media Translation API.
61 type internalSpeechTranslationClient interface {
62 Close() error
63 setGoogleClientInfo(...string)
64 Connection() *grpc.ClientConn
65 StreamingTranslateSpeech(context.Context, ...gax.CallOption) (mediatranslationpb.SpeechTranslationService_StreamingTranslateSpeechClient, error)
66 }
67
68 // SpeechTranslationClient is a client for interacting with Media Translation API.
69 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
70 //
71 // Provides translation from/to media types.
72 type SpeechTranslationClient struct {
73 // The internal transport-dependent client.
74 internalClient internalSpeechTranslationClient
75
76 // The call options for this service.
77 CallOptions *SpeechTranslationCallOptions
78 }
79
80 // Wrapper methods routed to the internal client.
81
82 // Close closes the connection to the API service. The user should invoke this when
83 // the client is no longer required.
84 func (c *SpeechTranslationClient) Close() error {
85 return c.internalClient.Close()
86 }
87
88 // setGoogleClientInfo sets the name and version of the application in
89 // the `x-goog-api-client` header passed on each request. Intended for
90 // use by Google-written clients.
91 func (c *SpeechTranslationClient) setGoogleClientInfo(keyval ...string) {
92 c.internalClient.setGoogleClientInfo(keyval...)
93 }
94
95 // Connection returns a connection to the API service.
96 //
97 // Deprecated: Connections are now pooled so this method does not always
98 // return the same resource.
99 func (c *SpeechTranslationClient) Connection() *grpc.ClientConn {
100 return c.internalClient.Connection()
101 }
102
103 // StreamingTranslateSpeech performs bidirectional streaming speech translation: receive results while
104 // sending audio. This method is only available via the gRPC API (not REST).
105 func (c *SpeechTranslationClient) StreamingTranslateSpeech(ctx context.Context, opts ...gax.CallOption) (mediatranslationpb.SpeechTranslationService_StreamingTranslateSpeechClient, error) {
106 return c.internalClient.StreamingTranslateSpeech(ctx, opts...)
107 }
108
109 // speechTranslationGRPCClient is a client for interacting with Media Translation API over gRPC transport.
110 //
111 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
112 type speechTranslationGRPCClient struct {
113 // Connection pool of gRPC connections to the service.
114 connPool gtransport.ConnPool
115
116 // Points back to the CallOptions field of the containing SpeechTranslationClient
117 CallOptions **SpeechTranslationCallOptions
118
119 // The gRPC API client.
120 speechTranslationClient mediatranslationpb.SpeechTranslationServiceClient
121
122 // The x-goog-* metadata to be sent with each request.
123 xGoogHeaders []string
124
125 logger *slog.Logger
126 }
127
128 // NewSpeechTranslationClient creates a new speech translation service client based on gRPC.
129 // The returned client must be Closed when it is done being used to clean up its underlying connections.
130 //
131 // Provides translation from/to media types.
132 func NewSpeechTranslationClient(ctx context.Context, opts ...option.ClientOption) (*SpeechTranslationClient, error) {
133 clientOpts := defaultSpeechTranslationGRPCClientOptions()
134 if newSpeechTranslationClientHook != nil {
135 hookOpts, err := newSpeechTranslationClientHook(ctx, clientHookParams{})
136 if err != nil {
137 return nil, err
138 }
139 clientOpts = append(clientOpts, hookOpts...)
140 }
141
142 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
143 if err != nil {
144 return nil, err
145 }
146 client := SpeechTranslationClient{CallOptions: defaultSpeechTranslationCallOptions()}
147
148 c := &speechTranslationGRPCClient{
149 connPool: connPool,
150 speechTranslationClient: mediatranslationpb.NewSpeechTranslationServiceClient(connPool),
151 CallOptions: &client.CallOptions,
152 logger: internaloption.GetLogger(opts),
153 }
154 c.setGoogleClientInfo()
155
156 client.internalClient = c
157
158 return &client, nil
159 }
160
161 // Connection returns a connection to the API service.
162 //
163 // Deprecated: Connections are now pooled so this method does not always
164 // return the same resource.
165 func (c *speechTranslationGRPCClient) Connection() *grpc.ClientConn {
166 return c.connPool.Conn()
167 }
168
169 // setGoogleClientInfo sets the name and version of the application in
170 // the `x-goog-api-client` header passed on each request. Intended for
171 // use by Google-written clients.
172 func (c *speechTranslationGRPCClient) setGoogleClientInfo(keyval ...string) {
173 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
174 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version, "pb", protoVersion)
175 c.xGoogHeaders = []string{
176 "x-goog-api-client", gax.XGoogHeader(kv...),
177 }
178 }
179
180 // Close closes the connection to the API service. The user should invoke this when
181 // the client is no longer required.
182 func (c *speechTranslationGRPCClient) Close() error {
183 return c.connPool.Close()
184 }
185
186 func (c *speechTranslationGRPCClient) StreamingTranslateSpeech(ctx context.Context, opts ...gax.CallOption) (mediatranslationpb.SpeechTranslationService_StreamingTranslateSpeechClient, error) {
187 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, c.xGoogHeaders...)
188 var resp mediatranslationpb.SpeechTranslationService_StreamingTranslateSpeechClient
189 opts = append((*c.CallOptions).StreamingTranslateSpeech[0:len((*c.CallOptions).StreamingTranslateSpeech):len((*c.CallOptions).StreamingTranslateSpeech)], opts...)
190 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
191 var err error
192 c.logger.DebugContext(ctx, "api streaming client request", "serviceName", serviceName, "rpcName", "StreamingTranslateSpeech")
193 resp, err = c.speechTranslationClient.StreamingTranslateSpeech(ctx, settings.GRPC...)
194 c.logger.DebugContext(ctx, "api streaming client response", "serviceName", serviceName, "rpcName", "StreamingTranslateSpeech")
195 return err
196 }, opts...)
197 if err != nil {
198 return nil, err
199 }
200 return resp, nil
201 }