Skip to content

Commit 2e01c63

Browse files
sborisenkoxgcf-owl-bot[bot]pmichalskiGDNeenu1995
authored
chore(samples): Retail Tutorials. CRUD products (#301)
* Add CRUD products. * Add kokoro configuration. * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Code changes. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Minor fixes. * * update wait to use Thread.sleep * remove add fulfillment place request with outdated request * added fulfilment places to default product * modified set inventory request to also set available quantity * cleaned typos and unused imports * Fixes. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Piotr Michalski <[email protected]> Co-authored-by: Neenu Shaji <[email protected]>
1 parent d8f3f96 commit 2e01c63

17 files changed

+1317
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* [START retail_add_fulfillment_places]
19+
*/
20+
21+
package product;
22+
23+
import static setup.SetupCleanup.createProduct;
24+
import static setup.SetupCleanup.deleteProduct;
25+
import static setup.SetupCleanup.getProduct;
26+
27+
import com.google.cloud.retail.v2.AddFulfillmentPlacesRequest;
28+
import com.google.cloud.retail.v2.ProductServiceClient;
29+
import com.google.protobuf.Timestamp;
30+
import java.io.IOException;
31+
import java.time.Instant;
32+
import java.util.UUID;
33+
34+
public class AddFulfillmentPlaces {
35+
36+
public static void main(String[] args) throws IOException, InterruptedException {
37+
// TODO(developer): Replace these variables before running the sample.
38+
String projectId = System.getenv("PROJECT_ID");
39+
String generatedProductId = UUID.randomUUID().toString();
40+
String productName =
41+
String.format(
42+
"projects/%s/locations/global/catalogs/default_catalog/branches/"
43+
+ "default_branch/products/%s",
44+
projectId, generatedProductId);
45+
Timestamp currentDate =
46+
Timestamp.newBuilder()
47+
.setSeconds(Instant.now().getEpochSecond())
48+
.setNanos(Instant.now().getNano())
49+
.build();
50+
createProduct(generatedProductId);
51+
System.out.printf("Add fulfilment places with current date: %s", currentDate);
52+
addFulfillmentPlaces(productName, currentDate, "store2");
53+
getProduct(productName);
54+
deleteProduct(productName);
55+
}
56+
57+
public static void addFulfillmentPlaces(String productName, Timestamp timestamp, String placeId)
58+
throws IOException, InterruptedException {
59+
AddFulfillmentPlacesRequest addFulfillmentRequest =
60+
getAddFulfillmentRequest(productName, timestamp, placeId);
61+
ProductServiceClient.create().addFulfillmentPlacesAsync(addFulfillmentRequest);
62+
/*
63+
This is a long-running operation and its result is not immediately
64+
present with get operations,thus we simulate wait with sleep method.
65+
*/
66+
System.out.println("Add fulfillment places, wait 30 seconds: ");
67+
Thread.sleep(30_000);
68+
}
69+
70+
public static AddFulfillmentPlacesRequest getAddFulfillmentRequest(
71+
String productName, Timestamp timestamp, String placeId) {
72+
AddFulfillmentPlacesRequest addfulfillmentPlacesRequest =
73+
AddFulfillmentPlacesRequest.newBuilder()
74+
.setProduct(productName)
75+
.setType("pickup-in-store")
76+
.addPlaceIds(placeId)
77+
.setAddTime(timestamp)
78+
.setAllowMissing(true)
79+
.build();
80+
System.out.println("Add fulfillment request " + addfulfillmentPlacesRequest);
81+
82+
return addfulfillmentPlacesRequest;
83+
}
84+
}
85+
86+
// [END retail_add_fulfillment_places]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// [START retail_create_product]
18+
19+
/*
20+
* Create product in a catalog using Retail API
21+
*/
22+
23+
package product;
24+
25+
import static setup.SetupCleanup.deleteProduct;
26+
27+
import com.google.cloud.retail.v2.CreateProductRequest;
28+
import com.google.cloud.retail.v2.PriceInfo;
29+
import com.google.cloud.retail.v2.Product;
30+
import com.google.cloud.retail.v2.Product.Availability;
31+
import com.google.cloud.retail.v2.Product.Type;
32+
import com.google.cloud.retail.v2.ProductServiceClient;
33+
import java.io.IOException;
34+
import java.util.UUID;
35+
36+
public class CreateProduct {
37+
38+
public static void main(String[] args) throws IOException {
39+
// TODO(developer): Replace these variables before running the sample.
40+
String projectId = System.getenv("PROJECT_ID");
41+
String defaultBranchName =
42+
String.format(
43+
"projects/%s/locations/global/catalogs/default_catalog/" + "branches/default_branch",
44+
projectId);
45+
String generatedProductId = UUID.randomUUID().toString();
46+
47+
Product createdProduct = createProduct(generatedProductId, defaultBranchName);
48+
deleteProduct(createdProduct.getName());
49+
}
50+
51+
// call the Retail API to create product
52+
public static Product createProduct(String productId, String defaultBranchName)
53+
throws IOException {
54+
CreateProductRequest createProductRequest =
55+
CreateProductRequest.newBuilder()
56+
.setProduct(generateProduct())
57+
.setProductId(productId)
58+
.setParent(defaultBranchName)
59+
.build();
60+
System.out.printf("Create product request: %s%n", createProductRequest);
61+
62+
Product createdProduct = ProductServiceClient.create().createProduct(createProductRequest);
63+
System.out.printf("Created product: %s%n", createdProduct);
64+
65+
return createdProduct;
66+
}
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+
}
89+
}
90+
91+
// [END retail_create_product]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// [START retail_crud_product]
18+
19+
/*
20+
* Create product in a catalog using Retail API
21+
*/
22+
23+
package product;
24+
25+
import com.google.api.gax.rpc.NotFoundException;
26+
import com.google.cloud.retail.v2.CreateProductRequest;
27+
import com.google.cloud.retail.v2.DeleteProductRequest;
28+
import com.google.cloud.retail.v2.GetProductRequest;
29+
import com.google.cloud.retail.v2.PriceInfo;
30+
import com.google.cloud.retail.v2.Product;
31+
import com.google.cloud.retail.v2.Product.Availability;
32+
import com.google.cloud.retail.v2.Product.Type;
33+
import com.google.cloud.retail.v2.ProductServiceClient;
34+
import com.google.cloud.retail.v2.UpdateProductRequest;
35+
import java.io.IOException;
36+
import java.util.UUID;
37+
38+
public class CrudProduct {
39+
40+
public static void main(String[] args) throws IOException {
41+
// TODO(developer): Replace these variables before running the sample.
42+
String projectId = System.getenv("PROJECT_ID");
43+
String generatedProductId = UUID.randomUUID().toString();
44+
String defaultBranchName =
45+
String.format(
46+
"projects/%s/locations/global/catalogs/default_catalog/" + "branches/default_branch",
47+
projectId);
48+
String productName = String.format("%s/products/%s", defaultBranchName, generatedProductId);
49+
50+
Product createdProduct = createProduct(generatedProductId, defaultBranchName);
51+
getProduct(productName);
52+
updateProduct(createdProduct, productName);
53+
deleteProduct(productName);
54+
}
55+
56+
// generate product for create
57+
public static Product generateProduct() {
58+
float price = 30.0f;
59+
float originalPrice = 35.5f;
60+
61+
PriceInfo priceInfo =
62+
PriceInfo.newBuilder()
63+
.setPrice(price)
64+
.setOriginalPrice(originalPrice)
65+
.setCurrencyCode("USD")
66+
.build();
67+
68+
return Product.newBuilder()
69+
.setTitle("Nest Mini")
70+
.setType(Type.PRIMARY)
71+
.addCategories("Speakers and displays")
72+
.addBrands("Google")
73+
.setPriceInfo(priceInfo)
74+
.setAvailability(Availability.IN_STOCK)
75+
.build();
76+
}
77+
78+
// generate product for update
79+
public static Product generateProductForUpdate(String productId, String productName) {
80+
final float price = 20.0f;
81+
final float originalPrice = 25.5f;
82+
83+
PriceInfo priceInfo =
84+
PriceInfo.newBuilder()
85+
.setPrice(price)
86+
.setOriginalPrice(originalPrice)
87+
.setCurrencyCode("EUR")
88+
.build();
89+
90+
return Product.newBuilder()
91+
.setId(productId)
92+
.setName(productName)
93+
.setTitle("Updated Nest Mini")
94+
.setType(Type.PRIMARY)
95+
.addCategories("Updated Speakers and displays")
96+
.addBrands("Updated Google")
97+
.setAvailability(Availability.OUT_OF_STOCK)
98+
.setPriceInfo(priceInfo)
99+
.build();
100+
}
101+
102+
// call the Retail API to create product
103+
public static Product createProduct(String productId, String defaultBranchName)
104+
throws IOException {
105+
CreateProductRequest createProductRequest =
106+
CreateProductRequest.newBuilder()
107+
.setProduct(generateProduct())
108+
.setProductId(productId)
109+
.setParent(defaultBranchName)
110+
.build();
111+
System.out.printf("Create product request: %s%n", createProductRequest);
112+
113+
Product createdProduct = ProductServiceClient.create().createProduct(createProductRequest);
114+
System.out.printf("Created product: %s%n", createdProduct);
115+
116+
return createdProduct;
117+
}
118+
119+
// get product
120+
public static Product getProduct(String productName) throws IOException {
121+
Product product = Product.newBuilder().build();
122+
123+
GetProductRequest getProductRequest =
124+
GetProductRequest.newBuilder().setName(productName).build();
125+
126+
try {
127+
product = ProductServiceClient.create().getProduct(getProductRequest);
128+
System.out.println("Get product response: " + product);
129+
return product;
130+
} catch (NotFoundException e) {
131+
System.out.printf("Product %s not found", productName);
132+
return product;
133+
}
134+
}
135+
136+
// update product
137+
public static void updateProduct(Product originalProduct, String productName) throws IOException {
138+
UpdateProductRequest updateProductRequest =
139+
UpdateProductRequest.newBuilder()
140+
.setProduct(generateProductForUpdate(originalProduct.getId(), productName))
141+
.setAllowMissing(true)
142+
.build();
143+
System.out.printf("Update product request: %s%n", updateProductRequest);
144+
145+
Product updatedProduct = ProductServiceClient.create().updateProduct(updateProductRequest);
146+
System.out.printf("Updated product: %s%n", updatedProduct);
147+
}
148+
149+
// delete product
150+
public static void deleteProduct(String productName) throws IOException {
151+
DeleteProductRequest deleteProductRequest =
152+
DeleteProductRequest.newBuilder().setName(productName).build();
153+
System.out.printf("Delete product request %s%n", deleteProductRequest);
154+
155+
ProductServiceClient.create().deleteProduct(deleteProductRequest);
156+
System.out.printf("Product %s was deleted.%n", productName);
157+
}
158+
}
159+
160+
// [END retail_crud_product]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// [START retail_delete_product]
18+
19+
/*
20+
* Delete product from a catalog using Retail API
21+
*/
22+
23+
package product;
24+
25+
import static setup.SetupCleanup.createProduct;
26+
27+
import com.google.cloud.retail.v2.DeleteProductRequest;
28+
import com.google.cloud.retail.v2.ProductServiceClient;
29+
import java.io.IOException;
30+
import java.util.UUID;
31+
32+
public class DeleteProduct {
33+
34+
public static void main(String[] args) throws IOException {
35+
String generatedProductId = UUID.randomUUID().toString();
36+
37+
String createdProductName = createProduct(generatedProductId).getName();
38+
deleteProduct(createdProductName);
39+
}
40+
41+
// call the Retail API to delete product
42+
public static void deleteProduct(String productName) throws IOException {
43+
DeleteProductRequest deleteProductRequest =
44+
DeleteProductRequest.newBuilder().setName(productName).build();
45+
System.out.printf("Delete product request %s%n", deleteProductRequest);
46+
47+
ProductServiceClient.create().deleteProduct(deleteProductRequest);
48+
System.out.printf("Product %s was deleted.%n", productName);
49+
}
50+
}
51+
52+
// [END retail_delete_product]

0 commit comments

Comments
 (0)