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

Python

Scenarios

To use Python to call an API through app authentication, obtain the Python SDK, create a new project, and then call the API by referring to the API calling example.

This section uses IntelliJ IDEA 2018.3.5 as an example.

Prerequisites

  • You have obtained API calling information. For details, see Preparation.
  • You have installed Python 2.7.9 or 3.X. If not, download the Python installation package from the official Python website and install it.

    After Python is installed, run the pip command to install the requests library in the CLI or Shell window.

    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.

  • You have installed IntelliJ IDEA 2018.3.5 or a later version. If not, download the installation package from the official IntelliJ IDEA website and install it.
  • You have installed the Python plug-in on IntelliJ IDEA. If not, install the Python plug-in according to Figure 1.
    Figure 1 Installing the Python plug-in

Obtaining the SDK

Log in to the APIG console, and download the SDK on the SDKs page by referring to section "SDKs" in the API Gateway User Guide.

Alternatively, download the latest SDK version. Then obtain the ApiGateway-python-sdk.zip package. The following table shows the files decompressed from the package.

Name

Description

apig_sdk\__init__.py

SDK code

apig_sdk\signer.py

main.py

Sample code

backend_signature.py

Sample code for backend signing

licenses\license-requests

Third-party license

Creating a Project

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

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

    Figure 2 Python

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

    Figure 3 Selecting the SDK directory after decompression

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

    Figure 4 Directory structure of the new project python

    Modify the parameters in sample code main.py as required. For details about the sample code, see API Calling Example.

API Calling Example

  1. Import apig_sdk to the project.

    1
    2
    3
    from apig_sdk import signer
    import requests
    import os
    

  2. Generate a new signer and enter the AppKey and AppSecret.

    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
      sig = signer.Signer()
      // 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')
      

  3. Generate a request, and specify the method, request URI, header, and request body.

    1
    2
    3
    4
    r = signer.HttpRequest("POST",
                           "https://c967a237-cd6c-470e-906f-a8655461897e.apigw.exampleRegion.com/app1?a=1",
                           {"x-stage": "RELEASE"},
                           "body")
    

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

    X-Sdk-Date is a request header parameter required for signing requests.

    1
    sig.Sign(r)
    

  5. 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)