Debian Codegraph @

shopping/merchant/lfp/apiv1beta/lfp_sale_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/apiv1beta/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 newLfpSaleClientHook clientHook
41
42 // LfpSaleCallOptions contains the retry settings for each method of LfpSaleClient.
43 type LfpSaleCallOptions struct {
44 InsertLfpSale []gax.CallOption
45 }
46
47 func defaultLfpSaleGRPCClientOptions() []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 defaultLfpSaleCallOptions() *LfpSaleCallOptions {
63 return &LfpSaleCallOptions{
64 InsertLfpSale: []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 defaultLfpSaleRESTCallOptions() *LfpSaleCallOptions {
80 return &LfpSaleCallOptions{
81 InsertLfpSale: []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 // internalLfpSaleClient is an interface that defines the methods available from Merchant API.
96 type internalLfpSaleClient interface {
97 Close() error
98 setGoogleClientInfo(...string)
99 Connection() *grpc.ClientConn
100 InsertLfpSale(context.Context, *lfppb.InsertLfpSaleRequest, ...gax.CallOption) (*lfppb.LfpSale, error)
101 }
102
103 // LfpSaleClient 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 sales
108 // data for a merchant.
109 type LfpSaleClient struct {
110 // The internal transport-dependent client.
111 internalClient internalLfpSaleClient
112
113 // The call options for this service.
114 CallOptions *LfpSaleCallOptions
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 *LfpSaleClient) 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 *LfpSaleClient) 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 *LfpSaleClient) Connection() *grpc.ClientConn {
137 return c.internalClient.Connection()
138 }
139
140 // InsertLfpSale inserts a LfpSale for the given merchant.
141 func (c *LfpSaleClient) InsertLfpSale(ctx context.Context, req *lfppb.InsertLfpSaleRequest, opts ...gax.CallOption) (*lfppb.LfpSale, error) {
142 return c.internalClient.InsertLfpSale(ctx, req, opts...)
143 }
144
145 // lfpSaleGRPCClient is a client for interacting with Merchant API over gRPC transport.
146 //
147 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
148 type lfpSaleGRPCClient struct {
149 // Connection pool of gRPC connections to the service.
150 connPool gtransport.ConnPool
151
152 // Points back to the CallOptions field of the containing LfpSaleClient
153 CallOptions **LfpSaleCallOptions
154
155 // The gRPC API client.
156 lfpSaleClient lfppb.LfpSaleServiceClient
157
158 // The x-goog-* metadata to be sent with each request.
159 xGoogHeaders []string
160
161 logger *slog.Logger
162 }
163
164 // NewLfpSaleClient creates a new lfp sale service client based on gRPC.
165 // The returned client must be Closed when it is done being used to clean up its underlying connections.
166 //
167 // Service for a LFP
168 // partner (at https://support.google.com/merchants/answer/7676652) to submit sales
169 // data for a merchant.
170 func NewLfpSaleClient(ctx context.Context, opts ...option.ClientOption) (*LfpSaleClient, error) {
171 clientOpts := defaultLfpSaleGRPCClientOptions()
172 if newLfpSaleClientHook != nil {
173 hookOpts, err := newLfpSaleClientHook(ctx, clientHookParams{})
174 if err != nil {
175 return nil, err
176 }
177 clientOpts = append(clientOpts, hookOpts...)
178 }
179
180 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
181 if err != nil {
182 return nil, err
183 }
184 client := LfpSaleClient{CallOptions: defaultLfpSaleCallOptions()}
185
186 c := &lfpSaleGRPCClient{
187 connPool: connPool,
188 lfpSaleClient: lfppb.NewLfpSaleServiceClient(connPool),
189 CallOptions: &client.CallOptions,
190 logger: internaloption.GetLogger(opts),
191 }
192 c.setGoogleClientInfo()
193
194 client.internalClient = c
195
196 return &client, nil
197 }
198
199 // Connection returns a connection to the API service.
200 //
201 // Deprecated: Connections are now pooled so this method does not always
202 // return the same resource.
203 func (c *lfpSaleGRPCClient) Connection() *grpc.ClientConn {
204 return c.connPool.Conn()
205 }
206
207 // setGoogleClientInfo sets the name and version of the application in
208 // the `x-goog-api-client` header passed on each request. Intended for
209 // use by Google-written clients.
210 func (c *lfpSaleGRPCClient) setGoogleClientInfo(keyval ...string) {
211 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
212 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version, "pb", protoVersion)
213 c.xGoogHeaders = []string{
214 "x-goog-api-client", gax.XGoogHeader(kv...),
215 }
216 }
217
218 // Close closes the connection to the API service. The user should invoke this when
219 // the client is no longer required.
220 func (c *lfpSaleGRPCClient) Close() error {
221 return c.connPool.Close()
222 }
223
224 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
225 type lfpSaleRESTClient struct {
226 // The http endpoint to connect to.
227 endpoint string
228
229 // The http client.
230 httpClient *http.Client
231
232 // The x-goog-* headers to be sent with each request.
233 xGoogHeaders []string
234
235 // Points back to the CallOptions field of the containing LfpSaleClient
236 CallOptions **LfpSaleCallOptions
237
238 logger *slog.Logger
239 }
240
241 // NewLfpSaleRESTClient creates a new lfp sale service rest client.
242 //
243 // Service for a LFP
244 // partner (at https://support.google.com/merchants/answer/7676652) to submit sales
245 // data for a merchant.
246 func NewLfpSaleRESTClient(ctx context.Context, opts ...option.ClientOption) (*LfpSaleClient, error) {
247 clientOpts := append(defaultLfpSaleRESTClientOptions(), opts...)
248 httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...)
249 if err != nil {
250 return nil, err
251 }
252
253 callOpts := defaultLfpSaleRESTCallOptions()
254 c := &lfpSaleRESTClient{
255 endpoint: endpoint,
256 httpClient: httpClient,
257 CallOptions: &callOpts,
258 logger: internaloption.GetLogger(opts),
259 }
260 c.setGoogleClientInfo()
261
262 return &LfpSaleClient{internalClient: c, CallOptions: callOpts}, nil
263 }
264
265 func defaultLfpSaleRESTClientOptions() []option.ClientOption {
266 return []option.ClientOption{
267 internaloption.WithDefaultEndpoint("https://merchantapi.googleapis.com"),
268 internaloption.WithDefaultEndpointTemplate("https://merchantapi.UNIVERSE_DOMAIN"),
269 internaloption.WithDefaultMTLSEndpoint("https://merchantapi.mtls.googleapis.com"),
270 internaloption.WithDefaultUniverseDomain("googleapis.com"),
271 internaloption.WithDefaultAudience("https://merchantapi.googleapis.com/"),
272 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
273 internaloption.EnableNewAuthLibrary(),
274 }
275 }
276
277 // setGoogleClientInfo sets the name and version of the application in
278 // the `x-goog-api-client` header passed on each request. Intended for
279 // use by Google-written clients.
280 func (c *lfpSaleRESTClient) setGoogleClientInfo(keyval ...string) {
281 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
282 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN", "pb", protoVersion)
283 c.xGoogHeaders = []string{
284 "x-goog-api-client", gax.XGoogHeader(kv...),
285 }
286 }
287
288 // Close closes the connection to the API service. The user should invoke this when
289 // the client is no longer required.
290 func (c *lfpSaleRESTClient) Close() error {
291 // Replace httpClient with nil to force cleanup.
292 c.httpClient = nil
293 return nil
294 }
295
296 // Connection returns a connection to the API service.
297 //
298 // Deprecated: This method always returns nil.
299 func (c *lfpSaleRESTClient) Connection() *grpc.ClientConn {
300 return nil
301 }
302 func (c *lfpSaleGRPCClient) InsertLfpSale(ctx context.Context, req *lfppb.InsertLfpSaleRequest, opts ...gax.CallOption) (*lfppb.LfpSale, error) {
303 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
304
305 hds = append(c.xGoogHeaders, hds...)
306 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
307 opts = append((*c.CallOptions).InsertLfpSale[0:len((*c.CallOptions).InsertLfpSale):len((*c.CallOptions).InsertLfpSale)], opts...)
308 var resp *lfppb.LfpSale
309 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
310 var err error
311 resp, err = executeRPC(ctx, c.lfpSaleClient.InsertLfpSale, req, settings.GRPC, c.logger, "InsertLfpSale")
312 return err
313 }, opts...)
314 if err != nil {
315 return nil, err
316 }
317 return resp, nil
318 }
319
320 // InsertLfpSale inserts a LfpSale for the given merchant.
321 func (c *lfpSaleRESTClient) InsertLfpSale(ctx context.Context, req *lfppb.InsertLfpSaleRequest, opts ...gax.CallOption) (*lfppb.LfpSale, error) {
322 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
323 body := req.GetLfpSale()
324 jsonReq, err := m.Marshal(body)
325 if err != nil {
326 return nil, err
327 }
328
329 baseUrl, err := url.Parse(c.endpoint)
330 if err != nil {
331 return nil, err
332 }
333 baseUrl.Path += fmt.Sprintf("/lfp/v1beta/%v/lfpSales:insert", req.GetParent())
334
335 params := url.Values{}
336 params.Add("$alt", "json;enum-encoding=int")
337
338 baseUrl.RawQuery = params.Encode()
339
340 // Build HTTP headers from client and context metadata.
341 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
342
343 hds = append(c.xGoogHeaders, hds...)
344 hds = append(hds, "Content-Type", "application/json")
345 headers := gax.BuildHeaders(ctx, hds...)
346 opts = append((*c.CallOptions).InsertLfpSale[0:len((*c.CallOptions).InsertLfpSale):len((*c.CallOptions).InsertLfpSale)], opts...)
347 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
348 resp := &lfppb.LfpSale{}
349 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
350 if settings.Path != "" {
351 baseUrl.Path = settings.Path
352 }
353 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
354 if err != nil {
355 return err
356 }
357 httpReq = httpReq.WithContext(ctx)
358 httpReq.Header = headers
359
360 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "InsertLfpSale")
361 if err != nil {
362 return err
363 }
364
365 if err := unm.Unmarshal(buf, resp); err != nil {
366 return err
367 }
368
369 return nil
370 }, opts...)
371 if e != nil {
372 return nil, e
373 }
374 return resp, nil
375 }