Skip to content

Commit 5ee4c2c

Browse files
sborisenkoxtetiana-karasovat-karasovagcf-owl-bot[bot]kweinmeister
authored andcommitted
docs(samples): Refactoring product package (CRUD) (#417)
* 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]>
1 parent 6600b6d commit 5ee4c2c

17 files changed

+215
-210
lines changed

retail/interactive-tutorials/src/main/java/product/AddFulfillmentPlaces.java

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,57 +31,55 @@
3131
import java.io.IOException;
3232
import java.time.Instant;
3333
import java.util.UUID;
34+
import java.util.concurrent.TimeUnit;
3435

3536
public class AddFulfillmentPlaces {
3637

3738
public static void main(String[] args) throws IOException, InterruptedException {
38-
// TODO(developer): Replace these variables before running the sample.
3939
String projectId = ServiceOptions.getDefaultProjectId();
4040
String generatedProductId = UUID.randomUUID().toString();
4141
String productName =
4242
String.format(
4343
"projects/%s/locations/global/catalogs/default_catalog/branches/0/products/%s",
4444
projectId, generatedProductId);
45-
Timestamp currentDate =
46-
Timestamp.newBuilder()
47-
.setSeconds(Instant.now().getEpochSecond())
48-
.setNanos(Instant.now().getNano())
49-
.build();
45+
5046
createProduct(generatedProductId);
51-
System.out.printf("Add fulfilment places with current date: %s", currentDate);
52-
addFulfillmentPlaces(productName, currentDate, "store2");
47+
addFulfillmentPlaces(productName, "store2");
5348
getProduct(productName);
5449
deleteProduct(productName);
5550
}
5651

57-
public static void addFulfillmentPlaces(String productName, Timestamp timestamp, String placeId)
52+
public static void addFulfillmentPlaces(String productName, String placeId)
5853
throws IOException, InterruptedException {
59-
AddFulfillmentPlacesRequest addFulfillmentRequest =
60-
getAddFulfillmentRequest(productName, timestamp, placeId);
61-
try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
62-
serviceClient.addFulfillmentPlacesAsync(addFulfillmentRequest);
63-
/*
64-
This is a long-running operation and its result is not immediately
65-
present with get operations,thus we simulate wait with sleep method.
66-
*/
67-
System.out.println("Add fulfillment places, wait 30 seconds: ");
68-
Thread.sleep(30_000);
69-
}
70-
}
54+
Timestamp currentDate =
55+
Timestamp.newBuilder()
56+
.setSeconds(Instant.now().getEpochSecond())
57+
.setNanos(Instant.now().getNano())
58+
.build();
7159

72-
public static AddFulfillmentPlacesRequest getAddFulfillmentRequest(
73-
String productName, Timestamp timestamp, String placeId) {
74-
AddFulfillmentPlacesRequest addfulfillmentPlacesRequest =
60+
System.out.printf("Add fulfilment places with current date: %s", currentDate);
61+
62+
AddFulfillmentPlacesRequest addFulfillmentPlacesRequest =
7563
AddFulfillmentPlacesRequest.newBuilder()
7664
.setProduct(productName)
7765
.setType("pickup-in-store")
7866
.addPlaceIds(placeId)
79-
.setAddTime(timestamp)
67+
.setAddTime(currentDate)
8068
.setAllowMissing(true)
8169
.build();
82-
System.out.println("Add fulfillment request " + addfulfillmentPlacesRequest);
70+
System.out.println("Add fulfillment request " + addFulfillmentPlacesRequest);
8371

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

retail/interactive-tutorials/src/main/java/product/CreateProduct.java

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
public class CreateProduct {
3838

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

5150
// call the Retail API to create product
5251
public static Product createProduct(String productId, String branchName) throws IOException {
52+
float price = 30.0f;
53+
float originalPrice = 35.5f;
54+
55+
PriceInfo priceInfo =
56+
PriceInfo.newBuilder()
57+
.setPrice(price)
58+
.setOriginalPrice(originalPrice)
59+
.setCurrencyCode("USD")
60+
.build();
61+
62+
Product generatedProduct =
63+
Product.newBuilder()
64+
.setTitle("Nest Mini")
65+
.setType(Type.PRIMARY)
66+
.addCategories("Speakers and displays")
67+
.addBrands("Google")
68+
.setPriceInfo(priceInfo)
69+
.setAvailability(Availability.IN_STOCK)
70+
.build();
71+
5372
CreateProductRequest createProductRequest =
5473
CreateProductRequest.newBuilder()
55-
.setProduct(generateProduct())
74+
.setProduct(generatedProduct)
5675
.setProductId(productId)
5776
.setParent(branchName)
5877
.build();
5978
System.out.printf("Create product request: %s%n", createProductRequest);
6079

80+
// Initialize client that will be used to send requests. This client only
81+
// needs to be created once, and can be reused for multiple requests. After
82+
// completing all of your requests, call the "close" method on the client to
83+
// safely clean up any remaining background resources.
6184
try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
6285
Product createdProduct = serviceClient.createProduct(createProductRequest);
6386
System.out.printf("Created product: %s%n", createdProduct);
6487
return createdProduct;
6588
}
6689
}
67-
68-
// generate product for create
69-
public static Product generateProduct() {
70-
float price = 30.0f;
71-
float originalPrice = 35.5f;
72-
73-
PriceInfo priceInfo =
74-
PriceInfo.newBuilder()
75-
.setPrice(price)
76-
.setOriginalPrice(originalPrice)
77-
.setCurrencyCode("USD")
78-
.build();
79-
80-
return Product.newBuilder()
81-
.setTitle("Nest Mini")
82-
.setType(Type.PRIMARY)
83-
.addCategories("Speakers and displays")
84-
.addBrands("Google")
85-
.setPriceInfo(priceInfo)
86-
.setAvailability(Availability.IN_STOCK)
87-
.build();
88-
}
8990
}
9091

9192
// [END retail_create_product]

retail/interactive-tutorials/src/main/java/product/CrudProduct.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
public class CrudProduct {
4040

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

56-
// generate product for create
55+
// Generate product for create
5756
public static Product generateProduct() {
5857
float price = 30.0f;
5958
float originalPrice = 35.5f;
@@ -75,7 +74,7 @@ public static Product generateProduct() {
7574
.build();
7675
}
7776

78-
// generate product for update
77+
// Generate product for update
7978
public static Product generateProductForUpdate(String productId, String productName) {
8079
final float price = 20.0f;
8180
final float originalPrice = 25.5f;
@@ -99,7 +98,7 @@ public static Product generateProductForUpdate(String productId, String productN
9998
.build();
10099
}
101100

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

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

118-
// get product
122+
// Get product
119123
public static Product getProduct(String productName) throws IOException {
120124
Product product = Product.newBuilder().build();
121125

122126
GetProductRequest getProductRequest =
123127
GetProductRequest.newBuilder().setName(productName).build();
124128

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

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

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

150-
// delete product
162+
// Delete product
151163
public static void deleteProduct(String productName) throws IOException {
152164
DeleteProductRequest deleteProductRequest =
153165
DeleteProductRequest.newBuilder().setName(productName).build();
154166
System.out.printf("Delete product request %s%n", deleteProductRequest);
155167

168+
// Initialize client that will be used to send requests. This client only
169+
// needs to be created once, and can be reused for multiple requests. After
170+
// completing all of your requests, call the "close" method on the client to
171+
// safely clean up any remaining background resources.
156172
try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
157173
serviceClient.deleteProduct(deleteProductRequest);
158174
System.out.printf("Product %s was deleted.%n", productName);

retail/interactive-tutorials/src/main/java/product/DeleteProduct.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public static void deleteProduct(String productName) throws IOException {
4444
DeleteProductRequest.newBuilder().setName(productName).build();
4545
System.out.printf("Delete product request %s%n", deleteProductRequest);
4646

47+
// Initialize client that will be used to send requests. This client only
48+
// needs to be created once, and can be reused for multiple requests. After
49+
// completing all of your requests, call the "close" method on the client to
50+
// safely clean up any remaining background resources.
4751
try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
4852
serviceClient.deleteProduct(deleteProductRequest);
4953
System.out.printf("Product %s was deleted.%n", productName);

retail/interactive-tutorials/src/main/java/product/GetProduct.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,12 @@ public static Product getProduct(String productName) throws IOException {
4949
GetProductRequest getProductRequest =
5050
GetProductRequest.newBuilder().setName(productName).build();
5151

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

retail/interactive-tutorials/src/main/java/product/RemoveFulfillmentPlaces.java

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -30,76 +30,56 @@
3030
import com.google.protobuf.Timestamp;
3131
import java.io.IOException;
3232
import java.time.Instant;
33-
import java.time.temporal.ChronoUnit;
3433
import java.util.UUID;
34+
import java.util.concurrent.TimeUnit;
3535

3636
public class RemoveFulfillmentPlaces {
3737

3838
public static void main(String[] args) throws IOException, InterruptedException {
39-
// TODO(developer): Replace these variables before running the sample.
4039
String projectId = ServiceOptions.getDefaultProjectId();
4140
String generatedProductId = UUID.randomUUID().toString();
4241
String productName =
4342
String.format(
4443
"projects/%s/locations/global/catalogs/default_catalog/branches/0/products/%s",
4544
projectId, generatedProductId);
46-
Timestamp currentDate =
47-
Timestamp.newBuilder()
48-
.setSeconds(Instant.now().getEpochSecond())
49-
.setNanos(Instant.now().getNano())
50-
.build();
51-
/*
52-
* The time when the fulfillment updates are issued. If set with outdated time
53-
* (yesterday), the fulfillment information will not updated.
54-
*/
55-
Timestamp outdatedDate =
56-
Timestamp.newBuilder()
57-
.setSeconds(Instant.now().minus(1, ChronoUnit.DAYS).getEpochSecond())
58-
.setNanos(Instant.now().getNano())
59-
.build();
6045

6146
createProduct(generatedProductId);
62-
System.out.printf("Remove fulfilment places with current date: %s", currentDate);
63-
removeFulfillmentPlaces(productName, currentDate, "store0");
64-
getProduct(productName);
65-
System.out.printf("Remove outdated fulfilment places: %s", outdatedDate);
66-
removeFulfillmentPlaces(productName, outdatedDate, "store1");
47+
removeFulfillmentPlaces(productName, "store0");
6748
getProduct(productName);
6849
deleteProduct(productName);
6950
}
7051

7152
// remove fulfillment places to product
72-
public static void removeFulfillmentPlaces(
73-
String productName, Timestamp timestamp, String storeId)
53+
public static void removeFulfillmentPlaces(String productName, String storeId)
7454
throws IOException, InterruptedException {
75-
RemoveFulfillmentPlacesRequest removeFulfillmentRequest =
76-
getRemoveFulfillmentRequest(productName, timestamp, storeId);
77-
78-
try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
79-
serviceClient.removeFulfillmentPlacesAsync(removeFulfillmentRequest);
80-
/*
81-
This is a long-running operation and its result is not immediately
82-
present with get operations,thus we simulate wait with sleep method.
83-
*/
84-
System.out.println("Remove fulfillment places, wait 30 seconds.");
85-
Thread.sleep(30_000);
86-
}
87-
}
55+
Timestamp currentDate =
56+
Timestamp.newBuilder()
57+
.setSeconds(Instant.now().getEpochSecond())
58+
.setNanos(Instant.now().getNano())
59+
.build();
8860

89-
// remove fulfillment request
90-
public static RemoveFulfillmentPlacesRequest getRemoveFulfillmentRequest(
91-
String productName, Timestamp timestamp, String storeId) {
61+
System.out.printf("Remove fulfilment places with current date: %s", currentDate);
9262
RemoveFulfillmentPlacesRequest removeFulfillmentRequest =
9363
RemoveFulfillmentPlacesRequest.newBuilder()
9464
.setProduct(productName)
9565
.setType("pickup-in-store")
9666
.addPlaceIds(storeId)
97-
.setRemoveTime(timestamp)
67+
.setRemoveTime(currentDate)
9868
.setAllowMissing(true)
9969
.build();
10070
System.out.println("Remove fulfillment request " + removeFulfillmentRequest);
10171

102-
return removeFulfillmentRequest;
72+
// Initialize client that will be used to send requests. This client only
73+
// needs to be created once, and can be reused for multiple requests. After
74+
// completing all of your requests, call the "close" method on the client to
75+
// safely clean up any remaining background resources.
76+
try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
77+
serviceClient.removeFulfillmentPlacesAsync(removeFulfillmentRequest);
78+
// This is a long-running operation and its result is not immediately
79+
// present with get operations,thus we simulate wait with sleep method.
80+
System.out.println("Remove fulfillment places, wait 30 seconds.");
81+
TimeUnit.SECONDS.sleep(30);
82+
}
10383
}
10484
}
10585

0 commit comments

Comments
 (0)