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 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 defaultPublisherClientOptions() []option.ClientOption {
40	return []option.ClientOption{
41		internaloption.WithDefaultEndpoint("pubsublite.googleapis.com:443"),
42		internaloption.WithDefaultMTLSEndpoint("pubsublite.mtls.googleapis.com:443"),
43		option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
44		option.WithScopes(DefaultAuthScopes()...),
45		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
46			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
47	}
48}
49
50func defaultPublisherCallOptions() *PublisherCallOptions {
51	return &PublisherCallOptions{
52		Publish: []gax.CallOption{},
53	}
54}
55
56// PublisherClient is a client for interacting with .
57//
58// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
59type PublisherClient struct {
60	// Connection pool of gRPC connections to the service.
61	connPool gtransport.ConnPool
62
63	// flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE
64	disableDeadlines bool
65
66	// The gRPC API client.
67	publisherClient pubsublitepb.PublisherServiceClient
68
69	// The call options for this service.
70	CallOptions *PublisherCallOptions
71
72	// The x-goog-* metadata to be sent with each request.
73	xGoogMetadata metadata.MD
74}
75
76// NewPublisherClient creates a new publisher service client.
77//
78// The service that a publisher client application uses to publish messages to
79// topics. Published messages are retained by the service for the duration of
80// the retention period configured for the respective topic, and are delivered
81// to subscriber clients upon request (via the SubscriberService).
82func NewPublisherClient(ctx context.Context, opts ...option.ClientOption) (*PublisherClient, error) {
83	clientOpts := defaultPublisherClientOptions()
84
85	if newPublisherClientHook != nil {
86		hookOpts, err := newPublisherClientHook(ctx, clientHookParams{})
87		if err != nil {
88			return nil, err
89		}
90		clientOpts = append(clientOpts, hookOpts...)
91	}
92
93	disableDeadlines, err := checkDisableDeadlines()
94	if err != nil {
95		return nil, err
96	}
97
98	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
99	if err != nil {
100		return nil, err
101	}
102	c := &PublisherClient{
103		connPool:         connPool,
104		disableDeadlines: disableDeadlines,
105		CallOptions:      defaultPublisherCallOptions(),
106
107		publisherClient: pubsublitepb.NewPublisherServiceClient(connPool),
108	}
109	c.setGoogleClientInfo()
110
111	return c, nil
112}
113
114// Connection returns a connection to the API service.
115//
116// Deprecated.
117func (c *PublisherClient) Connection() *grpc.ClientConn {
118	return c.connPool.Conn()
119}
120
121// Close closes the connection to the API service. The user should invoke this when
122// the client is no longer required.
123func (c *PublisherClient) Close() error {
124	return c.connPool.Close()
125}
126
127// setGoogleClientInfo sets the name and version of the application in
128// the `x-goog-api-client` header passed on each request. Intended for
129// use by Google-written clients.
130func (c *PublisherClient) setGoogleClientInfo(keyval ...string) {
131	kv := append([]string{"gl-go", versionGo()}, keyval...)
132	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
133	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
134}
135
136// Publish establishes a stream with the server for publishing messages. Once the
137// stream is initialized, the client publishes messages by sending publish
138// requests on the stream. The server responds with a PublishResponse for each
139// PublishRequest sent by the client, in the same order that the requests
140// were sent. Note that multiple PublishRequests can be in flight
141// simultaneously, but they will be processed by the server in the order that
142// they are sent by the client on a given stream.
143func (c *PublisherClient) Publish(ctx context.Context, opts ...gax.CallOption) (pubsublitepb.PublisherService_PublishClient, error) {
144	ctx = insertMetadata(ctx, c.xGoogMetadata)
145	opts = append(c.CallOptions.Publish[0:len(c.CallOptions.Publish):len(c.CallOptions.Publish)], opts...)
146	var resp pubsublitepb.PublisherService_PublishClient
147	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
148		var err error
149		resp, err = c.publisherClient.Publish(ctx, settings.GRPC...)
150		return err
151	}, opts...)
152	if err != nil {
153		return nil, err
154	}
155	return resp, nil
156}
157