All Products
Search
Document Center

Alibaba Cloud SDK:Generic calls

Last Updated:Oct 11, 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 .NET

Run the following command in the terminal to install the core library of Alibaba Cloud SDK V2.0 for .NET. For more information about the latest version of the core library, see AlibabaCloud.OpenApiClient.

dotnet add package AlibabaCloud.OpenApiClient --version 0.1.8

Sample code

Call an RPC API operation

In this example, the DescribeInstanceTypeFamilies operation of Elastic Compute Service (ECS) is called to show how to make a generic call of an operation.

namespace AlibabaCloud.SDK.Sample
{
    public class Sample 
    {

        public static AlibabaCloud.OpenApiClient.Client CreateClient()
        {
            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured in the code runtime environment. 
                AccessKeyId = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured in the code runtime environment. 
                AccessKeySecret = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
            };
            config.Endpoint = "ecs-cn-hangzhou.aliyuncs.com";
            return new AlibabaCloud.OpenApiClient.Client(config);
        }

        public static AlibabaCloud.OpenApiClient.Models.Params CreateApiInfo()
        {
            AlibabaCloud.OpenApiClient.Models.Params params_ = new AlibabaCloud.OpenApiClient.Models.Params
            {
                // The operation that you want to call.
                Action = "DescribeInstanceTypeFamilies",
                // The version number of the operation.
                Version = "2014-05-26",
                // The protocol over which the operation is called.
                Protocol = "HTTPS",
                // The HTTP method of the operation.
                Method = "POST",
                AuthType = "AK",
                Style = "RPC",
                // The URL of the operation.
                Pathname = "/",
                // The format of the request body.
                ReqBodyType = "json",
                // The format of the response body.
                BodyType = "json",
            };
            return params_;
        }

        public static void Main(string[] args)
        {
            AlibabaCloud.OpenApiClient.Client client = CreateClient();
            AlibabaCloud.OpenApiClient.Models.Params params_ = CreateApiInfo();
            // query params
            Dictionary<string, object> queries = new Dictionary<string, object>(){};
            queries["RegionId"] = "cn-hangzhou";
            // runtime options
            AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
            AlibabaCloud.OpenApiClient.Models.OpenApiRequest request = new AlibabaCloud.OpenApiClient.Models.OpenApiRequest
            {
                Query = AlibabaCloud.OpenApiUtil.Client.Query(queries),
            };
            // If you copy and run the sample code, write your code to display the response of the operation.
            // The response is of the MAP type, which contains the response body, response headers, and HTTP status code. 
            var response = client.CallApi(params_, request, runtime);
            Console.WriteLine(response["statusCode"]);
        }
    }
}

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.

namespace AlibabaCloud.SDK.Sample
{
    public class Sample 
    {

        public static AlibabaCloud.OpenApiClient.Client CreateClient()
        {
            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured in the code runtime environment. 
                AccessKeyId = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured in the code runtime environment. 
                AccessKeySecret = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
            };
            config.Endpoint = "cs.cn-hangzhou.aliyuncs.com";
            return new AlibabaCloud.OpenApiClient.Client(config);
        }

        public static AlibabaCloud.OpenApiClient.Models.Params CreateApiInfo()
        {
            AlibabaCloud.OpenApiClient.Models.Params params_ = new AlibabaCloud.OpenApiClient.Models.Params
            {
                // The operation that you want to call.
                Action = "DescribeClustersV1",
                // The version number of the operation.
                Version = "2015-12-15",
                // The protocol over which the operation is called.
                Protocol = "HTTPS",
                // The HTTP method of the operation.
                Method = "GET",
                AuthType = "AK",
                Style = "ROA",
                // The URL of the operation.
                Pathname = "/api/v1/clusters",
                // The format of the request body.
                ReqBodyType = "json",
                // The format of the response body.
                BodyType = "json",
            };
            return params_;
        }

        public static void Main(string[] args)
        {
            AlibabaCloud.OpenApiClient.Client client = CreateClient();
            AlibabaCloud.OpenApiClient.Models.Params params_ = CreateApiInfo();
            // runtime options
            AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
            AlibabaCloud.OpenApiClient.Models.OpenApiRequest request = new AlibabaCloud.OpenApiClient.Models.OpenApiRequest();
            // If you copy and run the sample code, write your code to display the response of the operation.
            // The response is of the MAP type, which contains the response body, response headers, and HTTP status code. 
            var response = client.CallApi(params_, request, runtime);
            Console.WriteLine(response["statusCode"]);
        }
    }
}