Skip to content

Commit

Permalink
docs(samples): Refactoring product package (CRUD) (#417)
Browse files Browse the repository at this point in the history
* Update README

* the bash scripts are added

* Update README.md

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* Update user_environment_setup.sh

* Update user_import_data_to_catalog.sh

* Update README.md

* Refactoring product package (CRUD).

* Minor fixes.

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

Co-authored-by: tetiana-karasova <[email protected]>
Co-authored-by: t-karasova <[email protected]>
Co-authored-by: t-karasova <[email protected]>
Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: Karl Weinmeister <[email protected]>
  • Loading branch information
6 people authored Jul 13, 2022
1 parent ff6af57 commit d0c5f39
Show file tree
Hide file tree
Showing 17 changed files with 215 additions and 210 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,76 +30,56 @@
import com.google.protobuf.Timestamp;
import java.io.IOException;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

public class RemoveFulfillmentPlaces {

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();
/*
* The time when the fulfillment updates are issued. If set with outdated time
* (yesterday), the fulfillment information will not updated.
*/
Timestamp outdatedDate =
Timestamp.newBuilder()
.setSeconds(Instant.now().minus(1, ChronoUnit.DAYS).getEpochSecond())
.setNanos(Instant.now().getNano())
.build();

createProduct(generatedProductId);
System.out.printf("Remove fulfilment places with current date: %s", currentDate);
removeFulfillmentPlaces(productName, currentDate, "store0");
getProduct(productName);
System.out.printf("Remove outdated fulfilment places: %s", outdatedDate);
removeFulfillmentPlaces(productName, outdatedDate, "store1");
removeFulfillmentPlaces(productName, "store0");
getProduct(productName);
deleteProduct(productName);
}

// remove fulfillment places to product
public static void removeFulfillmentPlaces(
String productName, Timestamp timestamp, String storeId)
public static void removeFulfillmentPlaces(String productName, String storeId)
throws IOException, InterruptedException {
RemoveFulfillmentPlacesRequest removeFulfillmentRequest =
getRemoveFulfillmentRequest(productName, timestamp, storeId);

try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
serviceClient.removeFulfillmentPlacesAsync(removeFulfillmentRequest);
/*
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("Remove fulfillment places, wait 30 seconds.");
Thread.sleep(30_000);
}
}
Timestamp currentDate =
Timestamp.newBuilder()
.setSeconds(Instant.now().getEpochSecond())
.setNanos(Instant.now().getNano())
.build();

// remove fulfillment request
public static RemoveFulfillmentPlacesRequest getRemoveFulfillmentRequest(
String productName, Timestamp timestamp, String storeId) {
System.out.printf("Remove fulfilment places with current date: %s", currentDate);
RemoveFulfillmentPlacesRequest removeFulfillmentRequest =
RemoveFulfillmentPlacesRequest.newBuilder()
.setProduct(productName)
.setType("pickup-in-store")
.addPlaceIds(storeId)
.setRemoveTime(timestamp)
.setRemoveTime(currentDate)
.setAllowMissing(true)
.build();
System.out.println("Remove fulfillment request " + removeFulfillmentRequest);

return removeFulfillmentRequest;
// 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.removeFulfillmentPlacesAsync(removeFulfillmentRequest);
// 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("Remove fulfillment places, wait 30 seconds.");
TimeUnit.SECONDS.sleep(30);
}
}
}

Expand Down
Loading

0 comments on commit d0c5f39

Please sign in to comment.