目录

FunctionCallable

Interface Info

public interface FunctionCallable

设置需要调用的云函数后,可以通过此类的方法调用云函数或设置超时时间。

Public Method Summary

Qualifier and Type

Method Name and Description

FunctionResult

call() throws AGCFunctionException

调用没有入参的“事件调用”函数。

<T> FunctionResult

call(T body) throws AGCFunctionException

调用有入参的“事件调用”函数。

<T> RestResponse

callHttpOnClient(HttpMethod method, String invokeURL, T body) throws AGCFunctionException

端侧调用,请求有自定义url、body的“http请求”函数。

<T> RestResponse

callHttpOnClient(HttpMethod method, String invokeURL, Map<String, String> header, T body) throws AGCFunctionException

端侧调用,请求有自定义url、自定义header、body的“http请求”函数。

RestResponse

callHttpOnClient(RestRequest request) throws AGCFunctionException

端侧调用,请求“http请求”函数,入参格式为封装RestRequest类型。

<T> RestResponse

callHttpOnCloud(HttpMethod method, String invokeURL, T body) throws AGCFunctionException

云侧调用,请求有自定义url、body的“http请求”函数。

<T> RestResponse

callHttpOnCloud(HttpMethod method, String invokeURL, Map<String, String> header, T body) throws AGCFunctionException

云侧调用,请求有自定义url、自定义header、body的“http请求”函数。

RestResponse

callHttpOnCloud(RestRequest request) throws AGCFunctionException

云侧调用,请求“http请求”函数,入参格式为封装RestRequest类型。

void

setTimeout(long timeout) throws AGCFunctionException

设置函数的超时时间。

void

setClientCredential(String projectId, String clientId, String clientSecret) throws AGCException

端侧调用“http请求”函数时,设置被调函数的项目信息。

Public Methods

[h2]call

Method

public FunctionResult call() throws AGCFunctionException

调用没有入参的“事件调用”函数。

Return

Type

Description

FunctionResult

返回函数执行的结果。

Throws

Exception Name

Exception Description

AGCFunctionException

Cloud Function异常基类。调用call()失败时,会抛出异常。您必须显式地捕获并处理此异常。

Sample Code
try {
    FunctionCallable function = agcFunction.wrap();
    FunctionResult result = function.call();
} catch (AGCFunctionException e) {
    // ...
}

[h2]call

Method

public FunctionResult call(T body) throws AGCFunctionException

调用有入参的“事件调用”函数。

Parameters

Name

Description

body

HTTP请求体,支持的数据类型可以是:

  • HashMap类型
  • com.alibaba.fastjson.JSONObject类型
  • com.huawei.agconnect.server.commons.json.JSONObject类型

Return

Type

Description

FunctionResult

返回函数执行的结果。

Throws

Exception Name

Exception Description

AGCFunctionException

Cloud Function异常基类。调用call(T body)失败时,会抛出异常。您必须显式地捕获并处理此异常。

Sample Code
try {
    FunctionCallable function = agcFunction.wrap();
    Map body = new HashMap<String, String>;
    body.put("key", "test");
    FunctionResult result = function.call(body);
} catch (AGCFunctionException e) {
    // ...
}

[h2]callHttpOnClient

Method

<T> RestResponse callHttpOnClient(HttpMethod method, String invokeURL, T body) throws AGCFunctionException

此方法为带有自定义url、body的“http请求”函数,供端侧调用。

Parameters

Name

Description

method

HTTP请求类型。

invokeURL

自定义url,以斜杠开头,例如:"/invoke"。

body

HTTP请求体,支持的数据类型可以是:

  • HashMap类型
  • com.alibaba.fastjson.JSONObject类型
  • com.huawei.agconnect.server.commons.json.JSONObject类型

Return

Type

Description

RestResponse

返回HTTP请求的结果。

Throws

Exception Name

Exception Description

AGCFunctionException

Cloud Function异常基类。调用失败时,会抛出异常。您必须显式地捕获并处理此异常。

Sample Code
try {
    FunctionCallable function = agcFunction.wrap("function-test", "$latest");
    function.setClientCredential("projectId", "clientId", "clientSecret");

    Map body = new HashMap<String, String>;
    body.put("key", "test");
    RestResponse restResponse = function.callHttpOnClient(HttpMethod.POST, "/invoke", body);
} catch (AGCException e) {
    // ...
}

[h2]callHttpOnClient

Method

<T> RestResponse callHttpOnClient(HttpMethod method, String invokeURL, Map<String, String> header, T body) throws AGCFunctionException

此方法为带有自定义url、自定义header、body的“http请求”函数,供端侧调用。

Parameters

Name

Description

method

HTTP请求类型。

invokeURL

自定义url,以斜杠开头,例如:"/invoke"。

header

HashMap类型。

body

HTTP请求体,支持的数据类型可以是:

  • HashMap类型
  • com.alibaba.fastjson.JSONObject类型
  • com.huawei.agconnect.server.commons.json.JSONObject类型

Return

Type

Description

RestResponse

返回HTTP请求的结果。

Throws

Exception Name

Exception Description

AGCFunctionException

Cloud Function异常基类。调用失败时,会抛出异常。您必须显式地捕获并处理此异常。

Sample Code
try {
    FunctionCallable function = agcFunction.wrap("function-test", "$latest");
    function.setClientCredential("projectId", "clientId", "clientSecret");

    Map<String,String> headers =new HashMap<>(); 
    headers.put("Content-Type","application/json");
    Map body = new HashMap<String, String>;
    body.put("key", "test");
    RestResponse restResponse = function.callHttpOnClient(HttpMethod.POST, "/invoke", headers, body);
} catch (AGCException e) {
    // ...
}

[h2]callHttpOnClient

Method

RestResponse callHttpOnClient(RestRequest request) throws AGCFunctionException

此方法为带有RestRequest类型的“http请求”函数,供端侧调用。

Parameters

Name

Description

RestRequest

HTTP请求信息类。

Return

Type

Description

RestResponse

返回HTTP请求的结果。

Throws

Exception Name

Exception Description

AGCFunctionException

Cloud Function异常基类。调用失败时,会抛出异常。您必须显式地捕获并处理此异常。

Sample Code
try {
    FunctionCallable function = agcFunction.wrap("function-test", "$latest");
    function.setClientCredential("projectId", "clientId", "clientSecret");

    Map<String,String> headers =new HashMap<>(); 
    headers.put("Content-Type","application/json");
    RestRequest restRequest = RestRequest.builder()
        .withHttpMethod(HttpMethod.POST)
        .withHeader(headers)
        .withUri("/invoke")
        .withBody(new JSONObject())
        .build();
    RestResponse restResponse = function.callHttpOnClient(restRequest);
} catch (AGCException e) {
    // ...
}

[h2]callHttpOnCloud

Method

<T> RestResponse callHttpOnCloud(HttpMethod method, String invokeURL, T body) throws AGCFunctionException

此方法为带有自定义url、body的“http请求”函数,供云侧调用。

Parameters

Name

Description

method

HTTP请求类型。

invokeURL

自定义url,以斜杠开头,例如:"/invoke"。

body

HTTP请求体,支持的数据类型可以是:

  • HashMap类型
  • com.alibaba.fastjson.JSONObject类型
  • com.huawei.agconnect.server.commons.json.JSONObject类型

Return

Type

Description

RestResponse

返回HTTP请求的结果。

Throws

Exception Name

Exception Description

AGCFunctionException

Cloud Function异常基类。调用失败时,会抛出异常。您必须显式地捕获并处理此异常。

Sample Code
try {
    FunctionCallable function = agcFunction.wrap("function-test", "$latest");
    Map body = new HashMap<String, String>;
    body.put("key", "test");
    RestResponse restResponse = function.callHttpOnCloud(HttpMethod.POST, "/invoke", body);
} catch (AGCException e) {
    // ...
}

[h2]callHttpOnCloud

Method

<T> RestResponse callHttpOnCloud(HttpMethod method, String invokeURL, Map<String, String> header, T body) throws AGCFunctionException

此方法为带有自定义url、自定义header、body的“http请求”函数,供云侧调用。

Parameters

Name

Description

method

HTTP请求类型。

invokeURL

自定义url,以斜杠开头,例如:"/invoke"。

header

HashMap类型。

body

HTTP请求体,支持的数据类型可以是:

  • HashMap类型
  • com.alibaba.fastjson.JSONObject类型
  • com.huawei.agconnect.server.commons.json.JSONObject类型

Return

Type

Description

RestResponse

返回HTTP请求的结果。

Throws

Exception Name

Exception Description

AGCFunctionException

Cloud Function异常基类。调用失败时,会抛出异常。您必须显式地捕获并处理此异常。

Sample Code
try {
    FunctionCallable function = agcFunction.wrap("function-test", "$latest");
    Map<String,String> headers =new HashMap<>(); 
    headers.put("Content-Type","application/json");
    Map body = new HashMap<String, String>;
    body.put("key", "test");
    RestResponse restResponse = function.callHttpOnCloud(HttpMethod.POST, "/invoke", headers, body);
} catch (AGCException e) {
    // ...
}

[h2]callHttpOnCloud

Method

RestResponse callHttpOnCloud(RestRequest request) throws AGCFunctionException

此方法为带有RestRequest类型的“http请求”函数,供云侧调用。

Parameters

Name

Description

RestRequest

HTTP请求信息类。

Return

Type

Description

RestResponse

返回HTTP请求的结果。

Throws

Exception Name

Exception Description

AGCFunctionException

Cloud Function异常基类。调用失败时,会抛出异常。您必须显式地捕获并处理此异常。

Sample Code
try {
    FunctionCallable function = agcFunction.wrap("function-test", "$latest");

    Map<String,String> headers =new HashMap<>(); 
    headers.put("Content-Type","application/json");
    RestRequest restRequest = RestRequest.builder()
        .withHttpMethod(HttpMethod.POST)
        .withHeader(headers)
        .withUri("/invoke")
        .withBody(new JSONObject())
        .build();
    RestResponse restResponse = function.callHttpOnCloud(restRequest);
} catch (AGCException e) {
    // ...
}

[h2]setTimeout

Method

public void setTimeout(long timeout) throws AGCFunctionException

设置函数执行的超时时间。

Parameters

Name

Description

timeout

设置连接被调用函数的超时时长,单位是毫秒。

Throws

Exception Name

Exception Description

AGCFunctionException

Cloud Function异常基类。调用setTimeout(long timeout),入参数值不符合规范(0,55000]时,会抛出异常。您必须显式地捕获并处理此异常。

Sample Code
try {
    FunctionCallable function = agcFunction.wrap();
    function.setTimeout(10000);
} catch (AGCFunctionException e) {
    // ...
}

[h2]setClientCredential

Method

public void setClientCredential(String projectId, String clientId, String clientSecret) throws AGCException

端侧调用“http请求”函数时,设置被调函数的项目信息。

Parameters

Name

Description

projectId

项目ID。

clientId

客户端ID。

clientSecret

客户端密钥。

Throws

Exception Name

Exception Description

AGCException

入参数值为空时,会抛出异常,您必须显式地捕获并处理此异常。

Sample Code
try {
    FunctionCallable function = agcFunction.wrap("function-test", "$latest");
    function.setClientCredential("projectId", "clientId", "clientSecret");
} catch (AGCException e) {
    // ...
}