All Products
Search
Document Center

Alibaba Cloud SDK:Use CommonRequest

Last Updated:Apr 06, 2022

Note

If you want to call the API operations of a cloud service that does not provide SDKs, you can use CommonRequest. You can use CommonRequest to call API operations.

Benefits

CommonRequest provides the following benefits:

  1. Lightweight: You need to download only the core package. You do not need to download and then install an SDK.

  2. Easy to use: You can call the newly released API operations without the need to update the SDK.

  3. Fast iteration.

Use CommonRequest to call an API operation

Alibaba Cloud provides RPC and RESTful APIs. The request method of CommonRequest varies based on the architectural style of the API that you want to call.

In most cases, an API uses the RPC style if the API operations include the Action parameter. An API uses the RESTful style if the API operations include the PathPattern parameter. All API operations of a service use the same API style. The API of each service supports only one style. If you pass an incorrect identifier, another API operation is called, or the ApiNotFound error is returned.

If you want to send a CommonRequest request, you must obtain the values of the following parameters. For information about the values of these parameters, see the API reference in Documentation. For information about the parameter values of an API, visit OpenAPI Explorer.

  • Domain: the endpoint of a service.

  • Version: the version of the API. The version is in the YYYY-MM-DD format.

  • Operation information: the name of the API operation that you want to call.

    • The APIs of most Alibaba Cloud services, such as Elastic Compute Service (ECS) and ApsaraDB RDS, are RPC APIs. If you want to call an RPC API operation, you must obtain the value of the Action parameter, and then specify the value in the request.ApiName = "<Action>" format.

      • For example, if you want to use CommonRequest to call the RunInstances operation, specify the operation name in the request.ApiName = "RunInstances" format.

    • If you want to call a RESTful API operation, for example, an operation for Container Service for Kubernetes (ACK), you must obtain the value of the PathPattern parameter, and then specify the RESTful path in the request.PathPattern = "<PathPattern>" format.

      • For example, if the value of the PathPattern parameter of the API operation that is used to query all clusters of ACK is /clusters and you want to send a CommonRequest request, you must specify the RESTful path in the request.PathPattern = "/clusters" format.

Example: Call an RPC API operation

The following code shows how to use CommonRequest to call the DescribeInstanceStatus operation of ECS:

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
client = AcsClient('{your_access_key_id}', '{your_access_key_secret}', '{your_region_id}')
request = CommonRequest()
request.set_domain('ecs.aliyuncs.com')
request.set_version('2014-05-26')
request.set_action_name('DescribeInstanceStatus')
# or:
# request = CommonRequest(domain='ecs.aliyuncs.com', version='2014-05-26', action_name='DescribeInstanceStatus')
request.add_query_param('PageNumber', '1')
request.add_query_param('PageSize', '30')
response = client.do_action_with_exception(request)

Example: Call a RESTful API operation

The following code shows how to use CommonRequest to call an API operation of ACK to query all cluster instances:

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
client = AcsClient('{your_access_key_id}', '{your_access_key_secret}', '{your_region_id}')
request = CommonRequest()
request.set_domain('cs.aliyuncs.com')
request.set_version('2015-12-15')
request.set_uri_pattern('/clusters')
# or:
# request = CommonRequest(domain='ecs.aliyuncs.com', version='2014-05-26', uri_pattern='/clusters')
response = client.do_action_with_exception(request)