You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

401 lines
14 KiB

  1. // Copyright 2018 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. // AUTO-GENERATED CODE. DO NOT EDIT.
  15. package pubsub
  16. import (
  17. "math"
  18. "time"
  19. "cloud.google.com/go/iam"
  20. "cloud.google.com/go/internal/version"
  21. gax "github.com/googleapis/gax-go"
  22. "golang.org/x/net/context"
  23. "google.golang.org/api/iterator"
  24. "google.golang.org/api/option"
  25. "google.golang.org/api/transport"
  26. pubsubpb "google.golang.org/genproto/googleapis/pubsub/v1"
  27. "google.golang.org/grpc"
  28. "google.golang.org/grpc/codes"
  29. "google.golang.org/grpc/metadata"
  30. )
  31. // PublisherCallOptions contains the retry settings for each method of PublisherClient.
  32. type PublisherCallOptions struct {
  33. CreateTopic []gax.CallOption
  34. UpdateTopic []gax.CallOption
  35. Publish []gax.CallOption
  36. GetTopic []gax.CallOption
  37. ListTopics []gax.CallOption
  38. ListTopicSubscriptions []gax.CallOption
  39. DeleteTopic []gax.CallOption
  40. }
  41. func defaultPublisherClientOptions() []option.ClientOption {
  42. return []option.ClientOption{
  43. option.WithEndpoint("pubsub.googleapis.com:443"),
  44. option.WithScopes(DefaultAuthScopes()...),
  45. }
  46. }
  47. func defaultPublisherCallOptions() *PublisherCallOptions {
  48. retry := map[[2]string][]gax.CallOption{
  49. {"default", "idempotent"}: {
  50. gax.WithRetry(func() gax.Retryer {
  51. return gax.OnCodes([]codes.Code{
  52. codes.DeadlineExceeded,
  53. codes.Unavailable,
  54. }, gax.Backoff{
  55. Initial: 100 * time.Millisecond,
  56. Max: 60000 * time.Millisecond,
  57. Multiplier: 1.3,
  58. })
  59. }),
  60. },
  61. {"messaging", "one_plus_delivery"}: {
  62. gax.WithRetry(func() gax.Retryer {
  63. return gax.OnCodes([]codes.Code{
  64. codes.Aborted,
  65. codes.Canceled,
  66. codes.DeadlineExceeded,
  67. codes.Internal,
  68. codes.ResourceExhausted,
  69. codes.Unavailable,
  70. codes.Unknown,
  71. }, gax.Backoff{
  72. Initial: 100 * time.Millisecond,
  73. Max: 60000 * time.Millisecond,
  74. Multiplier: 1.3,
  75. })
  76. }),
  77. },
  78. }
  79. return &PublisherCallOptions{
  80. CreateTopic: retry[[2]string{"default", "idempotent"}],
  81. UpdateTopic: retry[[2]string{"default", "idempotent"}],
  82. Publish: retry[[2]string{"messaging", "one_plus_delivery"}],
  83. GetTopic: retry[[2]string{"default", "idempotent"}],
  84. ListTopics: retry[[2]string{"default", "idempotent"}],
  85. ListTopicSubscriptions: retry[[2]string{"default", "idempotent"}],
  86. DeleteTopic: retry[[2]string{"default", "idempotent"}],
  87. }
  88. }
  89. // PublisherClient is a client for interacting with Google Cloud Pub/Sub API.
  90. //
  91. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  92. type PublisherClient struct {
  93. // The connection to the service.
  94. conn *grpc.ClientConn
  95. // The gRPC API client.
  96. publisherClient pubsubpb.PublisherClient
  97. // The call options for this service.
  98. CallOptions *PublisherCallOptions
  99. // The x-goog-* metadata to be sent with each request.
  100. xGoogMetadata metadata.MD
  101. }
  102. // NewPublisherClient creates a new publisher client.
  103. //
  104. // The service that an application uses to manipulate topics, and to send
  105. // messages to a topic.
  106. func NewPublisherClient(ctx context.Context, opts ...option.ClientOption) (*PublisherClient, error) {
  107. conn, err := transport.DialGRPC(ctx, append(defaultPublisherClientOptions(), opts...)...)
  108. if err != nil {
  109. return nil, err
  110. }
  111. c := &PublisherClient{
  112. conn: conn,
  113. CallOptions: defaultPublisherCallOptions(),
  114. publisherClient: pubsubpb.NewPublisherClient(conn),
  115. }
  116. c.SetGoogleClientInfo()
  117. return c, nil
  118. }
  119. // Connection returns the client's connection to the API service.
  120. func (c *PublisherClient) Connection() *grpc.ClientConn {
  121. return c.conn
  122. }
  123. // Close closes the connection to the API service. The user should invoke this when
  124. // the client is no longer required.
  125. func (c *PublisherClient) Close() error {
  126. return c.conn.Close()
  127. }
  128. // SetGoogleClientInfo sets the name and version of the application in
  129. // the `x-goog-api-client` header passed on each request. Intended for
  130. // use by Google-written clients.
  131. func (c *PublisherClient) SetGoogleClientInfo(keyval ...string) {
  132. kv := append([]string{"gl-go", version.Go()}, keyval...)
  133. kv = append(kv, "gapic", version.Repo, "gax", gax.Version, "grpc", grpc.Version)
  134. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  135. }
  136. func (c *PublisherClient) SubscriptionIAM(subscription *pubsubpb.Subscription) *iam.Handle {
  137. return iam.InternalNewHandle(c.Connection(), subscription.Name)
  138. }
  139. func (c *PublisherClient) TopicIAM(topic *pubsubpb.Topic) *iam.Handle {
  140. return iam.InternalNewHandle(c.Connection(), topic.Name)
  141. }
  142. // CreateTopic creates the given topic with the given name.
  143. func (c *PublisherClient) CreateTopic(ctx context.Context, req *pubsubpb.Topic, opts ...gax.CallOption) (*pubsubpb.Topic, error) {
  144. ctx = insertMetadata(ctx, c.xGoogMetadata)
  145. opts = append(c.CallOptions.CreateTopic[0:len(c.CallOptions.CreateTopic):len(c.CallOptions.CreateTopic)], opts...)
  146. var resp *pubsubpb.Topic
  147. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  148. var err error
  149. resp, err = c.publisherClient.CreateTopic(ctx, req, settings.GRPC...)
  150. return err
  151. }, opts...)
  152. if err != nil {
  153. return nil, err
  154. }
  155. return resp, nil
  156. }
  157. // UpdateTopic updates an existing topic. Note that certain properties of a topic are not
  158. // modifiable. Options settings follow the style guide:
  159. // NOTE: The style guide requires body: "topic" instead of body: "*".
  160. // Keeping the latter for internal consistency in V1, however it should be
  161. // corrected in V2. See
  162. // https://cloud.google.com/apis/design/standard_methods#update for details.
  163. func (c *PublisherClient) UpdateTopic(ctx context.Context, req *pubsubpb.UpdateTopicRequest, opts ...gax.CallOption) (*pubsubpb.Topic, error) {
  164. ctx = insertMetadata(ctx, c.xGoogMetadata)
  165. opts = append(c.CallOptions.UpdateTopic[0:len(c.CallOptions.UpdateTopic):len(c.CallOptions.UpdateTopic)], opts...)
  166. var resp *pubsubpb.Topic
  167. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  168. var err error
  169. resp, err = c.publisherClient.UpdateTopic(ctx, req, settings.GRPC...)
  170. return err
  171. }, opts...)
  172. if err != nil {
  173. return nil, err
  174. }
  175. return resp, nil
  176. }
  177. // Publish adds one or more messages to the topic. Returns NOT_FOUND if the topic
  178. // does not exist. The message payload must not be empty; it must contain
  179. // either a non-empty data field, or at least one attribute.
  180. func (c *PublisherClient) Publish(ctx context.Context, req *pubsubpb.PublishRequest, opts ...gax.CallOption) (*pubsubpb.PublishResponse, error) {
  181. ctx = insertMetadata(ctx, c.xGoogMetadata)
  182. opts = append(c.CallOptions.Publish[0:len(c.CallOptions.Publish):len(c.CallOptions.Publish)], opts...)
  183. var resp *pubsubpb.PublishResponse
  184. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  185. var err error
  186. resp, err = c.publisherClient.Publish(ctx, req, settings.GRPC...)
  187. return err
  188. }, opts...)
  189. if err != nil {
  190. return nil, err
  191. }
  192. return resp, nil
  193. }
  194. // GetTopic gets the configuration of a topic.
  195. func (c *PublisherClient) GetTopic(ctx context.Context, req *pubsubpb.GetTopicRequest, opts ...gax.CallOption) (*pubsubpb.Topic, error) {
  196. ctx = insertMetadata(ctx, c.xGoogMetadata)
  197. opts = append(c.CallOptions.GetTopic[0:len(c.CallOptions.GetTopic):len(c.CallOptions.GetTopic)], opts...)
  198. var resp *pubsubpb.Topic
  199. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  200. var err error
  201. resp, err = c.publisherClient.GetTopic(ctx, req, settings.GRPC...)
  202. return err
  203. }, opts...)
  204. if err != nil {
  205. return nil, err
  206. }
  207. return resp, nil
  208. }
  209. // ListTopics lists matching topics.
  210. func (c *PublisherClient) ListTopics(ctx context.Context, req *pubsubpb.ListTopicsRequest, opts ...gax.CallOption) *TopicIterator {
  211. ctx = insertMetadata(ctx, c.xGoogMetadata)
  212. opts = append(c.CallOptions.ListTopics[0:len(c.CallOptions.ListTopics):len(c.CallOptions.ListTopics)], opts...)
  213. it := &TopicIterator{}
  214. it.InternalFetch = func(pageSize int, pageToken string) ([]*pubsubpb.Topic, string, error) {
  215. var resp *pubsubpb.ListTopicsResponse
  216. req.PageToken = pageToken
  217. if pageSize > math.MaxInt32 {
  218. req.PageSize = math.MaxInt32
  219. } else {
  220. req.PageSize = int32(pageSize)
  221. }
  222. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  223. var err error
  224. resp, err = c.publisherClient.ListTopics(ctx, req, settings.GRPC...)
  225. return err
  226. }, opts...)
  227. if err != nil {
  228. return nil, "", err
  229. }
  230. return resp.Topics, resp.NextPageToken, nil
  231. }
  232. fetch := func(pageSize int, pageToken string) (string, error) {
  233. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  234. if err != nil {
  235. return "", err
  236. }
  237. it.items = append(it.items, items...)
  238. return nextPageToken, nil
  239. }
  240. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  241. return it
  242. }
  243. // ListTopicSubscriptions lists the name of the subscriptions for this topic.
  244. func (c *PublisherClient) ListTopicSubscriptions(ctx context.Context, req *pubsubpb.ListTopicSubscriptionsRequest, opts ...gax.CallOption) *StringIterator {
  245. ctx = insertMetadata(ctx, c.xGoogMetadata)
  246. opts = append(c.CallOptions.ListTopicSubscriptions[0:len(c.CallOptions.ListTopicSubscriptions):len(c.CallOptions.ListTopicSubscriptions)], opts...)
  247. it := &StringIterator{}
  248. it.InternalFetch = func(pageSize int, pageToken string) ([]string, string, error) {
  249. var resp *pubsubpb.ListTopicSubscriptionsResponse
  250. req.PageToken = pageToken
  251. if pageSize > math.MaxInt32 {
  252. req.PageSize = math.MaxInt32
  253. } else {
  254. req.PageSize = int32(pageSize)
  255. }
  256. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  257. var err error
  258. resp, err = c.publisherClient.ListTopicSubscriptions(ctx, req, settings.GRPC...)
  259. return err
  260. }, opts...)
  261. if err != nil {
  262. return nil, "", err
  263. }
  264. return resp.Subscriptions, resp.NextPageToken, nil
  265. }
  266. fetch := func(pageSize int, pageToken string) (string, error) {
  267. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  268. if err != nil {
  269. return "", err
  270. }
  271. it.items = append(it.items, items...)
  272. return nextPageToken, nil
  273. }
  274. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  275. return it
  276. }
  277. // DeleteTopic deletes the topic with the given name. Returns NOT_FOUND if the topic
  278. // does not exist. After a topic is deleted, a new topic may be created with
  279. // the same name; this is an entirely new topic with none of the old
  280. // configuration or subscriptions. Existing subscriptions to this topic are
  281. // not deleted, but their topic field is set to _deleted-topic_.
  282. func (c *PublisherClient) DeleteTopic(ctx context.Context, req *pubsubpb.DeleteTopicRequest, opts ...gax.CallOption) error {
  283. ctx = insertMetadata(ctx, c.xGoogMetadata)
  284. opts = append(c.CallOptions.DeleteTopic[0:len(c.CallOptions.DeleteTopic):len(c.CallOptions.DeleteTopic)], opts...)
  285. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  286. var err error
  287. _, err = c.publisherClient.DeleteTopic(ctx, req, settings.GRPC...)
  288. return err
  289. }, opts...)
  290. return err
  291. }
  292. // StringIterator manages a stream of string.
  293. type StringIterator struct {
  294. items []string
  295. pageInfo *iterator.PageInfo
  296. nextFunc func() error
  297. // InternalFetch is for use by the Google Cloud Libraries only.
  298. // It is not part of the stable interface of this package.
  299. //
  300. // InternalFetch returns results from a single call to the underlying RPC.
  301. // The number of results is no greater than pageSize.
  302. // If there are no more results, nextPageToken is empty and err is nil.
  303. InternalFetch func(pageSize int, pageToken string) (results []string, nextPageToken string, err error)
  304. }
  305. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  306. func (it *StringIterator) PageInfo() *iterator.PageInfo {
  307. return it.pageInfo
  308. }
  309. // Next returns the next result. Its second return value is iterator.Done if there are no more
  310. // results. Once Next returns Done, all subsequent calls will return Done.
  311. func (it *StringIterator) Next() (string, error) {
  312. var item string
  313. if err := it.nextFunc(); err != nil {
  314. return item, err
  315. }
  316. item = it.items[0]
  317. it.items = it.items[1:]
  318. return item, nil
  319. }
  320. func (it *StringIterator) bufLen() int {
  321. return len(it.items)
  322. }
  323. func (it *StringIterator) takeBuf() interface{} {
  324. b := it.items
  325. it.items = nil
  326. return b
  327. }
  328. // TopicIterator manages a stream of *pubsubpb.Topic.
  329. type TopicIterator struct {
  330. items []*pubsubpb.Topic
  331. pageInfo *iterator.PageInfo
  332. nextFunc func() error
  333. // InternalFetch is for use by the Google Cloud Libraries only.
  334. // It is not part of the stable interface of this package.
  335. //
  336. // InternalFetch returns results from a single call to the underlying RPC.
  337. // The number of results is no greater than pageSize.
  338. // If there are no more results, nextPageToken is empty and err is nil.
  339. InternalFetch func(pageSize int, pageToken string) (results []*pubsubpb.Topic, nextPageToken string, err error)
  340. }
  341. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  342. func (it *TopicIterator) PageInfo() *iterator.PageInfo {
  343. return it.pageInfo
  344. }
  345. // Next returns the next result. Its second return value is iterator.Done if there are no more
  346. // results. Once Next returns Done, all subsequent calls will return Done.
  347. func (it *TopicIterator) Next() (*pubsubpb.Topic, error) {
  348. var item *pubsubpb.Topic
  349. if err := it.nextFunc(); err != nil {
  350. return item, err
  351. }
  352. item = it.items[0]
  353. it.items = it.items[1:]
  354. return item, nil
  355. }
  356. func (it *TopicIterator) bufLen() int {
  357. return len(it.items)
  358. }
  359. func (it *TopicIterator) takeBuf() interface{} {
  360. b := it.items
  361. it.items = nil
  362. return b
  363. }