Java客户端com.softlayer.api.service.product.Package getItemPric


  1. 我可以列出所有包,并使用packageId缩小包对象的范围

ApiClient client = new RestApiClient().withCredentials(username, apikey); Package.Service service = Package.service(client); List<Package> packages = service.getAllObjects(); chosenPackage = pkg;

  1. 选择的包显示有效的地址选择的包:com.softlayer.api.service.product.Package@41a0aa7d
  2. 现在,我想列出所选的Package.getItemPrices()、chosenPackage.getConfiguration()等,所有这些值都是空的
  3. 我没有为com.softlayer.api.service.product.Package找到任何方法setMask()或withMask(
  4. 你能提供一些小代码,以获得项目价格吗?我使用的是SoftLayer Java客户端

请尝试以下示例:

package com.softlayer.api.Package;
import com.google.gson.Gson;
import com.softlayer.api.ApiClient;
import com.softlayer.api.RestApiClient;
import com.softlayer.api.service.product.Package;
/**
* This script retrieves a all packages from SoftLayer_Product_Package::getAllObjects method
* and their item prices and configuration
*
* Important Manual Page:
* http://sldn.softlayer.com/reference/services/SoftLayer_Product_Package/getAllObjects
* http://sldn.softlayer.com/reference/datatypes/SoftLayer_Product_Package
*
* @license <http://sldn.softlayer.com/article/License>
* @authon SoftLayer Technologies, Inc. <sldn@softlayer.com>
* @version 0.2.2
*/
public class GetPackages {
/**
* This is the constructor, is used to get all packages and their item prices
*/
public GetPackages() {
// Declare your SoftLayer username and apiKey
String username = "set me";
String apiKey = "set me";
// Create client
ApiClient client = new RestApiClient().withCredentials(username, apiKey);
// Define SoftLayer_Product_Package service
Package.Service packageService = Package.service(client);
// Declare and object mask
packageService.withMask().itemPrices();
packageService.withMask().configuration();
Gson gson = new Gson();
try {
for(Package pack : packageService.getAllObjects())
{
System.out.println(gson.toJson(pack.getItemPrices()));
System.out.println(gson.toJson(pack.getConfiguration()));
}
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
/**
* This is the main method which makes use of GetPackages method.
*
* @param args
* @return Nothing
*/
public static void main(String[] args) {
new GetPackages();
}

}

请告诉我你对此有任何疑问。

参考

  • SoftLayer_Product_Package::getAllObjects

您也可以尝试这个例子。

package SoftLayer_Java_Scripts.Examples;
import java.util.List;
import com.softlayer.api.ApiClient;
import com.softlayer.api.RestApiClient;
import com.softlayer.api.service.product.Package;
import com.softlayer.api.service.product.Item;
public class GetProductItemsById {
public static void main(String[] args){
String username = "set me";
String apikey = "set me";
ApiClient client = new RestApiClient().withCredentials(username, apikey);
Package.Service service = Package.service(client);
try
{
List<Package> packageList = service.getAllObjects();
for(Package pack : packageList){
System.out.println("------- Package: " + pack.getName() + " - Id: " + pack.getId());
service = Package.service(client, pack.getId());
List<Item> itemList = service.getItems();
for(Item item : itemList){
System.out.println("Item description: " + item.getDescription());
System.out.println("Item Id: " + item.getId());
}  
}
}
catch(Exception e)
{
System.out.println("Script failed, review the next message for further details: " + e); 
}
}
}