同步执行函数
功能介绍
同步调用指的是客户端请求需要明确等到响应结果,也就是说这样的请求必须得调用到用户的函数,并且等到调用完成才返回。
调用方法
请参见如何调用API。
URI
POST /v2/{project_id}/fgs/functions/{function_urn}/invocations
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
project_id |
是 |
String |
参数解释: 租户项目 ID,获取方式请参见获取项目ID。 约束限制: 不涉及。 取值范围: 只能由英文字母、数字组成,且长度为32个字符。 默认值: 不涉及。 |
function_urn |
是 |
String |
参数解释: 函数的URN,详细解释见FunctionGraph函数模型的描述。 约束限制: 不涉及。 取值范围: 只能由英文字母、数字、下划线、中划线、冒号组成。 默认值: 不涉及。 |
请求参数
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
X-Auth-Token |
是 |
String |
参数解释: 用户Token。 通过调用IAM服务获取用户Token接口获取(响应消息头中X-Subject-Token的值)。 约束限制: 不涉及。 取值范围: 不涉及。 默认值: 不涉及。 |
Content-Type |
是 |
String |
参数解释: 消息体的类型(格式)。 约束限制: 不涉及。 取值范围: 只能为application/json。 默认值: application/json |
X-Cff-Log-Type |
否 |
String |
参数解释: 是否返回日志。 约束限制: 不涉及。 取值范围: 默认取值: 不涉及。 |
X-CFF-Request-Version |
否 |
String |
参数解释: 返回格式选择。 约束限制: sdk需要使用此值。 取值范围: 默认取值: 不涉及。 |
X-Cff-Instance-Memory |
否 |
String |
参数解释: 设置本次执行函数使用的内存规格。单位:MB。 约束限制: 不涉及。 取值范围 默认取值 不涉及。 |
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
{自定义key} |
是 |
Object |
参数解释 执行函数请求体,为json格式。 约束限制 不涉及。 |
响应参数
状态码: 200
参数 |
参数类型 |
描述 |
---|---|---|
X-Cff-Invoke-Summary |
String |
参数解释 本次同步调用执行信息摘要。 取值范围 不涉及。 |
X-Cff-Request-Id |
String |
参数解释 本次同步调用请求id。 取值范围 不涉及。 |
X-Cff-Function-Log |
String |
参数解释 本次同步调用用户日志,需在请求头中设置X-Cff-Log-Type:tail。截取日志的后2000个字节并且做base编码。 取值范围 不涉及。 |
X-CFF-Billing-Duration |
String |
参数解释 本次同步调用计费信息。 取值范围 不涉及。 |
X-Cff-Response-Version |
String |
参数解释 返回体格式。 取值范围 v0:默认返回文本格式。 v1:默认返回json格式。 |
X-Func-Err-Code |
String |
参数解释 本次同步调用错误码,若执行成功则为0。 取值范围 不涉及。 |
X-Is-Func-Err |
String |
参数解释 是否为用户函数错误。 取值范围 不涉及。 |
参数 |
参数类型 |
描述 |
---|---|---|
request_id |
String |
参数解释 请求ID。 取值范围 只能由英文字母、数字及“-”组成,且长度为36位字符。 |
result |
String |
参数解释 函数执行结果。 取值范围 不涉及。 |
log |
String |
参数解释 函数执行返回日志。 取值范围 不涉及。 |
status |
Integer |
参数解释 函数执行返回状态码。 取值范围 不涉及。 |
状态码: 400
参数 |
参数类型 |
描述 |
---|---|---|
error_code |
String |
错误码 |
error_msg |
String |
错误信息 |
状态码: 401
参数 |
参数类型 |
描述 |
---|---|---|
error_code |
String |
错误码 |
error_msg |
String |
错误信息 |
状态码: 403
参数 |
参数类型 |
描述 |
---|---|---|
error_code |
String |
错误码 |
error_msg |
String |
错误信息 |
状态码: 404
参数 |
参数类型 |
描述 |
---|---|---|
error_code |
String |
错误码 |
error_msg |
String |
错误信息 |
状态码: 500
参数 |
参数类型 |
描述 |
---|---|---|
error_code |
String |
错误码 |
error_msg |
String |
错误信息 |
状态码: 503
参数 |
参数类型 |
描述 |
---|---|---|
error_code |
String |
错误码 |
error_msg |
String |
错误信息 |
请求示例
同步执行函数,请求参数为键值对“k”:“v”。
POST /v2/{project_id}/fgs/functions/{function_urn}/invocations
{
"k" : "v"
}
响应示例
状态码: 200
OK
{
"result" : "{\"statusCode\": 200, \"isBase64Encoded\": false, \"body\": \"{\\\"key\\\": \\\"value\\\"}\", \"headers\": {\"Content-Type\": \"application/json\"}}",
"log" : "2022-09-20T11:43:57Z Start invoke request '1cbe80f3-3c65-475e-ad88-76ac518d386a', version: v1\nHello, World!\n\n2022-09-20T11:43:58Z Finish invoke request '1cbe80f3-3c65-475e-ad88-76ac518d386a', duration: 65.828ms, billing duration: 66ms, memory used: 21.473MB, billing memory: 128MB",
"status" : 200,
"request_id" : "1cbe80f3-3c65-475e-ad88-76ac518d386x"
}
SDK代码示例
SDK代码示例如下。
Java
同步执行函数,请求参数为键值对“k”:“v”。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
package com.huaweicloud.sdk.test;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.functiongraph.v2.region.FunctionGraphRegion;
import com.huaweicloud.sdk.functiongraph.v2.*;
import com.huaweicloud.sdk.functiongraph.v2.model.*;
import java.util.Map;
import java.util.HashMap;
public class InvokeFunctionSolution {
public static void main(String[] args) {
// The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
// In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
String ak = System.getenv("CLOUD_SDK_AK");
String sk = System.getenv("CLOUD_SDK_SK");
ICredential auth = new BasicCredentials()
.withAk(ak)
.withSk(sk);
FunctionGraphClient client = FunctionGraphClient.newBuilder()
.withCredential(auth)
.withRegion(FunctionGraphRegion.valueOf("<YOUR REGION>"))
.build();
InvokeFunctionRequest request = new InvokeFunctionRequest();
Map<String, Object> listbodyInvokeFunctionRequestBody = new HashMap<>();
listbodyInvokeFunctionRequestBody.put("k", "v");
request.withBody(listbodyInvokeFunctionRequestBody);
try {
InvokeFunctionResponse response = client.invokeFunction(request);
System.out.println(response.toString());
} catch (ConnectionException e) {
e.printStackTrace();
} catch (RequestTimeoutException e) {
e.printStackTrace();
} catch (ServiceResponseException e) {
e.printStackTrace();
System.out.println(e.getHttpStatusCode());
System.out.println(e.getRequestId());
System.out.println(e.getErrorCode());
System.out.println(e.getErrorMsg());
}
}
}
|
Python
同步执行函数,请求参数为键值对“k”:“v”。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# 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__":
# The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
# In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
ak = __import__('os').getenv("CLOUD_SDK_AK")
sk = __import__('os').getenv("CLOUD_SDK_SK")
credentials = BasicCredentials(ak, sk) \
client = FunctionGraphClient.new_builder() \
.with_credentials(credentials) \
.with_region(FunctionGraphRegion.value_of("<YOUR REGION>")) \
.build()
try:
request = InvokeFunctionRequest()
listInvokeFunctionRequestBodybody = {
"k": "v"
}
request.body = listInvokeFunctionRequestBodybody
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)
|
Go
同步执行函数,请求参数为键值对“k”:“v”。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package main
import (
"fmt"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
functiongraph "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/model"
region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/region"
)
func main() {
// The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
// In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
ak := os.Getenv("CLOUD_SDK_AK")
sk := os.Getenv("CLOUD_SDK_SK")
auth := basic.NewCredentialsBuilder().
WithAk(ak).
WithSk(sk).
Build()
client := functiongraph.NewFunctionGraphClient(
functiongraph.FunctionGraphClientBuilder().
WithRegion(region.ValueOf("<YOUR REGION>")).
WithCredential(auth).
Build())
request := &model.InvokeFunctionRequest{}
var listInvokeFunctionRequestBodybody = map[string]interface{}{
"k": "v",
}
request.Body = listInvokeFunctionRequestBodybody
response, err := client.InvokeFunction(request)
if err == nil {
fmt.Printf("%+v\n", response)
} else {
fmt.Println(err)
}
}
|
更多
更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。
状态码
状态码 |
描述 |
---|---|
200 |
OK |
400 |
Bad Request 请求错误。 |
401 |
Unauthorized 鉴权失败。 |
403 |
Forbidden 没有操作权限。 |
404 |
Not Found 找不到资源。 |
500 |
Internal Server Error 服务内部错误。 |
503 |
Service Unavailable 服务不可用。 |
错误码
请参见错误码。