Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

405 lignes
14 KiB

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