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 defaultClientOptions() []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		option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
53		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
54			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
55	}
56}
57
58func defaultCallOptions() *CallOptions {
59	return &CallOptions{
60		AnalyzeSentiment: []gax.CallOption{
61			gax.WithRetry(func() gax.Retryer {
62				return gax.OnCodes([]codes.Code{
63					codes.DeadlineExceeded,
64					codes.Unavailable,
65				}, gax.Backoff{
66					Initial:    100 * time.Millisecond,
67					Max:        60000 * time.Millisecond,
68					Multiplier: 1.30,
69				})
70			}),
71		},
72		AnalyzeEntities: []gax.CallOption{
73			gax.WithRetry(func() gax.Retryer {
74				return gax.OnCodes([]codes.Code{
75					codes.DeadlineExceeded,
76					codes.Unavailable,
77				}, gax.Backoff{
78					Initial:    100 * time.Millisecond,
79					Max:        60000 * time.Millisecond,
80					Multiplier: 1.30,
81				})
82			}),
83		},
84		AnalyzeEntitySentiment: []gax.CallOption{
85			gax.WithRetry(func() gax.Retryer {
86				return gax.OnCodes([]codes.Code{
87					codes.DeadlineExceeded,
88					codes.Unavailable,
89				}, gax.Backoff{
90					Initial:    100 * time.Millisecond,
91					Max:        60000 * time.Millisecond,
92					Multiplier: 1.30,
93				})
94			}),
95		},
96		AnalyzeSyntax: []gax.CallOption{
97			gax.WithRetry(func() gax.Retryer {
98				return gax.OnCodes([]codes.Code{
99					codes.DeadlineExceeded,
100					codes.Unavailable,
101				}, gax.Backoff{
102					Initial:    100 * time.Millisecond,
103					Max:        60000 * time.Millisecond,
104					Multiplier: 1.30,
105				})
106			}),
107		},
108		ClassifyText: []gax.CallOption{
109			gax.WithRetry(func() gax.Retryer {
110				return gax.OnCodes([]codes.Code{
111					codes.DeadlineExceeded,
112					codes.Unavailable,
113				}, gax.Backoff{
114					Initial:    100 * time.Millisecond,
115					Max:        60000 * time.Millisecond,
116					Multiplier: 1.30,
117				})
118			}),
119		},
120		AnnotateText: []gax.CallOption{
121			gax.WithRetry(func() gax.Retryer {
122				return gax.OnCodes([]codes.Code{
123					codes.DeadlineExceeded,
124					codes.Unavailable,
125				}, gax.Backoff{
126					Initial:    100 * time.Millisecond,
127					Max:        60000 * time.Millisecond,
128					Multiplier: 1.30,
129				})
130			}),
131		},
132	}
133}
134
135// Client is a client for interacting with Cloud Natural Language API.
136//
137// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
138type Client struct {
139	// Connection pool of gRPC connections to the service.
140	connPool gtransport.ConnPool
141
142	// flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE
143	disableDeadlines bool
144
145	// The gRPC API client.
146	client languagepb.LanguageServiceClient
147
148	// The call options for this service.
149	CallOptions *CallOptions
150
151	// The x-goog-* metadata to be sent with each request.
152	xGoogMetadata metadata.MD
153}
154
155// NewClient creates a new language service client.
156//
157// Provides text analysis operations such as sentiment analysis and entity
158// recognition.
159func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
160	clientOpts := defaultClientOptions()
161
162	if newClientHook != nil {
163		hookOpts, err := newClientHook(ctx, clientHookParams{})
164		if err != nil {
165			return nil, err
166		}
167		clientOpts = append(clientOpts, hookOpts...)
168	}
169
170	disableDeadlines, err := checkDisableDeadlines()
171	if err != nil {
172		return nil, err
173	}
174
175	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
176	if err != nil {
177		return nil, err
178	}
179	c := &Client{
180		connPool:         connPool,
181		disableDeadlines: disableDeadlines,
182		CallOptions:      defaultCallOptions(),
183
184		client: languagepb.NewLanguageServiceClient(connPool),
185	}
186	c.setGoogleClientInfo()
187
188	return c, nil
189}
190
191// Connection returns a connection to the API service.
192//
193// Deprecated.
194func (c *Client) Connection() *grpc.ClientConn {
195	return c.connPool.Conn()
196}
197
198// Close closes the connection to the API service. The user should invoke this when
199// the client is no longer required.
200func (c *Client) Close() error {
201	return c.connPool.Close()
202}
203
204// setGoogleClientInfo sets the name and version of the application in
205// the `x-goog-api-client` header passed on each request. Intended for
206// use by Google-written clients.
207func (c *Client) setGoogleClientInfo(keyval ...string) {
208	kv := append([]string{"gl-go", versionGo()}, keyval...)
209	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
210	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
211}
212
213// AnalyzeSentiment analyzes the sentiment of the provided text.
214func (c *Client) AnalyzeSentiment(ctx context.Context, req *languagepb.AnalyzeSentimentRequest, opts ...gax.CallOption) (*languagepb.AnalyzeSentimentResponse, error) {
215	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
216		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
217		defer cancel()
218		ctx = cctx
219	}
220	ctx = insertMetadata(ctx, c.xGoogMetadata)
221	opts = append(c.CallOptions.AnalyzeSentiment[0:len(c.CallOptions.AnalyzeSentiment):len(c.CallOptions.AnalyzeSentiment)], opts...)
222	var resp *languagepb.AnalyzeSentimentResponse
223	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
224		var err error
225		resp, err = c.client.AnalyzeSentiment(ctx, req, settings.GRPC...)
226		return err
227	}, opts...)
228	if err != nil {
229		return nil, err
230	}
231	return resp, nil
232}
233
234// AnalyzeEntities finds named entities (currently proper names and common nouns) in the text
235// along with entity types, salience, mentions for each entity, and
236// other properties.
237func (c *Client) AnalyzeEntities(ctx context.Context, req *languagepb.AnalyzeEntitiesRequest, opts ...gax.CallOption) (*languagepb.AnalyzeEntitiesResponse, error) {
238	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
239		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
240		defer cancel()
241		ctx = cctx
242	}
243	ctx = insertMetadata(ctx, c.xGoogMetadata)
244	opts = append(c.CallOptions.AnalyzeEntities[0:len(c.CallOptions.AnalyzeEntities):len(c.CallOptions.AnalyzeEntities)], opts...)
245	var resp *languagepb.AnalyzeEntitiesResponse
246	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
247		var err error
248		resp, err = c.client.AnalyzeEntities(ctx, req, settings.GRPC...)
249		return err
250	}, opts...)
251	if err != nil {
252		return nil, err
253	}
254	return resp, nil
255}
256
257// AnalyzeEntitySentiment finds entities, similar to AnalyzeEntities in the text and analyzes
258// sentiment associated with each entity and its mentions.
259func (c *Client) AnalyzeEntitySentiment(ctx context.Context, req *languagepb.AnalyzeEntitySentimentRequest, opts ...gax.CallOption) (*languagepb.AnalyzeEntitySentimentResponse, error) {
260	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
261		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
262		defer cancel()
263		ctx = cctx
264	}
265	ctx = insertMetadata(ctx, c.xGoogMetadata)
266	opts = append(c.CallOptions.AnalyzeEntitySentiment[0:len(c.CallOptions.AnalyzeEntitySentiment):len(c.CallOptions.AnalyzeEntitySentiment)], opts...)
267	var resp *languagepb.AnalyzeEntitySentimentResponse
268	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
269		var err error
270		resp, err = c.client.AnalyzeEntitySentiment(ctx, req, settings.GRPC...)
271		return err
272	}, opts...)
273	if err != nil {
274		return nil, err
275	}
276	return resp, nil
277}
278
279// AnalyzeSyntax analyzes the syntax of the text and provides sentence boundaries and
280// tokenization along with part of speech tags, dependency trees, and other
281// properties.
282func (c *Client) AnalyzeSyntax(ctx context.Context, req *languagepb.AnalyzeSyntaxRequest, opts ...gax.CallOption) (*languagepb.AnalyzeSyntaxResponse, error) {
283	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
284		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
285		defer cancel()
286		ctx = cctx
287	}
288	ctx = insertMetadata(ctx, c.xGoogMetadata)
289	opts = append(c.CallOptions.AnalyzeSyntax[0:len(c.CallOptions.AnalyzeSyntax):len(c.CallOptions.AnalyzeSyntax)], opts...)
290	var resp *languagepb.AnalyzeSyntaxResponse
291	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
292		var err error
293		resp, err = c.client.AnalyzeSyntax(ctx, req, settings.GRPC...)
294		return err
295	}, opts...)
296	if err != nil {
297		return nil, err
298	}
299	return resp, nil
300}
301
302// ClassifyText classifies a document into categories.
303func (c *Client) ClassifyText(ctx context.Context, req *languagepb.ClassifyTextRequest, opts ...gax.CallOption) (*languagepb.ClassifyTextResponse, error) {
304	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
305		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
306		defer cancel()
307		ctx = cctx
308	}
309	ctx = insertMetadata(ctx, c.xGoogMetadata)
310	opts = append(c.CallOptions.ClassifyText[0:len(c.CallOptions.ClassifyText):len(c.CallOptions.ClassifyText)], opts...)
311	var resp *languagepb.ClassifyTextResponse
312	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
313		var err error
314		resp, err = c.client.ClassifyText(ctx, req, settings.GRPC...)
315		return err
316	}, opts...)
317	if err != nil {
318		return nil, err
319	}
320	return resp, nil
321}
322
323// AnnotateText a convenience method that provides all the features that analyzeSentiment,
324// analyzeEntities, and analyzeSyntax provide in one call.
325func (c *Client) AnnotateText(ctx context.Context, req *languagepb.AnnotateTextRequest, opts ...gax.CallOption) (*languagepb.AnnotateTextResponse, error) {
326	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
327		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
328		defer cancel()
329		ctx = cctx
330	}
331	ctx = insertMetadata(ctx, c.xGoogMetadata)
332	opts = append(c.CallOptions.AnnotateText[0:len(c.CallOptions.AnnotateText):len(c.CallOptions.AnnotateText)], opts...)
333	var resp *languagepb.AnnotateTextResponse
334	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
335		var err error
336		resp, err = c.client.AnnotateText(ctx, req, settings.GRPC...)
337		return err
338	}, opts...)
339	if err != nil {
340		return nil, err
341	}
342	return resp, nil
343}
344