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 language
18
19import (
20	"context"
21	"math"
22	"time"
23
24	gax "github.com/googleapis/gax-go/v2"
25	"google.golang.org/api/option"
26	"google.golang.org/api/option/internaloption"
27	gtransport "google.golang.org/api/transport/grpc"
28	languagepb "google.golang.org/genproto/googleapis/cloud/language/v1"
29	"google.golang.org/grpc"
30	"google.golang.org/grpc/codes"
31	"google.golang.org/grpc/metadata"
32)
33
34var newClientHook clientHook
35
36// CallOptions contains the retry settings for each method of Client.
37type CallOptions struct {
38	AnalyzeSentiment       []gax.CallOption
39	AnalyzeEntities        []gax.CallOption
40	AnalyzeEntitySentiment []gax.CallOption
41	AnalyzeSyntax          []gax.CallOption
42	ClassifyText           []gax.CallOption
43	AnnotateText           []gax.CallOption
44}
45
46func defaultGRPCClientOptions() []option.ClientOption {
47	return []option.ClientOption{
48		internaloption.WithDefaultEndpoint("language.googleapis.com:443"),
49		internaloption.WithDefaultMTLSEndpoint("language.mtls.googleapis.com:443"),
50		internaloption.WithDefaultAudience("https://language.googleapis.com/"),
51		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
52		internaloption.EnableJwtWithScope(),
53		option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
54		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
55			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
56	}
57}
58
59func defaultCallOptions() *CallOptions {
60	return &CallOptions{
61		AnalyzeSentiment: []gax.CallOption{
62			gax.WithRetry(func() gax.Retryer {
63				return gax.OnCodes([]codes.Code{
64					codes.DeadlineExceeded,
65					codes.Unavailable,
66				}, gax.Backoff{
67					Initial:    100 * time.Millisecond,
68					Max:        60000 * time.Millisecond,
69					Multiplier: 1.30,
70				})
71			}),
72		},
73		AnalyzeEntities: []gax.CallOption{
74			gax.WithRetry(func() gax.Retryer {
75				return gax.OnCodes([]codes.Code{
76					codes.DeadlineExceeded,
77					codes.Unavailable,
78				}, gax.Backoff{
79					Initial:    100 * time.Millisecond,
80					Max:        60000 * time.Millisecond,
81					Multiplier: 1.30,
82				})
83			}),
84		},
85		AnalyzeEntitySentiment: []gax.CallOption{
86			gax.WithRetry(func() gax.Retryer {
87				return gax.OnCodes([]codes.Code{
88					codes.DeadlineExceeded,
89					codes.Unavailable,
90				}, gax.Backoff{
91					Initial:    100 * time.Millisecond,
92					Max:        60000 * time.Millisecond,
93					Multiplier: 1.30,
94				})
95			}),
96		},
97		AnalyzeSyntax: []gax.CallOption{
98			gax.WithRetry(func() gax.Retryer {
99				return gax.OnCodes([]codes.Code{
100					codes.DeadlineExceeded,
101					codes.Unavailable,
102				}, gax.Backoff{
103					Initial:    100 * time.Millisecond,
104					Max:        60000 * time.Millisecond,
105					Multiplier: 1.30,
106				})
107			}),
108		},
109		ClassifyText: []gax.CallOption{
110			gax.WithRetry(func() gax.Retryer {
111				return gax.OnCodes([]codes.Code{
112					codes.DeadlineExceeded,
113					codes.Unavailable,
114				}, gax.Backoff{
115					Initial:    100 * time.Millisecond,
116					Max:        60000 * time.Millisecond,
117					Multiplier: 1.30,
118				})
119			}),
120		},
121		AnnotateText: []gax.CallOption{
122			gax.WithRetry(func() gax.Retryer {
123				return gax.OnCodes([]codes.Code{
124					codes.DeadlineExceeded,
125					codes.Unavailable,
126				}, gax.Backoff{
127					Initial:    100 * time.Millisecond,
128					Max:        60000 * time.Millisecond,
129					Multiplier: 1.30,
130				})
131			}),
132		},
133	}
134}
135
136// internalClient is an interface that defines the methods availaible from Cloud Natural Language API.
137type internalClient interface {
138	Close() error
139	setGoogleClientInfo(...string)
140	Connection() *grpc.ClientConn
141	AnalyzeSentiment(context.Context, *languagepb.AnalyzeSentimentRequest, ...gax.CallOption) (*languagepb.AnalyzeSentimentResponse, error)
142	AnalyzeEntities(context.Context, *languagepb.AnalyzeEntitiesRequest, ...gax.CallOption) (*languagepb.AnalyzeEntitiesResponse, error)
143	AnalyzeEntitySentiment(context.Context, *languagepb.AnalyzeEntitySentimentRequest, ...gax.CallOption) (*languagepb.AnalyzeEntitySentimentResponse, error)
144	AnalyzeSyntax(context.Context, *languagepb.AnalyzeSyntaxRequest, ...gax.CallOption) (*languagepb.AnalyzeSyntaxResponse, error)
145	ClassifyText(context.Context, *languagepb.ClassifyTextRequest, ...gax.CallOption) (*languagepb.ClassifyTextResponse, error)
146	AnnotateText(context.Context, *languagepb.AnnotateTextRequest, ...gax.CallOption) (*languagepb.AnnotateTextResponse, error)
147}
148
149// Client is a client for interacting with Cloud Natural Language API.
150// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
151//
152// Provides text analysis operations such as sentiment analysis and entity
153// recognition.
154type Client struct {
155	// The internal transport-dependent client.
156	internalClient internalClient
157
158	// The call options for this service.
159	CallOptions *CallOptions
160}
161
162// Wrapper methods routed to the internal client.
163
164// Close closes the connection to the API service. The user should invoke this when
165// the client is no longer required.
166func (c *Client) Close() error {
167	return c.internalClient.Close()
168}
169
170// setGoogleClientInfo sets the name and version of the application in
171// the `x-goog-api-client` header passed on each request. Intended for
172// use by Google-written clients.
173func (c *Client) setGoogleClientInfo(keyval ...string) {
174	c.internalClient.setGoogleClientInfo(keyval...)
175}
176
177// Connection returns a connection to the API service.
178//
179// Deprecated.
180func (c *Client) Connection() *grpc.ClientConn {
181	return c.internalClient.Connection()
182}
183
184// AnalyzeSentiment analyzes the sentiment of the provided text.
185func (c *Client) AnalyzeSentiment(ctx context.Context, req *languagepb.AnalyzeSentimentRequest, opts ...gax.CallOption) (*languagepb.AnalyzeSentimentResponse, error) {
186	return c.internalClient.AnalyzeSentiment(ctx, req, opts...)
187}
188
189// AnalyzeEntities finds named entities (currently proper names and common nouns) in the text
190// along with entity types, salience, mentions for each entity, and
191// other properties.
192func (c *Client) AnalyzeEntities(ctx context.Context, req *languagepb.AnalyzeEntitiesRequest, opts ...gax.CallOption) (*languagepb.AnalyzeEntitiesResponse, error) {
193	return c.internalClient.AnalyzeEntities(ctx, req, opts...)
194}
195
196// AnalyzeEntitySentiment finds entities, similar to AnalyzeEntities in the text and analyzes
197// sentiment associated with each entity and its mentions.
198func (c *Client) AnalyzeEntitySentiment(ctx context.Context, req *languagepb.AnalyzeEntitySentimentRequest, opts ...gax.CallOption) (*languagepb.AnalyzeEntitySentimentResponse, error) {
199	return c.internalClient.AnalyzeEntitySentiment(ctx, req, opts...)
200}
201
202// AnalyzeSyntax analyzes the syntax of the text and provides sentence boundaries and
203// tokenization along with part of speech tags, dependency trees, and other
204// properties.
205func (c *Client) AnalyzeSyntax(ctx context.Context, req *languagepb.AnalyzeSyntaxRequest, opts ...gax.CallOption) (*languagepb.AnalyzeSyntaxResponse, error) {
206	return c.internalClient.AnalyzeSyntax(ctx, req, opts...)
207}
208
209// ClassifyText classifies a document into categories.
210func (c *Client) ClassifyText(ctx context.Context, req *languagepb.ClassifyTextRequest, opts ...gax.CallOption) (*languagepb.ClassifyTextResponse, error) {
211	return c.internalClient.ClassifyText(ctx, req, opts...)
212}
213
214// AnnotateText a convenience method that provides all the features that analyzeSentiment,
215// analyzeEntities, and analyzeSyntax provide in one call.
216func (c *Client) AnnotateText(ctx context.Context, req *languagepb.AnnotateTextRequest, opts ...gax.CallOption) (*languagepb.AnnotateTextResponse, error) {
217	return c.internalClient.AnnotateText(ctx, req, opts...)
218}
219
220// gRPCClient is a client for interacting with Cloud Natural Language API over gRPC transport.
221//
222// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
223type gRPCClient struct {
224	// Connection pool of gRPC connections to the service.
225	connPool gtransport.ConnPool
226
227	// flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE
228	disableDeadlines bool
229
230	// Points back to the CallOptions field of the containing Client
231	CallOptions **CallOptions
232
233	// The gRPC API client.
234	client languagepb.LanguageServiceClient
235
236	// The x-goog-* metadata to be sent with each request.
237	xGoogMetadata metadata.MD
238}
239
240// NewClient creates a new language service client based on gRPC.
241// The returned client must be Closed when it is done being used to clean up its underlying connections.
242//
243// Provides text analysis operations such as sentiment analysis and entity
244// recognition.
245func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
246	clientOpts := defaultGRPCClientOptions()
247	if newClientHook != nil {
248		hookOpts, err := newClientHook(ctx, clientHookParams{})
249		if err != nil {
250			return nil, err
251		}
252		clientOpts = append(clientOpts, hookOpts...)
253	}
254
255	disableDeadlines, err := checkDisableDeadlines()
256	if err != nil {
257		return nil, err
258	}
259
260	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
261	if err != nil {
262		return nil, err
263	}
264	client := Client{CallOptions: defaultCallOptions()}
265
266	c := &gRPCClient{
267		connPool:         connPool,
268		disableDeadlines: disableDeadlines,
269		client:           languagepb.NewLanguageServiceClient(connPool),
270		CallOptions:      &client.CallOptions,
271	}
272	c.setGoogleClientInfo()
273
274	client.internalClient = c
275
276	return &client, nil
277}
278
279// Connection returns a connection to the API service.
280//
281// Deprecated.
282func (c *gRPCClient) Connection() *grpc.ClientConn {
283	return c.connPool.Conn()
284}
285
286// setGoogleClientInfo sets the name and version of the application in
287// the `x-goog-api-client` header passed on each request. Intended for
288// use by Google-written clients.
289func (c *gRPCClient) setGoogleClientInfo(keyval ...string) {
290	kv := append([]string{"gl-go", versionGo()}, keyval...)
291	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
292	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
293}
294
295// Close closes the connection to the API service. The user should invoke this when
296// the client is no longer required.
297func (c *gRPCClient) Close() error {
298	return c.connPool.Close()
299}
300
301func (c *gRPCClient) AnalyzeSentiment(ctx context.Context, req *languagepb.AnalyzeSentimentRequest, opts ...gax.CallOption) (*languagepb.AnalyzeSentimentResponse, error) {
302	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
303		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
304		defer cancel()
305		ctx = cctx
306	}
307	ctx = insertMetadata(ctx, c.xGoogMetadata)
308	opts = append((*c.CallOptions).AnalyzeSentiment[0:len((*c.CallOptions).AnalyzeSentiment):len((*c.CallOptions).AnalyzeSentiment)], opts...)
309	var resp *languagepb.AnalyzeSentimentResponse
310	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
311		var err error
312		resp, err = c.client.AnalyzeSentiment(ctx, req, settings.GRPC...)
313		return err
314	}, opts...)
315	if err != nil {
316		return nil, err
317	}
318	return resp, nil
319}
320
321func (c *gRPCClient) AnalyzeEntities(ctx context.Context, req *languagepb.AnalyzeEntitiesRequest, opts ...gax.CallOption) (*languagepb.AnalyzeEntitiesResponse, error) {
322	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
323		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
324		defer cancel()
325		ctx = cctx
326	}
327	ctx = insertMetadata(ctx, c.xGoogMetadata)
328	opts = append((*c.CallOptions).AnalyzeEntities[0:len((*c.CallOptions).AnalyzeEntities):len((*c.CallOptions).AnalyzeEntities)], opts...)
329	var resp *languagepb.AnalyzeEntitiesResponse
330	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
331		var err error
332		resp, err = c.client.AnalyzeEntities(ctx, req, settings.GRPC...)
333		return err
334	}, opts...)
335	if err != nil {
336		return nil, err
337	}
338	return resp, nil
339}
340
341func (c *gRPCClient) AnalyzeEntitySentiment(ctx context.Context, req *languagepb.AnalyzeEntitySentimentRequest, opts ...gax.CallOption) (*languagepb.AnalyzeEntitySentimentResponse, error) {
342	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
343		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
344		defer cancel()
345		ctx = cctx
346	}
347	ctx = insertMetadata(ctx, c.xGoogMetadata)
348	opts = append((*c.CallOptions).AnalyzeEntitySentiment[0:len((*c.CallOptions).AnalyzeEntitySentiment):len((*c.CallOptions).AnalyzeEntitySentiment)], opts...)
349	var resp *languagepb.AnalyzeEntitySentimentResponse
350	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
351		var err error
352		resp, err = c.client.AnalyzeEntitySentiment(ctx, req, settings.GRPC...)
353		return err
354	}, opts...)
355	if err != nil {
356		return nil, err
357	}
358	return resp, nil
359}
360
361func (c *gRPCClient) AnalyzeSyntax(ctx context.Context, req *languagepb.AnalyzeSyntaxRequest, opts ...gax.CallOption) (*languagepb.AnalyzeSyntaxResponse, error) {
362	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
363		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
364		defer cancel()
365		ctx = cctx
366	}
367	ctx = insertMetadata(ctx, c.xGoogMetadata)
368	opts = append((*c.CallOptions).AnalyzeSyntax[0:len((*c.CallOptions).AnalyzeSyntax):len((*c.CallOptions).AnalyzeSyntax)], opts...)
369	var resp *languagepb.AnalyzeSyntaxResponse
370	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
371		var err error
372		resp, err = c.client.AnalyzeSyntax(ctx, req, settings.GRPC...)
373		return err
374	}, opts...)
375	if err != nil {
376		return nil, err
377	}
378	return resp, nil
379}
380
381func (c *gRPCClient) ClassifyText(ctx context.Context, req *languagepb.ClassifyTextRequest, opts ...gax.CallOption) (*languagepb.ClassifyTextResponse, error) {
382	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
383		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
384		defer cancel()
385		ctx = cctx
386	}
387	ctx = insertMetadata(ctx, c.xGoogMetadata)
388	opts = append((*c.CallOptions).ClassifyText[0:len((*c.CallOptions).ClassifyText):len((*c.CallOptions).ClassifyText)], opts...)
389	var resp *languagepb.ClassifyTextResponse
390	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
391		var err error
392		resp, err = c.client.ClassifyText(ctx, req, settings.GRPC...)
393		return err
394	}, opts...)
395	if err != nil {
396		return nil, err
397	}
398	return resp, nil
399}
400
401func (c *gRPCClient) AnnotateText(ctx context.Context, req *languagepb.AnnotateTextRequest, opts ...gax.CallOption) (*languagepb.AnnotateTextResponse, error) {
402	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
403		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
404		defer cancel()
405		ctx = cctx
406	}
407	ctx = insertMetadata(ctx, c.xGoogMetadata)
408	opts = append((*c.CallOptions).AnnotateText[0:len((*c.CallOptions).AnnotateText):len((*c.CallOptions).AnnotateText)], opts...)
409	var resp *languagepb.AnnotateTextResponse
410	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
411		var err error
412		resp, err = c.client.AnnotateText(ctx, req, settings.GRPC...)
413		return err
414	}, opts...)
415	if err != nil {
416		return nil, err
417	}
418	return resp, nil
419}
420