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.
 
 
 

504 line
18 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. // Code generated by gapic-generator. DO NOT EDIT.
  15. package firestore
  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. firestorepb "google.golang.org/genproto/googleapis/firestore/v1"
  26. "google.golang.org/grpc"
  27. "google.golang.org/grpc/codes"
  28. "google.golang.org/grpc/metadata"
  29. )
  30. // CallOptions contains the retry settings for each method of Client.
  31. type CallOptions struct {
  32. GetDocument []gax.CallOption
  33. ListDocuments []gax.CallOption
  34. CreateDocument []gax.CallOption
  35. UpdateDocument []gax.CallOption
  36. DeleteDocument []gax.CallOption
  37. BatchGetDocuments []gax.CallOption
  38. BeginTransaction []gax.CallOption
  39. Commit []gax.CallOption
  40. Rollback []gax.CallOption
  41. RunQuery []gax.CallOption
  42. Write []gax.CallOption
  43. Listen []gax.CallOption
  44. ListCollectionIds []gax.CallOption
  45. }
  46. func defaultClientOptions() []option.ClientOption {
  47. return []option.ClientOption{
  48. option.WithEndpoint("firestore.googleapis.com:443"),
  49. option.WithScopes(DefaultAuthScopes()...),
  50. }
  51. }
  52. func defaultCallOptions() *CallOptions {
  53. retry := map[[2]string][]gax.CallOption{
  54. {"default", "idempotent"}: {
  55. gax.WithRetry(func() gax.Retryer {
  56. return gax.OnCodes([]codes.Code{
  57. codes.DeadlineExceeded,
  58. codes.Unavailable,
  59. }, gax.Backoff{
  60. Initial: 100 * time.Millisecond,
  61. Max: 60000 * time.Millisecond,
  62. Multiplier: 1.3,
  63. })
  64. }),
  65. },
  66. {"streaming", "idempotent"}: {
  67. gax.WithRetry(func() gax.Retryer {
  68. return gax.OnCodes([]codes.Code{
  69. codes.DeadlineExceeded,
  70. codes.Unavailable,
  71. }, gax.Backoff{
  72. Initial: 100 * time.Millisecond,
  73. Max: 60000 * time.Millisecond,
  74. Multiplier: 1.3,
  75. })
  76. }),
  77. },
  78. }
  79. return &CallOptions{
  80. GetDocument: retry[[2]string{"default", "idempotent"}],
  81. ListDocuments: retry[[2]string{"default", "idempotent"}],
  82. CreateDocument: retry[[2]string{"default", "non_idempotent"}],
  83. UpdateDocument: retry[[2]string{"default", "non_idempotent"}],
  84. DeleteDocument: retry[[2]string{"default", "idempotent"}],
  85. BatchGetDocuments: retry[[2]string{"streaming", "idempotent"}],
  86. BeginTransaction: retry[[2]string{"default", "idempotent"}],
  87. Commit: retry[[2]string{"default", "non_idempotent"}],
  88. Rollback: retry[[2]string{"default", "idempotent"}],
  89. RunQuery: retry[[2]string{"streaming", "idempotent"}],
  90. Write: retry[[2]string{"streaming", "non_idempotent"}],
  91. Listen: retry[[2]string{"streaming", "idempotent"}],
  92. ListCollectionIds: retry[[2]string{"default", "idempotent"}],
  93. }
  94. }
  95. // Client is a client for interacting with Google Cloud Firestore API.
  96. //
  97. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  98. type Client struct {
  99. // The connection to the service.
  100. conn *grpc.ClientConn
  101. // The gRPC API client.
  102. client firestorepb.FirestoreClient
  103. // The call options for this service.
  104. CallOptions *CallOptions
  105. // The x-goog-* metadata to be sent with each request.
  106. xGoogMetadata metadata.MD
  107. }
  108. // NewClient creates a new firestore client.
  109. //
  110. // The Cloud Firestore service.
  111. //
  112. // This service exposes several types of comparable timestamps:
  113. //
  114. // create_time - The time at which a document was created. Changes only
  115. // when a document is deleted, then re-created. Increases in a strict
  116. // monotonic fashion.
  117. //
  118. // update_time - The time at which a document was last updated. Changes
  119. // every time a document is modified. Does not change when a write results
  120. // in no modifications. Increases in a strict monotonic fashion.
  121. //
  122. // read_time - The time at which a particular state was observed. Used
  123. // to denote a consistent snapshot of the database or the time at which a
  124. // Document was observed to not exist.
  125. //
  126. // commit_time - The time at which the writes in a transaction were
  127. // committed. Any read with an equal or greater read_time is guaranteed
  128. // to see the effects of the transaction.
  129. func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
  130. conn, err := transport.DialGRPC(ctx, append(defaultClientOptions(), opts...)...)
  131. if err != nil {
  132. return nil, err
  133. }
  134. c := &Client{
  135. conn: conn,
  136. CallOptions: defaultCallOptions(),
  137. client: firestorepb.NewFirestoreClient(conn),
  138. }
  139. c.SetGoogleClientInfo()
  140. return c, nil
  141. }
  142. // Connection returns the client's connection to the API service.
  143. func (c *Client) Connection() *grpc.ClientConn {
  144. return c.conn
  145. }
  146. // Close closes the connection to the API service. The user should invoke this when
  147. // the client is no longer required.
  148. func (c *Client) Close() error {
  149. return c.conn.Close()
  150. }
  151. // SetGoogleClientInfo sets the name and version of the application in
  152. // the `x-goog-api-client` header passed on each request. Intended for
  153. // use by Google-written clients.
  154. func (c *Client) SetGoogleClientInfo(keyval ...string) {
  155. kv := append([]string{"gl-go", versionGo()}, keyval...)
  156. kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
  157. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  158. }
  159. // GetDocument gets a single document.
  160. func (c *Client) GetDocument(ctx context.Context, req *firestorepb.GetDocumentRequest, opts ...gax.CallOption) (*firestorepb.Document, error) {
  161. ctx = insertMetadata(ctx, c.xGoogMetadata)
  162. opts = append(c.CallOptions.GetDocument[0:len(c.CallOptions.GetDocument):len(c.CallOptions.GetDocument)], opts...)
  163. var resp *firestorepb.Document
  164. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  165. var err error
  166. resp, err = c.client.GetDocument(ctx, req, settings.GRPC...)
  167. return err
  168. }, opts...)
  169. if err != nil {
  170. return nil, err
  171. }
  172. return resp, nil
  173. }
  174. // ListDocuments lists documents.
  175. func (c *Client) ListDocuments(ctx context.Context, req *firestorepb.ListDocumentsRequest, opts ...gax.CallOption) *DocumentIterator {
  176. ctx = insertMetadata(ctx, c.xGoogMetadata)
  177. opts = append(c.CallOptions.ListDocuments[0:len(c.CallOptions.ListDocuments):len(c.CallOptions.ListDocuments)], opts...)
  178. it := &DocumentIterator{}
  179. req = proto.Clone(req).(*firestorepb.ListDocumentsRequest)
  180. it.InternalFetch = func(pageSize int, pageToken string) ([]*firestorepb.Document, string, error) {
  181. var resp *firestorepb.ListDocumentsResponse
  182. req.PageToken = pageToken
  183. if pageSize > math.MaxInt32 {
  184. req.PageSize = math.MaxInt32
  185. } else {
  186. req.PageSize = int32(pageSize)
  187. }
  188. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  189. var err error
  190. resp, err = c.client.ListDocuments(ctx, req, settings.GRPC...)
  191. return err
  192. }, opts...)
  193. if err != nil {
  194. return nil, "", err
  195. }
  196. return resp.Documents, resp.NextPageToken, nil
  197. }
  198. fetch := func(pageSize int, pageToken string) (string, error) {
  199. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  200. if err != nil {
  201. return "", err
  202. }
  203. it.items = append(it.items, items...)
  204. return nextPageToken, nil
  205. }
  206. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  207. it.pageInfo.MaxSize = int(req.PageSize)
  208. return it
  209. }
  210. // CreateDocument creates a new document.
  211. func (c *Client) CreateDocument(ctx context.Context, req *firestorepb.CreateDocumentRequest, opts ...gax.CallOption) (*firestorepb.Document, error) {
  212. ctx = insertMetadata(ctx, c.xGoogMetadata)
  213. opts = append(c.CallOptions.CreateDocument[0:len(c.CallOptions.CreateDocument):len(c.CallOptions.CreateDocument)], opts...)
  214. var resp *firestorepb.Document
  215. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  216. var err error
  217. resp, err = c.client.CreateDocument(ctx, req, settings.GRPC...)
  218. return err
  219. }, opts...)
  220. if err != nil {
  221. return nil, err
  222. }
  223. return resp, nil
  224. }
  225. // UpdateDocument updates or inserts a document.
  226. func (c *Client) UpdateDocument(ctx context.Context, req *firestorepb.UpdateDocumentRequest, opts ...gax.CallOption) (*firestorepb.Document, error) {
  227. ctx = insertMetadata(ctx, c.xGoogMetadata)
  228. opts = append(c.CallOptions.UpdateDocument[0:len(c.CallOptions.UpdateDocument):len(c.CallOptions.UpdateDocument)], opts...)
  229. var resp *firestorepb.Document
  230. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  231. var err error
  232. resp, err = c.client.UpdateDocument(ctx, req, settings.GRPC...)
  233. return err
  234. }, opts...)
  235. if err != nil {
  236. return nil, err
  237. }
  238. return resp, nil
  239. }
  240. // DeleteDocument deletes a document.
  241. func (c *Client) DeleteDocument(ctx context.Context, req *firestorepb.DeleteDocumentRequest, opts ...gax.CallOption) error {
  242. ctx = insertMetadata(ctx, c.xGoogMetadata)
  243. opts = append(c.CallOptions.DeleteDocument[0:len(c.CallOptions.DeleteDocument):len(c.CallOptions.DeleteDocument)], opts...)
  244. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  245. var err error
  246. _, err = c.client.DeleteDocument(ctx, req, settings.GRPC...)
  247. return err
  248. }, opts...)
  249. return err
  250. }
  251. // BatchGetDocuments gets multiple documents.
  252. //
  253. // Documents returned by this method are not guaranteed to be returned in the
  254. // same order that they were requested.
  255. func (c *Client) BatchGetDocuments(ctx context.Context, req *firestorepb.BatchGetDocumentsRequest, opts ...gax.CallOption) (firestorepb.Firestore_BatchGetDocumentsClient, error) {
  256. ctx = insertMetadata(ctx, c.xGoogMetadata)
  257. opts = append(c.CallOptions.BatchGetDocuments[0:len(c.CallOptions.BatchGetDocuments):len(c.CallOptions.BatchGetDocuments)], opts...)
  258. var resp firestorepb.Firestore_BatchGetDocumentsClient
  259. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  260. var err error
  261. resp, err = c.client.BatchGetDocuments(ctx, req, settings.GRPC...)
  262. return err
  263. }, opts...)
  264. if err != nil {
  265. return nil, err
  266. }
  267. return resp, nil
  268. }
  269. // BeginTransaction starts a new transaction.
  270. func (c *Client) BeginTransaction(ctx context.Context, req *firestorepb.BeginTransactionRequest, opts ...gax.CallOption) (*firestorepb.BeginTransactionResponse, error) {
  271. ctx = insertMetadata(ctx, c.xGoogMetadata)
  272. opts = append(c.CallOptions.BeginTransaction[0:len(c.CallOptions.BeginTransaction):len(c.CallOptions.BeginTransaction)], opts...)
  273. var resp *firestorepb.BeginTransactionResponse
  274. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  275. var err error
  276. resp, err = c.client.BeginTransaction(ctx, req, settings.GRPC...)
  277. return err
  278. }, opts...)
  279. if err != nil {
  280. return nil, err
  281. }
  282. return resp, nil
  283. }
  284. // Commit commits a transaction, while optionally updating documents.
  285. func (c *Client) Commit(ctx context.Context, req *firestorepb.CommitRequest, opts ...gax.CallOption) (*firestorepb.CommitResponse, error) {
  286. ctx = insertMetadata(ctx, c.xGoogMetadata)
  287. opts = append(c.CallOptions.Commit[0:len(c.CallOptions.Commit):len(c.CallOptions.Commit)], opts...)
  288. var resp *firestorepb.CommitResponse
  289. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  290. var err error
  291. resp, err = c.client.Commit(ctx, req, settings.GRPC...)
  292. return err
  293. }, opts...)
  294. if err != nil {
  295. return nil, err
  296. }
  297. return resp, nil
  298. }
  299. // Rollback rolls back a transaction.
  300. func (c *Client) Rollback(ctx context.Context, req *firestorepb.RollbackRequest, opts ...gax.CallOption) error {
  301. ctx = insertMetadata(ctx, c.xGoogMetadata)
  302. opts = append(c.CallOptions.Rollback[0:len(c.CallOptions.Rollback):len(c.CallOptions.Rollback)], opts...)
  303. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  304. var err error
  305. _, err = c.client.Rollback(ctx, req, settings.GRPC...)
  306. return err
  307. }, opts...)
  308. return err
  309. }
  310. // RunQuery runs a query.
  311. func (c *Client) RunQuery(ctx context.Context, req *firestorepb.RunQueryRequest, opts ...gax.CallOption) (firestorepb.Firestore_RunQueryClient, error) {
  312. ctx = insertMetadata(ctx, c.xGoogMetadata)
  313. opts = append(c.CallOptions.RunQuery[0:len(c.CallOptions.RunQuery):len(c.CallOptions.RunQuery)], opts...)
  314. var resp firestorepb.Firestore_RunQueryClient
  315. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  316. var err error
  317. resp, err = c.client.RunQuery(ctx, req, settings.GRPC...)
  318. return err
  319. }, opts...)
  320. if err != nil {
  321. return nil, err
  322. }
  323. return resp, nil
  324. }
  325. // Write streams batches of document updates and deletes, in order.
  326. func (c *Client) Write(ctx context.Context, opts ...gax.CallOption) (firestorepb.Firestore_WriteClient, error) {
  327. ctx = insertMetadata(ctx, c.xGoogMetadata)
  328. opts = append(c.CallOptions.Write[0:len(c.CallOptions.Write):len(c.CallOptions.Write)], opts...)
  329. var resp firestorepb.Firestore_WriteClient
  330. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  331. var err error
  332. resp, err = c.client.Write(ctx, settings.GRPC...)
  333. return err
  334. }, opts...)
  335. if err != nil {
  336. return nil, err
  337. }
  338. return resp, nil
  339. }
  340. // Listen listens to changes.
  341. func (c *Client) Listen(ctx context.Context, opts ...gax.CallOption) (firestorepb.Firestore_ListenClient, error) {
  342. ctx = insertMetadata(ctx, c.xGoogMetadata)
  343. opts = append(c.CallOptions.Listen[0:len(c.CallOptions.Listen):len(c.CallOptions.Listen)], opts...)
  344. var resp firestorepb.Firestore_ListenClient
  345. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  346. var err error
  347. resp, err = c.client.Listen(ctx, settings.GRPC...)
  348. return err
  349. }, opts...)
  350. if err != nil {
  351. return nil, err
  352. }
  353. return resp, nil
  354. }
  355. // ListCollectionIds lists all the collection IDs underneath a document.
  356. func (c *Client) ListCollectionIds(ctx context.Context, req *firestorepb.ListCollectionIdsRequest, opts ...gax.CallOption) *StringIterator {
  357. ctx = insertMetadata(ctx, c.xGoogMetadata)
  358. opts = append(c.CallOptions.ListCollectionIds[0:len(c.CallOptions.ListCollectionIds):len(c.CallOptions.ListCollectionIds)], opts...)
  359. it := &StringIterator{}
  360. req = proto.Clone(req).(*firestorepb.ListCollectionIdsRequest)
  361. it.InternalFetch = func(pageSize int, pageToken string) ([]string, string, error) {
  362. var resp *firestorepb.ListCollectionIdsResponse
  363. req.PageToken = pageToken
  364. if pageSize > math.MaxInt32 {
  365. req.PageSize = math.MaxInt32
  366. } else {
  367. req.PageSize = int32(pageSize)
  368. }
  369. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  370. var err error
  371. resp, err = c.client.ListCollectionIds(ctx, req, settings.GRPC...)
  372. return err
  373. }, opts...)
  374. if err != nil {
  375. return nil, "", err
  376. }
  377. return resp.CollectionIds, resp.NextPageToken, nil
  378. }
  379. fetch := func(pageSize int, pageToken string) (string, error) {
  380. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  381. if err != nil {
  382. return "", err
  383. }
  384. it.items = append(it.items, items...)
  385. return nextPageToken, nil
  386. }
  387. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  388. it.pageInfo.MaxSize = int(req.PageSize)
  389. return it
  390. }
  391. // DocumentIterator manages a stream of *firestorepb.Document.
  392. type DocumentIterator struct {
  393. items []*firestorepb.Document
  394. pageInfo *iterator.PageInfo
  395. nextFunc func() error
  396. // InternalFetch is for use by the Google Cloud Libraries only.
  397. // It is not part of the stable interface of this package.
  398. //
  399. // InternalFetch returns results from a single call to the underlying RPC.
  400. // The number of results is no greater than pageSize.
  401. // If there are no more results, nextPageToken is empty and err is nil.
  402. InternalFetch func(pageSize int, pageToken string) (results []*firestorepb.Document, nextPageToken string, err error)
  403. }
  404. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  405. func (it *DocumentIterator) PageInfo() *iterator.PageInfo {
  406. return it.pageInfo
  407. }
  408. // Next returns the next result. Its second return value is iterator.Done if there are no more
  409. // results. Once Next returns Done, all subsequent calls will return Done.
  410. func (it *DocumentIterator) Next() (*firestorepb.Document, error) {
  411. var item *firestorepb.Document
  412. if err := it.nextFunc(); err != nil {
  413. return item, err
  414. }
  415. item = it.items[0]
  416. it.items = it.items[1:]
  417. return item, nil
  418. }
  419. func (it *DocumentIterator) bufLen() int {
  420. return len(it.items)
  421. }
  422. func (it *DocumentIterator) takeBuf() interface{} {
  423. b := it.items
  424. it.items = nil
  425. return b
  426. }
  427. // StringIterator manages a stream of string.
  428. type StringIterator struct {
  429. items []string
  430. pageInfo *iterator.PageInfo
  431. nextFunc func() error
  432. // InternalFetch is for use by the Google Cloud Libraries only.
  433. // It is not part of the stable interface of this package.
  434. //
  435. // InternalFetch returns results from a single call to the underlying RPC.
  436. // The number of results is no greater than pageSize.
  437. // If there are no more results, nextPageToken is empty and err is nil.
  438. InternalFetch func(pageSize int, pageToken string) (results []string, nextPageToken string, err error)
  439. }
  440. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  441. func (it *StringIterator) PageInfo() *iterator.PageInfo {
  442. return it.pageInfo
  443. }
  444. // Next returns the next result. Its second return value is iterator.Done if there are no more
  445. // results. Once Next returns Done, all subsequent calls will return Done.
  446. func (it *StringIterator) Next() (string, error) {
  447. var item string
  448. if err := it.nextFunc(); err != nil {
  449. return item, err
  450. }
  451. item = it.items[0]
  452. it.items = it.items[1:]
  453. return item, nil
  454. }
  455. func (it *StringIterator) bufLen() int {
  456. return len(it.items)
  457. }
  458. func (it *StringIterator) takeBuf() interface{} {
  459. b := it.items
  460. it.items = nil
  461. return b
  462. }