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.
 
 
 

500 lines
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. // AUTO-GENERATED CODE. DO NOT EDIT.
  15. package firestore
  16. import (
  17. "math"
  18. "time"
  19. "cloud.google.com/go/internal/version"
  20. gax "github.com/googleapis/gax-go"
  21. "golang.org/x/net/context"
  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/v1beta1"
  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{"default", "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", version.Go()}, keyval...)
  156. kv = append(kv, "gapic", version.Repo, "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. it.InternalFetch = func(pageSize int, pageToken string) ([]*firestorepb.Document, string, error) {
  180. var resp *firestorepb.ListDocumentsResponse
  181. req.PageToken = pageToken
  182. if pageSize > math.MaxInt32 {
  183. req.PageSize = math.MaxInt32
  184. } else {
  185. req.PageSize = int32(pageSize)
  186. }
  187. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  188. var err error
  189. resp, err = c.client.ListDocuments(ctx, req, settings.GRPC...)
  190. return err
  191. }, opts...)
  192. if err != nil {
  193. return nil, "", err
  194. }
  195. return resp.Documents, resp.NextPageToken, nil
  196. }
  197. fetch := func(pageSize int, pageToken string) (string, error) {
  198. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  199. if err != nil {
  200. return "", err
  201. }
  202. it.items = append(it.items, items...)
  203. return nextPageToken, nil
  204. }
  205. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  206. return it
  207. }
  208. // CreateDocument creates a new document.
  209. func (c *Client) CreateDocument(ctx context.Context, req *firestorepb.CreateDocumentRequest, opts ...gax.CallOption) (*firestorepb.Document, error) {
  210. ctx = insertMetadata(ctx, c.xGoogMetadata)
  211. opts = append(c.CallOptions.CreateDocument[0:len(c.CallOptions.CreateDocument):len(c.CallOptions.CreateDocument)], opts...)
  212. var resp *firestorepb.Document
  213. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  214. var err error
  215. resp, err = c.client.CreateDocument(ctx, req, settings.GRPC...)
  216. return err
  217. }, opts...)
  218. if err != nil {
  219. return nil, err
  220. }
  221. return resp, nil
  222. }
  223. // UpdateDocument updates or inserts a document.
  224. func (c *Client) UpdateDocument(ctx context.Context, req *firestorepb.UpdateDocumentRequest, opts ...gax.CallOption) (*firestorepb.Document, error) {
  225. ctx = insertMetadata(ctx, c.xGoogMetadata)
  226. opts = append(c.CallOptions.UpdateDocument[0:len(c.CallOptions.UpdateDocument):len(c.CallOptions.UpdateDocument)], opts...)
  227. var resp *firestorepb.Document
  228. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  229. var err error
  230. resp, err = c.client.UpdateDocument(ctx, req, settings.GRPC...)
  231. return err
  232. }, opts...)
  233. if err != nil {
  234. return nil, err
  235. }
  236. return resp, nil
  237. }
  238. // DeleteDocument deletes a document.
  239. func (c *Client) DeleteDocument(ctx context.Context, req *firestorepb.DeleteDocumentRequest, opts ...gax.CallOption) error {
  240. ctx = insertMetadata(ctx, c.xGoogMetadata)
  241. opts = append(c.CallOptions.DeleteDocument[0:len(c.CallOptions.DeleteDocument):len(c.CallOptions.DeleteDocument)], opts...)
  242. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  243. var err error
  244. _, err = c.client.DeleteDocument(ctx, req, settings.GRPC...)
  245. return err
  246. }, opts...)
  247. return err
  248. }
  249. // BatchGetDocuments gets multiple documents.
  250. //
  251. // Documents returned by this method are not guaranteed to be returned in the
  252. // same order that they were requested.
  253. func (c *Client) BatchGetDocuments(ctx context.Context, req *firestorepb.BatchGetDocumentsRequest, opts ...gax.CallOption) (firestorepb.Firestore_BatchGetDocumentsClient, error) {
  254. ctx = insertMetadata(ctx, c.xGoogMetadata)
  255. opts = append(c.CallOptions.BatchGetDocuments[0:len(c.CallOptions.BatchGetDocuments):len(c.CallOptions.BatchGetDocuments)], opts...)
  256. var resp firestorepb.Firestore_BatchGetDocumentsClient
  257. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  258. var err error
  259. resp, err = c.client.BatchGetDocuments(ctx, req, settings.GRPC...)
  260. return err
  261. }, opts...)
  262. if err != nil {
  263. return nil, err
  264. }
  265. return resp, nil
  266. }
  267. // BeginTransaction starts a new transaction.
  268. func (c *Client) BeginTransaction(ctx context.Context, req *firestorepb.BeginTransactionRequest, opts ...gax.CallOption) (*firestorepb.BeginTransactionResponse, error) {
  269. ctx = insertMetadata(ctx, c.xGoogMetadata)
  270. opts = append(c.CallOptions.BeginTransaction[0:len(c.CallOptions.BeginTransaction):len(c.CallOptions.BeginTransaction)], opts...)
  271. var resp *firestorepb.BeginTransactionResponse
  272. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  273. var err error
  274. resp, err = c.client.BeginTransaction(ctx, req, settings.GRPC...)
  275. return err
  276. }, opts...)
  277. if err != nil {
  278. return nil, err
  279. }
  280. return resp, nil
  281. }
  282. // Commit commits a transaction, while optionally updating documents.
  283. func (c *Client) Commit(ctx context.Context, req *firestorepb.CommitRequest, opts ...gax.CallOption) (*firestorepb.CommitResponse, error) {
  284. ctx = insertMetadata(ctx, c.xGoogMetadata)
  285. opts = append(c.CallOptions.Commit[0:len(c.CallOptions.Commit):len(c.CallOptions.Commit)], opts...)
  286. var resp *firestorepb.CommitResponse
  287. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  288. var err error
  289. resp, err = c.client.Commit(ctx, req, settings.GRPC...)
  290. return err
  291. }, opts...)
  292. if err != nil {
  293. return nil, err
  294. }
  295. return resp, nil
  296. }
  297. // Rollback rolls back a transaction.
  298. func (c *Client) Rollback(ctx context.Context, req *firestorepb.RollbackRequest, opts ...gax.CallOption) error {
  299. ctx = insertMetadata(ctx, c.xGoogMetadata)
  300. opts = append(c.CallOptions.Rollback[0:len(c.CallOptions.Rollback):len(c.CallOptions.Rollback)], opts...)
  301. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  302. var err error
  303. _, err = c.client.Rollback(ctx, req, settings.GRPC...)
  304. return err
  305. }, opts...)
  306. return err
  307. }
  308. // RunQuery runs a query.
  309. func (c *Client) RunQuery(ctx context.Context, req *firestorepb.RunQueryRequest, opts ...gax.CallOption) (firestorepb.Firestore_RunQueryClient, error) {
  310. ctx = insertMetadata(ctx, c.xGoogMetadata)
  311. opts = append(c.CallOptions.RunQuery[0:len(c.CallOptions.RunQuery):len(c.CallOptions.RunQuery)], opts...)
  312. var resp firestorepb.Firestore_RunQueryClient
  313. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  314. var err error
  315. resp, err = c.client.RunQuery(ctx, req, settings.GRPC...)
  316. return err
  317. }, opts...)
  318. if err != nil {
  319. return nil, err
  320. }
  321. return resp, nil
  322. }
  323. // Write streams batches of document updates and deletes, in order.
  324. func (c *Client) Write(ctx context.Context, opts ...gax.CallOption) (firestorepb.Firestore_WriteClient, error) {
  325. ctx = insertMetadata(ctx, c.xGoogMetadata)
  326. opts = append(c.CallOptions.Write[0:len(c.CallOptions.Write):len(c.CallOptions.Write)], opts...)
  327. var resp firestorepb.Firestore_WriteClient
  328. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  329. var err error
  330. resp, err = c.client.Write(ctx, settings.GRPC...)
  331. return err
  332. }, opts...)
  333. if err != nil {
  334. return nil, err
  335. }
  336. return resp, nil
  337. }
  338. // Listen listens to changes.
  339. func (c *Client) Listen(ctx context.Context, opts ...gax.CallOption) (firestorepb.Firestore_ListenClient, error) {
  340. ctx = insertMetadata(ctx, c.xGoogMetadata)
  341. opts = append(c.CallOptions.Listen[0:len(c.CallOptions.Listen):len(c.CallOptions.Listen)], opts...)
  342. var resp firestorepb.Firestore_ListenClient
  343. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  344. var err error
  345. resp, err = c.client.Listen(ctx, settings.GRPC...)
  346. return err
  347. }, opts...)
  348. if err != nil {
  349. return nil, err
  350. }
  351. return resp, nil
  352. }
  353. // ListCollectionIds lists all the collection IDs underneath a document.
  354. func (c *Client) ListCollectionIds(ctx context.Context, req *firestorepb.ListCollectionIdsRequest, opts ...gax.CallOption) *StringIterator {
  355. ctx = insertMetadata(ctx, c.xGoogMetadata)
  356. opts = append(c.CallOptions.ListCollectionIds[0:len(c.CallOptions.ListCollectionIds):len(c.CallOptions.ListCollectionIds)], opts...)
  357. it := &StringIterator{}
  358. it.InternalFetch = func(pageSize int, pageToken string) ([]string, string, error) {
  359. var resp *firestorepb.ListCollectionIdsResponse
  360. req.PageToken = pageToken
  361. if pageSize > math.MaxInt32 {
  362. req.PageSize = math.MaxInt32
  363. } else {
  364. req.PageSize = int32(pageSize)
  365. }
  366. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  367. var err error
  368. resp, err = c.client.ListCollectionIds(ctx, req, settings.GRPC...)
  369. return err
  370. }, opts...)
  371. if err != nil {
  372. return nil, "", err
  373. }
  374. return resp.CollectionIds, resp.NextPageToken, nil
  375. }
  376. fetch := func(pageSize int, pageToken string) (string, error) {
  377. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  378. if err != nil {
  379. return "", err
  380. }
  381. it.items = append(it.items, items...)
  382. return nextPageToken, nil
  383. }
  384. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  385. return it
  386. }
  387. // DocumentIterator manages a stream of *firestorepb.Document.
  388. type DocumentIterator struct {
  389. items []*firestorepb.Document
  390. pageInfo *iterator.PageInfo
  391. nextFunc func() error
  392. // InternalFetch is for use by the Google Cloud Libraries only.
  393. // It is not part of the stable interface of this package.
  394. //
  395. // InternalFetch returns results from a single call to the underlying RPC.
  396. // The number of results is no greater than pageSize.
  397. // If there are no more results, nextPageToken is empty and err is nil.
  398. InternalFetch func(pageSize int, pageToken string) (results []*firestorepb.Document, nextPageToken string, err error)
  399. }
  400. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  401. func (it *DocumentIterator) PageInfo() *iterator.PageInfo {
  402. return it.pageInfo
  403. }
  404. // Next returns the next result. Its second return value is iterator.Done if there are no more
  405. // results. Once Next returns Done, all subsequent calls will return Done.
  406. func (it *DocumentIterator) Next() (*firestorepb.Document, error) {
  407. var item *firestorepb.Document
  408. if err := it.nextFunc(); err != nil {
  409. return item, err
  410. }
  411. item = it.items[0]
  412. it.items = it.items[1:]
  413. return item, nil
  414. }
  415. func (it *DocumentIterator) bufLen() int {
  416. return len(it.items)
  417. }
  418. func (it *DocumentIterator) takeBuf() interface{} {
  419. b := it.items
  420. it.items = nil
  421. return b
  422. }
  423. // StringIterator manages a stream of string.
  424. type StringIterator struct {
  425. items []string
  426. pageInfo *iterator.PageInfo
  427. nextFunc func() error
  428. // InternalFetch is for use by the Google Cloud Libraries only.
  429. // It is not part of the stable interface of this package.
  430. //
  431. // InternalFetch returns results from a single call to the underlying RPC.
  432. // The number of results is no greater than pageSize.
  433. // If there are no more results, nextPageToken is empty and err is nil.
  434. InternalFetch func(pageSize int, pageToken string) (results []string, nextPageToken string, err error)
  435. }
  436. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  437. func (it *StringIterator) PageInfo() *iterator.PageInfo {
  438. return it.pageInfo
  439. }
  440. // Next returns the next result. Its second return value is iterator.Done if there are no more
  441. // results. Once Next returns Done, all subsequent calls will return Done.
  442. func (it *StringIterator) Next() (string, error) {
  443. var item string
  444. if err := it.nextFunc(); err != nil {
  445. return item, err
  446. }
  447. item = it.items[0]
  448. it.items = it.items[1:]
  449. return item, nil
  450. }
  451. func (it *StringIterator) bufLen() int {
  452. return len(it.items)
  453. }
  454. func (it *StringIterator) takeBuf() interface{} {
  455. b := it.items
  456. it.items = nil
  457. return b
  458. }