Import a commodity library
Updated at:
CloudESL provides an SDK for developers. You can use POP to make API calls to manage your electronic shelf labels (ESLs) in CloudESL.
Before you use the SDK, you must obtain an AccessKey pair. For more information, see Step 4 in Build a CloudESL system. This topic describes how to import a product library using the CloudESL SDK for Java.
To avoid hard-coding your AccessKey pair when you access Alibaba Cloud OpenAPI, use the Alibaba Cloud Credentials tool. You must install the tool to use this method. For more information, see Credentials tool identity verification. You can also use other security methods to manage your AccessKey pair.
POM dependencies:
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-cloudesl</artifactId>
<version>2.0.8</version>
</dependency>Sample code:
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.cloudesl.model.v20200201.BatchInsertItemsRequest;
import com.aliyuncs.cloudesl.model.v20200201.BatchInsertItemsResponse;
import com.aliyuncs.cloudesl.model.v20200201.DescribeItemsRequest;
import com.aliyuncs.cloudesl.model.v20200201.DescribeItemsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import java.util.ArrayList;
import java.util.List;
public class EslPopTest {
/**
* The POP client.
*/
private DefaultAcsClient client;
/**
* The outlet ID.
*/
private String storeId = "";
/**
* Initializes the POP client.
*/
public void init(){
String regionId = "cn-hangzhou";
/**
* Generate an AccessKey pair in the Alibaba Cloud console.
* Initialize the Credentials Client using the default credential.
*/
com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client();
String accessKeyId = credentialClient.getAccessKeyId();
String accessKeySecret = credentialClient.getAccessKeySecret();
/**
* Encapsulates the required parameters.
*/
DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
/**
* Manually sets the Endpoint.
*/
DefaultProfile.addEndpoint(regionId,"cloudesl","cloudesl.cn-hangzhou.aliyuncs.com");
/**
* Creates a POP client.
*/
client = new DefaultAcsClient(profile);
}
/**
* Adds products.
*/
public void insertItem() {
/**
* Initializes the POP client before the call.
*/
init();
/**
* Prepares parameters.
*/
List<BatchInsertItemsRequest.ItemInfo> itemInfos = new ArrayList<BatchInsertItemsRequest.ItemInfo>();
/**
* The product object.
*/
BatchInsertItemsRequest.ItemInfo itemInfo = new BatchInsertItemsRequest.ItemInfo();
// The product barcode.
itemInfo.setItemBarCode("");
// The product ID. Only digits are supported.
itemInfo.setItemId("");
// The product title.
itemInfo.setItemTitle("");
// The price unit.
itemInfo.setPriceUnit("");
// The sales price.
itemInfo.setActionPrice();
// The product specifications.
itemInfo.setSaleSpec("");
/**
* The preceding six parameters are required. Other parameters are optional and can be specified as needed.
*/
// The place of origin.
itemInfo.setProductionPlace("");
// Specifies whether the product is on promotion.
itemInfo.setBePromotion();
// The promotion start time. Use UTC.
itemInfo.setPromotionStart("2020-02-01T08:00:00Z");
// The promotion end time. Use UTC.
itemInfo.setPromotionEnd("2020-02-02T08:00:00Z");
// Specifies whether the product is for members only.
itemInfo.setBeMember(false);
// Specifies whether to enable source tracing.
itemInfo.setBeSourceCode(false);
// The inventory status. OUT_OF_STOCK: The product is out of stock. NORMAL: The product is in stock.
itemInfo.setInventoryStatus("NORMAL");
// The custom template.
itemInfo.setTemplateSceneId("");
/**
* Product object 1.
*/
BatchInsertItemsRequest.ItemInfo itemInfo1 = new BatchInsertItemsRequest.ItemInfo();
itemInfo1.setItemBarCode("");
itemInfo1.setItemId("");
itemInfo1.setItemTitle("");
itemInfo1.setPriceUnit("");
itemInfo1.setActionPrice();
itemInfo1.setSaleSpec("");
/**
* Adds the product objects to a collection. You can bulk insert up to 100 products at a time.
*/
itemInfos.add(itemInfo);
itemInfos.add(itemInfo1);
/**
* Creates a request object and assigns values to its properties.
*/
BatchInsertItemsRequest request = new BatchInsertItemsRequest();
/**
* A POST request must be used for bulk inserts.
*/
request.setSysMethod(MethodType.POST);
request.setStoreId(storeId);
request.setItemInfos(itemInfos);
/**
* Add the following two parameters to the request to obtain Chinese error codes.
*/
request.putQueryParameter("AcceptLanguage","zh-CN");
request.putQueryParameter("Code","apiDynamicErrorCode");
try {
/**
* Sends the request.
*/
BatchInsertItemsResponse acsResponse = client.getAcsResponse(request);
/**
* Processes the response.
*/
if (acsResponse.getSuccess()) {
List<BatchInsertItemsResponse.BatchResult> batchResults = acsResponse.getBatchResults();
if (null != batchResults && batchResults.size() > 0) {
batchResults.forEach(System.out::println);
}
}
} catch (ClientException e) {
// Prints the error message.
System.out.println("POP call failed. ErrorMessage:" + e.getErrorDescription());
}
}
/**
* Queries products.
*/
public void describeItems() {
// Initializes the POP client before the call.
init();
// Prepares parameters.
DescribeItemsRequest request = new DescribeItemsRequest();
// The outlet ID.
request.setStoreId(storeId);
// The page number.
request.setPageNumber(1);
// The number of entries per page.
request.setPageSize(10);
// Add the following two parameters to the request to obtain Chinese error codes.
request.putQueryParameter("AcceptLanguage","zh-CN");
request.putQueryParameter("Code","apiDynamicErrorCode");
try {
// Sends the request.
DescribeItemsResponse acsResponse = client.getAcsResponse(request);
// Processes the response.
if (acsResponse.getSuccess()) {
List<DescribeItemsResponse.ItemInfo> items = acsResponse.getItems();
if (null != items) {
System.out.println(acsResponse.getRequestId());
items.forEach(System.out::println);
}
}
} catch (ClientException e) {
// Prints the error message.
System.out.println("POP call failed. ErrorMessage:" + e.getErrorDescription());
}
}
}Important
Add the outlet ID to the request and itemInfo.
Send the request using the POST method.
Is this page helpful?