Updated on 2024-06-27 GMT+08:00

Python

This section uses IntelliJ IDEA as an example to describe how to integrate the Python SDK for API request signing. You can import the sample project in the code package, and integrate the signing SDK into your application by referring to the API calling example.

Preparing the Environment

  • Download IntelliJ IDEA 2018.3.5 or later from the IntelliJ IDEA official website and install it.
  • Download the Python installation package (version 2.7.9 or later, or 3.x) from the Python official website and install it.
    After Python is installed, run the pip command to install the requests library.
    pip install requests

    If a certificate error occurs during the installation, download the get-pip.py file to upgrade the pip environment, and try again.

  • Install the Python plug-in on IDEA.

Obtaining the SDK

Download the SDK and demo.

The following table shows the directory structure of the downloaded package.

Name

Description

apig_sdk\__init__.py

SDK code

apig_sdk\signer.py

main.py

Sample code

licenses\license-requests

Third-party license

Importing the Sample Project

  1. Start IDEA and choose File > New > Project.

    On the displayed New Project page, choose Python and click Next.

  2. Click Next. Click ..., select the directory where the SDK is decompressed, and click Finish.

  3. View the directory structure shown in the following figure.

Request Signing and API Calling

  1. Run the pip command to install the requests library.

    1
    pip install requests
    

  2. Import apig_sdk to the project.

    1
    2
    from apig_sdk import signer
    import requests
    

  3. Generate a new signer and enter the AK and SK.

    1. In this example, the AK and SK stored in the environment variables are used. Specify the environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK in the local environment first. The following uses Linux as an example to describe how to set the obtained AK/SK as environment variables.
      1. Open the terminal and run the following command to open the environment variable configuration file:

        vi ~/.bashrc

      2. Set environment variables, save the file, and exit the editor.
        export HUAWEICLOUD_SDK_AK="Obtained AK"
        export HUAWEICLOUD_SDK_SK="Obtained SK"
      3. Run the following command to apply the modification:

        source ~/.bashrc

    2. Generate a new signer and enter the configured environment variables.
      1
      2
      3
      4
      5
      6
      sig = signer.Signer()
      # Set the AK/SK to sign and authenticate the request.
      # Directly writing AK/SK in code is risky. For security, encrypt your AK/SK and store them in the configuration file or environment variables.
      # In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, set environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK.
      sig.Key = os.getenv('HUAWEICLOUD_SDK_AK')
      sig.Secret = os.getenv('HUAWEICLOUD_SDK_SK')
      

  4. Generate a new request, and specify the domain name, method, request URI, and body.

    Take the API for querying VPCs with these parameters as an example: HTTP method GET, endpoint service.region.example.com, and URI /v1/77b6a44cba5143ab91d13ab9a8ff44fd/vpcs?limit=1
    1
    2
    3
    # The following example shows how to set the request URL and parameters to query a VPC list.
    r = signer.HttpRequest("GET", "https://{service}.region.example.com/v1/77b6a44cba5143ab91d13ab9a8ff44fd/vpcs?limit=1")
    # r.body = "{\"a\":1}"
    

  5. Add other headers required for request signing or other purposes. For example, add the X-Project-Id header in multi-project scenarios or the X-Domain-Id header for a global service. Separate multiple headers with commas.

    1
    r.headers = {"X-Project-Id": "xxx"}
    

  6. Execute the following function to add the X-Sdk-Date and Authorization headers for signing:

    1
    sig.Sign(r)
    
    • X-Sdk-Date is a request header parameter required for signing requests.
    • The SDK automatically completes signing requests, and you do not need to know which header parameters are involved in the signing process.

  7. Access the API and view the access result.

    1
    2
    3
    resp = requests.request(r.method, r.scheme + "://" + r.host + r.uri, headers=r.headers, data=r.body)
    print(resp.status_code, resp.reason)
    print(resp.content)