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 dialogflow 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 "google.golang.org/api/option/internaloption" 31 gtransport "google.golang.org/api/transport/grpc" 32 dialogflowpb "google.golang.org/genproto/googleapis/cloud/dialogflow/v2" 33 "google.golang.org/grpc" 34 "google.golang.org/grpc/codes" 35 "google.golang.org/grpc/metadata" 36) 37 38var newEnvironmentsClientHook clientHook 39 40// EnvironmentsCallOptions contains the retry settings for each method of EnvironmentsClient. 41type EnvironmentsCallOptions struct { 42 ListEnvironments []gax.CallOption 43} 44 45func defaultEnvironmentsClientOptions() []option.ClientOption { 46 return []option.ClientOption{ 47 internaloption.WithDefaultEndpoint("dialogflow.googleapis.com:443"), 48 internaloption.WithDefaultMTLSEndpoint("dialogflow.mtls.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 defaultEnvironmentsCallOptions() *EnvironmentsCallOptions { 57 return &EnvironmentsCallOptions{ 58 ListEnvironments: []gax.CallOption{ 59 gax.WithRetry(func() gax.Retryer { 60 return gax.OnCodes([]codes.Code{ 61 codes.Unavailable, 62 }, gax.Backoff{ 63 Initial: 100 * time.Millisecond, 64 Max: 60000 * time.Millisecond, 65 Multiplier: 1.30, 66 }) 67 }), 68 }, 69 } 70} 71 72// EnvironmentsClient is a client for interacting with Dialogflow API. 73// 74// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 75type EnvironmentsClient struct { 76 // Connection pool of gRPC connections to the service. 77 connPool gtransport.ConnPool 78 79 // flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE 80 disableDeadlines bool 81 82 // The gRPC API client. 83 environmentsClient dialogflowpb.EnvironmentsClient 84 85 // The call options for this service. 86 CallOptions *EnvironmentsCallOptions 87 88 // The x-goog-* metadata to be sent with each request. 89 xGoogMetadata metadata.MD 90} 91 92// NewEnvironmentsClient creates a new environments client. 93// 94// Service for managing Environments. 95func NewEnvironmentsClient(ctx context.Context, opts ...option.ClientOption) (*EnvironmentsClient, error) { 96 clientOpts := defaultEnvironmentsClientOptions() 97 98 if newEnvironmentsClientHook != nil { 99 hookOpts, err := newEnvironmentsClientHook(ctx, clientHookParams{}) 100 if err != nil { 101 return nil, err 102 } 103 clientOpts = append(clientOpts, hookOpts...) 104 } 105 106 disableDeadlines, err := checkDisableDeadlines() 107 if err != nil { 108 return nil, err 109 } 110 111 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) 112 if err != nil { 113 return nil, err 114 } 115 c := &EnvironmentsClient{ 116 connPool: connPool, 117 disableDeadlines: disableDeadlines, 118 CallOptions: defaultEnvironmentsCallOptions(), 119 120 environmentsClient: dialogflowpb.NewEnvironmentsClient(connPool), 121 } 122 c.setGoogleClientInfo() 123 124 return c, nil 125} 126 127// Connection returns a connection to the API service. 128// 129// Deprecated. 130func (c *EnvironmentsClient) Connection() *grpc.ClientConn { 131 return c.connPool.Conn() 132} 133 134// Close closes the connection to the API service. The user should invoke this when 135// the client is no longer required. 136func (c *EnvironmentsClient) Close() error { 137 return c.connPool.Close() 138} 139 140// setGoogleClientInfo sets the name and version of the application in 141// the `x-goog-api-client` header passed on each request. Intended for 142// use by Google-written clients. 143func (c *EnvironmentsClient) setGoogleClientInfo(keyval ...string) { 144 kv := append([]string{"gl-go", versionGo()}, keyval...) 145 kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) 146 c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) 147} 148 149// ListEnvironments returns the list of all non-draft environments of the specified agent. 150func (c *EnvironmentsClient) ListEnvironments(ctx context.Context, req *dialogflowpb.ListEnvironmentsRequest, opts ...gax.CallOption) *EnvironmentIterator { 151 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 152 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 153 opts = append(c.CallOptions.ListEnvironments[0:len(c.CallOptions.ListEnvironments):len(c.CallOptions.ListEnvironments)], opts...) 154 it := &EnvironmentIterator{} 155 req = proto.Clone(req).(*dialogflowpb.ListEnvironmentsRequest) 156 it.InternalFetch = func(pageSize int, pageToken string) ([]*dialogflowpb.Environment, string, error) { 157 var resp *dialogflowpb.ListEnvironmentsResponse 158 req.PageToken = pageToken 159 if pageSize > math.MaxInt32 { 160 req.PageSize = math.MaxInt32 161 } else { 162 req.PageSize = int32(pageSize) 163 } 164 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 165 var err error 166 resp, err = c.environmentsClient.ListEnvironments(ctx, req, settings.GRPC...) 167 return err 168 }, opts...) 169 if err != nil { 170 return nil, "", err 171 } 172 173 it.Response = resp 174 return resp.GetEnvironments(), resp.GetNextPageToken(), nil 175 } 176 fetch := func(pageSize int, pageToken string) (string, error) { 177 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 178 if err != nil { 179 return "", err 180 } 181 it.items = append(it.items, items...) 182 return nextPageToken, nil 183 } 184 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 185 it.pageInfo.MaxSize = int(req.GetPageSize()) 186 it.pageInfo.Token = req.GetPageToken() 187 return it 188} 189 190// EnvironmentIterator manages a stream of *dialogflowpb.Environment. 191type EnvironmentIterator struct { 192 items []*dialogflowpb.Environment 193 pageInfo *iterator.PageInfo 194 nextFunc func() error 195 196 // Response is the raw response for the current page. 197 // It must be cast to the RPC response type. 198 // Calling Next() or InternalFetch() updates this value. 199 Response interface{} 200 201 // InternalFetch is for use by the Google Cloud Libraries only. 202 // It is not part of the stable interface of this package. 203 // 204 // InternalFetch returns results from a single call to the underlying RPC. 205 // The number of results is no greater than pageSize. 206 // If there are no more results, nextPageToken is empty and err is nil. 207 InternalFetch func(pageSize int, pageToken string) (results []*dialogflowpb.Environment, nextPageToken string, err error) 208} 209 210// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 211func (it *EnvironmentIterator) PageInfo() *iterator.PageInfo { 212 return it.pageInfo 213} 214 215// Next returns the next result. Its second return value is iterator.Done if there are no more 216// results. Once Next returns Done, all subsequent calls will return Done. 217func (it *EnvironmentIterator) Next() (*dialogflowpb.Environment, error) { 218 var item *dialogflowpb.Environment 219 if err := it.nextFunc(); err != nil { 220 return item, err 221 } 222 item = it.items[0] 223 it.items = it.items[1:] 224 return item, nil 225} 226 227func (it *EnvironmentIterator) bufLen() int { 228 return len(it.items) 229} 230 231func (it *EnvironmentIterator) takeBuf() interface{} { 232 b := it.items 233 it.items = nil 234 return b 235} 236