def create_product(project_id, location, product_id, product_display_name,
                   product_category):
    """Create one product.
    Args:
        project_id: Id of the project.
        location: A compute region name.
        product_id: Id of the product.
        product_display_name: Display name of the product.
        product_category: Category of the product.
    """
    client = vision.ProductSearchClient()

    # A resource that represents Google Cloud Platform location.
    location_path = f"projects/{project_id}/locations/{location}"

    # Create a product with the product specification in the region.
    # Set product display name and product category.
    product = vision.Product(display_name=product_display_name,
                             product_category=product_category)

    # The response is the product with the `name` field populated.
    response = client.create_product(parent=location_path,
                                     product=product,
                                     product_id=product_id)

    # Display the product information.
    print('Product name: {}'.format(response.name))
def update_product_labels(project_id, location, product_id, key, value):
    """Update the product labels.
    Args:
        project_id: Id of the project.
        location: A compute region name.
        product_id: Id of the product.
        key: The key of the label.
        value: The value of the label.
    """
    client = vision.ProductSearchClient()

    # Get the name of the product.
    product_path = client.product_path(project=project_id,
                                       location=location,
                                       product=product_id)

    # Set product name, product label and product display name.
    # Multiple labels are also supported.
    key_value = vision.Product.KeyValue(key=key, value=value)
    product = vision.Product(name=product_path, product_labels=[key_value])

    # Updating only the product_labels field here.
    update_mask = field_mask.FieldMask(paths=['product_labels'])

    # This overwrites the product_labels.
    updated_product = client.update_product(product=product,
                                            update_mask=update_mask)

    # Display the updated product information.
    print('Product name: {}'.format(updated_product.name))
    print('Updated product labels: {}'.format(product.product_labels))
예제 #3
0
 def test_update_product(self):
     # Create a Product.
     product = vision.Product(display_name="product display name",
                              product_category="apparel")
     product_id = "product" + unique_resource_id()
     product_path = self.ps_client.product_path(project=PROJECT_ID,
                                                location=self.location,
                                                product=product_id)
     response = self.ps_client.create_product(parent=self.location_path,
                                              product=product,
                                              product_id=product_id)
     self.products_to_delete.append(response.name)
     self.assertEqual(response.name, product_path)
     # Update the Product.
     new_display_name = "updated product name"
     updated_product_request = vision.Product(name=product_path,
                                              display_name=new_display_name)
     update_mask = field_mask.FieldMask(paths=["display_name"])
     updated_product = self.ps_client.update_product(
         product=updated_product_request, update_mask=update_mask)
     self.assertEqual(updated_product.display_name, new_display_name)
예제 #4
0
 def test_create_product(self):
     # Create a Product.
     product = vision.Product(display_name="product display name",
                              product_category="apparel")
     product_id = "product" + unique_resource_id()
     product_path = self.ps_client.product_path(project=PROJECT_ID,
                                                location=self.location,
                                                product=product_id)
     response = self.ps_client.create_product(parent=self.location_path,
                                              product=product,
                                              product_id=product_id)
     self.products_to_delete.append(response.name)
     # Verify the Product was successfully created.
     self.assertEqual(response.name, product_path)
예제 #5
0
    def test_reference_image(self):
        # Create a Product.
        product = vision.Product(display_name="product display name",
                                 product_category="apparel")
        product_id = "product" + unique_resource_id()
        product_path = self.ps_client.product_path(project=PROJECT_ID,
                                                   location=self.location,
                                                   product=product_id)
        response = self.ps_client.create_product(parent=self.location_path,
                                                 product=product,
                                                 product_id=product_id)
        self.products_to_delete.append(response.name)
        self.assertEqual(response.name, product_path)

        # Upload image to gcs.
        gcs_uri = self._upload_image("reference_image_test.jpg")

        # Create a ReferenceImage.
        reference_image_id = "reference_image" + unique_resource_id()
        reference_image_path = self.ps_client.reference_image_path(
            project=PROJECT_ID,
            location=self.location,
            product=product_id,
            reference_image=reference_image_id,
        )
        reference_image = vision.ReferenceImage(uri=gcs_uri)
        response = self.ps_client.create_reference_image(
            parent=product_path,
            reference_image=reference_image,
            reference_image_id=reference_image_id,
        )
        self.reference_images_to_delete.append(response.name)
        self.assertEqual(response.name, reference_image_path)

        # Get the ReferenceImage.
        get_response = self.ps_client.get_reference_image(
            name=reference_image_path)
        self.assertEqual(get_response.name, reference_image_path)

        # List the ReferenceImages in the Product.
        listed_reference_images = list(
            self.ps_client.list_reference_images(parent=product_path))
        self.assertEqual(len(listed_reference_images), 1)
        self.assertEqual(listed_reference_images[0].name, reference_image_path)
예제 #6
0
 def test_list_products(self):
     # Create a Product.
     product = vision.Product(display_name="product display name",
                              product_category="apparel")
     product_id = "product" + unique_resource_id()
     product_path = self.ps_client.product_path(project=PROJECT_ID,
                                                location=self.location,
                                                product=product_id)
     response = self.ps_client.create_product(parent=self.location_path,
                                              product=product,
                                              product_id=product_id)
     self.products_to_delete.append(response.name)
     self.assertEqual(response.name, product_path)
     # Verify Products can be listed.
     products_iterator = self.ps_client.list_products(
         parent=self.location_path)
     products_exist = False
     for product in products_iterator:
         products_exist = True
         break
     self.assertTrue(products_exist)
예제 #7
0
 def test_list_products_in_product_set(self):
     # Create a ProductSet.
     product_set = vision.ProductSet(display_name="display name")
     product_set_id = "set" + unique_resource_id()
     product_set_path = self.ps_client.product_set_path(
         project=PROJECT_ID,
         location=self.location,
         product_set=product_set_id)
     response = self.ps_client.create_product_set(
         parent=self.location_path,
         product_set=product_set,
         product_set_id=product_set_id,
     )
     self.product_sets_to_delete.append(response.name)
     self.assertEqual(response.name, product_set_path)
     # Create a Product.
     product = vision.Product(display_name="product display name",
                              product_category="apparel")
     product_id = "product" + unique_resource_id()
     product_path = self.ps_client.product_path(project=PROJECT_ID,
                                                location=self.location,
                                                product=product_id)
     response = self.ps_client.create_product(parent=self.location_path,
                                              product=product,
                                              product_id=product_id)
     self.products_to_delete.append(response.name)
     self.assertEqual(response.name, product_path)
     # Add the Product to the ProductSet.
     self.ps_client.add_product_to_product_set(name=product_set_path,
                                               product=product_path)
     # List the Products in the ProductSet.
     listed_products = list(
         self.ps_client.list_products_in_product_set(name=product_set_path))
     self.assertEqual(len(listed_products), 1)
     self.assertEqual(listed_products[0].name, product_path)
     # Remove the Product from the ProductSet.
     self.ps_client.remove_product_from_product_set(name=product_set_path,
                                                    product=product_path)
예제 #8
0
    def test_update_product_blocked(self):
        product = vision.Product(name=self.product_path)
        with pytest.raises(exceptions.PermissionDenied) as exc:
            self.ps_client.update_product(product=product)

        assert exc.value.message.startswith(_VPCSC_PROHIBITED_MESSAGE)