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 storage
18
19import (
20	"context"
21	"fmt"
22	"math"
23	"net/url"
24	"time"
25
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	storagepb "google.golang.org/genproto/googleapis/cloud/bigquery/storage/v1beta2"
31	"google.golang.org/grpc"
32	"google.golang.org/grpc/codes"
33	"google.golang.org/grpc/metadata"
34)
35
36var newBigQueryWriteClientHook clientHook
37
38// BigQueryWriteCallOptions contains the retry settings for each method of BigQueryWriteClient.
39type BigQueryWriteCallOptions struct {
40	CreateWriteStream       []gax.CallOption
41	AppendRows              []gax.CallOption
42	GetWriteStream          []gax.CallOption
43	FinalizeWriteStream     []gax.CallOption
44	BatchCommitWriteStreams []gax.CallOption
45	FlushRows               []gax.CallOption
46}
47
48func defaultBigQueryWriteGRPCClientOptions() []option.ClientOption {
49	return []option.ClientOption{
50		internaloption.WithDefaultEndpoint("bigquerystorage.googleapis.com:443"),
51		internaloption.WithDefaultMTLSEndpoint("bigquerystorage.mtls.googleapis.com:443"),
52		internaloption.WithDefaultAudience("https://bigquerystorage.googleapis.com/"),
53		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
54		internaloption.EnableJwtWithScope(),
55		option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
56		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
57			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
58	}
59}
60
61func defaultBigQueryWriteCallOptions() *BigQueryWriteCallOptions {
62	return &BigQueryWriteCallOptions{
63		CreateWriteStream: []gax.CallOption{
64			gax.WithRetry(func() gax.Retryer {
65				return gax.OnCodes([]codes.Code{
66					codes.DeadlineExceeded,
67					codes.Unavailable,
68					codes.ResourceExhausted,
69				}, gax.Backoff{
70					Initial:    100 * time.Millisecond,
71					Max:        60000 * time.Millisecond,
72					Multiplier: 1.30,
73				})
74			}),
75		},
76		AppendRows: []gax.CallOption{
77			gax.WithRetry(func() gax.Retryer {
78				return gax.OnCodes([]codes.Code{
79					codes.Unavailable,
80					codes.ResourceExhausted,
81				}, gax.Backoff{
82					Initial:    100 * time.Millisecond,
83					Max:        60000 * time.Millisecond,
84					Multiplier: 1.30,
85				})
86			}),
87		},
88		GetWriteStream: []gax.CallOption{
89			gax.WithRetry(func() gax.Retryer {
90				return gax.OnCodes([]codes.Code{
91					codes.DeadlineExceeded,
92					codes.Unavailable,
93				}, gax.Backoff{
94					Initial:    100 * time.Millisecond,
95					Max:        60000 * time.Millisecond,
96					Multiplier: 1.30,
97				})
98			}),
99		},
100		FinalizeWriteStream: []gax.CallOption{
101			gax.WithRetry(func() gax.Retryer {
102				return gax.OnCodes([]codes.Code{
103					codes.DeadlineExceeded,
104					codes.Unavailable,
105				}, gax.Backoff{
106					Initial:    100 * time.Millisecond,
107					Max:        60000 * time.Millisecond,
108					Multiplier: 1.30,
109				})
110			}),
111		},
112		BatchCommitWriteStreams: []gax.CallOption{
113			gax.WithRetry(func() gax.Retryer {
114				return gax.OnCodes([]codes.Code{
115					codes.DeadlineExceeded,
116					codes.Unavailable,
117				}, gax.Backoff{
118					Initial:    100 * time.Millisecond,
119					Max:        60000 * time.Millisecond,
120					Multiplier: 1.30,
121				})
122			}),
123		},
124		FlushRows: []gax.CallOption{
125			gax.WithRetry(func() gax.Retryer {
126				return gax.OnCodes([]codes.Code{
127					codes.DeadlineExceeded,
128					codes.Unavailable,
129				}, gax.Backoff{
130					Initial:    100 * time.Millisecond,
131					Max:        60000 * time.Millisecond,
132					Multiplier: 1.30,
133				})
134			}),
135		},
136	}
137}
138
139// internalBigQueryWriteClient is an interface that defines the methods availaible from BigQuery Storage API.
140type internalBigQueryWriteClient interface {
141	Close() error
142	setGoogleClientInfo(...string)
143	Connection() *grpc.ClientConn
144	CreateWriteStream(context.Context, *storagepb.CreateWriteStreamRequest, ...gax.CallOption) (*storagepb.WriteStream, error)
145	AppendRows(context.Context, ...gax.CallOption) (storagepb.BigQueryWrite_AppendRowsClient, error)
146	GetWriteStream(context.Context, *storagepb.GetWriteStreamRequest, ...gax.CallOption) (*storagepb.WriteStream, error)
147	FinalizeWriteStream(context.Context, *storagepb.FinalizeWriteStreamRequest, ...gax.CallOption) (*storagepb.FinalizeWriteStreamResponse, error)
148	BatchCommitWriteStreams(context.Context, *storagepb.BatchCommitWriteStreamsRequest, ...gax.CallOption) (*storagepb.BatchCommitWriteStreamsResponse, error)
149	FlushRows(context.Context, *storagepb.FlushRowsRequest, ...gax.CallOption) (*storagepb.FlushRowsResponse, error)
150}
151
152// BigQueryWriteClient is a client for interacting with BigQuery Storage API.
153// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
154//
155// BigQuery Write API.
156//
157// The Write API can be used to write data to BigQuery.
158type BigQueryWriteClient struct {
159	// The internal transport-dependent client.
160	internalClient internalBigQueryWriteClient
161
162	// The call options for this service.
163	CallOptions *BigQueryWriteCallOptions
164}
165
166// Wrapper methods routed to the internal client.
167
168// Close closes the connection to the API service. The user should invoke this when
169// the client is no longer required.
170func (c *BigQueryWriteClient) Close() error {
171	return c.internalClient.Close()
172}
173
174// setGoogleClientInfo sets the name and version of the application in
175// the `x-goog-api-client` header passed on each request. Intended for
176// use by Google-written clients.
177func (c *BigQueryWriteClient) setGoogleClientInfo(keyval ...string) {
178	c.internalClient.setGoogleClientInfo(keyval...)
179}
180
181// Connection returns a connection to the API service.
182//
183// Deprecated.
184func (c *BigQueryWriteClient) Connection() *grpc.ClientConn {
185	return c.internalClient.Connection()
186}
187
188// CreateWriteStream creates a write stream to the given table.
189// Additionally, every table has a special COMMITTED stream named ‘_default’
190// to which data can be written. This stream doesn’t need to be created using
191// CreateWriteStream. It is a stream that can be used simultaneously by any
192// number of clients. Data written to this stream is considered committed as
193// soon as an acknowledgement is received.
194func (c *BigQueryWriteClient) CreateWriteStream(ctx context.Context, req *storagepb.CreateWriteStreamRequest, opts ...gax.CallOption) (*storagepb.WriteStream, error) {
195	return c.internalClient.CreateWriteStream(ctx, req, opts...)
196}
197
198// AppendRows appends data to the given stream.
199//
200// If offset is specified, the offset is checked against the end of
201// stream. The server returns OUT_OF_RANGE in AppendRowsResponse if an
202// attempt is made to append to an offset beyond the current end of the stream
203// or ALREADY_EXISTS if user provids an offset that has already been
204// written to. User can retry with adjusted offset within the same RPC
205// stream. If offset is not specified, append happens at the end of the
206// stream.
207//
208// The response contains the offset at which the append happened. Responses
209// are received in the same order in which requests are sent. There will be
210// one response for each successful request. If the offset is not set in
211// response, it means append didn’t happen due to some errors. If one request
212// fails, all the subsequent requests will also fail until a success request
213// is made again.
214//
215// If the stream is of PENDING type, data will only be available for read
216// operations after the stream is committed.
217func (c *BigQueryWriteClient) AppendRows(ctx context.Context, opts ...gax.CallOption) (storagepb.BigQueryWrite_AppendRowsClient, error) {
218	return c.internalClient.AppendRows(ctx, opts...)
219}
220
221// GetWriteStream gets a write stream.
222func (c *BigQueryWriteClient) GetWriteStream(ctx context.Context, req *storagepb.GetWriteStreamRequest, opts ...gax.CallOption) (*storagepb.WriteStream, error) {
223	return c.internalClient.GetWriteStream(ctx, req, opts...)
224}
225
226// FinalizeWriteStream finalize a write stream so that no new data can be appended to the
227// stream. Finalize is not supported on the ‘_default’ stream.
228func (c *BigQueryWriteClient) FinalizeWriteStream(ctx context.Context, req *storagepb.FinalizeWriteStreamRequest, opts ...gax.CallOption) (*storagepb.FinalizeWriteStreamResponse, error) {
229	return c.internalClient.FinalizeWriteStream(ctx, req, opts...)
230}
231
232// BatchCommitWriteStreams atomically commits a group of PENDING streams that belong to the same
233// parent table.
234// Streams must be finalized before commit and cannot be committed multiple
235// times. Once a stream is committed, data in the stream becomes available
236// for read operations.
237func (c *BigQueryWriteClient) BatchCommitWriteStreams(ctx context.Context, req *storagepb.BatchCommitWriteStreamsRequest, opts ...gax.CallOption) (*storagepb.BatchCommitWriteStreamsResponse, error) {
238	return c.internalClient.BatchCommitWriteStreams(ctx, req, opts...)
239}
240
241// FlushRows flushes rows to a BUFFERED stream.
242// If users are appending rows to BUFFERED stream, flush operation is
243// required in order for the rows to become available for reading. A
244// Flush operation flushes up to any previously flushed offset in a BUFFERED
245// stream, to the offset specified in the request.
246// Flush is not supported on the _default stream, since it is not BUFFERED.
247func (c *BigQueryWriteClient) FlushRows(ctx context.Context, req *storagepb.FlushRowsRequest, opts ...gax.CallOption) (*storagepb.FlushRowsResponse, error) {
248	return c.internalClient.FlushRows(ctx, req, opts...)
249}
250
251// bigQueryWriteGRPCClient is a client for interacting with BigQuery Storage API over gRPC transport.
252//
253// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
254type bigQueryWriteGRPCClient struct {
255	// Connection pool of gRPC connections to the service.
256	connPool gtransport.ConnPool
257
258	// flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE
259	disableDeadlines bool
260
261	// Points back to the CallOptions field of the containing BigQueryWriteClient
262	CallOptions **BigQueryWriteCallOptions
263
264	// The gRPC API client.
265	bigQueryWriteClient storagepb.BigQueryWriteClient
266
267	// The x-goog-* metadata to be sent with each request.
268	xGoogMetadata metadata.MD
269}
270
271// NewBigQueryWriteClient creates a new big query write client based on gRPC.
272// The returned client must be Closed when it is done being used to clean up its underlying connections.
273//
274// BigQuery Write API.
275//
276// The Write API can be used to write data to BigQuery.
277func NewBigQueryWriteClient(ctx context.Context, opts ...option.ClientOption) (*BigQueryWriteClient, error) {
278	clientOpts := defaultBigQueryWriteGRPCClientOptions()
279	if newBigQueryWriteClientHook != nil {
280		hookOpts, err := newBigQueryWriteClientHook(ctx, clientHookParams{})
281		if err != nil {
282			return nil, err
283		}
284		clientOpts = append(clientOpts, hookOpts...)
285	}
286
287	disableDeadlines, err := checkDisableDeadlines()
288	if err != nil {
289		return nil, err
290	}
291
292	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
293	if err != nil {
294		return nil, err
295	}
296	client := BigQueryWriteClient{CallOptions: defaultBigQueryWriteCallOptions()}
297
298	c := &bigQueryWriteGRPCClient{
299		connPool:            connPool,
300		disableDeadlines:    disableDeadlines,
301		bigQueryWriteClient: storagepb.NewBigQueryWriteClient(connPool),
302		CallOptions:         &client.CallOptions,
303	}
304	c.setGoogleClientInfo()
305
306	client.internalClient = c
307
308	return &client, nil
309}
310
311// Connection returns a connection to the API service.
312//
313// Deprecated.
314func (c *bigQueryWriteGRPCClient) Connection() *grpc.ClientConn {
315	return c.connPool.Conn()
316}
317
318// setGoogleClientInfo sets the name and version of the application in
319// the `x-goog-api-client` header passed on each request. Intended for
320// use by Google-written clients.
321func (c *bigQueryWriteGRPCClient) setGoogleClientInfo(keyval ...string) {
322	kv := append([]string{"gl-go", versionGo()}, keyval...)
323	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
324	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
325}
326
327// Close closes the connection to the API service. The user should invoke this when
328// the client is no longer required.
329func (c *bigQueryWriteGRPCClient) Close() error {
330	return c.connPool.Close()
331}
332
333func (c *bigQueryWriteGRPCClient) CreateWriteStream(ctx context.Context, req *storagepb.CreateWriteStreamRequest, opts ...gax.CallOption) (*storagepb.WriteStream, error) {
334	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
335		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
336		defer cancel()
337		ctx = cctx
338	}
339	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
340	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
341	opts = append((*c.CallOptions).CreateWriteStream[0:len((*c.CallOptions).CreateWriteStream):len((*c.CallOptions).CreateWriteStream)], opts...)
342	var resp *storagepb.WriteStream
343	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
344		var err error
345		resp, err = c.bigQueryWriteClient.CreateWriteStream(ctx, req, settings.GRPC...)
346		return err
347	}, opts...)
348	if err != nil {
349		return nil, err
350	}
351	return resp, nil
352}
353
354func (c *bigQueryWriteGRPCClient) AppendRows(ctx context.Context, opts ...gax.CallOption) (storagepb.BigQueryWrite_AppendRowsClient, error) {
355	ctx = insertMetadata(ctx, c.xGoogMetadata)
356	var resp storagepb.BigQueryWrite_AppendRowsClient
357	opts = append((*c.CallOptions).AppendRows[0:len((*c.CallOptions).AppendRows):len((*c.CallOptions).AppendRows)], opts...)
358	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
359		var err error
360		resp, err = c.bigQueryWriteClient.AppendRows(ctx, settings.GRPC...)
361		return err
362	}, opts...)
363	if err != nil {
364		return nil, err
365	}
366	return resp, nil
367}
368
369func (c *bigQueryWriteGRPCClient) GetWriteStream(ctx context.Context, req *storagepb.GetWriteStreamRequest, opts ...gax.CallOption) (*storagepb.WriteStream, error) {
370	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
371		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
372		defer cancel()
373		ctx = cctx
374	}
375	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
376	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
377	opts = append((*c.CallOptions).GetWriteStream[0:len((*c.CallOptions).GetWriteStream):len((*c.CallOptions).GetWriteStream)], opts...)
378	var resp *storagepb.WriteStream
379	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
380		var err error
381		resp, err = c.bigQueryWriteClient.GetWriteStream(ctx, req, settings.GRPC...)
382		return err
383	}, opts...)
384	if err != nil {
385		return nil, err
386	}
387	return resp, nil
388}
389
390func (c *bigQueryWriteGRPCClient) FinalizeWriteStream(ctx context.Context, req *storagepb.FinalizeWriteStreamRequest, opts ...gax.CallOption) (*storagepb.FinalizeWriteStreamResponse, error) {
391	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
392		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
393		defer cancel()
394		ctx = cctx
395	}
396	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
397	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
398	opts = append((*c.CallOptions).FinalizeWriteStream[0:len((*c.CallOptions).FinalizeWriteStream):len((*c.CallOptions).FinalizeWriteStream)], opts...)
399	var resp *storagepb.FinalizeWriteStreamResponse
400	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
401		var err error
402		resp, err = c.bigQueryWriteClient.FinalizeWriteStream(ctx, req, settings.GRPC...)
403		return err
404	}, opts...)
405	if err != nil {
406		return nil, err
407	}
408	return resp, nil
409}
410
411func (c *bigQueryWriteGRPCClient) BatchCommitWriteStreams(ctx context.Context, req *storagepb.BatchCommitWriteStreamsRequest, opts ...gax.CallOption) (*storagepb.BatchCommitWriteStreamsResponse, error) {
412	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
413		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
414		defer cancel()
415		ctx = cctx
416	}
417	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
418	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
419	opts = append((*c.CallOptions).BatchCommitWriteStreams[0:len((*c.CallOptions).BatchCommitWriteStreams):len((*c.CallOptions).BatchCommitWriteStreams)], opts...)
420	var resp *storagepb.BatchCommitWriteStreamsResponse
421	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
422		var err error
423		resp, err = c.bigQueryWriteClient.BatchCommitWriteStreams(ctx, req, settings.GRPC...)
424		return err
425	}, opts...)
426	if err != nil {
427		return nil, err
428	}
429	return resp, nil
430}
431
432func (c *bigQueryWriteGRPCClient) FlushRows(ctx context.Context, req *storagepb.FlushRowsRequest, opts ...gax.CallOption) (*storagepb.FlushRowsResponse, error) {
433	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
434		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
435		defer cancel()
436		ctx = cctx
437	}
438	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "write_stream", url.QueryEscape(req.GetWriteStream())))
439	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
440	opts = append((*c.CallOptions).FlushRows[0:len((*c.CallOptions).FlushRows):len((*c.CallOptions).FlushRows)], opts...)
441	var resp *storagepb.FlushRowsResponse
442	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
443		var err error
444		resp, err = c.bigQueryWriteClient.FlushRows(ctx, req, settings.GRPC...)
445		return err
446	}, opts...)
447	if err != nil {
448		return nil, err
449	}
450	return resp, nil
451}
452