1// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Code generated by protoc-gen-go_gapic. DO NOT EDIT.
16
17package pubsublite
18
19import (
20	"context"
21	"math"
22
23	gax "github.com/googleapis/gax-go/v2"
24	"google.golang.org/api/option"
25	"google.golang.org/api/option/internaloption"
26	gtransport "google.golang.org/api/transport/grpc"
27	pubsublitepb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1"
28	"google.golang.org/grpc"
29	"google.golang.org/grpc/metadata"
30)
31
32var newPublisherClientHook clientHook
33
34// PublisherCallOptions contains the retry settings for each method of PublisherClient.
35type PublisherCallOptions struct {
36	Publish []gax.CallOption
37}
38
39func defaultPublisherGRPCClientOptions() []option.ClientOption {
40	return []option.ClientOption{
41		internaloption.WithDefaultEndpoint("pubsublite.googleapis.com:443"),
42		internaloption.WithDefaultMTLSEndpoint("pubsublite.mtls.googleapis.com:443"),
43		internaloption.WithDefaultAudience("https://pubsublite.googleapis.com/"),
44		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
45		internaloption.EnableJwtWithScope(),
46		option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
47		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
48			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
49	}
50}
51
52func defaultPublisherCallOptions() *PublisherCallOptions {
53	return &PublisherCallOptions{
54		Publish: []gax.CallOption{},
55	}
56}
57
58// internalPublisherClient is an interface that defines the methods availaible from Pub/Sub Lite API.
59type internalPublisherClient interface {
60	Close() error
61	setGoogleClientInfo(...string)
62	Connection() *grpc.ClientConn
63	Publish(context.Context, ...gax.CallOption) (pubsublitepb.PublisherService_PublishClient, error)
64}
65
66// PublisherClient is a client for interacting with Pub/Sub Lite API.
67// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
68//
69// The service that a publisher client application uses to publish messages to
70// topics. Published messages are retained by the service for the duration of
71// the retention period configured for the respective topic, and are delivered
72// to subscriber clients upon request (via the SubscriberService).
73type PublisherClient struct {
74	// The internal transport-dependent client.
75	internalClient internalPublisherClient
76
77	// The call options for this service.
78	CallOptions *PublisherCallOptions
79}
80
81// Wrapper methods routed to the internal client.
82
83// Close closes the connection to the API service. The user should invoke this when
84// the client is no longer required.
85func (c *PublisherClient) Close() error {
86	return c.internalClient.Close()
87}
88
89// setGoogleClientInfo sets the name and version of the application in
90// the `x-goog-api-client` header passed on each request. Intended for
91// use by Google-written clients.
92func (c *PublisherClient) setGoogleClientInfo(keyval ...string) {
93	c.internalClient.setGoogleClientInfo(keyval...)
94}
95
96// Connection returns a connection to the API service.
97//
98// Deprecated.
99func (c *PublisherClient) Connection() *grpc.ClientConn {
100	return c.internalClient.Connection()
101}
102
103// Publish establishes a stream with the server for publishing messages. Once the
104// stream is initialized, the client publishes messages by sending publish
105// requests on the stream. The server responds with a PublishResponse for each
106// PublishRequest sent by the client, in the same order that the requests
107// were sent. Note that multiple PublishRequests can be in flight
108// simultaneously, but they will be processed by the server in the order that
109// they are sent by the client on a given stream.
110func (c *PublisherClient) Publish(ctx context.Context, opts ...gax.CallOption) (pubsublitepb.PublisherService_PublishClient, error) {
111	return c.internalClient.Publish(ctx, opts...)
112}
113
114// publisherGRPCClient is a client for interacting with Pub/Sub Lite API over gRPC transport.
115//
116// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
117type publisherGRPCClient struct {
118	// Connection pool of gRPC connections to the service.
119	connPool gtransport.ConnPool
120
121	// flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE
122	disableDeadlines bool
123
124	// Points back to the CallOptions field of the containing PublisherClient
125	CallOptions **PublisherCallOptions
126
127	// The gRPC API client.
128	publisherClient pubsublitepb.PublisherServiceClient
129
130	// The x-goog-* metadata to be sent with each request.
131	xGoogMetadata metadata.MD
132}
133
134// NewPublisherClient creates a new publisher service client based on gRPC.
135// The returned client must be Closed when it is done being used to clean up its underlying connections.
136//
137// The service that a publisher client application uses to publish messages to
138// topics. Published messages are retained by the service for the duration of
139// the retention period configured for the respective topic, and are delivered
140// to subscriber clients upon request (via the SubscriberService).
141func NewPublisherClient(ctx context.Context, opts ...option.ClientOption) (*PublisherClient, error) {
142	clientOpts := defaultPublisherGRPCClientOptions()
143	if newPublisherClientHook != nil {
144		hookOpts, err := newPublisherClientHook(ctx, clientHookParams{})
145		if err != nil {
146			return nil, err
147		}
148		clientOpts = append(clientOpts, hookOpts...)
149	}
150
151	disableDeadlines, err := checkDisableDeadlines()
152	if err != nil {
153		return nil, err
154	}
155
156	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
157	if err != nil {
158		return nil, err
159	}
160	client := PublisherClient{CallOptions: defaultPublisherCallOptions()}
161
162	c := &publisherGRPCClient{
163		connPool:         connPool,
164		disableDeadlines: disableDeadlines,
165		publisherClient:  pubsublitepb.NewPublisherServiceClient(connPool),
166		CallOptions:      &client.CallOptions,
167	}
168	c.setGoogleClientInfo()
169
170	client.internalClient = c
171
172	return &client, nil
173}
174
175// Connection returns a connection to the API service.
176//
177// Deprecated.
178func (c *publisherGRPCClient) Connection() *grpc.ClientConn {
179	return c.connPool.Conn()
180}
181
182// setGoogleClientInfo sets the name and version of the application in
183// the `x-goog-api-client` header passed on each request. Intended for
184// use by Google-written clients.
185func (c *publisherGRPCClient) setGoogleClientInfo(keyval ...string) {
186	kv := append([]string{"gl-go", versionGo()}, keyval...)
187	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
188	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
189}
190
191// Close closes the connection to the API service. The user should invoke this when
192// the client is no longer required.
193func (c *publisherGRPCClient) Close() error {
194	return c.connPool.Close()
195}
196
197func (c *publisherGRPCClient) Publish(ctx context.Context, opts ...gax.CallOption) (pubsublitepb.PublisherService_PublishClient, error) {
198	ctx = insertMetadata(ctx, c.xGoogMetadata)
199	var resp pubsublitepb.PublisherService_PublishClient
200	opts = append((*c.CallOptions).Publish[0:len((*c.CallOptions).Publish):len((*c.CallOptions).Publish)], opts...)
201	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
202		var err error
203		resp, err = c.publisherClient.Publish(ctx, settings.GRPC...)
204		return err
205	}, opts...)
206	if err != nil {
207		return nil, err
208	}
209	return resp, nil
210}
211