From d2e777f56e08146646b3ffb7a78856795094ab4e Mon Sep 17 00:00:00 2001 From: Alex Hong <9397363+hongalex@users.noreply.github.com> Date: Thu, 6 Aug 2020 18:56:57 -0700 Subject: [PATCH] feat(pubsublite): start generating v1 (#2700) * feat(pubsublite): start generating v1 * add internal gen files --- internal/.repo-metadata-full.json | 8 + internal/gapicgen/generator/config.go | 8 + pubsublite/apiv1/admin_client.go | 556 ++++++++++++++++++ pubsublite/apiv1/admin_client_example_test.go | 288 +++++++++ pubsublite/apiv1/cursor_client.go | 274 +++++++++ .../apiv1/cursor_client_example_test.go | 119 ++++ pubsublite/apiv1/doc.go | 102 ++++ .../apiv1/partition_assignment_client.go | 143 +++++ ...artition_assignment_client_example_test.go | 71 +++ pubsublite/apiv1/publisher_client.go | 145 +++++ .../apiv1/publisher_client_example_test.go | 71 +++ pubsublite/apiv1/subscriber_client.go | 137 +++++ .../apiv1/subscriber_client_example_test.go | 71 +++ pubsublite/apiv1/topic_stats_client.go | 153 +++++ .../apiv1/topic_stats_client_example_test.go | 54 ++ 15 files changed, 2200 insertions(+) create mode 100644 pubsublite/apiv1/admin_client.go create mode 100644 pubsublite/apiv1/admin_client_example_test.go create mode 100644 pubsublite/apiv1/cursor_client.go create mode 100644 pubsublite/apiv1/cursor_client_example_test.go create mode 100644 pubsublite/apiv1/doc.go create mode 100644 pubsublite/apiv1/partition_assignment_client.go create mode 100644 pubsublite/apiv1/partition_assignment_client_example_test.go create mode 100644 pubsublite/apiv1/publisher_client.go create mode 100644 pubsublite/apiv1/publisher_client_example_test.go create mode 100644 pubsublite/apiv1/subscriber_client.go create mode 100644 pubsublite/apiv1/subscriber_client_example_test.go create mode 100644 pubsublite/apiv1/topic_stats_client.go create mode 100644 pubsublite/apiv1/topic_stats_client_example_test.go diff --git a/internal/.repo-metadata-full.json b/internal/.repo-metadata-full.json index 2debc4eda1e..536f89cb36e 100644 --- a/internal/.repo-metadata-full.json +++ b/internal/.repo-metadata-full.json @@ -535,6 +535,14 @@ "docs_url": "https://pkg.go.dev/cloud.google.com/go/pubsub/apiv1", "release_level": "ga" }, + "cloud.google.com/go/pubsublite/apiv1": { + "distribution_name": "cloud.google.com/go/pubsublite/apiv1", + "description": "", + "language": "Go", + "client_library_type": "generated", + "docs_url": "https://pkg.go.dev/cloud.google.com/go/pubsublite/apiv1", + "release_level": "beta" + }, "cloud.google.com/go/recaptchaenterprise/apiv1": { "distribution_name": "cloud.google.com/go/recaptchaenterprise/apiv1", "description": "reCAPTCHA Enterprise API", diff --git a/internal/gapicgen/generator/config.go b/internal/gapicgen/generator/config.go index d936fa86258..8538fdcfc61 100644 --- a/internal/gapicgen/generator/config.go +++ b/internal/gapicgen/generator/config.go @@ -672,6 +672,14 @@ var microgenGapicConfigs = []*microgenConfig{ apiServiceConfigPath: "google/pubsub/v1/pubsub_v1.yaml", releaseLevel: "ga", }, + { + inputDirectoryPath: "google/cloud/pubsublite/v1", + pkg: "pubsublite", + importPath: "cloud.google.com/go/pubsublite/apiv1", + gRPCServiceConfigPath: "google/cloud/pubsublite/v1/pubsublite_grpc_service_config.json", + apiServiceConfigPath: "google/cloud/pubsublite/v1/gapic.yaml", + releaseLevel: "beta", + }, { inputDirectoryPath: "google/cloud/automl/v1", pkg: "automl", diff --git a/pubsublite/apiv1/admin_client.go b/pubsublite/apiv1/admin_client.go new file mode 100644 index 00000000000..fc8359a7fdb --- /dev/null +++ b/pubsublite/apiv1/admin_client.go @@ -0,0 +1,556 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by protoc-gen-go_gapic. DO NOT EDIT. + +package pubsublite + +import ( + "context" + "fmt" + "math" + "net/url" + + "github.com/golang/protobuf/proto" + gax "github.com/googleapis/gax-go/v2" + "google.golang.org/api/iterator" + "google.golang.org/api/option" + gtransport "google.golang.org/api/transport/grpc" + pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" +) + +var newAdminClientHook clientHook + +// AdminCallOptions contains the retry settings for each method of AdminClient. +type AdminCallOptions struct { + CreateTopic []gax.CallOption + GetTopic []gax.CallOption + GetTopicPartitions []gax.CallOption + ListTopics []gax.CallOption + UpdateTopic []gax.CallOption + DeleteTopic []gax.CallOption + ListTopicSubscriptions []gax.CallOption + CreateSubscription []gax.CallOption + GetSubscription []gax.CallOption + ListSubscriptions []gax.CallOption + UpdateSubscription []gax.CallOption + DeleteSubscription []gax.CallOption +} + +func defaultAdminClientOptions() []option.ClientOption { + return []option.ClientOption{ + option.WithEndpoint("pubsublite.googleapis.com:443"), + option.WithGRPCDialOption(grpc.WithDisableServiceConfig()), + option.WithScopes(DefaultAuthScopes()...), + option.WithGRPCDialOption(grpc.WithDefaultCallOptions( + grpc.MaxCallRecvMsgSize(math.MaxInt32))), + } +} + +func defaultAdminCallOptions() *AdminCallOptions { + return &AdminCallOptions{ + CreateTopic: []gax.CallOption{}, + GetTopic: []gax.CallOption{}, + GetTopicPartitions: []gax.CallOption{}, + ListTopics: []gax.CallOption{}, + UpdateTopic: []gax.CallOption{}, + DeleteTopic: []gax.CallOption{}, + ListTopicSubscriptions: []gax.CallOption{}, + CreateSubscription: []gax.CallOption{}, + GetSubscription: []gax.CallOption{}, + ListSubscriptions: []gax.CallOption{}, + UpdateSubscription: []gax.CallOption{}, + DeleteSubscription: []gax.CallOption{}, + } +} + +// AdminClient is a client for interacting with . +// +// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. +type AdminClient struct { + // Connection pool of gRPC connections to the service. + connPool gtransport.ConnPool + + // The gRPC API client. + adminClient pubsublitepb.AdminServiceClient + + // The call options for this service. + CallOptions *AdminCallOptions + + // The x-goog-* metadata to be sent with each request. + xGoogMetadata metadata.MD +} + +// NewAdminClient creates a new admin service client. +// +// The service that a client application uses to manage topics and +// subscriptions, such creating, listing, and deleting topics and subscriptions. +func NewAdminClient(ctx context.Context, opts ...option.ClientOption) (*AdminClient, error) { + clientOpts := defaultAdminClientOptions() + + if newAdminClientHook != nil { + hookOpts, err := newAdminClientHook(ctx, clientHookParams{}) + if err != nil { + return nil, err + } + clientOpts = append(clientOpts, hookOpts...) + } + + connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) + if err != nil { + return nil, err + } + c := &AdminClient{ + connPool: connPool, + CallOptions: defaultAdminCallOptions(), + + adminClient: pubsublitepb.NewAdminServiceClient(connPool), + } + c.setGoogleClientInfo() + + return c, nil +} + +// Connection returns a connection to the API service. +// +// Deprecated. +func (c *AdminClient) Connection() *grpc.ClientConn { + return c.connPool.Conn() +} + +// Close closes the connection to the API service. The user should invoke this when +// the client is no longer required. +func (c *AdminClient) Close() error { + return c.connPool.Close() +} + +// setGoogleClientInfo sets the name and version of the application in +// the `x-goog-api-client` header passed on each request. Intended for +// use by Google-written clients. +func (c *AdminClient) setGoogleClientInfo(keyval ...string) { + kv := append([]string{"gl-go", versionGo()}, keyval...) + kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) + c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) +} + +// CreateTopic creates a new topic. +func (c *AdminClient) CreateTopic(ctx context.Context, req *pubsublitepb.CreateTopicRequest, opts ...gax.CallOption) (*pubsublitepb.Topic, error) { + md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) + ctx = insertMetadata(ctx, c.xGoogMetadata, md) + opts = append(c.CallOptions.CreateTopic[0:len(c.CallOptions.CreateTopic):len(c.CallOptions.CreateTopic)], opts...) + var resp *pubsublitepb.Topic + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + resp, err = c.adminClient.CreateTopic(ctx, req, settings.GRPC...) + return err + }, opts...) + if err != nil { + return nil, err + } + return resp, nil +} + +// GetTopic returns the topic configuration. +func (c *AdminClient) GetTopic(ctx context.Context, req *pubsublitepb.GetTopicRequest, opts ...gax.CallOption) (*pubsublitepb.Topic, error) { + md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) + ctx = insertMetadata(ctx, c.xGoogMetadata, md) + opts = append(c.CallOptions.GetTopic[0:len(c.CallOptions.GetTopic):len(c.CallOptions.GetTopic)], opts...) + var resp *pubsublitepb.Topic + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + resp, err = c.adminClient.GetTopic(ctx, req, settings.GRPC...) + return err + }, opts...) + if err != nil { + return nil, err + } + return resp, nil +} + +// GetTopicPartitions returns the partition information for the requested topic. +func (c *AdminClient) GetTopicPartitions(ctx context.Context, req *pubsublitepb.GetTopicPartitionsRequest, opts ...gax.CallOption) (*pubsublitepb.TopicPartitions, error) { + md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) + ctx = insertMetadata(ctx, c.xGoogMetadata, md) + opts = append(c.CallOptions.GetTopicPartitions[0:len(c.CallOptions.GetTopicPartitions):len(c.CallOptions.GetTopicPartitions)], opts...) + var resp *pubsublitepb.TopicPartitions + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + resp, err = c.adminClient.GetTopicPartitions(ctx, req, settings.GRPC...) + return err + }, opts...) + if err != nil { + return nil, err + } + return resp, nil +} + +// ListTopics returns the list of topics for the given project. +func (c *AdminClient) ListTopics(ctx context.Context, req *pubsublitepb.ListTopicsRequest, opts ...gax.CallOption) *TopicIterator { + md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) + ctx = insertMetadata(ctx, c.xGoogMetadata, md) + opts = append(c.CallOptions.ListTopics[0:len(c.CallOptions.ListTopics):len(c.CallOptions.ListTopics)], opts...) + it := &TopicIterator{} + req = proto.Clone(req).(*pubsublitepb.ListTopicsRequest) + it.InternalFetch = func(pageSize int, pageToken string) ([]*pubsublitepb.Topic, string, error) { + var resp *pubsublitepb.ListTopicsResponse + req.PageToken = pageToken + if pageSize > math.MaxInt32 { + req.PageSize = math.MaxInt32 + } else { + req.PageSize = int32(pageSize) + } + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + resp, err = c.adminClient.ListTopics(ctx, req, settings.GRPC...) + return err + }, opts...) + if err != nil { + return nil, "", err + } + + it.Response = resp + return resp.GetTopics(), resp.GetNextPageToken(), nil + } + fetch := func(pageSize int, pageToken string) (string, error) { + items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) + if err != nil { + return "", err + } + it.items = append(it.items, items...) + return nextPageToken, nil + } + it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) + it.pageInfo.MaxSize = int(req.GetPageSize()) + it.pageInfo.Token = req.GetPageToken() + return it +} + +// UpdateTopic updates properties of the specified topic. +func (c *AdminClient) UpdateTopic(ctx context.Context, req *pubsublitepb.UpdateTopicRequest, opts ...gax.CallOption) (*pubsublitepb.Topic, error) { + md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "topic.name", url.QueryEscape(req.GetTopic().GetName()))) + ctx = insertMetadata(ctx, c.xGoogMetadata, md) + opts = append(c.CallOptions.UpdateTopic[0:len(c.CallOptions.UpdateTopic):len(c.CallOptions.UpdateTopic)], opts...) + var resp *pubsublitepb.Topic + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + resp, err = c.adminClient.UpdateTopic(ctx, req, settings.GRPC...) + return err + }, opts...) + if err != nil { + return nil, err + } + return resp, nil +} + +// DeleteTopic deletes the specified topic. +func (c *AdminClient) DeleteTopic(ctx context.Context, req *pubsublitepb.DeleteTopicRequest, opts ...gax.CallOption) error { + md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) + ctx = insertMetadata(ctx, c.xGoogMetadata, md) + opts = append(c.CallOptions.DeleteTopic[0:len(c.CallOptions.DeleteTopic):len(c.CallOptions.DeleteTopic)], opts...) + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + _, err = c.adminClient.DeleteTopic(ctx, req, settings.GRPC...) + return err + }, opts...) + return err +} + +// ListTopicSubscriptions lists the subscriptions attached to the specified topic. +func (c *AdminClient) ListTopicSubscriptions(ctx context.Context, req *pubsublitepb.ListTopicSubscriptionsRequest, opts ...gax.CallOption) *StringIterator { + md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) + ctx = insertMetadata(ctx, c.xGoogMetadata, md) + opts = append(c.CallOptions.ListTopicSubscriptions[0:len(c.CallOptions.ListTopicSubscriptions):len(c.CallOptions.ListTopicSubscriptions)], opts...) + it := &StringIterator{} + req = proto.Clone(req).(*pubsublitepb.ListTopicSubscriptionsRequest) + it.InternalFetch = func(pageSize int, pageToken string) ([]string, string, error) { + var resp *pubsublitepb.ListTopicSubscriptionsResponse + req.PageToken = pageToken + if pageSize > math.MaxInt32 { + req.PageSize = math.MaxInt32 + } else { + req.PageSize = int32(pageSize) + } + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + resp, err = c.adminClient.ListTopicSubscriptions(ctx, req, settings.GRPC...) + return err + }, opts...) + if err != nil { + return nil, "", err + } + + it.Response = resp + return resp.GetSubscriptions(), resp.GetNextPageToken(), nil + } + fetch := func(pageSize int, pageToken string) (string, error) { + items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) + if err != nil { + return "", err + } + it.items = append(it.items, items...) + return nextPageToken, nil + } + it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) + it.pageInfo.MaxSize = int(req.GetPageSize()) + it.pageInfo.Token = req.GetPageToken() + return it +} + +// CreateSubscription creates a new subscription. +func (c *AdminClient) CreateSubscription(ctx context.Context, req *pubsublitepb.CreateSubscriptionRequest, opts ...gax.CallOption) (*pubsublitepb.Subscription, error) { + md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) + ctx = insertMetadata(ctx, c.xGoogMetadata, md) + opts = append(c.CallOptions.CreateSubscription[0:len(c.CallOptions.CreateSubscription):len(c.CallOptions.CreateSubscription)], opts...) + var resp *pubsublitepb.Subscription + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + resp, err = c.adminClient.CreateSubscription(ctx, req, settings.GRPC...) + return err + }, opts...) + if err != nil { + return nil, err + } + return resp, nil +} + +// GetSubscription returns the subscription configuration. +func (c *AdminClient) GetSubscription(ctx context.Context, req *pubsublitepb.GetSubscriptionRequest, opts ...gax.CallOption) (*pubsublitepb.Subscription, error) { + md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) + ctx = insertMetadata(ctx, c.xGoogMetadata, md) + opts = append(c.CallOptions.GetSubscription[0:len(c.CallOptions.GetSubscription):len(c.CallOptions.GetSubscription)], opts...) + var resp *pubsublitepb.Subscription + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + resp, err = c.adminClient.GetSubscription(ctx, req, settings.GRPC...) + return err + }, opts...) + if err != nil { + return nil, err + } + return resp, nil +} + +// ListSubscriptions returns the list of subscriptions for the given project. +func (c *AdminClient) ListSubscriptions(ctx context.Context, req *pubsublitepb.ListSubscriptionsRequest, opts ...gax.CallOption) *SubscriptionIterator { + md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) + ctx = insertMetadata(ctx, c.xGoogMetadata, md) + opts = append(c.CallOptions.ListSubscriptions[0:len(c.CallOptions.ListSubscriptions):len(c.CallOptions.ListSubscriptions)], opts...) + it := &SubscriptionIterator{} + req = proto.Clone(req).(*pubsublitepb.ListSubscriptionsRequest) + it.InternalFetch = func(pageSize int, pageToken string) ([]*pubsublitepb.Subscription, string, error) { + var resp *pubsublitepb.ListSubscriptionsResponse + req.PageToken = pageToken + if pageSize > math.MaxInt32 { + req.PageSize = math.MaxInt32 + } else { + req.PageSize = int32(pageSize) + } + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + resp, err = c.adminClient.ListSubscriptions(ctx, req, settings.GRPC...) + return err + }, opts...) + if err != nil { + return nil, "", err + } + + it.Response = resp + return resp.GetSubscriptions(), resp.GetNextPageToken(), nil + } + fetch := func(pageSize int, pageToken string) (string, error) { + items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) + if err != nil { + return "", err + } + it.items = append(it.items, items...) + return nextPageToken, nil + } + it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) + it.pageInfo.MaxSize = int(req.GetPageSize()) + it.pageInfo.Token = req.GetPageToken() + return it +} + +// UpdateSubscription updates properties of the specified subscription. +func (c *AdminClient) UpdateSubscription(ctx context.Context, req *pubsublitepb.UpdateSubscriptionRequest, opts ...gax.CallOption) (*pubsublitepb.Subscription, error) { + md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "subscription.name", url.QueryEscape(req.GetSubscription().GetName()))) + ctx = insertMetadata(ctx, c.xGoogMetadata, md) + opts = append(c.CallOptions.UpdateSubscription[0:len(c.CallOptions.UpdateSubscription):len(c.CallOptions.UpdateSubscription)], opts...) + var resp *pubsublitepb.Subscription + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + resp, err = c.adminClient.UpdateSubscription(ctx, req, settings.GRPC...) + return err + }, opts...) + if err != nil { + return nil, err + } + return resp, nil +} + +// DeleteSubscription deletes the specified subscription. +func (c *AdminClient) DeleteSubscription(ctx context.Context, req *pubsublitepb.DeleteSubscriptionRequest, opts ...gax.CallOption) error { + md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) + ctx = insertMetadata(ctx, c.xGoogMetadata, md) + opts = append(c.CallOptions.DeleteSubscription[0:len(c.CallOptions.DeleteSubscription):len(c.CallOptions.DeleteSubscription)], opts...) + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + _, err = c.adminClient.DeleteSubscription(ctx, req, settings.GRPC...) + return err + }, opts...) + return err +} + +// StringIterator manages a stream of string. +type StringIterator struct { + items []string + pageInfo *iterator.PageInfo + nextFunc func() error + + // Response is the raw response for the current page. + // It must be cast to the RPC response type. + // Calling Next() or InternalFetch() updates this value. + Response interface{} + + // InternalFetch is for use by the Google Cloud Libraries only. + // It is not part of the stable interface of this package. + // + // InternalFetch returns results from a single call to the underlying RPC. + // The number of results is no greater than pageSize. + // If there are no more results, nextPageToken is empty and err is nil. + InternalFetch func(pageSize int, pageToken string) (results []string, nextPageToken string, err error) +} + +// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. +func (it *StringIterator) PageInfo() *iterator.PageInfo { + return it.pageInfo +} + +// Next returns the next result. Its second return value is iterator.Done if there are no more +// results. Once Next returns Done, all subsequent calls will return Done. +func (it *StringIterator) Next() (string, error) { + var item string + if err := it.nextFunc(); err != nil { + return item, err + } + item = it.items[0] + it.items = it.items[1:] + return item, nil +} + +func (it *StringIterator) bufLen() int { + return len(it.items) +} + +func (it *StringIterator) takeBuf() interface{} { + b := it.items + it.items = nil + return b +} + +// SubscriptionIterator manages a stream of *pubsublitepb.Subscription. +type SubscriptionIterator struct { + items []*pubsublitepb.Subscription + pageInfo *iterator.PageInfo + nextFunc func() error + + // Response is the raw response for the current page. + // It must be cast to the RPC response type. + // Calling Next() or InternalFetch() updates this value. + Response interface{} + + // InternalFetch is for use by the Google Cloud Libraries only. + // It is not part of the stable interface of this package. + // + // InternalFetch returns results from a single call to the underlying RPC. + // The number of results is no greater than pageSize. + // If there are no more results, nextPageToken is empty and err is nil. + InternalFetch func(pageSize int, pageToken string) (results []*pubsublitepb.Subscription, nextPageToken string, err error) +} + +// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. +func (it *SubscriptionIterator) PageInfo() *iterator.PageInfo { + return it.pageInfo +} + +// Next returns the next result. Its second return value is iterator.Done if there are no more +// results. Once Next returns Done, all subsequent calls will return Done. +func (it *SubscriptionIterator) Next() (*pubsublitepb.Subscription, error) { + var item *pubsublitepb.Subscription + if err := it.nextFunc(); err != nil { + return item, err + } + item = it.items[0] + it.items = it.items[1:] + return item, nil +} + +func (it *SubscriptionIterator) bufLen() int { + return len(it.items) +} + +func (it *SubscriptionIterator) takeBuf() interface{} { + b := it.items + it.items = nil + return b +} + +// TopicIterator manages a stream of *pubsublitepb.Topic. +type TopicIterator struct { + items []*pubsublitepb.Topic + pageInfo *iterator.PageInfo + nextFunc func() error + + // Response is the raw response for the current page. + // It must be cast to the RPC response type. + // Calling Next() or InternalFetch() updates this value. + Response interface{} + + // InternalFetch is for use by the Google Cloud Libraries only. + // It is not part of the stable interface of this package. + // + // InternalFetch returns results from a single call to the underlying RPC. + // The number of results is no greater than pageSize. + // If there are no more results, nextPageToken is empty and err is nil. + InternalFetch func(pageSize int, pageToken string) (results []*pubsublitepb.Topic, nextPageToken string, err error) +} + +// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. +func (it *TopicIterator) PageInfo() *iterator.PageInfo { + return it.pageInfo +} + +// Next returns the next result. Its second return value is iterator.Done if there are no more +// results. Once Next returns Done, all subsequent calls will return Done. +func (it *TopicIterator) Next() (*pubsublitepb.Topic, error) { + var item *pubsublitepb.Topic + if err := it.nextFunc(); err != nil { + return item, err + } + item = it.items[0] + it.items = it.items[1:] + return item, nil +} + +func (it *TopicIterator) bufLen() int { + return len(it.items) +} + +func (it *TopicIterator) takeBuf() interface{} { + b := it.items + it.items = nil + return b +} diff --git a/pubsublite/apiv1/admin_client_example_test.go b/pubsublite/apiv1/admin_client_example_test.go new file mode 100644 index 00000000000..4750830ca77 --- /dev/null +++ b/pubsublite/apiv1/admin_client_example_test.go @@ -0,0 +1,288 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by protoc-gen-go_gapic. DO NOT EDIT. + +package pubsublite_test + +import ( + "context" + + pubsublite "cloud.google.com/go/pubsublite/apiv1" + "google.golang.org/api/iterator" + pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" +) + +func ExampleNewAdminClient() { + ctx := context.Background() + c, err := pubsublite.NewAdminClient(ctx) + if err != nil { + // TODO: Handle error. + } + // TODO: Use client. + _ = c +} + +func ExampleAdminClient_CreateTopic() { + // import pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" + + ctx := context.Background() + c, err := pubsublite.NewAdminClient(ctx) + if err != nil { + // TODO: Handle error. + } + + req := &pubsublitepb.CreateTopicRequest{ + // TODO: Fill request struct fields. + } + resp, err := c.CreateTopic(ctx, req) + if err != nil { + // TODO: Handle error. + } + // TODO: Use resp. + _ = resp +} + +func ExampleAdminClient_GetTopic() { + // import pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" + + ctx := context.Background() + c, err := pubsublite.NewAdminClient(ctx) + if err != nil { + // TODO: Handle error. + } + + req := &pubsublitepb.GetTopicRequest{ + // TODO: Fill request struct fields. + } + resp, err := c.GetTopic(ctx, req) + if err != nil { + // TODO: Handle error. + } + // TODO: Use resp. + _ = resp +} + +func ExampleAdminClient_GetTopicPartitions() { + // import pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" + + ctx := context.Background() + c, err := pubsublite.NewAdminClient(ctx) + if err != nil { + // TODO: Handle error. + } + + req := &pubsublitepb.GetTopicPartitionsRequest{ + // TODO: Fill request struct fields. + } + resp, err := c.GetTopicPartitions(ctx, req) + if err != nil { + // TODO: Handle error. + } + // TODO: Use resp. + _ = resp +} + +func ExampleAdminClient_ListTopics() { + // import pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" + // import "google.golang.org/api/iterator" + + ctx := context.Background() + c, err := pubsublite.NewAdminClient(ctx) + if err != nil { + // TODO: Handle error. + } + + req := &pubsublitepb.ListTopicsRequest{ + // TODO: Fill request struct fields. + } + it := c.ListTopics(ctx, req) + for { + resp, err := it.Next() + if err == iterator.Done { + break + } + if err != nil { + // TODO: Handle error. + } + // TODO: Use resp. + _ = resp + } +} + +func ExampleAdminClient_UpdateTopic() { + // import pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" + + ctx := context.Background() + c, err := pubsublite.NewAdminClient(ctx) + if err != nil { + // TODO: Handle error. + } + + req := &pubsublitepb.UpdateTopicRequest{ + // TODO: Fill request struct fields. + } + resp, err := c.UpdateTopic(ctx, req) + if err != nil { + // TODO: Handle error. + } + // TODO: Use resp. + _ = resp +} + +func ExampleAdminClient_DeleteTopic() { + ctx := context.Background() + c, err := pubsublite.NewAdminClient(ctx) + if err != nil { + // TODO: Handle error. + } + + req := &pubsublitepb.DeleteTopicRequest{ + // TODO: Fill request struct fields. + } + err = c.DeleteTopic(ctx, req) + if err != nil { + // TODO: Handle error. + } +} + +func ExampleAdminClient_ListTopicSubscriptions() { + // import pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" + // import "google.golang.org/api/iterator" + + ctx := context.Background() + c, err := pubsublite.NewAdminClient(ctx) + if err != nil { + // TODO: Handle error. + } + + req := &pubsublitepb.ListTopicSubscriptionsRequest{ + // TODO: Fill request struct fields. + } + it := c.ListTopicSubscriptions(ctx, req) + for { + resp, err := it.Next() + if err == iterator.Done { + break + } + if err != nil { + // TODO: Handle error. + } + // TODO: Use resp. + _ = resp + } +} + +func ExampleAdminClient_CreateSubscription() { + // import pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" + + ctx := context.Background() + c, err := pubsublite.NewAdminClient(ctx) + if err != nil { + // TODO: Handle error. + } + + req := &pubsublitepb.CreateSubscriptionRequest{ + // TODO: Fill request struct fields. + } + resp, err := c.CreateSubscription(ctx, req) + if err != nil { + // TODO: Handle error. + } + // TODO: Use resp. + _ = resp +} + +func ExampleAdminClient_GetSubscription() { + // import pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" + + ctx := context.Background() + c, err := pubsublite.NewAdminClient(ctx) + if err != nil { + // TODO: Handle error. + } + + req := &pubsublitepb.GetSubscriptionRequest{ + // TODO: Fill request struct fields. + } + resp, err := c.GetSubscription(ctx, req) + if err != nil { + // TODO: Handle error. + } + // TODO: Use resp. + _ = resp +} + +func ExampleAdminClient_ListSubscriptions() { + // import pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" + // import "google.golang.org/api/iterator" + + ctx := context.Background() + c, err := pubsublite.NewAdminClient(ctx) + if err != nil { + // TODO: Handle error. + } + + req := &pubsublitepb.ListSubscriptionsRequest{ + // TODO: Fill request struct fields. + } + it := c.ListSubscriptions(ctx, req) + for { + resp, err := it.Next() + if err == iterator.Done { + break + } + if err != nil { + // TODO: Handle error. + } + // TODO: Use resp. + _ = resp + } +} + +func ExampleAdminClient_UpdateSubscription() { + // import pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" + + ctx := context.Background() + c, err := pubsublite.NewAdminClient(ctx) + if err != nil { + // TODO: Handle error. + } + + req := &pubsublitepb.UpdateSubscriptionRequest{ + // TODO: Fill request struct fields. + } + resp, err := c.UpdateSubscription(ctx, req) + if err != nil { + // TODO: Handle error. + } + // TODO: Use resp. + _ = resp +} + +func ExampleAdminClient_DeleteSubscription() { + ctx := context.Background() + c, err := pubsublite.NewAdminClient(ctx) + if err != nil { + // TODO: Handle error. + } + + req := &pubsublitepb.DeleteSubscriptionRequest{ + // TODO: Fill request struct fields. + } + err = c.DeleteSubscription(ctx, req) + if err != nil { + // TODO: Handle error. + } +} diff --git a/pubsublite/apiv1/cursor_client.go b/pubsublite/apiv1/cursor_client.go new file mode 100644 index 00000000000..5979b2359bf --- /dev/null +++ b/pubsublite/apiv1/cursor_client.go @@ -0,0 +1,274 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by protoc-gen-go_gapic. DO NOT EDIT. + +package pubsublite + +import ( + "context" + "fmt" + "math" + "net/url" + "time" + + "github.com/golang/protobuf/proto" + gax "github.com/googleapis/gax-go/v2" + "google.golang.org/api/iterator" + "google.golang.org/api/option" + gtransport "google.golang.org/api/transport/grpc" + pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/metadata" +) + +var newCursorClientHook clientHook + +// CursorCallOptions contains the retry settings for each method of CursorClient. +type CursorCallOptions struct { + StreamingCommitCursor []gax.CallOption + CommitCursor []gax.CallOption + ListPartitionCursors []gax.CallOption +} + +func defaultCursorClientOptions() []option.ClientOption { + return []option.ClientOption{ + option.WithEndpoint("pubsublite.googleapis.com:443"), + option.WithGRPCDialOption(grpc.WithDisableServiceConfig()), + option.WithScopes(DefaultAuthScopes()...), + option.WithGRPCDialOption(grpc.WithDefaultCallOptions( + grpc.MaxCallRecvMsgSize(math.MaxInt32))), + } +} + +func defaultCursorCallOptions() *CursorCallOptions { + return &CursorCallOptions{ + StreamingCommitCursor: []gax.CallOption{}, + CommitCursor: []gax.CallOption{ + gax.WithRetry(func() gax.Retryer { + return gax.OnCodes([]codes.Code{ + codes.DeadlineExceeded, + codes.Unavailable, + }, gax.Backoff{ + Initial: 100 * time.Millisecond, + Max: 60000 * time.Millisecond, + Multiplier: 1.30, + }) + }), + }, + ListPartitionCursors: []gax.CallOption{ + gax.WithRetry(func() gax.Retryer { + return gax.OnCodes([]codes.Code{ + codes.DeadlineExceeded, + codes.Unavailable, + }, gax.Backoff{ + Initial: 100 * time.Millisecond, + Max: 60000 * time.Millisecond, + Multiplier: 1.30, + }) + }), + }, + } +} + +// CursorClient is a client for interacting with . +// +// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. +type CursorClient struct { + // Connection pool of gRPC connections to the service. + connPool gtransport.ConnPool + + // The gRPC API client. + cursorClient pubsublitepb.CursorServiceClient + + // The call options for this service. + CallOptions *CursorCallOptions + + // The x-goog-* metadata to be sent with each request. + xGoogMetadata metadata.MD +} + +// NewCursorClient creates a new cursor service client. +// +// The service that a subscriber client application uses to manage committed +// cursors while receiving messsages. A cursor represents a subscriber’s +// progress within a topic partition for a given subscription. +func NewCursorClient(ctx context.Context, opts ...option.ClientOption) (*CursorClient, error) { + clientOpts := defaultCursorClientOptions() + + if newCursorClientHook != nil { + hookOpts, err := newCursorClientHook(ctx, clientHookParams{}) + if err != nil { + return nil, err + } + clientOpts = append(clientOpts, hookOpts...) + } + + connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) + if err != nil { + return nil, err + } + c := &CursorClient{ + connPool: connPool, + CallOptions: defaultCursorCallOptions(), + + cursorClient: pubsublitepb.NewCursorServiceClient(connPool), + } + c.setGoogleClientInfo() + + return c, nil +} + +// Connection returns a connection to the API service. +// +// Deprecated. +func (c *CursorClient) Connection() *grpc.ClientConn { + return c.connPool.Conn() +} + +// Close closes the connection to the API service. The user should invoke this when +// the client is no longer required. +func (c *CursorClient) Close() error { + return c.connPool.Close() +} + +// setGoogleClientInfo sets the name and version of the application in +// the `x-goog-api-client` header passed on each request. Intended for +// use by Google-written clients. +func (c *CursorClient) setGoogleClientInfo(keyval ...string) { + kv := append([]string{"gl-go", versionGo()}, keyval...) + kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) + c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) +} + +// StreamingCommitCursor establishes a stream with the server for managing committed cursors. +func (c *CursorClient) StreamingCommitCursor(ctx context.Context, opts ...gax.CallOption) (pubsublitepb.CursorService_StreamingCommitCursorClient, error) { + ctx = insertMetadata(ctx, c.xGoogMetadata) + opts = append(c.CallOptions.StreamingCommitCursor[0:len(c.CallOptions.StreamingCommitCursor):len(c.CallOptions.StreamingCommitCursor)], opts...) + var resp pubsublitepb.CursorService_StreamingCommitCursorClient + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + resp, err = c.cursorClient.StreamingCommitCursor(ctx, settings.GRPC...) + return err + }, opts...) + if err != nil { + return nil, err + } + return resp, nil +} + +// CommitCursor updates the committed cursor. +func (c *CursorClient) CommitCursor(ctx context.Context, req *pubsublitepb.CommitCursorRequest, opts ...gax.CallOption) (*pubsublitepb.CommitCursorResponse, error) { + ctx = insertMetadata(ctx, c.xGoogMetadata) + opts = append(c.CallOptions.CommitCursor[0:len(c.CallOptions.CommitCursor):len(c.CallOptions.CommitCursor)], opts...) + var resp *pubsublitepb.CommitCursorResponse + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + resp, err = c.cursorClient.CommitCursor(ctx, req, settings.GRPC...) + return err + }, opts...) + if err != nil { + return nil, err + } + return resp, nil +} + +// ListPartitionCursors returns all committed cursor information for a subscription. +func (c *CursorClient) ListPartitionCursors(ctx context.Context, req *pubsublitepb.ListPartitionCursorsRequest, opts ...gax.CallOption) *PartitionCursorIterator { + md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) + ctx = insertMetadata(ctx, c.xGoogMetadata, md) + opts = append(c.CallOptions.ListPartitionCursors[0:len(c.CallOptions.ListPartitionCursors):len(c.CallOptions.ListPartitionCursors)], opts...) + it := &PartitionCursorIterator{} + req = proto.Clone(req).(*pubsublitepb.ListPartitionCursorsRequest) + it.InternalFetch = func(pageSize int, pageToken string) ([]*pubsublitepb.PartitionCursor, string, error) { + var resp *pubsublitepb.ListPartitionCursorsResponse + req.PageToken = pageToken + if pageSize > math.MaxInt32 { + req.PageSize = math.MaxInt32 + } else { + req.PageSize = int32(pageSize) + } + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + resp, err = c.cursorClient.ListPartitionCursors(ctx, req, settings.GRPC...) + return err + }, opts...) + if err != nil { + return nil, "", err + } + + it.Response = resp + return resp.GetPartitionCursors(), resp.GetNextPageToken(), nil + } + fetch := func(pageSize int, pageToken string) (string, error) { + items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) + if err != nil { + return "", err + } + it.items = append(it.items, items...) + return nextPageToken, nil + } + it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) + it.pageInfo.MaxSize = int(req.GetPageSize()) + it.pageInfo.Token = req.GetPageToken() + return it +} + +// PartitionCursorIterator manages a stream of *pubsublitepb.PartitionCursor. +type PartitionCursorIterator struct { + items []*pubsublitepb.PartitionCursor + pageInfo *iterator.PageInfo + nextFunc func() error + + // Response is the raw response for the current page. + // It must be cast to the RPC response type. + // Calling Next() or InternalFetch() updates this value. + Response interface{} + + // InternalFetch is for use by the Google Cloud Libraries only. + // It is not part of the stable interface of this package. + // + // InternalFetch returns results from a single call to the underlying RPC. + // The number of results is no greater than pageSize. + // If there are no more results, nextPageToken is empty and err is nil. + InternalFetch func(pageSize int, pageToken string) (results []*pubsublitepb.PartitionCursor, nextPageToken string, err error) +} + +// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. +func (it *PartitionCursorIterator) PageInfo() *iterator.PageInfo { + return it.pageInfo +} + +// Next returns the next result. Its second return value is iterator.Done if there are no more +// results. Once Next returns Done, all subsequent calls will return Done. +func (it *PartitionCursorIterator) Next() (*pubsublitepb.PartitionCursor, error) { + var item *pubsublitepb.PartitionCursor + if err := it.nextFunc(); err != nil { + return item, err + } + item = it.items[0] + it.items = it.items[1:] + return item, nil +} + +func (it *PartitionCursorIterator) bufLen() int { + return len(it.items) +} + +func (it *PartitionCursorIterator) takeBuf() interface{} { + b := it.items + it.items = nil + return b +} diff --git a/pubsublite/apiv1/cursor_client_example_test.go b/pubsublite/apiv1/cursor_client_example_test.go new file mode 100644 index 00000000000..26bc88ccadc --- /dev/null +++ b/pubsublite/apiv1/cursor_client_example_test.go @@ -0,0 +1,119 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by protoc-gen-go_gapic. DO NOT EDIT. + +package pubsublite_test + +import ( + "context" + "io" + + pubsublite "cloud.google.com/go/pubsublite/apiv1" + "google.golang.org/api/iterator" + pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" +) + +func ExampleNewCursorClient() { + ctx := context.Background() + c, err := pubsublite.NewCursorClient(ctx) + if err != nil { + // TODO: Handle error. + } + // TODO: Use client. + _ = c +} + +func ExampleCursorClient_StreamingCommitCursor() { + // import pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" + + ctx := context.Background() + c, err := pubsublite.NewCursorClient(ctx) + if err != nil { + // TODO: Handle error. + } + stream, err := c.StreamingCommitCursor(ctx) + if err != nil { + // TODO: Handle error. + } + go func() { + reqs := []*pubsublitepb.StreamingCommitCursorRequest{ + // TODO: Create requests. + } + for _, req := range reqs { + if err := stream.Send(req); err != nil { + // TODO: Handle error. + } + } + stream.CloseSend() + }() + for { + resp, err := stream.Recv() + if err == io.EOF { + break + } + if err != nil { + // TODO: handle error. + } + // TODO: Use resp. + _ = resp + } +} + +func ExampleCursorClient_CommitCursor() { + // import pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" + + ctx := context.Background() + c, err := pubsublite.NewCursorClient(ctx) + if err != nil { + // TODO: Handle error. + } + + req := &pubsublitepb.CommitCursorRequest{ + // TODO: Fill request struct fields. + } + resp, err := c.CommitCursor(ctx, req) + if err != nil { + // TODO: Handle error. + } + // TODO: Use resp. + _ = resp +} + +func ExampleCursorClient_ListPartitionCursors() { + // import pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" + // import "google.golang.org/api/iterator" + + ctx := context.Background() + c, err := pubsublite.NewCursorClient(ctx) + if err != nil { + // TODO: Handle error. + } + + req := &pubsublitepb.ListPartitionCursorsRequest{ + // TODO: Fill request struct fields. + } + it := c.ListPartitionCursors(ctx, req) + for { + resp, err := it.Next() + if err == iterator.Done { + break + } + if err != nil { + // TODO: Handle error. + } + // TODO: Use resp. + _ = resp + } +} diff --git a/pubsublite/apiv1/doc.go b/pubsublite/apiv1/doc.go new file mode 100644 index 00000000000..a8682488359 --- /dev/null +++ b/pubsublite/apiv1/doc.go @@ -0,0 +1,102 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by protoc-gen-go_gapic. DO NOT EDIT. + +// +// NOTE: This package is in beta. It is not stable, and may be subject to changes. +// +// Use of Context +// +// The ctx passed to NewClient is used for authentication requests and +// for creating the underlying connection, but is not used for subsequent calls. +// Individual methods on the client use the ctx given to them. +// +// To close the open connection, use the Close() method. +// +// For information about setting deadlines, reusing contexts, and more +// please visit pkg.go.dev/cloud.google.com/go. +package pubsublite // import "cloud.google.com/go/pubsublite/apiv1" + +import ( + "context" + "runtime" + "strings" + "unicode" + + "google.golang.org/api/option" + "google.golang.org/grpc/metadata" +) + +// For more information on implementing a client constructor hook, see +// https://github.com/googleapis/google-cloud-go/wiki/Customizing-constructors. +type clientHookParams struct{} +type clientHook func(context.Context, clientHookParams) ([]option.ClientOption, error) + +const versionClient = "20200806" + +func insertMetadata(ctx context.Context, mds ...metadata.MD) context.Context { + out, _ := metadata.FromOutgoingContext(ctx) + out = out.Copy() + for _, md := range mds { + for k, v := range md { + out[k] = append(out[k], v...) + } + } + return metadata.NewOutgoingContext(ctx, out) +} + +// DefaultAuthScopes reports the default set of authentication scopes to use with this package. +func DefaultAuthScopes() []string { + return []string{ + "https://www.googleapis.com/auth/cloud-platform", + } +} + +// versionGo returns the Go runtime version. The returned string +// has no whitespace, suitable for reporting in header. +func versionGo() string { + const develPrefix = "devel +" + + s := runtime.Version() + if strings.HasPrefix(s, develPrefix) { + s = s[len(develPrefix):] + if p := strings.IndexFunc(s, unicode.IsSpace); p >= 0 { + s = s[:p] + } + return s + } + + notSemverRune := func(r rune) bool { + return !strings.ContainsRune("0123456789.", r) + } + + if strings.HasPrefix(s, "go1") { + s = s[2:] + var prerelease string + if p := strings.IndexFunc(s, notSemverRune); p >= 0 { + s, prerelease = s[:p], s[p:] + } + if strings.HasSuffix(s, ".") { + s += "0" + } else if strings.Count(s, ".") < 2 { + s += ".0" + } + if prerelease != "" { + s += "-" + prerelease + } + return s + } + return "UNKNOWN" +} diff --git a/pubsublite/apiv1/partition_assignment_client.go b/pubsublite/apiv1/partition_assignment_client.go new file mode 100644 index 00000000000..3c6229c7e52 --- /dev/null +++ b/pubsublite/apiv1/partition_assignment_client.go @@ -0,0 +1,143 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by protoc-gen-go_gapic. DO NOT EDIT. + +package pubsublite + +import ( + "context" + "math" + + gax "github.com/googleapis/gax-go/v2" + "google.golang.org/api/option" + gtransport "google.golang.org/api/transport/grpc" + pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" +) + +var newPartitionAssignmentClientHook clientHook + +// PartitionAssignmentCallOptions contains the retry settings for each method of PartitionAssignmentClient. +type PartitionAssignmentCallOptions struct { + AssignPartitions []gax.CallOption +} + +func defaultPartitionAssignmentClientOptions() []option.ClientOption { + return []option.ClientOption{ + option.WithEndpoint("pubsublite.googleapis.com:443"), + option.WithGRPCDialOption(grpc.WithDisableServiceConfig()), + option.WithScopes(DefaultAuthScopes()...), + option.WithGRPCDialOption(grpc.WithDefaultCallOptions( + grpc.MaxCallRecvMsgSize(math.MaxInt32))), + } +} + +func defaultPartitionAssignmentCallOptions() *PartitionAssignmentCallOptions { + return &PartitionAssignmentCallOptions{ + AssignPartitions: []gax.CallOption{}, + } +} + +// PartitionAssignmentClient is a client for interacting with . +// +// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. +type PartitionAssignmentClient struct { + // Connection pool of gRPC connections to the service. + connPool gtransport.ConnPool + + // The gRPC API client. + partitionAssignmentClient pubsublitepb.PartitionAssignmentServiceClient + + // The call options for this service. + CallOptions *PartitionAssignmentCallOptions + + // The x-goog-* metadata to be sent with each request. + xGoogMetadata metadata.MD +} + +// NewPartitionAssignmentClient creates a new partition assignment service client. +// +// The service that a subscriber client application uses to determine which +// partitions it should connect to. +func NewPartitionAssignmentClient(ctx context.Context, opts ...option.ClientOption) (*PartitionAssignmentClient, error) { + clientOpts := defaultPartitionAssignmentClientOptions() + + if newPartitionAssignmentClientHook != nil { + hookOpts, err := newPartitionAssignmentClientHook(ctx, clientHookParams{}) + if err != nil { + return nil, err + } + clientOpts = append(clientOpts, hookOpts...) + } + + connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) + if err != nil { + return nil, err + } + c := &PartitionAssignmentClient{ + connPool: connPool, + CallOptions: defaultPartitionAssignmentCallOptions(), + + partitionAssignmentClient: pubsublitepb.NewPartitionAssignmentServiceClient(connPool), + } + c.setGoogleClientInfo() + + return c, nil +} + +// Connection returns a connection to the API service. +// +// Deprecated. +func (c *PartitionAssignmentClient) Connection() *grpc.ClientConn { + return c.connPool.Conn() +} + +// Close closes the connection to the API service. The user should invoke this when +// the client is no longer required. +func (c *PartitionAssignmentClient) Close() error { + return c.connPool.Close() +} + +// setGoogleClientInfo sets the name and version of the application in +// the `x-goog-api-client` header passed on each request. Intended for +// use by Google-written clients. +func (c *PartitionAssignmentClient) setGoogleClientInfo(keyval ...string) { + kv := append([]string{"gl-go", versionGo()}, keyval...) + kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) + c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) +} + +// AssignPartitions assign partitions for this client to handle for the specified subscription. +// +// The client must send an InitialPartitionAssignmentRequest first. +// The server will then send at most one unacknowledged PartitionAssignment +// outstanding on the stream at a time. +// The client should send a PartitionAssignmentAck after updating the +// partitions it is connected to to reflect the new assignment. +func (c *PartitionAssignmentClient) AssignPartitions(ctx context.Context, opts ...gax.CallOption) (pubsublitepb.PartitionAssignmentService_AssignPartitionsClient, error) { + ctx = insertMetadata(ctx, c.xGoogMetadata) + opts = append(c.CallOptions.AssignPartitions[0:len(c.CallOptions.AssignPartitions):len(c.CallOptions.AssignPartitions)], opts...) + var resp pubsublitepb.PartitionAssignmentService_AssignPartitionsClient + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + resp, err = c.partitionAssignmentClient.AssignPartitions(ctx, settings.GRPC...) + return err + }, opts...) + if err != nil { + return nil, err + } + return resp, nil +} diff --git a/pubsublite/apiv1/partition_assignment_client_example_test.go b/pubsublite/apiv1/partition_assignment_client_example_test.go new file mode 100644 index 00000000000..64f9a79d774 --- /dev/null +++ b/pubsublite/apiv1/partition_assignment_client_example_test.go @@ -0,0 +1,71 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by protoc-gen-go_gapic. DO NOT EDIT. + +package pubsublite_test + +import ( + "context" + "io" + + pubsublite "cloud.google.com/go/pubsublite/apiv1" + pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" +) + +func ExampleNewPartitionAssignmentClient() { + ctx := context.Background() + c, err := pubsublite.NewPartitionAssignmentClient(ctx) + if err != nil { + // TODO: Handle error. + } + // TODO: Use client. + _ = c +} + +func ExamplePartitionAssignmentClient_AssignPartitions() { + // import pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" + + ctx := context.Background() + c, err := pubsublite.NewPartitionAssignmentClient(ctx) + if err != nil { + // TODO: Handle error. + } + stream, err := c.AssignPartitions(ctx) + if err != nil { + // TODO: Handle error. + } + go func() { + reqs := []*pubsublitepb.PartitionAssignmentRequest{ + // TODO: Create requests. + } + for _, req := range reqs { + if err := stream.Send(req); err != nil { + // TODO: Handle error. + } + } + stream.CloseSend() + }() + for { + resp, err := stream.Recv() + if err == io.EOF { + break + } + if err != nil { + // TODO: handle error. + } + // TODO: Use resp. + _ = resp + } +} diff --git a/pubsublite/apiv1/publisher_client.go b/pubsublite/apiv1/publisher_client.go new file mode 100644 index 00000000000..71c48612451 --- /dev/null +++ b/pubsublite/apiv1/publisher_client.go @@ -0,0 +1,145 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by protoc-gen-go_gapic. DO NOT EDIT. + +package pubsublite + +import ( + "context" + "math" + + gax "github.com/googleapis/gax-go/v2" + "google.golang.org/api/option" + gtransport "google.golang.org/api/transport/grpc" + pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" +) + +var newPublisherClientHook clientHook + +// PublisherCallOptions contains the retry settings for each method of PublisherClient. +type PublisherCallOptions struct { + Publish []gax.CallOption +} + +func defaultPublisherClientOptions() []option.ClientOption { + return []option.ClientOption{ + option.WithEndpoint("pubsublite.googleapis.com:443"), + option.WithGRPCDialOption(grpc.WithDisableServiceConfig()), + option.WithScopes(DefaultAuthScopes()...), + option.WithGRPCDialOption(grpc.WithDefaultCallOptions( + grpc.MaxCallRecvMsgSize(math.MaxInt32))), + } +} + +func defaultPublisherCallOptions() *PublisherCallOptions { + return &PublisherCallOptions{ + Publish: []gax.CallOption{}, + } +} + +// PublisherClient is a client for interacting with . +// +// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. +type PublisherClient struct { + // Connection pool of gRPC connections to the service. + connPool gtransport.ConnPool + + // The gRPC API client. + publisherClient pubsublitepb.PublisherServiceClient + + // The call options for this service. + CallOptions *PublisherCallOptions + + // The x-goog-* metadata to be sent with each request. + xGoogMetadata metadata.MD +} + +// NewPublisherClient creates a new publisher service client. +// +// The service that a publisher client application uses to publish messages to +// topics. Published messages are retained by the service for the duration of +// the retention period configured for the respective topic, and are delivered +// to subscriber clients upon request (via the SubscriberService). +func NewPublisherClient(ctx context.Context, opts ...option.ClientOption) (*PublisherClient, error) { + clientOpts := defaultPublisherClientOptions() + + if newPublisherClientHook != nil { + hookOpts, err := newPublisherClientHook(ctx, clientHookParams{}) + if err != nil { + return nil, err + } + clientOpts = append(clientOpts, hookOpts...) + } + + connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) + if err != nil { + return nil, err + } + c := &PublisherClient{ + connPool: connPool, + CallOptions: defaultPublisherCallOptions(), + + publisherClient: pubsublitepb.NewPublisherServiceClient(connPool), + } + c.setGoogleClientInfo() + + return c, nil +} + +// Connection returns a connection to the API service. +// +// Deprecated. +func (c *PublisherClient) Connection() *grpc.ClientConn { + return c.connPool.Conn() +} + +// Close closes the connection to the API service. The user should invoke this when +// the client is no longer required. +func (c *PublisherClient) Close() error { + return c.connPool.Close() +} + +// setGoogleClientInfo sets the name and version of the application in +// the `x-goog-api-client` header passed on each request. Intended for +// use by Google-written clients. +func (c *PublisherClient) setGoogleClientInfo(keyval ...string) { + kv := append([]string{"gl-go", versionGo()}, keyval...) + kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) + c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) +} + +// Publish establishes a stream with the server for publishing messages. Once the +// stream is initialized, the client publishes messages by sending publish +// requests on the stream. The server responds with a PublishResponse for each +// PublishRequest sent by the client, in the same order that the requests +// were sent. Note that multiple PublishRequests can be in flight +// simultaneously, but they will be processed by the server in the order that +// they are sent by the client on a given stream. +func (c *PublisherClient) Publish(ctx context.Context, opts ...gax.CallOption) (pubsublitepb.PublisherService_PublishClient, error) { + ctx = insertMetadata(ctx, c.xGoogMetadata) + opts = append(c.CallOptions.Publish[0:len(c.CallOptions.Publish):len(c.CallOptions.Publish)], opts...) + var resp pubsublitepb.PublisherService_PublishClient + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + resp, err = c.publisherClient.Publish(ctx, settings.GRPC...) + return err + }, opts...) + if err != nil { + return nil, err + } + return resp, nil +} diff --git a/pubsublite/apiv1/publisher_client_example_test.go b/pubsublite/apiv1/publisher_client_example_test.go new file mode 100644 index 00000000000..4aa30ef4c3f --- /dev/null +++ b/pubsublite/apiv1/publisher_client_example_test.go @@ -0,0 +1,71 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by protoc-gen-go_gapic. DO NOT EDIT. + +package pubsublite_test + +import ( + "context" + "io" + + pubsublite "cloud.google.com/go/pubsublite/apiv1" + pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" +) + +func ExampleNewPublisherClient() { + ctx := context.Background() + c, err := pubsublite.NewPublisherClient(ctx) + if err != nil { + // TODO: Handle error. + } + // TODO: Use client. + _ = c +} + +func ExamplePublisherClient_Publish() { + // import pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" + + ctx := context.Background() + c, err := pubsublite.NewPublisherClient(ctx) + if err != nil { + // TODO: Handle error. + } + stream, err := c.Publish(ctx) + if err != nil { + // TODO: Handle error. + } + go func() { + reqs := []*pubsublitepb.PublishRequest{ + // TODO: Create requests. + } + for _, req := range reqs { + if err := stream.Send(req); err != nil { + // TODO: Handle error. + } + } + stream.CloseSend() + }() + for { + resp, err := stream.Recv() + if err == io.EOF { + break + } + if err != nil { + // TODO: handle error. + } + // TODO: Use resp. + _ = resp + } +} diff --git a/pubsublite/apiv1/subscriber_client.go b/pubsublite/apiv1/subscriber_client.go new file mode 100644 index 00000000000..9e9d5f28dfb --- /dev/null +++ b/pubsublite/apiv1/subscriber_client.go @@ -0,0 +1,137 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by protoc-gen-go_gapic. DO NOT EDIT. + +package pubsublite + +import ( + "context" + "math" + + gax "github.com/googleapis/gax-go/v2" + "google.golang.org/api/option" + gtransport "google.golang.org/api/transport/grpc" + pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" +) + +var newSubscriberClientHook clientHook + +// SubscriberCallOptions contains the retry settings for each method of SubscriberClient. +type SubscriberCallOptions struct { + Subscribe []gax.CallOption +} + +func defaultSubscriberClientOptions() []option.ClientOption { + return []option.ClientOption{ + option.WithEndpoint("pubsublite.googleapis.com:443"), + option.WithGRPCDialOption(grpc.WithDisableServiceConfig()), + option.WithScopes(DefaultAuthScopes()...), + option.WithGRPCDialOption(grpc.WithDefaultCallOptions( + grpc.MaxCallRecvMsgSize(math.MaxInt32))), + } +} + +func defaultSubscriberCallOptions() *SubscriberCallOptions { + return &SubscriberCallOptions{ + Subscribe: []gax.CallOption{}, + } +} + +// SubscriberClient is a client for interacting with . +// +// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. +type SubscriberClient struct { + // Connection pool of gRPC connections to the service. + connPool gtransport.ConnPool + + // The gRPC API client. + subscriberClient pubsublitepb.SubscriberServiceClient + + // The call options for this service. + CallOptions *SubscriberCallOptions + + // The x-goog-* metadata to be sent with each request. + xGoogMetadata metadata.MD +} + +// NewSubscriberClient creates a new subscriber service client. +// +// The service that a subscriber client application uses to receive messages +// from subscriptions. +func NewSubscriberClient(ctx context.Context, opts ...option.ClientOption) (*SubscriberClient, error) { + clientOpts := defaultSubscriberClientOptions() + + if newSubscriberClientHook != nil { + hookOpts, err := newSubscriberClientHook(ctx, clientHookParams{}) + if err != nil { + return nil, err + } + clientOpts = append(clientOpts, hookOpts...) + } + + connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) + if err != nil { + return nil, err + } + c := &SubscriberClient{ + connPool: connPool, + CallOptions: defaultSubscriberCallOptions(), + + subscriberClient: pubsublitepb.NewSubscriberServiceClient(connPool), + } + c.setGoogleClientInfo() + + return c, nil +} + +// Connection returns a connection to the API service. +// +// Deprecated. +func (c *SubscriberClient) Connection() *grpc.ClientConn { + return c.connPool.Conn() +} + +// Close closes the connection to the API service. The user should invoke this when +// the client is no longer required. +func (c *SubscriberClient) Close() error { + return c.connPool.Close() +} + +// setGoogleClientInfo sets the name and version of the application in +// the `x-goog-api-client` header passed on each request. Intended for +// use by Google-written clients. +func (c *SubscriberClient) setGoogleClientInfo(keyval ...string) { + kv := append([]string{"gl-go", versionGo()}, keyval...) + kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) + c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) +} + +// Subscribe establishes a stream with the server for receiving messages. +func (c *SubscriberClient) Subscribe(ctx context.Context, opts ...gax.CallOption) (pubsublitepb.SubscriberService_SubscribeClient, error) { + ctx = insertMetadata(ctx, c.xGoogMetadata) + opts = append(c.CallOptions.Subscribe[0:len(c.CallOptions.Subscribe):len(c.CallOptions.Subscribe)], opts...) + var resp pubsublitepb.SubscriberService_SubscribeClient + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + resp, err = c.subscriberClient.Subscribe(ctx, settings.GRPC...) + return err + }, opts...) + if err != nil { + return nil, err + } + return resp, nil +} diff --git a/pubsublite/apiv1/subscriber_client_example_test.go b/pubsublite/apiv1/subscriber_client_example_test.go new file mode 100644 index 00000000000..68857de2cef --- /dev/null +++ b/pubsublite/apiv1/subscriber_client_example_test.go @@ -0,0 +1,71 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by protoc-gen-go_gapic. DO NOT EDIT. + +package pubsublite_test + +import ( + "context" + "io" + + pubsublite "cloud.google.com/go/pubsublite/apiv1" + pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" +) + +func ExampleNewSubscriberClient() { + ctx := context.Background() + c, err := pubsublite.NewSubscriberClient(ctx) + if err != nil { + // TODO: Handle error. + } + // TODO: Use client. + _ = c +} + +func ExampleSubscriberClient_Subscribe() { + // import pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" + + ctx := context.Background() + c, err := pubsublite.NewSubscriberClient(ctx) + if err != nil { + // TODO: Handle error. + } + stream, err := c.Subscribe(ctx) + if err != nil { + // TODO: Handle error. + } + go func() { + reqs := []*pubsublitepb.SubscribeRequest{ + // TODO: Create requests. + } + for _, req := range reqs { + if err := stream.Send(req); err != nil { + // TODO: Handle error. + } + } + stream.CloseSend() + }() + for { + resp, err := stream.Recv() + if err == io.EOF { + break + } + if err != nil { + // TODO: handle error. + } + // TODO: Use resp. + _ = resp + } +} diff --git a/pubsublite/apiv1/topic_stats_client.go b/pubsublite/apiv1/topic_stats_client.go new file mode 100644 index 00000000000..28e7652c3bb --- /dev/null +++ b/pubsublite/apiv1/topic_stats_client.go @@ -0,0 +1,153 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by protoc-gen-go_gapic. DO NOT EDIT. + +package pubsublite + +import ( + "context" + "fmt" + "math" + "net/url" + "time" + + gax "github.com/googleapis/gax-go/v2" + "google.golang.org/api/option" + gtransport "google.golang.org/api/transport/grpc" + pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/metadata" +) + +var newTopicStatsClientHook clientHook + +// TopicStatsCallOptions contains the retry settings for each method of TopicStatsClient. +type TopicStatsCallOptions struct { + ComputeMessageStats []gax.CallOption +} + +func defaultTopicStatsClientOptions() []option.ClientOption { + return []option.ClientOption{ + option.WithEndpoint("pubsublite.googleapis.com:443"), + option.WithGRPCDialOption(grpc.WithDisableServiceConfig()), + option.WithScopes(DefaultAuthScopes()...), + option.WithGRPCDialOption(grpc.WithDefaultCallOptions( + grpc.MaxCallRecvMsgSize(math.MaxInt32))), + } +} + +func defaultTopicStatsCallOptions() *TopicStatsCallOptions { + return &TopicStatsCallOptions{ + ComputeMessageStats: []gax.CallOption{ + gax.WithRetry(func() gax.Retryer { + return gax.OnCodes([]codes.Code{ + codes.DeadlineExceeded, + codes.Unavailable, + }, gax.Backoff{ + Initial: 100 * time.Millisecond, + Max: 60000 * time.Millisecond, + Multiplier: 1.30, + }) + }), + }, + } +} + +// TopicStatsClient is a client for interacting with . +// +// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. +type TopicStatsClient struct { + // Connection pool of gRPC connections to the service. + connPool gtransport.ConnPool + + // The gRPC API client. + topicStatsClient pubsublitepb.TopicStatsServiceClient + + // The call options for this service. + CallOptions *TopicStatsCallOptions + + // The x-goog-* metadata to be sent with each request. + xGoogMetadata metadata.MD +} + +// NewTopicStatsClient creates a new topic stats service client. +// +// This service allows users to get stats about messages in their topic. +func NewTopicStatsClient(ctx context.Context, opts ...option.ClientOption) (*TopicStatsClient, error) { + clientOpts := defaultTopicStatsClientOptions() + + if newTopicStatsClientHook != nil { + hookOpts, err := newTopicStatsClientHook(ctx, clientHookParams{}) + if err != nil { + return nil, err + } + clientOpts = append(clientOpts, hookOpts...) + } + + connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) + if err != nil { + return nil, err + } + c := &TopicStatsClient{ + connPool: connPool, + CallOptions: defaultTopicStatsCallOptions(), + + topicStatsClient: pubsublitepb.NewTopicStatsServiceClient(connPool), + } + c.setGoogleClientInfo() + + return c, nil +} + +// Connection returns a connection to the API service. +// +// Deprecated. +func (c *TopicStatsClient) Connection() *grpc.ClientConn { + return c.connPool.Conn() +} + +// Close closes the connection to the API service. The user should invoke this when +// the client is no longer required. +func (c *TopicStatsClient) Close() error { + return c.connPool.Close() +} + +// setGoogleClientInfo sets the name and version of the application in +// the `x-goog-api-client` header passed on each request. Intended for +// use by Google-written clients. +func (c *TopicStatsClient) setGoogleClientInfo(keyval ...string) { + kv := append([]string{"gl-go", versionGo()}, keyval...) + kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) + c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) +} + +// ComputeMessageStats compute statistics about a range of messages in a given topic and +// partition. +func (c *TopicStatsClient) ComputeMessageStats(ctx context.Context, req *pubsublitepb.ComputeMessageStatsRequest, opts ...gax.CallOption) (*pubsublitepb.ComputeMessageStatsResponse, error) { + md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "topic", url.QueryEscape(req.GetTopic()))) + ctx = insertMetadata(ctx, c.xGoogMetadata, md) + opts = append(c.CallOptions.ComputeMessageStats[0:len(c.CallOptions.ComputeMessageStats):len(c.CallOptions.ComputeMessageStats)], opts...) + var resp *pubsublitepb.ComputeMessageStatsResponse + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + resp, err = c.topicStatsClient.ComputeMessageStats(ctx, req, settings.GRPC...) + return err + }, opts...) + if err != nil { + return nil, err + } + return resp, nil +} diff --git a/pubsublite/apiv1/topic_stats_client_example_test.go b/pubsublite/apiv1/topic_stats_client_example_test.go new file mode 100644 index 00000000000..e8ee7013ac9 --- /dev/null +++ b/pubsublite/apiv1/topic_stats_client_example_test.go @@ -0,0 +1,54 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by protoc-gen-go_gapic. DO NOT EDIT. + +package pubsublite_test + +import ( + "context" + + pubsublite "cloud.google.com/go/pubsublite/apiv1" + pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" +) + +func ExampleNewTopicStatsClient() { + ctx := context.Background() + c, err := pubsublite.NewTopicStatsClient(ctx) + if err != nil { + // TODO: Handle error. + } + // TODO: Use client. + _ = c +} + +func ExampleTopicStatsClient_ComputeMessageStats() { + // import pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" + + ctx := context.Background() + c, err := pubsublite.NewTopicStatsClient(ctx) + if err != nil { + // TODO: Handle error. + } + + req := &pubsublitepb.ComputeMessageStatsRequest{ + // TODO: Fill request struct fields. + } + resp, err := c.ComputeMessageStats(ctx, req) + if err != nil { + // TODO: Handle error. + } + // TODO: Use resp. + _ = resp +}