Updated on 2023-01-17 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 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

Log in to the API Gateway console and choose Help Center > SDK Process Flow. Then download the SDK. For details, see section "SDKs" in the User Guide.

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
    2
    3
    4
    sig = signer.Signer()
    # Set the AK/SK to sign and authenticate the request.
    sig.Key = "QTWAOY********VKYUC"
    sig.Secret = "MFyfvK41ba2giqM7**********KGpownRZlmVmHc"
    

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

    The following is an example request for querying VPCs. The HTTP method is GET, the domain name (endpoint) is service.region.example.com, and the API URI is /v1/77b6a44cba5143ab91d13ab9a8ff44fd/vpcs?limit=1.

    For details, see section "Querying VPCs" in the VPC API Reference.
    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)