Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

878 строки
34 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 vision
  16. import (
  17. "context"
  18. "math"
  19. "time"
  20. "cloud.google.com/go/longrunning"
  21. lroauto "cloud.google.com/go/longrunning/autogen"
  22. "github.com/golang/protobuf/proto"
  23. gax "github.com/googleapis/gax-go/v2"
  24. "google.golang.org/api/iterator"
  25. "google.golang.org/api/option"
  26. "google.golang.org/api/transport"
  27. visionpb "google.golang.org/genproto/googleapis/cloud/vision/v1"
  28. longrunningpb "google.golang.org/genproto/googleapis/longrunning"
  29. "google.golang.org/grpc"
  30. "google.golang.org/grpc/codes"
  31. "google.golang.org/grpc/metadata"
  32. )
  33. // ProductSearchCallOptions contains the retry settings for each method of ProductSearchClient.
  34. type ProductSearchCallOptions struct {
  35. CreateProduct []gax.CallOption
  36. ListProducts []gax.CallOption
  37. GetProduct []gax.CallOption
  38. UpdateProduct []gax.CallOption
  39. DeleteProduct []gax.CallOption
  40. ListReferenceImages []gax.CallOption
  41. GetReferenceImage []gax.CallOption
  42. DeleteReferenceImage []gax.CallOption
  43. CreateReferenceImage []gax.CallOption
  44. CreateProductSet []gax.CallOption
  45. ListProductSets []gax.CallOption
  46. GetProductSet []gax.CallOption
  47. UpdateProductSet []gax.CallOption
  48. DeleteProductSet []gax.CallOption
  49. AddProductToProductSet []gax.CallOption
  50. RemoveProductFromProductSet []gax.CallOption
  51. ListProductsInProductSet []gax.CallOption
  52. ImportProductSets []gax.CallOption
  53. }
  54. func defaultProductSearchClientOptions() []option.ClientOption {
  55. return []option.ClientOption{
  56. option.WithEndpoint("vision.googleapis.com:443"),
  57. option.WithScopes(DefaultAuthScopes()...),
  58. }
  59. }
  60. func defaultProductSearchCallOptions() *ProductSearchCallOptions {
  61. retry := map[[2]string][]gax.CallOption{
  62. {"default", "idempotent"}: {
  63. gax.WithRetry(func() gax.Retryer {
  64. return gax.OnCodes([]codes.Code{
  65. codes.DeadlineExceeded,
  66. codes.Unavailable,
  67. }, gax.Backoff{
  68. Initial: 100 * time.Millisecond,
  69. Max: 60000 * time.Millisecond,
  70. Multiplier: 1.3,
  71. })
  72. }),
  73. },
  74. }
  75. return &ProductSearchCallOptions{
  76. CreateProduct: retry[[2]string{"default", "non_idempotent"}],
  77. ListProducts: retry[[2]string{"default", "idempotent"}],
  78. GetProduct: retry[[2]string{"default", "idempotent"}],
  79. UpdateProduct: retry[[2]string{"default", "idempotent"}],
  80. DeleteProduct: retry[[2]string{"default", "idempotent"}],
  81. ListReferenceImages: retry[[2]string{"default", "idempotent"}],
  82. GetReferenceImage: retry[[2]string{"default", "idempotent"}],
  83. DeleteReferenceImage: retry[[2]string{"default", "idempotent"}],
  84. CreateReferenceImage: retry[[2]string{"default", "non_idempotent"}],
  85. CreateProductSet: retry[[2]string{"default", "non_idempotent"}],
  86. ListProductSets: retry[[2]string{"default", "idempotent"}],
  87. GetProductSet: retry[[2]string{"default", "idempotent"}],
  88. UpdateProductSet: retry[[2]string{"default", "idempotent"}],
  89. DeleteProductSet: retry[[2]string{"default", "idempotent"}],
  90. AddProductToProductSet: retry[[2]string{"default", "idempotent"}],
  91. RemoveProductFromProductSet: retry[[2]string{"default", "idempotent"}],
  92. ListProductsInProductSet: retry[[2]string{"default", "idempotent"}],
  93. ImportProductSets: retry[[2]string{"default", "non_idempotent"}],
  94. }
  95. }
  96. // ProductSearchClient is a client for interacting with Cloud Vision API.
  97. //
  98. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  99. type ProductSearchClient struct {
  100. // The connection to the service.
  101. conn *grpc.ClientConn
  102. // The gRPC API client.
  103. productSearchClient visionpb.ProductSearchClient
  104. // LROClient is used internally to handle longrunning operations.
  105. // It is exposed so that its CallOptions can be modified if required.
  106. // Users should not Close this client.
  107. LROClient *lroauto.OperationsClient
  108. // The call options for this service.
  109. CallOptions *ProductSearchCallOptions
  110. // The x-goog-* metadata to be sent with each request.
  111. xGoogMetadata metadata.MD
  112. }
  113. // NewProductSearchClient creates a new product search client.
  114. //
  115. // Manages Products and ProductSets of reference images for use in product
  116. // search. It uses the following resource model:
  117. //
  118. // The API has a collection of [ProductSet][google.cloud.vision.v1.ProductSet]
  119. // resources, named projects/*/locations/*/productSets/*, which acts as a way
  120. // to put different products into groups to limit identification.
  121. //
  122. // In parallel,
  123. //
  124. // The API has a collection of [Product][google.cloud.vision.v1.Product]
  125. // resources, named
  126. // projects/*/locations/*/products/*
  127. //
  128. // Each [Product][google.cloud.vision.v1.Product] has a collection of
  129. // [ReferenceImage][google.cloud.vision.v1.ReferenceImage] resources, named
  130. // projects/*/locations/*/products/*/referenceImages/*
  131. func NewProductSearchClient(ctx context.Context, opts ...option.ClientOption) (*ProductSearchClient, error) {
  132. conn, err := transport.DialGRPC(ctx, append(defaultProductSearchClientOptions(), opts...)...)
  133. if err != nil {
  134. return nil, err
  135. }
  136. c := &ProductSearchClient{
  137. conn: conn,
  138. CallOptions: defaultProductSearchCallOptions(),
  139. productSearchClient: visionpb.NewProductSearchClient(conn),
  140. }
  141. c.setGoogleClientInfo()
  142. c.LROClient, err = lroauto.NewOperationsClient(ctx, option.WithGRPCConn(conn))
  143. if err != nil {
  144. // This error "should not happen", since we are just reusing old connection
  145. // and never actually need to dial.
  146. // If this does happen, we could leak conn. However, we cannot close conn:
  147. // If the user invoked the function with option.WithGRPCConn,
  148. // we would close a connection that's still in use.
  149. // TODO(pongad): investigate error conditions.
  150. return nil, err
  151. }
  152. return c, nil
  153. }
  154. // Connection returns the client's connection to the API service.
  155. func (c *ProductSearchClient) Connection() *grpc.ClientConn {
  156. return c.conn
  157. }
  158. // Close closes the connection to the API service. The user should invoke this when
  159. // the client is no longer required.
  160. func (c *ProductSearchClient) Close() error {
  161. return c.conn.Close()
  162. }
  163. // setGoogleClientInfo sets the name and version of the application in
  164. // the `x-goog-api-client` header passed on each request. Intended for
  165. // use by Google-written clients.
  166. func (c *ProductSearchClient) setGoogleClientInfo(keyval ...string) {
  167. kv := append([]string{"gl-go", versionGo()}, keyval...)
  168. kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
  169. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  170. }
  171. // CreateProduct creates and returns a new product resource.
  172. //
  173. // Possible errors:
  174. //
  175. // Returns INVALID_ARGUMENT if display_name is missing or longer than 4096
  176. // characters.
  177. //
  178. // Returns INVALID_ARGUMENT if description is longer than 4096 characters.
  179. //
  180. // Returns INVALID_ARGUMENT if product_category is missing or invalid.
  181. func (c *ProductSearchClient) CreateProduct(ctx context.Context, req *visionpb.CreateProductRequest, opts ...gax.CallOption) (*visionpb.Product, error) {
  182. ctx = insertMetadata(ctx, c.xGoogMetadata)
  183. opts = append(c.CallOptions.CreateProduct[0:len(c.CallOptions.CreateProduct):len(c.CallOptions.CreateProduct)], opts...)
  184. var resp *visionpb.Product
  185. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  186. var err error
  187. resp, err = c.productSearchClient.CreateProduct(ctx, req, settings.GRPC...)
  188. return err
  189. }, opts...)
  190. if err != nil {
  191. return nil, err
  192. }
  193. return resp, nil
  194. }
  195. // ListProducts lists products in an unspecified order.
  196. //
  197. // Possible errors:
  198. //
  199. // Returns INVALID_ARGUMENT if page_size is greater than 100 or less than 1.
  200. func (c *ProductSearchClient) ListProducts(ctx context.Context, req *visionpb.ListProductsRequest, opts ...gax.CallOption) *ProductIterator {
  201. ctx = insertMetadata(ctx, c.xGoogMetadata)
  202. opts = append(c.CallOptions.ListProducts[0:len(c.CallOptions.ListProducts):len(c.CallOptions.ListProducts)], opts...)
  203. it := &ProductIterator{}
  204. req = proto.Clone(req).(*visionpb.ListProductsRequest)
  205. it.InternalFetch = func(pageSize int, pageToken string) ([]*visionpb.Product, string, error) {
  206. var resp *visionpb.ListProductsResponse
  207. req.PageToken = pageToken
  208. if pageSize > math.MaxInt32 {
  209. req.PageSize = math.MaxInt32
  210. } else {
  211. req.PageSize = int32(pageSize)
  212. }
  213. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  214. var err error
  215. resp, err = c.productSearchClient.ListProducts(ctx, req, settings.GRPC...)
  216. return err
  217. }, opts...)
  218. if err != nil {
  219. return nil, "", err
  220. }
  221. return resp.Products, resp.NextPageToken, nil
  222. }
  223. fetch := func(pageSize int, pageToken string) (string, error) {
  224. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  225. if err != nil {
  226. return "", err
  227. }
  228. it.items = append(it.items, items...)
  229. return nextPageToken, nil
  230. }
  231. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  232. it.pageInfo.MaxSize = int(req.PageSize)
  233. return it
  234. }
  235. // GetProduct gets information associated with a Product.
  236. //
  237. // Possible errors:
  238. //
  239. // Returns NOT_FOUND if the Product does not exist.
  240. func (c *ProductSearchClient) GetProduct(ctx context.Context, req *visionpb.GetProductRequest, opts ...gax.CallOption) (*visionpb.Product, error) {
  241. ctx = insertMetadata(ctx, c.xGoogMetadata)
  242. opts = append(c.CallOptions.GetProduct[0:len(c.CallOptions.GetProduct):len(c.CallOptions.GetProduct)], opts...)
  243. var resp *visionpb.Product
  244. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  245. var err error
  246. resp, err = c.productSearchClient.GetProduct(ctx, req, settings.GRPC...)
  247. return err
  248. }, opts...)
  249. if err != nil {
  250. return nil, err
  251. }
  252. return resp, nil
  253. }
  254. // UpdateProduct makes changes to a Product resource.
  255. // Only the display_name, description, and labels fields can be updated
  256. // right now.
  257. //
  258. // If labels are updated, the change will not be reflected in queries until
  259. // the next index time.
  260. //
  261. // Possible errors:
  262. //
  263. // Returns NOT_FOUND if the Product does not exist.
  264. //
  265. // Returns INVALID_ARGUMENT if display_name is present in update_mask but is
  266. // missing from the request or longer than 4096 characters.
  267. //
  268. // Returns INVALID_ARGUMENT if description is present in update_mask but is
  269. // longer than 4096 characters.
  270. //
  271. // Returns INVALID_ARGUMENT if product_category is present in update_mask.
  272. func (c *ProductSearchClient) UpdateProduct(ctx context.Context, req *visionpb.UpdateProductRequest, opts ...gax.CallOption) (*visionpb.Product, error) {
  273. ctx = insertMetadata(ctx, c.xGoogMetadata)
  274. opts = append(c.CallOptions.UpdateProduct[0:len(c.CallOptions.UpdateProduct):len(c.CallOptions.UpdateProduct)], opts...)
  275. var resp *visionpb.Product
  276. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  277. var err error
  278. resp, err = c.productSearchClient.UpdateProduct(ctx, req, settings.GRPC...)
  279. return err
  280. }, opts...)
  281. if err != nil {
  282. return nil, err
  283. }
  284. return resp, nil
  285. }
  286. // DeleteProduct permanently deletes a product and its reference images.
  287. //
  288. // Metadata of the product and all its images will be deleted right away, but
  289. // search queries against ProductSets containing the product may still work
  290. // until all related caches are refreshed.
  291. //
  292. // Possible errors:
  293. //
  294. // Returns NOT_FOUND if the product does not exist.
  295. func (c *ProductSearchClient) DeleteProduct(ctx context.Context, req *visionpb.DeleteProductRequest, opts ...gax.CallOption) error {
  296. ctx = insertMetadata(ctx, c.xGoogMetadata)
  297. opts = append(c.CallOptions.DeleteProduct[0:len(c.CallOptions.DeleteProduct):len(c.CallOptions.DeleteProduct)], opts...)
  298. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  299. var err error
  300. _, err = c.productSearchClient.DeleteProduct(ctx, req, settings.GRPC...)
  301. return err
  302. }, opts...)
  303. return err
  304. }
  305. // ListReferenceImages lists reference images.
  306. //
  307. // Possible errors:
  308. //
  309. // Returns NOT_FOUND if the parent product does not exist.
  310. //
  311. // Returns INVALID_ARGUMENT if the page_size is greater than 100, or less
  312. // than 1.
  313. func (c *ProductSearchClient) ListReferenceImages(ctx context.Context, req *visionpb.ListReferenceImagesRequest, opts ...gax.CallOption) *ReferenceImageIterator {
  314. ctx = insertMetadata(ctx, c.xGoogMetadata)
  315. opts = append(c.CallOptions.ListReferenceImages[0:len(c.CallOptions.ListReferenceImages):len(c.CallOptions.ListReferenceImages)], opts...)
  316. it := &ReferenceImageIterator{}
  317. req = proto.Clone(req).(*visionpb.ListReferenceImagesRequest)
  318. it.InternalFetch = func(pageSize int, pageToken string) ([]*visionpb.ReferenceImage, string, error) {
  319. var resp *visionpb.ListReferenceImagesResponse
  320. req.PageToken = pageToken
  321. if pageSize > math.MaxInt32 {
  322. req.PageSize = math.MaxInt32
  323. } else {
  324. req.PageSize = int32(pageSize)
  325. }
  326. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  327. var err error
  328. resp, err = c.productSearchClient.ListReferenceImages(ctx, req, settings.GRPC...)
  329. return err
  330. }, opts...)
  331. if err != nil {
  332. return nil, "", err
  333. }
  334. return resp.ReferenceImages, resp.NextPageToken, nil
  335. }
  336. fetch := func(pageSize int, pageToken string) (string, error) {
  337. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  338. if err != nil {
  339. return "", err
  340. }
  341. it.items = append(it.items, items...)
  342. return nextPageToken, nil
  343. }
  344. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  345. it.pageInfo.MaxSize = int(req.PageSize)
  346. return it
  347. }
  348. // GetReferenceImage gets information associated with a ReferenceImage.
  349. //
  350. // Possible errors:
  351. //
  352. // Returns NOT_FOUND if the specified image does not exist.
  353. func (c *ProductSearchClient) GetReferenceImage(ctx context.Context, req *visionpb.GetReferenceImageRequest, opts ...gax.CallOption) (*visionpb.ReferenceImage, error) {
  354. ctx = insertMetadata(ctx, c.xGoogMetadata)
  355. opts = append(c.CallOptions.GetReferenceImage[0:len(c.CallOptions.GetReferenceImage):len(c.CallOptions.GetReferenceImage)], opts...)
  356. var resp *visionpb.ReferenceImage
  357. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  358. var err error
  359. resp, err = c.productSearchClient.GetReferenceImage(ctx, req, settings.GRPC...)
  360. return err
  361. }, opts...)
  362. if err != nil {
  363. return nil, err
  364. }
  365. return resp, nil
  366. }
  367. // DeleteReferenceImage permanently deletes a reference image.
  368. //
  369. // The image metadata will be deleted right away, but search queries
  370. // against ProductSets containing the image may still work until all related
  371. // caches are refreshed.
  372. //
  373. // The actual image files are not deleted from Google Cloud Storage.
  374. //
  375. // Possible errors:
  376. //
  377. // Returns NOT_FOUND if the reference image does not exist.
  378. func (c *ProductSearchClient) DeleteReferenceImage(ctx context.Context, req *visionpb.DeleteReferenceImageRequest, opts ...gax.CallOption) error {
  379. ctx = insertMetadata(ctx, c.xGoogMetadata)
  380. opts = append(c.CallOptions.DeleteReferenceImage[0:len(c.CallOptions.DeleteReferenceImage):len(c.CallOptions.DeleteReferenceImage)], opts...)
  381. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  382. var err error
  383. _, err = c.productSearchClient.DeleteReferenceImage(ctx, req, settings.GRPC...)
  384. return err
  385. }, opts...)
  386. return err
  387. }
  388. // CreateReferenceImage creates and returns a new ReferenceImage resource.
  389. //
  390. // The bounding_poly field is optional. If bounding_poly is not specified,
  391. // the system will try to detect regions of interest in the image that are
  392. // compatible with the product_category on the parent product. If it is
  393. // specified, detection is ALWAYS skipped. The system converts polygons into
  394. // non-rotated rectangles.
  395. //
  396. // Note that the pipeline will resize the image if the image resolution is too
  397. // large to process (above 50MP).
  398. //
  399. // Possible errors:
  400. //
  401. // Returns INVALID_ARGUMENT if the image_uri is missing or longer than 4096
  402. // characters.
  403. //
  404. // Returns INVALID_ARGUMENT if the product does not exist.
  405. //
  406. // Returns INVALID_ARGUMENT if bounding_poly is not provided, and nothing
  407. // compatible with the parent product's product_category is detected.
  408. //
  409. // Returns INVALID_ARGUMENT if bounding_poly contains more than 10 polygons.
  410. func (c *ProductSearchClient) CreateReferenceImage(ctx context.Context, req *visionpb.CreateReferenceImageRequest, opts ...gax.CallOption) (*visionpb.ReferenceImage, error) {
  411. ctx = insertMetadata(ctx, c.xGoogMetadata)
  412. opts = append(c.CallOptions.CreateReferenceImage[0:len(c.CallOptions.CreateReferenceImage):len(c.CallOptions.CreateReferenceImage)], opts...)
  413. var resp *visionpb.ReferenceImage
  414. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  415. var err error
  416. resp, err = c.productSearchClient.CreateReferenceImage(ctx, req, settings.GRPC...)
  417. return err
  418. }, opts...)
  419. if err != nil {
  420. return nil, err
  421. }
  422. return resp, nil
  423. }
  424. // CreateProductSet creates and returns a new ProductSet resource.
  425. //
  426. // Possible errors:
  427. //
  428. // Returns INVALID_ARGUMENT if display_name is missing, or is longer than
  429. // 4096 characters.
  430. func (c *ProductSearchClient) CreateProductSet(ctx context.Context, req *visionpb.CreateProductSetRequest, opts ...gax.CallOption) (*visionpb.ProductSet, error) {
  431. ctx = insertMetadata(ctx, c.xGoogMetadata)
  432. opts = append(c.CallOptions.CreateProductSet[0:len(c.CallOptions.CreateProductSet):len(c.CallOptions.CreateProductSet)], opts...)
  433. var resp *visionpb.ProductSet
  434. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  435. var err error
  436. resp, err = c.productSearchClient.CreateProductSet(ctx, req, settings.GRPC...)
  437. return err
  438. }, opts...)
  439. if err != nil {
  440. return nil, err
  441. }
  442. return resp, nil
  443. }
  444. // ListProductSets lists ProductSets in an unspecified order.
  445. //
  446. // Possible errors:
  447. //
  448. // Returns INVALID_ARGUMENT if page_size is greater than 100, or less
  449. // than 1.
  450. func (c *ProductSearchClient) ListProductSets(ctx context.Context, req *visionpb.ListProductSetsRequest, opts ...gax.CallOption) *ProductSetIterator {
  451. ctx = insertMetadata(ctx, c.xGoogMetadata)
  452. opts = append(c.CallOptions.ListProductSets[0:len(c.CallOptions.ListProductSets):len(c.CallOptions.ListProductSets)], opts...)
  453. it := &ProductSetIterator{}
  454. req = proto.Clone(req).(*visionpb.ListProductSetsRequest)
  455. it.InternalFetch = func(pageSize int, pageToken string) ([]*visionpb.ProductSet, string, error) {
  456. var resp *visionpb.ListProductSetsResponse
  457. req.PageToken = pageToken
  458. if pageSize > math.MaxInt32 {
  459. req.PageSize = math.MaxInt32
  460. } else {
  461. req.PageSize = int32(pageSize)
  462. }
  463. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  464. var err error
  465. resp, err = c.productSearchClient.ListProductSets(ctx, req, settings.GRPC...)
  466. return err
  467. }, opts...)
  468. if err != nil {
  469. return nil, "", err
  470. }
  471. return resp.ProductSets, resp.NextPageToken, nil
  472. }
  473. fetch := func(pageSize int, pageToken string) (string, error) {
  474. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  475. if err != nil {
  476. return "", err
  477. }
  478. it.items = append(it.items, items...)
  479. return nextPageToken, nil
  480. }
  481. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  482. it.pageInfo.MaxSize = int(req.PageSize)
  483. return it
  484. }
  485. // GetProductSet gets information associated with a ProductSet.
  486. //
  487. // Possible errors:
  488. //
  489. // Returns NOT_FOUND if the ProductSet does not exist.
  490. func (c *ProductSearchClient) GetProductSet(ctx context.Context, req *visionpb.GetProductSetRequest, opts ...gax.CallOption) (*visionpb.ProductSet, error) {
  491. ctx = insertMetadata(ctx, c.xGoogMetadata)
  492. opts = append(c.CallOptions.GetProductSet[0:len(c.CallOptions.GetProductSet):len(c.CallOptions.GetProductSet)], opts...)
  493. var resp *visionpb.ProductSet
  494. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  495. var err error
  496. resp, err = c.productSearchClient.GetProductSet(ctx, req, settings.GRPC...)
  497. return err
  498. }, opts...)
  499. if err != nil {
  500. return nil, err
  501. }
  502. return resp, nil
  503. }
  504. // UpdateProductSet makes changes to a ProductSet resource.
  505. // Only display_name can be updated currently.
  506. //
  507. // Possible errors:
  508. //
  509. // Returns NOT_FOUND if the ProductSet does not exist.
  510. //
  511. // Returns INVALID_ARGUMENT if display_name is present in update_mask but
  512. // missing from the request or longer than 4096 characters.
  513. func (c *ProductSearchClient) UpdateProductSet(ctx context.Context, req *visionpb.UpdateProductSetRequest, opts ...gax.CallOption) (*visionpb.ProductSet, error) {
  514. ctx = insertMetadata(ctx, c.xGoogMetadata)
  515. opts = append(c.CallOptions.UpdateProductSet[0:len(c.CallOptions.UpdateProductSet):len(c.CallOptions.UpdateProductSet)], opts...)
  516. var resp *visionpb.ProductSet
  517. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  518. var err error
  519. resp, err = c.productSearchClient.UpdateProductSet(ctx, req, settings.GRPC...)
  520. return err
  521. }, opts...)
  522. if err != nil {
  523. return nil, err
  524. }
  525. return resp, nil
  526. }
  527. // DeleteProductSet permanently deletes a ProductSet. Products and ReferenceImages in the
  528. // ProductSet are not deleted.
  529. //
  530. // The actual image files are not deleted from Google Cloud Storage.
  531. //
  532. // Possible errors:
  533. //
  534. // Returns NOT_FOUND if the ProductSet does not exist.
  535. func (c *ProductSearchClient) DeleteProductSet(ctx context.Context, req *visionpb.DeleteProductSetRequest, opts ...gax.CallOption) error {
  536. ctx = insertMetadata(ctx, c.xGoogMetadata)
  537. opts = append(c.CallOptions.DeleteProductSet[0:len(c.CallOptions.DeleteProductSet):len(c.CallOptions.DeleteProductSet)], opts...)
  538. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  539. var err error
  540. _, err = c.productSearchClient.DeleteProductSet(ctx, req, settings.GRPC...)
  541. return err
  542. }, opts...)
  543. return err
  544. }
  545. // AddProductToProductSet adds a Product to the specified ProductSet. If the Product is already
  546. // present, no change is made.
  547. //
  548. // One Product can be added to at most 100 ProductSets.
  549. //
  550. // Possible errors:
  551. //
  552. // Returns NOT_FOUND if the Product or the ProductSet doesn't exist.
  553. func (c *ProductSearchClient) AddProductToProductSet(ctx context.Context, req *visionpb.AddProductToProductSetRequest, opts ...gax.CallOption) error {
  554. ctx = insertMetadata(ctx, c.xGoogMetadata)
  555. opts = append(c.CallOptions.AddProductToProductSet[0:len(c.CallOptions.AddProductToProductSet):len(c.CallOptions.AddProductToProductSet)], opts...)
  556. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  557. var err error
  558. _, err = c.productSearchClient.AddProductToProductSet(ctx, req, settings.GRPC...)
  559. return err
  560. }, opts...)
  561. return err
  562. }
  563. // RemoveProductFromProductSet removes a Product from the specified ProductSet.
  564. //
  565. // Possible errors:
  566. //
  567. // Returns NOT_FOUND If the Product is not found under the ProductSet.
  568. func (c *ProductSearchClient) RemoveProductFromProductSet(ctx context.Context, req *visionpb.RemoveProductFromProductSetRequest, opts ...gax.CallOption) error {
  569. ctx = insertMetadata(ctx, c.xGoogMetadata)
  570. opts = append(c.CallOptions.RemoveProductFromProductSet[0:len(c.CallOptions.RemoveProductFromProductSet):len(c.CallOptions.RemoveProductFromProductSet)], opts...)
  571. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  572. var err error
  573. _, err = c.productSearchClient.RemoveProductFromProductSet(ctx, req, settings.GRPC...)
  574. return err
  575. }, opts...)
  576. return err
  577. }
  578. // ListProductsInProductSet lists the Products in a ProductSet, in an unspecified order. If the
  579. // ProductSet does not exist, the products field of the response will be
  580. // empty.
  581. //
  582. // Possible errors:
  583. //
  584. // Returns INVALID_ARGUMENT if page_size is greater than 100 or less than 1.
  585. func (c *ProductSearchClient) ListProductsInProductSet(ctx context.Context, req *visionpb.ListProductsInProductSetRequest, opts ...gax.CallOption) *ProductIterator {
  586. ctx = insertMetadata(ctx, c.xGoogMetadata)
  587. opts = append(c.CallOptions.ListProductsInProductSet[0:len(c.CallOptions.ListProductsInProductSet):len(c.CallOptions.ListProductsInProductSet)], opts...)
  588. it := &ProductIterator{}
  589. req = proto.Clone(req).(*visionpb.ListProductsInProductSetRequest)
  590. it.InternalFetch = func(pageSize int, pageToken string) ([]*visionpb.Product, string, error) {
  591. var resp *visionpb.ListProductsInProductSetResponse
  592. req.PageToken = pageToken
  593. if pageSize > math.MaxInt32 {
  594. req.PageSize = math.MaxInt32
  595. } else {
  596. req.PageSize = int32(pageSize)
  597. }
  598. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  599. var err error
  600. resp, err = c.productSearchClient.ListProductsInProductSet(ctx, req, settings.GRPC...)
  601. return err
  602. }, opts...)
  603. if err != nil {
  604. return nil, "", err
  605. }
  606. return resp.Products, resp.NextPageToken, nil
  607. }
  608. fetch := func(pageSize int, pageToken string) (string, error) {
  609. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  610. if err != nil {
  611. return "", err
  612. }
  613. it.items = append(it.items, items...)
  614. return nextPageToken, nil
  615. }
  616. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  617. it.pageInfo.MaxSize = int(req.PageSize)
  618. return it
  619. }
  620. // ImportProductSets asynchronous API that imports a list of reference images to specified
  621. // product sets based on a list of image information.
  622. //
  623. // The [google.longrunning.Operation][google.longrunning.Operation] API can be
  624. // used to keep track of the progress and results of the request.
  625. // Operation.metadata contains BatchOperationMetadata. (progress)
  626. // Operation.response contains ImportProductSetsResponse. (results)
  627. //
  628. // The input source of this method is a csv file on Google Cloud Storage.
  629. // For the format of the csv file please see
  630. // [ImportProductSetsGcsSource.csv_file_uri][google.cloud.vision.v1.ImportProductSetsGcsSource.csv_file_uri].
  631. func (c *ProductSearchClient) ImportProductSets(ctx context.Context, req *visionpb.ImportProductSetsRequest, opts ...gax.CallOption) (*ImportProductSetsOperation, error) {
  632. ctx = insertMetadata(ctx, c.xGoogMetadata)
  633. opts = append(c.CallOptions.ImportProductSets[0:len(c.CallOptions.ImportProductSets):len(c.CallOptions.ImportProductSets)], opts...)
  634. var resp *longrunningpb.Operation
  635. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  636. var err error
  637. resp, err = c.productSearchClient.ImportProductSets(ctx, req, settings.GRPC...)
  638. return err
  639. }, opts...)
  640. if err != nil {
  641. return nil, err
  642. }
  643. return &ImportProductSetsOperation{
  644. lro: longrunning.InternalNewOperation(c.LROClient, resp),
  645. }, nil
  646. }
  647. // ProductIterator manages a stream of *visionpb.Product.
  648. type ProductIterator struct {
  649. items []*visionpb.Product
  650. pageInfo *iterator.PageInfo
  651. nextFunc func() error
  652. // InternalFetch is for use by the Google Cloud Libraries only.
  653. // It is not part of the stable interface of this package.
  654. //
  655. // InternalFetch returns results from a single call to the underlying RPC.
  656. // The number of results is no greater than pageSize.
  657. // If there are no more results, nextPageToken is empty and err is nil.
  658. InternalFetch func(pageSize int, pageToken string) (results []*visionpb.Product, nextPageToken string, err error)
  659. }
  660. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  661. func (it *ProductIterator) PageInfo() *iterator.PageInfo {
  662. return it.pageInfo
  663. }
  664. // Next returns the next result. Its second return value is iterator.Done if there are no more
  665. // results. Once Next returns Done, all subsequent calls will return Done.
  666. func (it *ProductIterator) Next() (*visionpb.Product, error) {
  667. var item *visionpb.Product
  668. if err := it.nextFunc(); err != nil {
  669. return item, err
  670. }
  671. item = it.items[0]
  672. it.items = it.items[1:]
  673. return item, nil
  674. }
  675. func (it *ProductIterator) bufLen() int {
  676. return len(it.items)
  677. }
  678. func (it *ProductIterator) takeBuf() interface{} {
  679. b := it.items
  680. it.items = nil
  681. return b
  682. }
  683. // ProductSetIterator manages a stream of *visionpb.ProductSet.
  684. type ProductSetIterator struct {
  685. items []*visionpb.ProductSet
  686. pageInfo *iterator.PageInfo
  687. nextFunc func() error
  688. // InternalFetch is for use by the Google Cloud Libraries only.
  689. // It is not part of the stable interface of this package.
  690. //
  691. // InternalFetch returns results from a single call to the underlying RPC.
  692. // The number of results is no greater than pageSize.
  693. // If there are no more results, nextPageToken is empty and err is nil.
  694. InternalFetch func(pageSize int, pageToken string) (results []*visionpb.ProductSet, nextPageToken string, err error)
  695. }
  696. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  697. func (it *ProductSetIterator) PageInfo() *iterator.PageInfo {
  698. return it.pageInfo
  699. }
  700. // Next returns the next result. Its second return value is iterator.Done if there are no more
  701. // results. Once Next returns Done, all subsequent calls will return Done.
  702. func (it *ProductSetIterator) Next() (*visionpb.ProductSet, error) {
  703. var item *visionpb.ProductSet
  704. if err := it.nextFunc(); err != nil {
  705. return item, err
  706. }
  707. item = it.items[0]
  708. it.items = it.items[1:]
  709. return item, nil
  710. }
  711. func (it *ProductSetIterator) bufLen() int {
  712. return len(it.items)
  713. }
  714. func (it *ProductSetIterator) takeBuf() interface{} {
  715. b := it.items
  716. it.items = nil
  717. return b
  718. }
  719. // ReferenceImageIterator manages a stream of *visionpb.ReferenceImage.
  720. type ReferenceImageIterator struct {
  721. items []*visionpb.ReferenceImage
  722. pageInfo *iterator.PageInfo
  723. nextFunc func() error
  724. // InternalFetch is for use by the Google Cloud Libraries only.
  725. // It is not part of the stable interface of this package.
  726. //
  727. // InternalFetch returns results from a single call to the underlying RPC.
  728. // The number of results is no greater than pageSize.
  729. // If there are no more results, nextPageToken is empty and err is nil.
  730. InternalFetch func(pageSize int, pageToken string) (results []*visionpb.ReferenceImage, nextPageToken string, err error)
  731. }
  732. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  733. func (it *ReferenceImageIterator) PageInfo() *iterator.PageInfo {
  734. return it.pageInfo
  735. }
  736. // Next returns the next result. Its second return value is iterator.Done if there are no more
  737. // results. Once Next returns Done, all subsequent calls will return Done.
  738. func (it *ReferenceImageIterator) Next() (*visionpb.ReferenceImage, error) {
  739. var item *visionpb.ReferenceImage
  740. if err := it.nextFunc(); err != nil {
  741. return item, err
  742. }
  743. item = it.items[0]
  744. it.items = it.items[1:]
  745. return item, nil
  746. }
  747. func (it *ReferenceImageIterator) bufLen() int {
  748. return len(it.items)
  749. }
  750. func (it *ReferenceImageIterator) takeBuf() interface{} {
  751. b := it.items
  752. it.items = nil
  753. return b
  754. }
  755. // ImportProductSetsOperation manages a long-running operation from ImportProductSets.
  756. type ImportProductSetsOperation struct {
  757. lro *longrunning.Operation
  758. }
  759. // ImportProductSetsOperation returns a new ImportProductSetsOperation from a given name.
  760. // The name must be that of a previously created ImportProductSetsOperation, possibly from a different process.
  761. func (c *ProductSearchClient) ImportProductSetsOperation(name string) *ImportProductSetsOperation {
  762. return &ImportProductSetsOperation{
  763. lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
  764. }
  765. }
  766. // Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
  767. //
  768. // See documentation of Poll for error-handling information.
  769. func (op *ImportProductSetsOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*visionpb.ImportProductSetsResponse, error) {
  770. var resp visionpb.ImportProductSetsResponse
  771. if err := op.lro.WaitWithInterval(ctx, &resp, 45000*time.Millisecond, opts...); err != nil {
  772. return nil, err
  773. }
  774. return &resp, nil
  775. }
  776. // Poll fetches the latest state of the long-running operation.
  777. //
  778. // Poll also fetches the latest metadata, which can be retrieved by Metadata.
  779. //
  780. // If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
  781. // the operation has completed with failure, the error is returned and op.Done will return true.
  782. // If Poll succeeds and the operation has completed successfully,
  783. // op.Done will return true, and the response of the operation is returned.
  784. // If Poll succeeds and the operation has not completed, the returned response and error are both nil.
  785. func (op *ImportProductSetsOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*visionpb.ImportProductSetsResponse, error) {
  786. var resp visionpb.ImportProductSetsResponse
  787. if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
  788. return nil, err
  789. }
  790. if !op.Done() {
  791. return nil, nil
  792. }
  793. return &resp, nil
  794. }
  795. // Metadata returns metadata associated with the long-running operation.
  796. // Metadata itself does not contact the server, but Poll does.
  797. // To get the latest metadata, call this method after a successful call to Poll.
  798. // If the metadata is not available, the returned metadata and error are both nil.
  799. func (op *ImportProductSetsOperation) Metadata() (*visionpb.BatchOperationMetadata, error) {
  800. var meta visionpb.BatchOperationMetadata
  801. if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
  802. return nil, nil
  803. } else if err != nil {
  804. return nil, err
  805. }
  806. return &meta, nil
  807. }
  808. // Done reports whether the long-running operation has completed.
  809. func (op *ImportProductSetsOperation) Done() bool {
  810. return op.lro.Done()
  811. }
  812. // Name returns the name of the long-running operation.
  813. // The name is assigned by the server and is unique within the service from which the operation is created.
  814. func (op *ImportProductSetsOperation) Name() string {
  815. return op.lro.Name()
  816. }