All Products
Search
Document Center

Alibaba Cloud SDK:Generic calls

最終更新日:Aug 12, 2024

You can make generic calls and specialized calls of operations by using Alibaba Cloud SDKs. For more information about the differences between the generic calls and specialized calls, see Generic calls and specialized calls. This topic describes how to view API information and install SDK for Java V2.0, and provide sample code. This helps you make generic calls of operations.

View API information

Go to the Alibaba Cloud API Documentation page and select an Alibaba Cloud service. In this example, Elastic Compute Service (ECS) is selected.

  1. In the left-side pane, click Metadata below the cloud service name. On the page that appears, find the info.style parameter to view the API style supported by the cloud service, such as remote procedure call (RPC) or resource-oriented architecture (ROA).

    image

    image

    Note

    The metadata displayed on the page contains all API information of the cloud service. If you want to view the metadata of an API operation, go to Step 2.

  2. Select the API operation that you want to call and click Metadata in the upper-right corner of the page.

    image

    The metadata of an API operation defines the network protocols, request methods, parameters, and parameter locations supported by the API operation. As shown in the following figure, the metadata of the RunInstances operation contains the following information:

    • Supported network protocols: HTTP and HTTPS. We recommend that you use HTTPS.

    • Supported request methods: GET and POST. You can use the two request methods to obtain the same response. However, if you use the GET method, a request packet can be up to 32 KB in size. We recommend that you use the POST method.

    • Supported parameters include RegionId and ImageId. The parameter location is query, which indicates that these parameters are concatenated and placed after the request URL. Example: https://ecs.cn-beijing.aliyuncs.com/?ImageId=aliyun_2_1903_x64_20G_alibase_20231221.vhd&InstanceChargeType=PostPaid&InstanceType=ecs.e-c1m1.large&InternetChargeType=PayByTraffic&MinAmount=1&Password=test%401234&RegionId=cn-beijing&SecurityGroupId=sg-2zec0dm6qi66XXXXXXXX&SystemDisk.Category=cloud_essd&SystemDisk.Size=40&VSwitchId=vsw-2ze3aagwn397gXXXXXXXX.

    image

    Note

    Other content of the metadata has no impact on signature calculation. For more information about the metadata, see Guidelines for use.

Install SDK for Java V2.0

Add the following dependency to the pom.xml file to install the core library of SDK for Java V2.0.

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>tea-openapi</artifactId>
    <version>0.2.8</version>
</dependency>

Sample code

Call an RPC API operation

In this example, the DescribeRegions operation of ECS is called to show how to make a generic call of an operation.

import com.aliyun.teaopenapi.Client;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teaopenapi.models.OpenApiRequest;
import com.aliyun.teaopenapi.models.Params;
import com.aliyun.teautil.models.RuntimeOptions;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import static com.aliyun.openapiutil.Client.query;

public class Sample {
    public static void main(String[] args) throws Exception {
        Config config = new Config()
                .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
                .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
                .setEndpoint("ecs-cn-hangzhou.aliyuncs.com");
        Client client = new Client(config);
        Params params = new Params()
                .setStyle("RPC")  // The API style.
                .setVersion("2014-05-26") // The version number of the operation.
                .setMethod("POST") // The HTTP method of the operation.
                .setAction("DescribeRegions")  // The operation that you want to call.
                .setPathname("/") // The URL of the operation.
                .setProtocol("HTTPS")
                .setAuthType("AK")
                .setReqBodyType("json")
                .setBodyType("json");
        // Specify the query parameters.
        Map<String, Object> queries = new HashMap<>();
        queries.put("InstanceChargeType", "PrePaid");
        // Create a runtime configuration object.
        RuntimeOptions runtime = new RuntimeOptions();
        OpenApiRequest request = new OpenApiRequest()
                .setQuery(query(queries));
        // The response is of the MAP type, which contains the response body, response headers, and HTTP status code. 
        Map<String, ?> response = client.callApi(params, request, runtime);
        System.out.println(new Gson().toJson(response));
    }
}

Call a RESTful API operation

In this example, the DescribeClustersV1 operation of Container Service for Kubernetes (ACK) is called to show how to make a generic call of an operation.

import com.aliyun.teaopenapi.Client;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teaopenapi.models.OpenApiRequest;
import com.aliyun.teaopenapi.models.Params;
import com.aliyun.teautil.models.RuntimeOptions;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import static com.aliyun.openapiutil.Client.query;

public class Test3 {
    public static void main(String[] args_) throws Exception {
        Config config = new Config()
                .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
                .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
                .setEndpoint("cs.cn-qingdao.aliyuncs.com");
        Client client = new Client(config);
        Params params = new Params()
                .setStyle("ROA")
                // The version number of the operation.
                .setVersion("2015-12-15")
                // The operation that you want to call.
                .setAction("DescribeClustersV1")
                // The URL of the operation.
                .setPathname("/api/v1/clusters")
                // The protocol of the operation.
                .setProtocol("HTTPS")
                // The HTTP method of the operation.
                .setMethod("GET")
                .setAuthType("AK")
                // The format of the request body.
                .setReqBodyType("json")
                // The format of the response body.
                .setBodyType("json");
        // Specify the query parameters.
        Map<String, Object> queries = new HashMap<>();
        queries.put("name", "cluster-demo");
        OpenApiRequest request = new OpenApiRequest()
                .setQuery(query(queries));
        RuntimeOptions runtime = new RuntimeOptions();
        // The response is of the MAP type, which contains the response body, response headers, and HTTP status code. 
        Map<String, ?> response = client.callApi(params, request, runtime);
        System.out.println(new Gson().toJson(response));
    }
}