Alibaba Cloud SDK call example

Updated at:

The Alibaba Cloud SDK simplifies calling OpenAPI operations, which helps you automate integration with Hybrid Disaster Recovery (HDR) and improve development efficiency. This topic shows how to call the DescribeSitePairs operation.

Step 1: Review the OpenAPI documentation

Read the OpenAPI documentation for the DescribeSitePairs operation to understand the parameters and permissions required to call it: DescribeSitePairs.

Step 2: Create a RAM user and grant permissions

  1. Create a RAM user.

    You can call this API operation by using an Alibaba Cloud account (primary account), a RAM user, or a RAM role. For more information about the differences between these identities, see Identities, credentials, and authorization. This example uses a RAM user. For more information, see Create a RAM user.

  2. Grant permissions to the RAM user.

    1. Go to the RAM User List page. In the row for the target RAM user, click Add Permissions in the Actions column.

    2. In the search box, enter hdr to search for policies. Select the AliyunHdrFullAccess system policy. To create a custom policy, see Authorization information.

      AliyunHdrFullAccess: Grants full permissions to manage Hybrid Disaster Recovery (HDR).

    3. Click OK to complete the authorization.

Calling an Alibaba Cloud API requires an AccessKey for identity authentication. An AccessKey consists of an AccessKey ID and an AccessKey Secret. For information about how to create and obtain an AccessKey, see Create an AccessKey.

(Optional) Step 3: Debug the OpenAPI call online

  1. Use the RAM user to log on to the Alibaba Cloud OpenAPI Portal.

  2. In the left-side navigation pane, find and click the DescribeSitePairs operation. For Service Address, select China (Shanghai). For the SitePairType parameter, enter cloud2cloud.

  3. Click Send Request.

    The Call Result section displays the query result, Request Header, Response Header, and other information. A status code of 200 indicates a successful call.

Step 4: Get the SDK sample code

The OpenAPI platform provides SDKs for multiple programming languages, such as Java, Go, Python, Node.js, TypeScript, PHP, and C++. You can integrate an SDK and use its methods to call the OpenAPI directly. The SDKs handle signing, timeouts, and retries, and return structured response objects based on the documentation, which simplifies development.

The following example shows how to get a Python SDK sample. The steps are the same for other languages.

  1. Go to the Debugging page for this API operation in the OpenAPI Portal.

  2. On the Sample Code tab, select the SDK version and programming language. We recommend that you use version 2.0. For information about the differences between V1.0 and V2.0, see Differences between V1.0 SDK and V2.0 SDK.

  3. In the Parameter Configuration section, fill in the SitePairType parameter.

  4. Switch the language to Python and click Download Complete Project. Then, decompress the downloaded package.

    Note

    In the upper-right corner of the sample code area, you can click the image icon to copy the SDK sample code.

Step 5: Run the SDK sample code

This section describes how to run the Python sample code.

  1. To prepare the Python environment, download and install Python 3.

    To check your Python version, open a terminal and run the python --version command.

  2. Configure environment variables.

    After you obtain the AccessKey for the RAM user, configure it as an environment variable. For detailed instructions, see Configure environment variables on Linux, macOS, and Windows.

  3. Install dependencies.

    pip install alibabacloud_credentials
    pip install alibabacloud_dypnsapi20170525
    pip install alibabacloud_tea_console
  4. Open PyCharm and choose File > Open. Select the decompressed alibabacloud_sample folder and wait for the dependencies to be automatically installed.

  5. Double-click sample.py to open it, verify that no errors are present, and then run the code.

    # ... (omitted)
    ) -> None:
            client = Sample.create_client()
            describe_site_pairs_request = hdr_20170925_models.DescribeSitePairsRequest(
                site_pair_type='cloud2cloud'
            )
            runtime = util_models.RuntimeOptions()
            try:
                # When you copy and run the code, print the return value of the API.
                await client.describe_site_pairs_with_options_async(describe_site_pairs_request, runtime)
            except Exception as error:
                # This is for demonstration purposes only. Handle exceptions with care. Do not ignore exceptions in your project.
                # Error message
                print(error.message)
                # Diagnostics address
                print(error.data.get("Recommend"))
                UtilClient.assert_as_string(error.message)
    if __name__ == '__main__':
        Sample.main(sys.argv[1:])
  6. View the result. In the console at the bottom, press Ctrl+F to search for statusCode. If you see "statusCode": 200, the call was successful.