Debian Codegraph @

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