Debian Codegraph @

shopping/merchant/datasources/apiv1/file_uploads_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 datasources
18
19 import (
20 "context"
21 "fmt"
22 "log/slog"
23 "math"
24 "net/http"
25 "net/url"
26 "time"
27
28 datasourcespb "cloud.google.com/go/shopping/merchant/datasources/apiv1/datasourcespb"
29 gax "github.com/googleapis/gax-go/v2"
30 "google.golang.org/api/option"
31 "google.golang.org/api/option/internaloption"
32 gtransport "google.golang.org/api/transport/grpc"
33 httptransport "google.golang.org/api/transport/http"
34 "google.golang.org/grpc"
35 "google.golang.org/grpc/codes"
36 "google.golang.org/protobuf/encoding/protojson"
37 )
38
39 var newFileUploadsClientHook clientHook
40
41 // FileUploadsCallOptions contains the retry settings for each method of FileUploadsClient.
42 type FileUploadsCallOptions struct {
43 GetFileUpload []gax.CallOption
44 }
45
46 func defaultFileUploadsGRPCClientOptions() []option.ClientOption {
47 return []option.ClientOption{
48 internaloption.WithDefaultEndpoint("merchantapi.googleapis.com:443"),
49 internaloption.WithDefaultEndpointTemplate("merchantapi.UNIVERSE_DOMAIN:443"),
50 internaloption.WithDefaultMTLSEndpoint("merchantapi.mtls.googleapis.com:443"),
51 internaloption.WithDefaultUniverseDomain("googleapis.com"),
52 internaloption.WithDefaultAudience("https://merchantapi.googleapis.com/"),
53 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
54 internaloption.EnableJwtWithScope(),
55 internaloption.EnableNewAuthLibrary(),
56 option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
57 grpc.MaxCallRecvMsgSize(math.MaxInt32))),
58 }
59 }
60
61 func defaultFileUploadsCallOptions() *FileUploadsCallOptions {
62 return &FileUploadsCallOptions{
63 GetFileUpload: []gax.CallOption{
64 gax.WithTimeout(60000 * time.Millisecond),
65 gax.WithRetry(func() gax.Retryer {
66 return gax.OnCodes([]codes.Code{
67 codes.Unavailable,
68 }, gax.Backoff{
69 Initial: 1000 * time.Millisecond,
70 Max: 10000 * time.Millisecond,
71 Multiplier: 1.30,
72 })
73 }),
74 },
75 }
76 }
77
78 func defaultFileUploadsRESTCallOptions() *FileUploadsCallOptions {
79 return &FileUploadsCallOptions{
80 GetFileUpload: []gax.CallOption{
81 gax.WithTimeout(60000 * time.Millisecond),
82 gax.WithRetry(func() gax.Retryer {
83 return gax.OnHTTPCodes(gax.Backoff{
84 Initial: 1000 * time.Millisecond,
85 Max: 10000 * time.Millisecond,
86 Multiplier: 1.30,
87 },
88 http.StatusServiceUnavailable)
89 }),
90 },
91 }
92 }
93
94 // internalFileUploadsClient is an interface that defines the methods available from Merchant API.
95 type internalFileUploadsClient interface {
96 Close() error
97 setGoogleClientInfo(...string)
98 Connection() *grpc.ClientConn
99 GetFileUpload(context.Context, *datasourcespb.GetFileUploadRequest, ...gax.CallOption) (*datasourcespb.FileUpload, error)
100 }
101
102 // FileUploadsClient is a client for interacting with Merchant API.
103 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
104 //
105 // Service to manage data source file uploads.
106 type FileUploadsClient struct {
107 // The internal transport-dependent client.
108 internalClient internalFileUploadsClient
109
110 // The call options for this service.
111 CallOptions *FileUploadsCallOptions
112 }
113
114 // Wrapper methods routed to the internal client.
115
116 // Close closes the connection to the API service. The user should invoke this when
117 // the client is no longer required.
118 func (c *FileUploadsClient) Close() error {
119 return c.internalClient.Close()
120 }
121
122 // setGoogleClientInfo sets the name and version of the application in
123 // the `x-goog-api-client` header passed on each request. Intended for
124 // use by Google-written clients.
125 func (c *FileUploadsClient) setGoogleClientInfo(keyval ...string) {
126 c.internalClient.setGoogleClientInfo(keyval...)
127 }
128
129 // Connection returns a connection to the API service.
130 //
131 // Deprecated: Connections are now pooled so this method does not always
132 // return the same resource.
133 func (c *FileUploadsClient) Connection() *grpc.ClientConn {
134 return c.internalClient.Connection()
135 }
136
137 // GetFileUpload gets the latest data source file upload. Only the latest alias is
138 // accepted for a file upload.
139 func (c *FileUploadsClient) GetFileUpload(ctx context.Context, req *datasourcespb.GetFileUploadRequest, opts ...gax.CallOption) (*datasourcespb.FileUpload, error) {
140 return c.internalClient.GetFileUpload(ctx, req, opts...)
141 }
142
143 // fileUploadsGRPCClient is a client for interacting with Merchant API over gRPC transport.
144 //
145 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
146 type fileUploadsGRPCClient struct {
147 // Connection pool of gRPC connections to the service.
148 connPool gtransport.ConnPool
149
150 // Points back to the CallOptions field of the containing FileUploadsClient
151 CallOptions **FileUploadsCallOptions
152
153 // The gRPC API client.
154 fileUploadsClient datasourcespb.FileUploadsServiceClient
155
156 // The x-goog-* metadata to be sent with each request.
157 xGoogHeaders []string
158
159 logger *slog.Logger
160 }
161
162 // NewFileUploadsClient creates a new file uploads service client based on gRPC.
163 // The returned client must be Closed when it is done being used to clean up its underlying connections.
164 //
165 // Service to manage data source file uploads.
166 func NewFileUploadsClient(ctx context.Context, opts ...option.ClientOption) (*FileUploadsClient, error) {
167 clientOpts := defaultFileUploadsGRPCClientOptions()
168 if newFileUploadsClientHook != nil {
169 hookOpts, err := newFileUploadsClientHook(ctx, clientHookParams{})
170 if err != nil {
171 return nil, err
172 }
173 clientOpts = append(clientOpts, hookOpts...)
174 }
175
176 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
177 if err != nil {
178 return nil, err
179 }
180 client := FileUploadsClient{CallOptions: defaultFileUploadsCallOptions()}
181
182 c := &fileUploadsGRPCClient{
183 connPool: connPool,
184 fileUploadsClient: datasourcespb.NewFileUploadsServiceClient(connPool),
185 CallOptions: &client.CallOptions,
186 logger: internaloption.GetLogger(opts),
187 }
188 c.setGoogleClientInfo()
189
190 client.internalClient = c
191
192 return &client, nil
193 }
194
195 // Connection returns a connection to the API service.
196 //
197 // Deprecated: Connections are now pooled so this method does not always
198 // return the same resource.
199 func (c *fileUploadsGRPCClient) Connection() *grpc.ClientConn {
200 return c.connPool.Conn()
201 }
202
203 // setGoogleClientInfo sets the name and version of the application in
204 // the `x-goog-api-client` header passed on each request. Intended for
205 // use by Google-written clients.
206 func (c *fileUploadsGRPCClient) setGoogleClientInfo(keyval ...string) {
207 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
208 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version, "pb", protoVersion)
209 c.xGoogHeaders = []string{
210 "x-goog-api-client", gax.XGoogHeader(kv...),
211 }
212 }
213
214 // Close closes the connection to the API service. The user should invoke this when
215 // the client is no longer required.
216 func (c *fileUploadsGRPCClient) Close() error {
217 return c.connPool.Close()
218 }
219
220 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
221 type fileUploadsRESTClient struct {
222 // The http endpoint to connect to.
223 endpoint string
224
225 // The http client.
226 httpClient *http.Client
227
228 // The x-goog-* headers to be sent with each request.
229 xGoogHeaders []string
230
231 // Points back to the CallOptions field of the containing FileUploadsClient
232 CallOptions **FileUploadsCallOptions
233
234 logger *slog.Logger
235 }
236
237 // NewFileUploadsRESTClient creates a new file uploads service rest client.
238 //
239 // Service to manage data source file uploads.
240 func NewFileUploadsRESTClient(ctx context.Context, opts ...option.ClientOption) (*FileUploadsClient, error) {
241 clientOpts := append(defaultFileUploadsRESTClientOptions(), opts...)
242 httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...)
243 if err != nil {
244 return nil, err
245 }
246
247 callOpts := defaultFileUploadsRESTCallOptions()
248 c := &fileUploadsRESTClient{
249 endpoint: endpoint,
250 httpClient: httpClient,
251 CallOptions: &callOpts,
252 logger: internaloption.GetLogger(opts),
253 }
254 c.setGoogleClientInfo()
255
256 return &FileUploadsClient{internalClient: c, CallOptions: callOpts}, nil
257 }
258
259 func defaultFileUploadsRESTClientOptions() []option.ClientOption {
260 return []option.ClientOption{
261 internaloption.WithDefaultEndpoint("https://merchantapi.googleapis.com"),
262 internaloption.WithDefaultEndpointTemplate("https://merchantapi.UNIVERSE_DOMAIN"),
263 internaloption.WithDefaultMTLSEndpoint("https://merchantapi.mtls.googleapis.com"),
264 internaloption.WithDefaultUniverseDomain("googleapis.com"),
265 internaloption.WithDefaultAudience("https://merchantapi.googleapis.com/"),
266 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
267 internaloption.EnableNewAuthLibrary(),
268 }
269 }
270
271 // setGoogleClientInfo sets the name and version of the application in
272 // the `x-goog-api-client` header passed on each request. Intended for
273 // use by Google-written clients.
274 func (c *fileUploadsRESTClient) setGoogleClientInfo(keyval ...string) {
275 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
276 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN", "pb", protoVersion)
277 c.xGoogHeaders = []string{
278 "x-goog-api-client", gax.XGoogHeader(kv...),
279 }
280 }
281
282 // Close closes the connection to the API service. The user should invoke this when
283 // the client is no longer required.
284 func (c *fileUploadsRESTClient) Close() error {
285 // Replace httpClient with nil to force cleanup.
286 c.httpClient = nil
287 return nil
288 }
289
290 // Connection returns a connection to the API service.
291 //
292 // Deprecated: This method always returns nil.
293 func (c *fileUploadsRESTClient) Connection() *grpc.ClientConn {
294 return nil
295 }
296 func (c *fileUploadsGRPCClient) GetFileUpload(ctx context.Context, req *datasourcespb.GetFileUploadRequest, opts ...gax.CallOption) (*datasourcespb.FileUpload, error) {
297 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
298
299 hds = append(c.xGoogHeaders, hds...)
300 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
301 opts = append((*c.CallOptions).GetFileUpload[0:len((*c.CallOptions).GetFileUpload):len((*c.CallOptions).GetFileUpload)], opts...)
302 var resp *datasourcespb.FileUpload
303 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
304 var err error
305 resp, err = executeRPC(ctx, c.fileUploadsClient.GetFileUpload, req, settings.GRPC, c.logger, "GetFileUpload")
306 return err
307 }, opts...)
308 if err != nil {
309 return nil, err
310 }
311 return resp, nil
312 }
313
314 // GetFileUpload gets the latest data source file upload. Only the latest alias is
315 // accepted for a file upload.
316 func (c *fileUploadsRESTClient) GetFileUpload(ctx context.Context, req *datasourcespb.GetFileUploadRequest, opts ...gax.CallOption) (*datasourcespb.FileUpload, error) {
317 baseUrl, err := url.Parse(c.endpoint)
318 if err != nil {
319 return nil, err
320 }
321 baseUrl.Path += fmt.Sprintf("/datasources/v1/%v", req.GetName())
322
323 params := url.Values{}
324 params.Add("$alt", "json;enum-encoding=int")
325
326 baseUrl.RawQuery = params.Encode()
327
328 // Build HTTP headers from client and context metadata.
329 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
330
331 hds = append(c.xGoogHeaders, hds...)
332 hds = append(hds, "Content-Type", "application/json")
333 headers := gax.BuildHeaders(ctx, hds...)
334 opts = append((*c.CallOptions).GetFileUpload[0:len((*c.CallOptions).GetFileUpload):len((*c.CallOptions).GetFileUpload)], opts...)
335 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
336 resp := &datasourcespb.FileUpload{}
337 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
338 if settings.Path != "" {
339 baseUrl.Path = settings.Path
340 }
341 httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
342 if err != nil {
343 return err
344 }
345 httpReq = httpReq.WithContext(ctx)
346 httpReq.Header = headers
347
348 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "GetFileUpload")
349 if err != nil {
350 return err
351 }
352
353 if err := unm.Unmarshal(buf, resp); err != nil {
354 return err
355 }
356
357 return nil
358 }, opts...)
359 if e != nil {
360 return nil, e
361 }
362 return resp, nil
363 }