Updated on 2024-10-10 GMT+08:00

Python

Scenarios

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

Prerequisites

  • You have obtained API calling information. For details, see Preparations.
  • You have installed the development tool and Python development environment. For details, see Preparations.

    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.

  • You have installed the Python plug-in on IntelliJ IDEA. Otherwise, install it according to Figure 1.
    Figure 1 Installing the Python plug-in

Obtaining the SDK

Old version: Log in to the ROMA Connect console, choose API Connect > API Calling > SDKs, and download the SDK.

New version: Log in to the ROMA Connect console, choose API Connect > Credentials > SDKs, and download the SDK.

The following shows the directory structure after the decompression.

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 licenses

Creating a Project

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

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

    Figure 2 New Project
  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 of the project.
    Figure 4 Directory structure of the new project

    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.
    from apig_sdk import signer
    import requests
    import os
  2. Generate a signer and enter the key and secret of the authorized credential. For details about how to obtain the information, see Obtaining API Calling Information.
    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 URL, header, and body. For details about how to obtain the information, see Obtaining API Calling Information.
    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.

    sig.Sign(r)
  5. Access the API and view the access result.
    //If the subdomain name allocated by the system is used to access the API of HTTPS requests, add ,verify=False to the end of data=r.body to ignore the certificate verification.
    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)