Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

docs(samples): Refactoring product package (CRUD) #417

Merged
merged 15 commits into from
Jul 13, 2022
Merged
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ If you are using Maven, add this to your pom.xml file:
If you are using Gradle without BOM, add this to your dependencies

```Groovy
implementation 'com.google.cloud:google-cloud-retail:2.0.19'
implementation 'com.google.cloud:google-cloud-retail:2.3.0'
```

If you are using SBT, add this to your dependencies

```Scala
libraryDependencies += "com.google.cloud" % "google-cloud-retail" % "2.0.19"
libraryDependencies += "com.google.cloud" % "google-cloud-retail" % "2.3.0"
```

## Authentication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,57 +31,55 @@
import java.io.IOException;
import java.time.Instant;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

public class AddFulfillmentPlaces {

public static void main(String[] args) throws IOException, InterruptedException {
// TODO(developer): Replace these variables before running the sample.
String projectId = ServiceOptions.getDefaultProjectId();
String generatedProductId = UUID.randomUUID().toString();
String productName =
String.format(
"projects/%s/locations/global/catalogs/default_catalog/branches/0/products/%s",
projectId, generatedProductId);
Timestamp currentDate =
Timestamp.newBuilder()
.setSeconds(Instant.now().getEpochSecond())
.setNanos(Instant.now().getNano())
.build();

createProduct(generatedProductId);
System.out.printf("Add fulfilment places with current date: %s", currentDate);
addFulfillmentPlaces(productName, currentDate, "store2");
addFulfillmentPlaces(productName, "store2");
getProduct(productName);
deleteProduct(productName);
}

public static void addFulfillmentPlaces(String productName, Timestamp timestamp, String placeId)
public static void addFulfillmentPlaces(String productName, String placeId)
throws IOException, InterruptedException {
AddFulfillmentPlacesRequest addFulfillmentRequest =
getAddFulfillmentRequest(productName, timestamp, placeId);
try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
serviceClient.addFulfillmentPlacesAsync(addFulfillmentRequest);
/*
This is a long-running operation and its result is not immediately
present with get operations,thus we simulate wait with sleep method.
*/
System.out.println("Add fulfillment places, wait 30 seconds: ");
Thread.sleep(30_000);
}
}
Timestamp currentDate =
Timestamp.newBuilder()
.setSeconds(Instant.now().getEpochSecond())
.setNanos(Instant.now().getNano())
.build();

public static AddFulfillmentPlacesRequest getAddFulfillmentRequest(
String productName, Timestamp timestamp, String placeId) {
AddFulfillmentPlacesRequest addfulfillmentPlacesRequest =
System.out.printf("Add fulfilment places with current date: %s", currentDate);

AddFulfillmentPlacesRequest addFulfillmentPlacesRequest =
AddFulfillmentPlacesRequest.newBuilder()
.setProduct(productName)
.setType("pickup-in-store")
.addPlaceIds(placeId)
.setAddTime(timestamp)
.setAddTime(currentDate)
.setAllowMissing(true)
.build();
System.out.println("Add fulfillment request " + addfulfillmentPlacesRequest);
System.out.println("Add fulfillment request " + addFulfillmentPlacesRequest);

return addfulfillmentPlacesRequest;
// Initialize client that will be used to send requests. This client only
// needs to be created once, and can be reused for multiple requests. After
// completing all of your requests, call the "close" method on the client to
// safely clean up any remaining background resources.
try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
serviceClient.addFulfillmentPlacesAsync(addFulfillmentPlacesRequest);
// This is a long-running operation and its result is not immediately
// present with get operations,thus we simulate wait with sleep method.
System.out.println("Add fulfillment places, wait 30 seconds: ");
TimeUnit.SECONDS.sleep(30);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
public class CreateProduct {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = ServiceOptions.getDefaultProjectId();
String branchName =
String.format(
Expand All @@ -50,42 +49,44 @@ public static void main(String[] args) throws IOException {

// call the Retail API to create product
public static Product createProduct(String productId, String branchName) throws IOException {
float price = 30.0f;
float originalPrice = 35.5f;

PriceInfo priceInfo =
PriceInfo.newBuilder()
.setPrice(price)
.setOriginalPrice(originalPrice)
.setCurrencyCode("USD")
.build();

Product generatedProduct =
Product.newBuilder()
.setTitle("Nest Mini")
.setType(Type.PRIMARY)
.addCategories("Speakers and displays")
.addBrands("Google")
.setPriceInfo(priceInfo)
.setAvailability(Availability.IN_STOCK)
.build();

CreateProductRequest createProductRequest =
CreateProductRequest.newBuilder()
.setProduct(generateProduct())
.setProduct(generatedProduct)
.setProductId(productId)
.setParent(branchName)
.build();
System.out.printf("Create product request: %s%n", createProductRequest);

// Initialize client that will be used to send requests. This client only
// needs to be created once, and can be reused for multiple requests. After
// completing all of your requests, call the "close" method on the client to
// safely clean up any remaining background resources.
try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
Product createdProduct = serviceClient.createProduct(createProductRequest);
System.out.printf("Created product: %s%n", createdProduct);
return createdProduct;
}
}

// generate product for create
public static Product generateProduct() {
float price = 30.0f;
float originalPrice = 35.5f;

PriceInfo priceInfo =
PriceInfo.newBuilder()
.setPrice(price)
.setOriginalPrice(originalPrice)
.setCurrencyCode("USD")
.build();

return Product.newBuilder()
.setTitle("Nest Mini")
.setType(Type.PRIMARY)
.addCategories("Speakers and displays")
.addBrands("Google")
.setPriceInfo(priceInfo)
.setAvailability(Availability.IN_STOCK)
.build();
}
}

// [END retail_create_product]
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
public class CrudProduct {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = ServiceOptions.getDefaultProjectId();
String generatedProductId = UUID.randomUUID().toString();
String branchName =
Expand All @@ -53,7 +52,7 @@ public static void main(String[] args) throws IOException {
deleteProduct(productName);
}

// generate product for create
// Generate product for create
public static Product generateProduct() {
float price = 30.0f;
float originalPrice = 35.5f;
Expand All @@ -75,7 +74,7 @@ public static Product generateProduct() {
.build();
}

// generate product for update
// Generate product for update
public static Product generateProductForUpdate(String productId, String productName) {
final float price = 20.0f;
final float originalPrice = 25.5f;
Expand All @@ -99,7 +98,7 @@ public static Product generateProductForUpdate(String productId, String productN
.build();
}

// call the Retail API to create product
// Call the Retail API to create product
public static Product createProduct(String productId, String branchName) throws IOException {
CreateProductRequest createProductRequest =
CreateProductRequest.newBuilder()
Expand All @@ -109,19 +108,28 @@ public static Product createProduct(String productId, String branchName) throws
.build();
System.out.printf("Create product request: %s%n", createProductRequest);

Product createdProduct = ProductServiceClient.create().createProduct(createProductRequest);
System.out.printf("Created product: %s%n", createdProduct);

return createdProduct;
// Initialize client that will be used to send requests. This client only
// needs to be created once, and can be reused for multiple requests. After
// completing all of your requests, call the "close" method on the client to
// safely clean up any remaining background resources.
try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
Product createdProduct = serviceClient.createProduct(createProductRequest);
System.out.printf("Created product: %s%n", createdProduct);
return createdProduct;
}
}

// get product
// Get product
public static Product getProduct(String productName) throws IOException {
Product product = Product.newBuilder().build();

GetProductRequest getProductRequest =
GetProductRequest.newBuilder().setName(productName).build();

// Initialize client that will be used to send requests. This client only
// needs to be created once, and can be reused for multiple requests. After
// completing all of your requests, call the "close" method on the client to
// safely clean up any remaining background resources.
try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
product = serviceClient.getProduct(getProductRequest);
System.out.println("Get product response: " + product);
Expand All @@ -132,7 +140,7 @@ public static Product getProduct(String productName) throws IOException {
}
}

// update product
// Update product
public static void updateProduct(Product originalProduct, String productName) throws IOException {
UpdateProductRequest updateProductRequest =
UpdateProductRequest.newBuilder()
Expand All @@ -141,18 +149,26 @@ public static void updateProduct(Product originalProduct, String productName) th
.build();
System.out.printf("Update product request: %s%n", updateProductRequest);

// Initialize client that will be used to send requests. This client only
// needs to be created once, and can be reused for multiple requests. After
// completing all of your requests, call the "close" method on the client to
// safely clean up any remaining background resources.
try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
Product updatedProduct = serviceClient.updateProduct(updateProductRequest);
System.out.printf("Updated product: %s%n", updatedProduct);
}
}

// delete product
// Delete product
public static void deleteProduct(String productName) throws IOException {
DeleteProductRequest deleteProductRequest =
DeleteProductRequest.newBuilder().setName(productName).build();
System.out.printf("Delete product request %s%n", deleteProductRequest);

// Initialize client that will be used to send requests. This client only
// needs to be created once, and can be reused for multiple requests. After
// completing all of your requests, call the "close" method on the client to
// safely clean up any remaining background resources.
try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
serviceClient.deleteProduct(deleteProductRequest);
System.out.printf("Product %s was deleted.%n", productName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public static void deleteProduct(String productName) throws IOException {
DeleteProductRequest.newBuilder().setName(productName).build();
System.out.printf("Delete product request %s%n", deleteProductRequest);

// Initialize client that will be used to send requests. This client only
// needs to be created once, and can be reused for multiple requests. After
// completing all of your requests, call the "close" method on the client to
// safely clean up any remaining background resources.
try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
serviceClient.deleteProduct(deleteProductRequest);
System.out.printf("Product %s was deleted.%n", productName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ public static Product getProduct(String productName) throws IOException {
GetProductRequest getProductRequest =
GetProductRequest.newBuilder().setName(productName).build();

try {
product = ProductServiceClient.create().getProduct(getProductRequest);
// Initialize client that will be used to send requests. This client only
// needs to be created once, and can be reused for multiple requests. After
// completing all of your requests, call the "close" method on the client to
// safely clean up any remaining background resources.
try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
product = serviceClient.getProduct(getProductRequest);
System.out.println("Get product response: " + product);
return product;
} catch (NotFoundException e) {
Expand Down
Loading