1// Copyright 2020 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 errorreporting
18
19import (
20	"context"
21	"fmt"
22	"math"
23	"net/url"
24	"time"
25
26	"github.com/golang/protobuf/proto"
27	gax "github.com/googleapis/gax-go/v2"
28	"google.golang.org/api/iterator"
29	"google.golang.org/api/option"
30	gtransport "google.golang.org/api/transport/grpc"
31	clouderrorreportingpb "google.golang.org/genproto/googleapis/devtools/clouderrorreporting/v1beta1"
32	"google.golang.org/grpc"
33	"google.golang.org/grpc/codes"
34	"google.golang.org/grpc/metadata"
35)
36
37var newErrorStatsClientHook clientHook
38
39// ErrorStatsCallOptions contains the retry settings for each method of ErrorStatsClient.
40type ErrorStatsCallOptions struct {
41	ListGroupStats []gax.CallOption
42	ListEvents     []gax.CallOption
43	DeleteEvents   []gax.CallOption
44}
45
46func defaultErrorStatsClientOptions() []option.ClientOption {
47	return []option.ClientOption{
48		option.WithEndpoint("clouderrorreporting.googleapis.com:443"),
49		option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
50		option.WithScopes(DefaultAuthScopes()...),
51		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
52			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
53	}
54}
55
56func defaultErrorStatsCallOptions() *ErrorStatsCallOptions {
57	return &ErrorStatsCallOptions{
58		ListGroupStats: []gax.CallOption{
59			gax.WithRetry(func() gax.Retryer {
60				return gax.OnCodes([]codes.Code{
61					codes.Unavailable,
62					codes.DeadlineExceeded,
63				}, gax.Backoff{
64					Initial:    100 * time.Millisecond,
65					Max:        60000 * time.Millisecond,
66					Multiplier: 1.30,
67				})
68			}),
69		},
70		ListEvents: []gax.CallOption{
71			gax.WithRetry(func() gax.Retryer {
72				return gax.OnCodes([]codes.Code{
73					codes.Unavailable,
74					codes.DeadlineExceeded,
75				}, gax.Backoff{
76					Initial:    100 * time.Millisecond,
77					Max:        60000 * time.Millisecond,
78					Multiplier: 1.30,
79				})
80			}),
81		},
82		DeleteEvents: []gax.CallOption{
83			gax.WithRetry(func() gax.Retryer {
84				return gax.OnCodes([]codes.Code{
85					codes.Unavailable,
86					codes.DeadlineExceeded,
87				}, gax.Backoff{
88					Initial:    100 * time.Millisecond,
89					Max:        60000 * time.Millisecond,
90					Multiplier: 1.30,
91				})
92			}),
93		},
94	}
95}
96
97// ErrorStatsClient is a client for interacting with Stackdriver Error Reporting API.
98//
99// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
100type ErrorStatsClient struct {
101	// Connection pool of gRPC connections to the service.
102	connPool gtransport.ConnPool
103
104	// The gRPC API client.
105	errorStatsClient clouderrorreportingpb.ErrorStatsServiceClient
106
107	// The call options for this service.
108	CallOptions *ErrorStatsCallOptions
109
110	// The x-goog-* metadata to be sent with each request.
111	xGoogMetadata metadata.MD
112}
113
114// NewErrorStatsClient creates a new error stats service client.
115//
116// An API for retrieving and managing error statistics as well as data for
117// individual events.
118func NewErrorStatsClient(ctx context.Context, opts ...option.ClientOption) (*ErrorStatsClient, error) {
119	clientOpts := defaultErrorStatsClientOptions()
120
121	if newErrorStatsClientHook != nil {
122		hookOpts, err := newErrorStatsClientHook(ctx, clientHookParams{})
123		if err != nil {
124			return nil, err
125		}
126		clientOpts = append(clientOpts, hookOpts...)
127	}
128
129	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
130	if err != nil {
131		return nil, err
132	}
133	c := &ErrorStatsClient{
134		connPool:    connPool,
135		CallOptions: defaultErrorStatsCallOptions(),
136
137		errorStatsClient: clouderrorreportingpb.NewErrorStatsServiceClient(connPool),
138	}
139	c.setGoogleClientInfo()
140
141	return c, nil
142}
143
144// Connection returns a connection to the API service.
145//
146// Deprecated.
147func (c *ErrorStatsClient) Connection() *grpc.ClientConn {
148	return c.connPool.Conn()
149}
150
151// Close closes the connection to the API service. The user should invoke this when
152// the client is no longer required.
153func (c *ErrorStatsClient) Close() error {
154	return c.connPool.Close()
155}
156
157// setGoogleClientInfo sets the name and version of the application in
158// the `x-goog-api-client` header passed on each request. Intended for
159// use by Google-written clients.
160func (c *ErrorStatsClient) setGoogleClientInfo(keyval ...string) {
161	kv := append([]string{"gl-go", versionGo()}, keyval...)
162	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
163	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
164}
165
166// ListGroupStats lists the specified groups.
167func (c *ErrorStatsClient) ListGroupStats(ctx context.Context, req *clouderrorreportingpb.ListGroupStatsRequest, opts ...gax.CallOption) *ErrorGroupStatsIterator {
168	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "project_name", url.QueryEscape(req.GetProjectName())))
169	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
170	opts = append(c.CallOptions.ListGroupStats[0:len(c.CallOptions.ListGroupStats):len(c.CallOptions.ListGroupStats)], opts...)
171	it := &ErrorGroupStatsIterator{}
172	req = proto.Clone(req).(*clouderrorreportingpb.ListGroupStatsRequest)
173	it.InternalFetch = func(pageSize int, pageToken string) ([]*clouderrorreportingpb.ErrorGroupStats, string, error) {
174		var resp *clouderrorreportingpb.ListGroupStatsResponse
175		req.PageToken = pageToken
176		if pageSize > math.MaxInt32 {
177			req.PageSize = math.MaxInt32
178		} else {
179			req.PageSize = int32(pageSize)
180		}
181		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
182			var err error
183			resp, err = c.errorStatsClient.ListGroupStats(ctx, req, settings.GRPC...)
184			return err
185		}, opts...)
186		if err != nil {
187			return nil, "", err
188		}
189
190		it.Response = resp
191		return resp.ErrorGroupStats, resp.NextPageToken, nil
192	}
193	fetch := func(pageSize int, pageToken string) (string, error) {
194		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
195		if err != nil {
196			return "", err
197		}
198		it.items = append(it.items, items...)
199		return nextPageToken, nil
200	}
201	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
202	it.pageInfo.MaxSize = int(req.PageSize)
203	it.pageInfo.Token = req.PageToken
204	return it
205}
206
207// ListEvents lists the specified events.
208func (c *ErrorStatsClient) ListEvents(ctx context.Context, req *clouderrorreportingpb.ListEventsRequest, opts ...gax.CallOption) *ErrorEventIterator {
209	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "project_name", url.QueryEscape(req.GetProjectName())))
210	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
211	opts = append(c.CallOptions.ListEvents[0:len(c.CallOptions.ListEvents):len(c.CallOptions.ListEvents)], opts...)
212	it := &ErrorEventIterator{}
213	req = proto.Clone(req).(*clouderrorreportingpb.ListEventsRequest)
214	it.InternalFetch = func(pageSize int, pageToken string) ([]*clouderrorreportingpb.ErrorEvent, string, error) {
215		var resp *clouderrorreportingpb.ListEventsResponse
216		req.PageToken = pageToken
217		if pageSize > math.MaxInt32 {
218			req.PageSize = math.MaxInt32
219		} else {
220			req.PageSize = int32(pageSize)
221		}
222		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
223			var err error
224			resp, err = c.errorStatsClient.ListEvents(ctx, req, settings.GRPC...)
225			return err
226		}, opts...)
227		if err != nil {
228			return nil, "", err
229		}
230
231		it.Response = resp
232		return resp.ErrorEvents, resp.NextPageToken, nil
233	}
234	fetch := func(pageSize int, pageToken string) (string, error) {
235		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
236		if err != nil {
237			return "", err
238		}
239		it.items = append(it.items, items...)
240		return nextPageToken, nil
241	}
242	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
243	it.pageInfo.MaxSize = int(req.PageSize)
244	it.pageInfo.Token = req.PageToken
245	return it
246}
247
248// DeleteEvents deletes all error events of a given project.
249func (c *ErrorStatsClient) DeleteEvents(ctx context.Context, req *clouderrorreportingpb.DeleteEventsRequest, opts ...gax.CallOption) (*clouderrorreportingpb.DeleteEventsResponse, error) {
250	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "project_name", url.QueryEscape(req.GetProjectName())))
251	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
252	opts = append(c.CallOptions.DeleteEvents[0:len(c.CallOptions.DeleteEvents):len(c.CallOptions.DeleteEvents)], opts...)
253	var resp *clouderrorreportingpb.DeleteEventsResponse
254	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
255		var err error
256		resp, err = c.errorStatsClient.DeleteEvents(ctx, req, settings.GRPC...)
257		return err
258	}, opts...)
259	if err != nil {
260		return nil, err
261	}
262	return resp, nil
263}
264
265// ErrorEventIterator manages a stream of *clouderrorreportingpb.ErrorEvent.
266type ErrorEventIterator struct {
267	items    []*clouderrorreportingpb.ErrorEvent
268	pageInfo *iterator.PageInfo
269	nextFunc func() error
270
271	// Response is the raw response for the current page.
272	// It must be cast to the RPC response type.
273	// Calling Next() or InternalFetch() updates this value.
274	Response interface{}
275
276	// InternalFetch is for use by the Google Cloud Libraries only.
277	// It is not part of the stable interface of this package.
278	//
279	// InternalFetch returns results from a single call to the underlying RPC.
280	// The number of results is no greater than pageSize.
281	// If there are no more results, nextPageToken is empty and err is nil.
282	InternalFetch func(pageSize int, pageToken string) (results []*clouderrorreportingpb.ErrorEvent, nextPageToken string, err error)
283}
284
285// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
286func (it *ErrorEventIterator) PageInfo() *iterator.PageInfo {
287	return it.pageInfo
288}
289
290// Next returns the next result. Its second return value is iterator.Done if there are no more
291// results. Once Next returns Done, all subsequent calls will return Done.
292func (it *ErrorEventIterator) Next() (*clouderrorreportingpb.ErrorEvent, error) {
293	var item *clouderrorreportingpb.ErrorEvent
294	if err := it.nextFunc(); err != nil {
295		return item, err
296	}
297	item = it.items[0]
298	it.items = it.items[1:]
299	return item, nil
300}
301
302func (it *ErrorEventIterator) bufLen() int {
303	return len(it.items)
304}
305
306func (it *ErrorEventIterator) takeBuf() interface{} {
307	b := it.items
308	it.items = nil
309	return b
310}
311
312// ErrorGroupStatsIterator manages a stream of *clouderrorreportingpb.ErrorGroupStats.
313type ErrorGroupStatsIterator struct {
314	items    []*clouderrorreportingpb.ErrorGroupStats
315	pageInfo *iterator.PageInfo
316	nextFunc func() error
317
318	// Response is the raw response for the current page.
319	// It must be cast to the RPC response type.
320	// Calling Next() or InternalFetch() updates this value.
321	Response interface{}
322
323	// InternalFetch is for use by the Google Cloud Libraries only.
324	// It is not part of the stable interface of this package.
325	//
326	// InternalFetch returns results from a single call to the underlying RPC.
327	// The number of results is no greater than pageSize.
328	// If there are no more results, nextPageToken is empty and err is nil.
329	InternalFetch func(pageSize int, pageToken string) (results []*clouderrorreportingpb.ErrorGroupStats, nextPageToken string, err error)
330}
331
332// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
333func (it *ErrorGroupStatsIterator) PageInfo() *iterator.PageInfo {
334	return it.pageInfo
335}
336
337// Next returns the next result. Its second return value is iterator.Done if there are no more
338// results. Once Next returns Done, all subsequent calls will return Done.
339func (it *ErrorGroupStatsIterator) Next() (*clouderrorreportingpb.ErrorGroupStats, error) {
340	var item *clouderrorreportingpb.ErrorGroupStats
341	if err := it.nextFunc(); err != nil {
342		return item, err
343	}
344	item = it.items[0]
345	it.items = it.items[1:]
346	return item, nil
347}
348
349func (it *ErrorGroupStatsIterator) bufLen() int {
350	return len(it.items)
351}
352
353func (it *ErrorGroupStatsIterator) takeBuf() interface{} {
354	b := it.items
355	it.items = nil
356	return b
357}
358