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

Python SDK

A synchronous function execution SDK is used as an example. To use the sample code, you must add the SDK dependency with the same language.

Table 1 Python SDK information

SDK Info

Description

Installation

pip install huaweicloudsdkfunctiongraph

Links

SDK Dependency

Python SDK User Guide

The request/response parameters and example requests/responses of the SDK are the same as those of the corresponding APIs. For details about the parameters and examples, see the API for executing a function synchronously.

SDK Request Example
# coding: utf-8
 
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkfunctiongraph.v2.region.functiongraph_region import FunctionGraphRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkfunctiongraph.v2 import *

if __name__ == "__main__":
    # This example is only used for testing purposes. Do not hardcode your AK/SK in the production environment.
    ak = "<YOUR AK>"
    sk = "<YOUR SK>"
    security_token = os.getenv("HUAWEICLOUD_SDK_SECURITY_TOKEN")
    project_id = "{your projectId string}"
 
    credentials = BasicCredentials(ak, sk, project_id).with_security_token(security_token) \
 
    client = FunctionGraphClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(FunctionGraphRegion.value_of("<region>")) \
        .build()
 
    try:
        request = InvokeFunctionRequest()
        request.x_cff_log_type = "tail"
        request.x_cff_request_version = "v1"
        request.function_urn = "urn:fss:<region>:<project_id>:function:default:<func_name>:<version>"
        response = client.invoke_function(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

Obtain AK/SK, region (endpoint), and project_id by referring to AK/SK Signing and Authentication Guide.

Obtain func_name and version from the function details page.

xCFFRequestVersionRequest indicates the response body format. v0: text format; v1: JSON format. Select this format when using an SDK.