Search in sources :

Example 1 with ProductServiceClient

use of com.google.cloud.retail.v2.ProductServiceClient in project java-retail by googleapis.

the class CreateTestResources method importProductsFromGcs.

public static void importProductsFromGcs() throws IOException, InterruptedException {
    ImportProductsRequest importGcsRequest = getImportProductsGcsRequest("products.json");
    try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
        String operationName = serviceClient.importProductsCallable().call(importGcsRequest).getName();
        System.out.printf("OperationName = %s\n", operationName);
        OperationsClient operationsClient = serviceClient.getOperationsClient();
        Operation operation = operationsClient.getOperation(operationName);
        while (!operation.getDone()) {
            System.out.println("Please wait till operation is completed.");
            // Keep polling the operation periodically until the import task is done.
            int awaitDuration = 30000;
            Thread.sleep(awaitDuration);
            operation = operationsClient.getOperation(operationName);
        }
        System.out.println("Import products operation is completed.");
        if (operation.hasMetadata()) {
            ImportMetadata metadata = operation.getMetadata().unpack(ImportMetadata.class);
            System.out.printf("Number of successfully imported products: %s\n", metadata.getSuccessCount());
            System.out.printf("Number of failures during the importing: %s\n", metadata.getFailureCount());
        }
        if (operation.hasResponse()) {
            ImportProductsResponse response = operation.getResponse().unpack(ImportProductsResponse.class);
            System.out.printf("Operation result: %s", response);
        }
    }
}
Also used : ImportProductsRequest(com.google.cloud.retail.v2.ImportProductsRequest) ImportProductsResponse(com.google.cloud.retail.v2.ImportProductsResponse) ProductServiceClient(com.google.cloud.retail.v2.ProductServiceClient) OperationsClient(com.google.longrunning.OperationsClient) Operation(com.google.longrunning.Operation) ImportMetadata(com.google.cloud.retail.v2.ImportMetadata)

Example 2 with ProductServiceClient

use of com.google.cloud.retail.v2.ProductServiceClient in project java-retail by googleapis.

the class RemoveTestResources method deleteAllProducts.

public static void deleteAllProducts() throws IOException {
    System.out.println("Deleting products in process, please wait...");
    try (ProductServiceClient productServiceClient = ProductServiceClient.create()) {
        ListProductsRequest listRequest = ListProductsRequest.newBuilder().setParent(DEFAULT_CATALOG).build();
        ListProductsPagedResponse products = productServiceClient.listProducts(listRequest);
        int deleteCount = 0;
        for (Product product : products.iterateAll()) {
            DeleteProductRequest deleteRequest = DeleteProductRequest.newBuilder().setName(product.getName()).build();
            try {
                productServiceClient.deleteProduct(deleteRequest);
                deleteCount++;
            } catch (PermissionDeniedException e) {
                System.out.println("Ignore PermissionDenied in case the product does not exist " + "at time of deletion.");
            }
        }
        System.out.printf("%s products were deleted from %s%n", deleteCount, DEFAULT_CATALOG);
    }
}
Also used : DeleteProductRequest(com.google.cloud.retail.v2.DeleteProductRequest) ListProductsRequest(com.google.cloud.retail.v2.ListProductsRequest) ProductServiceClient(com.google.cloud.retail.v2.ProductServiceClient) Product(com.google.cloud.retail.v2.Product) PermissionDeniedException(com.google.api.gax.rpc.PermissionDeniedException) ListProductsPagedResponse(com.google.cloud.retail.v2.ProductServiceClient.ListProductsPagedResponse)

Example 3 with ProductServiceClient

use of com.google.cloud.retail.v2.ProductServiceClient in project java-retail by googleapis.

the class ImportProductsGcs method waitForOperationCompletion.

private static void waitForOperationCompletion(ImportProductsRequest importRequest) throws IOException, InterruptedException {
    try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
        String operationName = serviceClient.importProductsCallable().call(importRequest).getName();
        System.out.printf("OperationName = %s\n", operationName);
        OperationsClient operationsClient = serviceClient.getOperationsClient();
        Operation operation = operationsClient.getOperation(operationName);
        while (!operation.getDone()) {
            // Keep polling the operation periodically until the import task is done.
            int awaitDuration = 30000;
            Thread.sleep(awaitDuration);
            operation = operationsClient.getOperation(operationName);
        }
        if (operation.hasMetadata()) {
            ImportMetadata metadata = operation.getMetadata().unpack(ImportMetadata.class);
            System.out.printf("Number of successfully imported products: %s\n", metadata.getSuccessCount());
            System.out.printf("Number of failures during the importing: %s\n", metadata.getFailureCount());
        }
        if (operation.hasResponse()) {
            ImportProductsResponse response = operation.getResponse().unpack(ImportProductsResponse.class);
            System.out.printf("Operation result: %s%n", response);
        }
    }
}
Also used : ImportProductsResponse(com.google.cloud.retail.v2.ImportProductsResponse) ProductServiceClient(com.google.cloud.retail.v2.ProductServiceClient) OperationsClient(com.google.longrunning.OperationsClient) Operation(com.google.longrunning.Operation) ImportMetadata(com.google.cloud.retail.v2.ImportMetadata)

Example 4 with ProductServiceClient

use of com.google.cloud.retail.v2.ProductServiceClient in project java-retail by googleapis.

the class ImportProductsBigQueryTable method waitForOperationCompletion.

private static void waitForOperationCompletion(ImportProductsRequest importRequest) throws IOException, InterruptedException {
    try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
        String operationName = serviceClient.importProductsCallable().call(importRequest).getName();
        System.out.printf("OperationName = %s\n", operationName);
        OperationsClient operationsClient = serviceClient.getOperationsClient();
        Operation operation = operationsClient.getOperation(operationName);
        while (!operation.getDone()) {
            // Keep polling the operation periodically until the import task is done.
            int awaitDuration = 30000;
            Thread.sleep(awaitDuration);
            operation = operationsClient.getOperation(operationName);
        }
        if (operation.hasMetadata()) {
            ImportMetadata metadata = operation.getMetadata().unpack(ImportMetadata.class);
            System.out.printf("Number of successfully imported products: %s\n", metadata.getSuccessCount());
            System.out.printf("Number of failures during the importing: %s\n", metadata.getFailureCount());
        }
        if (operation.hasResponse()) {
            ImportProductsResponse response = operation.getResponse().unpack(ImportProductsResponse.class);
            System.out.printf("Operation result: %s%n", response);
        }
    }
}
Also used : ImportProductsResponse(com.google.cloud.retail.v2.ImportProductsResponse) ProductServiceClient(com.google.cloud.retail.v2.ProductServiceClient) OperationsClient(com.google.longrunning.OperationsClient) Operation(com.google.longrunning.Operation) ImportMetadata(com.google.cloud.retail.v2.ImportMetadata)

Example 5 with ProductServiceClient

use of com.google.cloud.retail.v2.ProductServiceClient in project java-retail by googleapis.

the class ImportProductsInlineSource method waitForOperationCompletion.

private static void waitForOperationCompletion(ImportProductsRequest importRequest) throws IOException, InterruptedException {
    try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
        String operationName = serviceClient.importProductsCallable().call(importRequest).getName();
        System.out.printf("OperationName = %s\n", operationName);
        OperationsClient operationsClient = serviceClient.getOperationsClient();
        Operation operation = operationsClient.getOperation(operationName);
        while (!operation.getDone()) {
            // Keep polling the operation periodically until the import task is done.
            int awaitDuration = 30000;
            Thread.sleep(awaitDuration);
            operation = operationsClient.getOperation(operationName);
        }
        if (operation.hasMetadata()) {
            ImportMetadata metadata = operation.getMetadata().unpack(ImportMetadata.class);
            System.out.printf("Number of successfully imported products: %s\n", metadata.getSuccessCount());
            System.out.printf("Number of failures during the importing: %s\n", metadata.getFailureCount());
        }
        if (operation.hasResponse()) {
            ImportProductsResponse response = operation.getResponse().unpack(ImportProductsResponse.class);
            System.out.printf("Operation result: %s%n", response);
        }
    }
}
Also used : ImportProductsResponse(com.google.cloud.retail.v2.ImportProductsResponse) ProductServiceClient(com.google.cloud.retail.v2.ProductServiceClient) OperationsClient(com.google.longrunning.OperationsClient) Operation(com.google.longrunning.Operation) ImportMetadata(com.google.cloud.retail.v2.ImportMetadata)

Aggregations

ProductServiceClient (com.google.cloud.retail.v2.ProductServiceClient)5 ImportMetadata (com.google.cloud.retail.v2.ImportMetadata)4 ImportProductsResponse (com.google.cloud.retail.v2.ImportProductsResponse)4 Operation (com.google.longrunning.Operation)4 OperationsClient (com.google.longrunning.OperationsClient)4 PermissionDeniedException (com.google.api.gax.rpc.PermissionDeniedException)1 DeleteProductRequest (com.google.cloud.retail.v2.DeleteProductRequest)1 ImportProductsRequest (com.google.cloud.retail.v2.ImportProductsRequest)1 ListProductsRequest (com.google.cloud.retail.v2.ListProductsRequest)1 Product (com.google.cloud.retail.v2.Product)1 ListProductsPagedResponse (com.google.cloud.retail.v2.ProductServiceClient.ListProductsPagedResponse)1