...

Source file src/cloud.google.com/go/bigquery/migration/apiv2alpha/sql_translation_client.go

Documentation: cloud.google.com/go/bigquery/migration/apiv2alpha

     1  // Copyright 2024 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 migration
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"fmt"
    23  	"io"
    24  	"math"
    25  	"net/http"
    26  	"net/url"
    27  
    28  	migrationpb "cloud.google.com/go/bigquery/migration/apiv2alpha/migrationpb"
    29  	gax "github.com/googleapis/gax-go/v2"
    30  	"google.golang.org/api/googleapi"
    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/protobuf/encoding/protojson"
    37  )
    38  
    39  var newSqlTranslationClientHook clientHook
    40  
    41  // SqlTranslationCallOptions contains the retry settings for each method of SqlTranslationClient.
    42  type SqlTranslationCallOptions struct {
    43  	TranslateQuery []gax.CallOption
    44  }
    45  
    46  func defaultSqlTranslationGRPCClientOptions() []option.ClientOption {
    47  	return []option.ClientOption{
    48  		internaloption.WithDefaultEndpoint("bigquerymigration.googleapis.com:443"),
    49  		internaloption.WithDefaultEndpointTemplate("bigquerymigration.UNIVERSE_DOMAIN:443"),
    50  		internaloption.WithDefaultMTLSEndpoint("bigquerymigration.mtls.googleapis.com:443"),
    51  		internaloption.WithDefaultUniverseDomain("googleapis.com"),
    52  		internaloption.WithDefaultAudience("https://bigquerymigration.googleapis.com/"),
    53  		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
    54  		internaloption.EnableJwtWithScope(),
    55  		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
    56  			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
    57  	}
    58  }
    59  
    60  func defaultSqlTranslationCallOptions() *SqlTranslationCallOptions {
    61  	return &SqlTranslationCallOptions{
    62  		TranslateQuery: []gax.CallOption{},
    63  	}
    64  }
    65  
    66  func defaultSqlTranslationRESTCallOptions() *SqlTranslationCallOptions {
    67  	return &SqlTranslationCallOptions{
    68  		TranslateQuery: []gax.CallOption{},
    69  	}
    70  }
    71  
    72  // internalSqlTranslationClient is an interface that defines the methods available from BigQuery Migration API.
    73  type internalSqlTranslationClient interface {
    74  	Close() error
    75  	setGoogleClientInfo(...string)
    76  	Connection() *grpc.ClientConn
    77  	TranslateQuery(context.Context, *migrationpb.TranslateQueryRequest, ...gax.CallOption) (*migrationpb.TranslateQueryResponse, error)
    78  }
    79  
    80  // SqlTranslationClient is a client for interacting with BigQuery Migration API.
    81  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
    82  //
    83  // Provides other SQL dialects to GoogleSQL translation operations.
    84  type SqlTranslationClient struct {
    85  	// The internal transport-dependent client.
    86  	internalClient internalSqlTranslationClient
    87  
    88  	// The call options for this service.
    89  	CallOptions *SqlTranslationCallOptions
    90  }
    91  
    92  // Wrapper methods routed to the internal client.
    93  
    94  // Close closes the connection to the API service. The user should invoke this when
    95  // the client is no longer required.
    96  func (c *SqlTranslationClient) Close() error {
    97  	return c.internalClient.Close()
    98  }
    99  
   100  // setGoogleClientInfo sets the name and version of the application in
   101  // the `x-goog-api-client` header passed on each request. Intended for
   102  // use by Google-written clients.
   103  func (c *SqlTranslationClient) setGoogleClientInfo(keyval ...string) {
   104  	c.internalClient.setGoogleClientInfo(keyval...)
   105  }
   106  
   107  // Connection returns a connection to the API service.
   108  //
   109  // Deprecated: Connections are now pooled so this method does not always
   110  // return the same resource.
   111  func (c *SqlTranslationClient) Connection() *grpc.ClientConn {
   112  	return c.internalClient.Connection()
   113  }
   114  
   115  // TranslateQuery translates input queries from source dialects to GoogleSQL.
   116  func (c *SqlTranslationClient) TranslateQuery(ctx context.Context, req *migrationpb.TranslateQueryRequest, opts ...gax.CallOption) (*migrationpb.TranslateQueryResponse, error) {
   117  	return c.internalClient.TranslateQuery(ctx, req, opts...)
   118  }
   119  
   120  // sqlTranslationGRPCClient is a client for interacting with BigQuery Migration API over gRPC transport.
   121  //
   122  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   123  type sqlTranslationGRPCClient struct {
   124  	// Connection pool of gRPC connections to the service.
   125  	connPool gtransport.ConnPool
   126  
   127  	// Points back to the CallOptions field of the containing SqlTranslationClient
   128  	CallOptions **SqlTranslationCallOptions
   129  
   130  	// The gRPC API client.
   131  	sqlTranslationClient migrationpb.SqlTranslationServiceClient
   132  
   133  	// The x-goog-* metadata to be sent with each request.
   134  	xGoogHeaders []string
   135  }
   136  
   137  // NewSqlTranslationClient creates a new sql translation service client based on gRPC.
   138  // The returned client must be Closed when it is done being used to clean up its underlying connections.
   139  //
   140  // Provides other SQL dialects to GoogleSQL translation operations.
   141  func NewSqlTranslationClient(ctx context.Context, opts ...option.ClientOption) (*SqlTranslationClient, error) {
   142  	clientOpts := defaultSqlTranslationGRPCClientOptions()
   143  	if newSqlTranslationClientHook != nil {
   144  		hookOpts, err := newSqlTranslationClientHook(ctx, clientHookParams{})
   145  		if err != nil {
   146  			return nil, err
   147  		}
   148  		clientOpts = append(clientOpts, hookOpts...)
   149  	}
   150  
   151  	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
   152  	if err != nil {
   153  		return nil, err
   154  	}
   155  	client := SqlTranslationClient{CallOptions: defaultSqlTranslationCallOptions()}
   156  
   157  	c := &sqlTranslationGRPCClient{
   158  		connPool:             connPool,
   159  		sqlTranslationClient: migrationpb.NewSqlTranslationServiceClient(connPool),
   160  		CallOptions:          &client.CallOptions,
   161  	}
   162  	c.setGoogleClientInfo()
   163  
   164  	client.internalClient = c
   165  
   166  	return &client, nil
   167  }
   168  
   169  // Connection returns a connection to the API service.
   170  //
   171  // Deprecated: Connections are now pooled so this method does not always
   172  // return the same resource.
   173  func (c *sqlTranslationGRPCClient) Connection() *grpc.ClientConn {
   174  	return c.connPool.Conn()
   175  }
   176  
   177  // setGoogleClientInfo sets the name and version of the application in
   178  // the `x-goog-api-client` header passed on each request. Intended for
   179  // use by Google-written clients.
   180  func (c *sqlTranslationGRPCClient) setGoogleClientInfo(keyval ...string) {
   181  	kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
   182  	kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version)
   183  	c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
   184  }
   185  
   186  // Close closes the connection to the API service. The user should invoke this when
   187  // the client is no longer required.
   188  func (c *sqlTranslationGRPCClient) Close() error {
   189  	return c.connPool.Close()
   190  }
   191  
   192  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   193  type sqlTranslationRESTClient struct {
   194  	// The http endpoint to connect to.
   195  	endpoint string
   196  
   197  	// The http client.
   198  	httpClient *http.Client
   199  
   200  	// The x-goog-* headers to be sent with each request.
   201  	xGoogHeaders []string
   202  
   203  	// Points back to the CallOptions field of the containing SqlTranslationClient
   204  	CallOptions **SqlTranslationCallOptions
   205  }
   206  
   207  // NewSqlTranslationRESTClient creates a new sql translation service rest client.
   208  //
   209  // Provides other SQL dialects to GoogleSQL translation operations.
   210  func NewSqlTranslationRESTClient(ctx context.Context, opts ...option.ClientOption) (*SqlTranslationClient, error) {
   211  	clientOpts := append(defaultSqlTranslationRESTClientOptions(), opts...)
   212  	httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...)
   213  	if err != nil {
   214  		return nil, err
   215  	}
   216  
   217  	callOpts := defaultSqlTranslationRESTCallOptions()
   218  	c := &sqlTranslationRESTClient{
   219  		endpoint:    endpoint,
   220  		httpClient:  httpClient,
   221  		CallOptions: &callOpts,
   222  	}
   223  	c.setGoogleClientInfo()
   224  
   225  	return &SqlTranslationClient{internalClient: c, CallOptions: callOpts}, nil
   226  }
   227  
   228  func defaultSqlTranslationRESTClientOptions() []option.ClientOption {
   229  	return []option.ClientOption{
   230  		internaloption.WithDefaultEndpoint("https://bigquerymigration.googleapis.com"),
   231  		internaloption.WithDefaultEndpointTemplate("https://bigquerymigration.UNIVERSE_DOMAIN"),
   232  		internaloption.WithDefaultMTLSEndpoint("https://bigquerymigration.mtls.googleapis.com"),
   233  		internaloption.WithDefaultUniverseDomain("googleapis.com"),
   234  		internaloption.WithDefaultAudience("https://bigquerymigration.googleapis.com/"),
   235  		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
   236  	}
   237  }
   238  
   239  // setGoogleClientInfo sets the name and version of the application in
   240  // the `x-goog-api-client` header passed on each request. Intended for
   241  // use by Google-written clients.
   242  func (c *sqlTranslationRESTClient) setGoogleClientInfo(keyval ...string) {
   243  	kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
   244  	kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN")
   245  	c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
   246  }
   247  
   248  // Close closes the connection to the API service. The user should invoke this when
   249  // the client is no longer required.
   250  func (c *sqlTranslationRESTClient) Close() error {
   251  	// Replace httpClient with nil to force cleanup.
   252  	c.httpClient = nil
   253  	return nil
   254  }
   255  
   256  // Connection returns a connection to the API service.
   257  //
   258  // Deprecated: This method always returns nil.
   259  func (c *sqlTranslationRESTClient) Connection() *grpc.ClientConn {
   260  	return nil
   261  }
   262  func (c *sqlTranslationGRPCClient) TranslateQuery(ctx context.Context, req *migrationpb.TranslateQueryRequest, opts ...gax.CallOption) (*migrationpb.TranslateQueryResponse, error) {
   263  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
   264  
   265  	hds = append(c.xGoogHeaders, hds...)
   266  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   267  	opts = append((*c.CallOptions).TranslateQuery[0:len((*c.CallOptions).TranslateQuery):len((*c.CallOptions).TranslateQuery)], opts...)
   268  	var resp *migrationpb.TranslateQueryResponse
   269  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   270  		var err error
   271  		resp, err = c.sqlTranslationClient.TranslateQuery(ctx, req, settings.GRPC...)
   272  		return err
   273  	}, opts...)
   274  	if err != nil {
   275  		return nil, err
   276  	}
   277  	return resp, nil
   278  }
   279  
   280  // TranslateQuery translates input queries from source dialects to GoogleSQL.
   281  func (c *sqlTranslationRESTClient) TranslateQuery(ctx context.Context, req *migrationpb.TranslateQueryRequest, opts ...gax.CallOption) (*migrationpb.TranslateQueryResponse, error) {
   282  	m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
   283  	jsonReq, err := m.Marshal(req)
   284  	if err != nil {
   285  		return nil, err
   286  	}
   287  
   288  	baseUrl, err := url.Parse(c.endpoint)
   289  	if err != nil {
   290  		return nil, err
   291  	}
   292  	baseUrl.Path += fmt.Sprintf("/v2alpha/%v:translateQuery", req.GetParent())
   293  
   294  	// Build HTTP headers from client and context metadata.
   295  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
   296  
   297  	hds = append(c.xGoogHeaders, hds...)
   298  	hds = append(hds, "Content-Type", "application/json")
   299  	headers := gax.BuildHeaders(ctx, hds...)
   300  	opts = append((*c.CallOptions).TranslateQuery[0:len((*c.CallOptions).TranslateQuery):len((*c.CallOptions).TranslateQuery)], opts...)
   301  	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
   302  	resp := &migrationpb.TranslateQueryResponse{}
   303  	e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   304  		if settings.Path != "" {
   305  			baseUrl.Path = settings.Path
   306  		}
   307  		httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
   308  		if err != nil {
   309  			return err
   310  		}
   311  		httpReq = httpReq.WithContext(ctx)
   312  		httpReq.Header = headers
   313  
   314  		httpRsp, err := c.httpClient.Do(httpReq)
   315  		if err != nil {
   316  			return err
   317  		}
   318  		defer httpRsp.Body.Close()
   319  
   320  		if err = googleapi.CheckResponse(httpRsp); err != nil {
   321  			return err
   322  		}
   323  
   324  		buf, err := io.ReadAll(httpRsp.Body)
   325  		if err != nil {
   326  			return err
   327  		}
   328  
   329  		if err := unm.Unmarshal(buf, resp); err != nil {
   330  			return err
   331  		}
   332  
   333  		return nil
   334  	}, opts...)
   335  	if e != nil {
   336  		return nil, e
   337  	}
   338  	return resp, nil
   339  }
   340  

View as plain text