云函数

触发云函数

POST https://cloud.minapp.com/oserve/v1/cloud-function/job/

info 推荐使用发起一个云函数请求触发云函数。

参数说明

Content-Type: Content-Type: application/json

参数 类型 必填 说明
function_name String 云函数名
data Object 传递给云函数的参数
sync Bool 是否等待返回函数执行结果,默认为 true。同步与异步云函数有不同的超时时间,同步云函数为 5 秒,而异步云函数为 5 分钟(300 秒)

返回参数说明

在 sync 为 true 的情况下

参数 类型 描述
code Number code 为 0 时表示成功执行云函数,否则为执行云函数失败
data 由云函数返回的数据类型决定 函数通过 callback 返回的数据
error Object 返回的错误信息,成功则返回空对象

测试云函数

接口地址

POST https://cloud.minapp.com/oserve/v1.3/cloud-function/:cloud_function_name/debug/

其中 :cloud_function_name 是云函数的名字。

参数说明

Content-Type: Content-Type: application/json

参数 类型 必填 说明
data Object 自定义数据,没有则需要提供一个空对象
subject String 触发类型,若不提供默认为 sdk, 可选值有:sdk, open_api, cloud_function, flex_schema, wechat_pay_success, timer, file_operation
schema_id Integer 表 ID,如果 subject 为 FlexSchema 则必填

返回参数

参数 类型 说明
billing_time Integer 计费时间
code Integer 函数调用正常:0,函数调用出错:1
data 由云函数返回的数据类型决定 函数通过 callback 返回的数据
error Object 包含三个键,错误信息:message, 错误堆栈:stack, 错误类型:type
execution_time Float 执行时间,毫秒
job_id String 任务 ID
log String 日志
mem_usage Integer 内存使用量,字节数

代码示例

{% tabs debugCloudFunctionCurl=”Curl”,debugCloudFunctionNode=”Node”,debugCloudFunctionPHP=”PHP” %}

{% content “debugCloudFunctionCurl” %}

  1. curl -X POST \
  2. https://cloud.minapp.com/oserve/v1.3/cloud-function/test/debug/ \
  3. -H "Authorization: Bearer cfb5912724dd7ff0b0c17683cc3074bb548bc7f4" \
  4. -H 'Content-Type: application/json' \
  5. -d '{
  6. "data": {}
  7. }'

{% content “debugCloudFunctionNode” %}

  1. var request = require("request");
  2. var cloudFunctionName = '';
  3. var options = {
  4. method: 'POST',
  5. url: 'https://cloud.minapp.com/oserve/v1.3/cloud-function/' + cloudFunctionName + '/debug/',
  6. headers:
  7. {
  8. 'Content-Type': 'application/json',
  9. Authorization: 'Bearer 2323d124881bd3d63c9bb7845454f80a676b'
  10. },
  11. body: { data: {} },
  12. json: true
  13. };
  14. request(options, function (error, response, body) {
  15. if (error) throw new Error(error);
  16. console.log(body);
  17. });

{% content “debugCloudFunctionPHP” %}

  1. <?php
  2. $token = '';
  3. $cloud_function_name = ''; // 云函数的名字
  4. $url = "https://cloud.minapp.com/oserve/v1.3/cloud-function/{$cloud_function_name}/debug/";
  5. $ch = curl_init();
  6. $header = array(
  7. "Authorization: Bearer {$token}",
  8. 'Content-Type: application/json; charset=utf-8'
  9. );
  10. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  11. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  12. curl_setopt($ch, CURLOPT_URL, $url);
  13. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  14. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  15. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  16. curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"data\": {}}");
  17. $response = curl_exec($ch);
  18. $err = curl_error($ch);
  19. curl_close($ch);
  20. if ($err) {
  21. echo "CURL Error #:" . $err;
  22. } else {
  23. echo $response;
  24. }

{% endtabs %}

返回示例

正常返回:

  1. {
  2. "billing_time": 100,
  3. "code": 0,
  4. "data": "hello world",
  5. "error": {},
  6. "execution_time": 13.46,
  7. "job_id": "490f08ff9b2e4a6b874581de53950e54",
  8. "log": "2018-09-21T03:46:26.026Z LOG event.data: {}\n2018-09-21T03:46:26.029Z LOG return: hello world\n",
  9. "mem_usage": 78340096
  10. }

异常返回:

  1. {
  2. "billing_time": 100,
  3. "code": 1,
  4. "data": null,
  5. "error": {
  6. "message": "Unexpected identifier",
  7. "stack": ":2\n callback(null, \"hello world\") fot debug\n ^^^\n\nSyntaxError: Unexpected identifier\n at createScript (vm.js:80:10)\n at Object.runInThisContext (vm.js:139:10)\n at Module._compile (module.js:607:28)\n at loadUserFunction (/opt/hydrogen/worker.js:37:11)\n at IncomingMessage.request.on.on (/opt/hydrogen/worker.js:82:18)\n at emitNone (events.js:106:13)\n at IncomingMessage.emit (events.js:208:7)\n at endReadableNT (_stream_readable.js:1055:12)\n at _combinedTickCallback (internal/process/next_tick.js:138:11)\n at process._tickCallback (internal/process/next_tick.js:180:9)",
  8. "type": "SyntaxError"
  9. },
  10. "execution_time": 18.46,
  11. "job_id": "9f9fe6f08be94888b8b56e981a5a041e",
  12. "log": "2018-09-21T07:49:54.792Z LOG event.data: {}\n2018-09-21T07:49:54.799Z ERROR :2\n callback(null, \"hello world\") fot debug\n ^^^\n\nSyntaxError: Unexpected identifier\n at createScript (vm.js:80:10)\n at Object.runInThisContext (vm.js:139:10)\n at Module._compile (module.js:607:28)\n at loadUserFunction (/opt/hydrogen/worker.js:37:11)\n at IncomingMessage.request.on.on (/opt/hydrogen/worker.js:82:18)\n at emitNone (events.js:106:13)\n at IncomingMessage.emit (events.js:208:7)\n at endReadableNT (_stream_readable.js:1055:12)\n at _combinedTickCallback (internal/process/next_tick.js:138:11)\n at process._tickCallback (internal/process/next_tick.js:180:9)\n",
  13. "mem_usage": 78528512
  14. }

状态码说明

200: 成功。

400: 参数校验失败,任务调度失败。

401: 未授权,请检查请求头中的 Authorization 字段是否正确。

获取某云函数的任务记录

接口地址

GET https://cloud.minapp.com/oserve/v1.3/cloud-function/:cloud-function-name/job/

其中 :cloud-function-name 是云函数的名字。

参数说明

参数 类型 必填 说明
source String 云函数任务来源,可选值:sdk, open_api, cloud_function, flex_schema, wechat_pay_success, timer, file_operation
status String 云函数任务状态,可选值:waiting, executing, fulfilled, schedule_failed, execution_rejected, deadline_exceeded, execution_failed
service_type String 云函数同步类型,可选值:time_sensitive, batch
created_at__gte Integer 创建时间大于,unix 时间戳
created_at__lte Integer 创建时间小于,unix 时间戳

返回参数

参数 类型 说明
id Integer 任务 ID
created_at Float 创建时间
finished_at Float 完成时间
mem_usage Integer 内存用量
service_type String 可能的值:time_sensitive, batch
source String 任务来源
status String 任务状态
user_log_url String 日志地址

代码示例

{% tabs getCloudFunctionJobCurl=”Curl”,getCloudFunctionJobNode=”Node”,getCloudFunctionJobPHP=”PHP” %}

{% content “getCloudFunctionJobCurl” %}

  1. curl -X GET \
  2. https://cloud.minapp.com/oserve/v1.3/cloud-function/test/job/ \
  3. -H 'Authorization: Bearer 2323d124881bd3d63c9bb78458252454f676b'

{% content “getCloudFunctionJobNode” %}

  1. var request = require("request");
  2. var cloudFunctionName = '';
  3. var options = {
  4. method: 'GET',
  5. url: 'https://cloud.minapp.com/oserve/v1.3/cloud-function/' + cloudFunctionName + '/job/',
  6. headers:
  7. {
  8. 'Content-Type': 'application/json',
  9. Authorization: 'Bearer 2323d124881bd3d63c9bb78458252454f80ab'
  10. },
  11. json: true
  12. };
  13. request(options, function (error, response, body) {
  14. if (error) throw new Error(error);
  15. console.log(body);
  16. });

{% content “getCloudFunctionJobPHP” %}

  1. <?php
  2. $token = '';
  3. $cloud_function_name = ''; // 云函数的名字
  4. $url = "https://cloud.minapp.com/oserve/v1.3/cloud-function/{$cloud_function_name}/job/";
  5. $ch = curl_init();
  6. $header = array(
  7. "Authorization: Bearer {$token}",
  8. 'Content-Type: application/json; charset=utf-8'
  9. );
  10. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  11. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  12. curl_setopt($ch, CURLOPT_URL, $url);
  13. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
  14. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  15. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  16. $response = curl_exec($ch);
  17. $err = curl_error($ch);
  18. curl_close($ch);
  19. if ($err) {
  20. echo "CURL Error #:" . $err;
  21. } else {
  22. echo $response;
  23. }

{% endtabs %}

返回示例

  1. {
  2. "meta": {
  3. "limit": 20,
  4. "next": "/oserve/v1.3/cloud-function/test/job/?limit=20&offset=20",
  5. "offset": 0,
  6. "previous": null,
  7. "total_count": 1525
  8. },
  9. "objects": [
  10. {
  11. "created_at": 1537505171.419368,
  12. "finished_at": 1537505171.605735,
  13. "id": 779734,
  14. "mem_usage": 78336000,
  15. "service_type": "batch",
  16. "source": "flex_schema",
  17. "started_at": 1537505171.5807,
  18. "status": "fulfilled",
  19. "user_log_url": "https://hydrogen-faas-logs.s3.cn-north-1.amazonaws.com.cn/ticket_778289.log?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Expires=86400&X-Amz-Security-Token=FQoDYXdzEP3%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaDM50xRDK8JETeulNLiK%2FA08zFCZM8zvsnb6VG81mDV0onfgVwXhAjUi6p3Y250g9GiVeWVs%2FpI6S9d0S6mD9xPjmkWw9ven%2BkRVME2xAbzfgio8%2BUbQkrOgvnFx%2BziHj0DGGvMEZYnoT6ui4QD8y8YQaJd7oWH%2B0hdIUGdE%2BFTSiZzSicuVgdr6WLkxipnuujV2yS9%2ByrXK4A0g1HaUifZVnD3K2Bz1AiHrr9abaFSFoWzyzhGYGcyp7TM4iodW%2FTI5CkPFj2f%2BNvCOSCBWWWOsY%2FUyaAdCFZFqXJoJ6uuXFZ5yl7xd5W28egT5Jgfeq%2FG5j9xp9OVu8sAlnPre8WoSpspHt8R%2BRMDPr6klJE3yxXIxRODlt4nHAA30wcwunO09x8zoK54pgrveIWNlIRYeSSMG%2FGAzYXNq%2FxXjcpQOa3ekyirC%2Bj0n45dBZY4oS8c3au32WjoLjD2gnPAZsvBg1gR2wcUxs3PFi30ochY9VAxXQyXeqcy1Jw0LXQnBLT6Uw9pVV8%2BikrK13EBUiIAow1FEeKJ3xjng7mrPz60pEDYZcymJ0Uj4VkNBXMQwPHBfsfChxsLWYXaQ3EitWD3GhBaim65Tz%2FoZHU6TPRijP5ZHdBQ%3D%3D&X-Amz-Date=20180921T044620Z&X-Amz-Signature=f5d74870d8b52cbb490fae4d81fc486b09da1e026bf9571c172b4d8d29c84a66&X-Amz-Credential=ASIAQUWH6XTCOEBVBVFG%2F20180921%2Fcn-north-1%2Fs3%2Faws4_request&X-Amz-SignedHeaders=host"
  20. },
  21. ...
  22. ]
  23. }

状态码说明

200: 成功。

400: 参数错误。

401: 未授权,请检查请求头中的 Authorization 字段是否正确。

发起一个云函数请求

接口地址

POST https://cloud.minapp.com/oserve/v1.3/cloud-function/:cloud-function-name/job/

其中 :cloud-function-name 是云函数的名字。

请求参数说明

Content-Type: Content-Type: application/json

参数 类型 必填 说明
data Object 传递给云函数的参数,如果没有需要提供一个空对象
sync Bool 是否等待返回函数执行结果,默认为 true。同步与异步云函数有不同的超时时间,同步云函数为 5 秒,而异步云函数为 5 分钟(300 秒)

返回参数说明

sync 为 true 时:

参数 类型 说明
code Number code 为 0 时表示成功执行云函数,否则为执行云函数失败
data 由云函数返回的数据类型决定 函数通过 callback 返回的数据
error Object 返回的错误信息,成功则返回空对象

sync 为 false 时:

参数 类型 说明
status String 默认为 ‘ok’

代码示例

{% tabs invokeCloudFunctionCurl=”Curl”,invokeCloudFunctionNode=”Node”,invokeCloudFunctionPHP=”PHP” %}

{% content “invokeCloudFunctionCurl” %}

  1. curl -X POST \
  2. https://cloud.minapp.com/oserve/v1.3/cloud-function/test/job/ \
  3. -H 'Authorization: Bearer 2323d124881bd3d63c9bb78458252454f676b' \
  4. -H "Content-Type: application/json" \
  5. -d '{
  6. "data": {}
  7. }'

{% content “invokeCloudFunctionNode” %}

  1. var request = require("request");
  2. var cloudFunctionName = 'come_from_hahah';
  3. var options = {
  4. method: 'POST',
  5. url: 'https://cloud.minapp.com/oserve/v1.3/cloud-function/' + cloudFunctionName + '/job/',
  6. headers:
  7. {
  8. 'Content-Type': 'application/json',
  9. Authorization: 'Bearer 2323d124881bd3d63c9bb78458252454f80a676b'
  10. },
  11. body:
  12. { data: {} },
  13. json: true
  14. };
  15. request(options, function (error, response, body) {
  16. if (error) throw new Error(error);
  17. console.log(body);
  18. });

{% content “invokeCloudFunctionPHP” %}

  1. <?php
  2. $token = '';
  3. $cloud_function_name = ''; // 云函数的名字
  4. $url = "https://cloud.minapp.com/oserve/v1.3/cloud-function/{$cloud_function_name}/job/";
  5. $ch = curl_init();
  6. $header = array(
  7. "Authorization: Bearer {$token}",
  8. 'Content-Type: application/json; charset=utf-8'
  9. );
  10. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  11. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  12. curl_setopt($ch, CURLOPT_URL, $url);
  13. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  14. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  15. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  16. curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"data\": {}}");
  17. $response = curl_exec($ch);
  18. $err = curl_error($ch);
  19. curl_close($ch);
  20. if ($err) {
  21. echo "CURL Error #:" . $err;
  22. } else {
  23. echo $response;
  24. }

{% endtabs %}

返回示例

sync 为 true:

  1. {
  2. "code": 1,
  3. "data": null,
  4. "error": {
  5. "message": "Unexpected identifier",
  6. "type": "SyntaxError"
  7. }
  8. }

sync 为 false:

  1. {
  2. "status": "ok"
  3. }

状态码说明

200: 成功。

401: 未授权,请检查请求头中的 Authorization 字段是否正确。

404: 没有找到对应的云函数。

新建云函数

接口地址

POST https://cloud.minapp.com/oserve/v1.3/cloud-function/

请求参数说明

参数 类型 必填 说明
name String 云函数名,不能为空字符串
function_code String 云函数代码,不能为空字符串
remark String 备注

返回参数说明

参数 类型 说明
audit_status String 可能的值: approved, rejected, waiting
created_by String 创建者昵称
function_code String 云函数代码
id Integer 云函数 ID
name String 云函数名
plan_circle String 套餐类型
remark String 备注
updated_at Integer 最近一次更新时间
updated_by String 更新者昵称

代码示例

{% tabs newCloudFunctionCurl=”Curl”,newCloudFunctionNode=”Node”, newCloudFunctionPHP=”PHP” %}

{% content “newCloudFunctionCurl” %}

  1. curl -X POST \
  2. https://cloud.minapp.com/oserve/v1.3/cloud-function/ \
  3. -H 'Authorization: Bearer 2323d124881bd3d63c9bb78458252454f676b' \
  4. -H "Content-Type: application/json" \
  5. -d '{
  6. "function_code": "exports.main = function functionName(event, callback) {\n callback(null, \"hello world\")\n}",
  7. "remark": "你好,云函数"
  8. }'

{% content “newCloudFunctionNode” %}

  1. var request = require("request");
  2. var options = {
  3. method: 'POST',
  4. url: 'https://cloud.minapp.com/oserve/v1.3/cloud-function/',
  5. headers:
  6. {
  7. 'Content-Type': 'application/json',
  8. Authorization: 'Bearer 2323d124881bd3d63c9bb78458252454f80a676b'
  9. },
  10. body:
  11. {
  12. function_code: 'exports.main = function functionName(event, callback) {\n callback(null, "hello world")\n}',
  13. remark: '你好,云函数'
  14. },
  15. json: true
  16. };
  17. request(options, function (error, response, body) {
  18. if (error) throw new Error(error);
  19. console.log(body);
  20. });

{% content “newCloudFunctionPHP” %}

  1. <?php
  2. $token = '';
  3. $url = "https://cloud.minapp.com/oserve/v1.3/cloud-function/";
  4. $ch = curl_init();
  5. $header = array(
  6. "Authorization: Bearer {$token}",
  7. 'Content-Type: application/json; charset=utf-8'
  8. );
  9. $payload = json_encode(
  10. array(
  11. 'function_code'=> "exports.main = function functionName(event, callback) {\n callback(null, \"hello world\")\n}",
  12. 'remark'=> '你好,云函数'
  13. )
  14. );
  15. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  16. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  17. curl_setopt($ch, CURLOPT_URL, $url);
  18. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  19. curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  20. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  21. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  22. $response = curl_exec($ch);
  23. $err = curl_error($ch);
  24. curl_close($ch);
  25. if ($err) {
  26. echo "CURL Error #:" . $err;
  27. } else {
  28. echo $response;
  29. }

{% endtabs %}

返回示例

  1. {
  2. "audit_status": "approved",
  3. "created_at": 1537704381,
  4. "created_by": "",
  5. "function_code": "exports.main = function functionName(event, callback) {\n callback(null, \"hello world\")\n}",
  6. "id": 1105,
  7. "name": "come_from_open_api",
  8. "plan_circle": "P_FREE",
  9. "remark": "你好,云函数",
  10. "updated_at": 1537704381,
  11. "updated_by": ""
  12. }

状态码说明

201: 创建成功。

400: 参数不合法:云函数名或代码为字段为空。

401: 未授权,请检查请求头中的 Authorization 字段是否正确。

获取当前小程序的所有云函数

接口地址

GET https://cloud.minapp.com/oserve/v1.3/cloud-function/

返回参数说明

参数 类型 说明
audit_status String 可能的值: approved, rejected, waiting
created_by String 创建者昵称
function_code String 云函数代码
id Integer 云函数 ID
name String 云函数名
plan_circle String 套餐类型
remark String 备注
updated_at Integer 最近一次更新时间
updated_by String 更新者昵称

代码示例

{% tabs getAllCloudFunctionCurl=”Curl”, getAllCloudFunctionNode=”Node”, getAllCloudFunctionPHP=”PHP” %}

{% content “getAllCloudFunctionCurl” %}

  1. curl -X GET \
  2. https://cloud.minapp.com/oserve/v1.3/cloud-function/ \
  3. -H 'Authorization: Bearer 2323d124881bd3d63c9bb78458252454f676b'

{% content “getAllCloudFunctionNode” %}

  1. var request = require("request");
  2. var options = {
  3. method: 'GET',
  4. url: 'https://cloud.minapp.com/oserve/v1.3/cloud-function/',
  5. headers:
  6. {
  7. Authorization: 'Bearer 2323d124881bd3d6bb78458252454f80a676b'
  8. },
  9. json: true
  10. };
  11. request(options, function (error, response, body) {
  12. if (error) throw new Error(error);
  13. console.log(body);
  14. });

{% content “getAllCloudFunctionPHP” %}

  1. <?php
  2. $token = '';
  3. $url = "https://cloud.minapp.com/oserve/v1.3/cloud-function/";
  4. $ch = curl_init();
  5. $header = array(
  6. "Authorization: Bearer {$token}",
  7. 'Content-Type: application/json; charset=utf-8'
  8. );
  9. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  10. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  11. curl_setopt($ch, CURLOPT_URL, $url);
  12. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
  13. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  14. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  15. $response = curl_exec($ch);
  16. $err = curl_error($ch);
  17. curl_close($ch);
  18. if ($err) {
  19. echo "cURL Error #:" . $err;
  20. } else {
  21. echo $response;
  22. }

{% endtabs %}

返回示例

  1. {
  2. "meta": {
  3. "limit": 20,
  4. "next": null,
  5. "offset": 0,
  6. "previous": null,
  7. "total_count": 2
  8. },
  9. "objects": [
  10. {
  11. "audit_status": "approved",
  12. "created_at": 1537924886,
  13. "created_by": "somebody",
  14. "function_code": "exports.main = function functionName(event, callback) {\n callback(null, \"hello world\")\n}",
  15. "id": 1121,
  16. "name": "come_from_open_API_v1.3",
  17. "plan_circle": "P_FREE",
  18. "remark": "你好,云函数",
  19. "updated_at": 1537924886,
  20. "updated_by": ""
  21. },
  22. {
  23. "audit_status": "approved",
  24. "created_at": 1537704269,
  25. "created_by": "somebody",
  26. "function_code": "exports.main = function functionName(event, callback) {\n callback(null, \"hello world\")\n}",
  27. "id": 1102,
  28. "name": "测试",
  29. "plan_circle": "P_FREE",
  30. "remark": "你好,云函数",
  31. "updated_at": 1537704426,
  32. "updated_by": "somebody"
  33. },
  34. ]
  35. }

状态码说明

200: 成功。

401: 未授权,请检查请求头中的 Authorization 字段是否正确。

401: 参数不合法:云函数名或代码为字段为空

获取云函数详细信息

接口地址

GET https://cloud.minapp.com/oserve/v1.3/cloud-function/:cloud-function-name/

其中 :cloud-function-name 为云函数的名字。

返回参数说明

参数 类型 说明
audit_status String 可能的值: approved, rejected, waiting
created_by String 创建者昵称
function_code String 云函数代码
id Integer 云函数 ID
name String 云函数名
plan_circle String 套餐类型
remark String 备注
updated_at Integer 最近一次更新时间
updated_by String 更新者昵称

代码示例

{% tabs getCloudFunctionInfoCurl=”Curl”, getCloudFunctionInfoNode=”Node”, getCloudFunctionInfoPHP=”PHP” %}

{% content “getCloudFunctionInfoCurl” %}

  1. curl -X GET \
  2. https://cloud.minapp.com/oserve/v1.3/cloud-function/test/ \
  3. -H 'Authorization: Bearer 2323d124881bd3d63c9bb78458252454f676b'

{% content “getCloudFunctionInfoNode” %}

  1. var request = require("request");
  2. var cloudFunctionName = '';
  3. var options = {
  4. method: 'GET',
  5. url: 'https://cloud.minapp.com/oserve/v1.3/cloud-function/' + cloudFunctionName + '/',
  6. headers:
  7. {
  8. Authorization: 'Bearer 2323d124881bd3d63c9bb78458252454f80a676b'
  9. },
  10. json: true
  11. };
  12. request(options, function (error, response, body) {
  13. if (error) throw new Error(error);
  14. console.log(body);
  15. });

{% content “getCloudFunctionInfoPHP” %}

  1. <?php
  2. $token = '';
  3. $cloud_function_name = '';
  4. $url = "https://cloud.minapp.com/oserve/v1.3/cloud-function/{$cloud_function_name}/";
  5. $ch = curl_init();
  6. $header = array(
  7. "Authorization: Bearer {$token}",
  8. 'Content-Type: application/json; charset=utf-8'
  9. );
  10. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  11. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  12. curl_setopt($ch, CURLOPT_URL, $url);
  13. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
  14. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  15. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  16. $response = curl_exec($ch);
  17. $err = curl_error($ch);
  18. curl_close($ch);
  19. if ($err) {
  20. echo "CURL Error #:" . $err;
  21. } else {
  22. echo $response;
  23. }

{% endtabs %}

返回示例

  1. {
  2. "audit_status": "approved",
  3. "created_at": 1535903214,
  4. "created_by": "somebody",
  5. "function_code": "exports.main = function functionName(event, callback) {\n callback(null, \"hello world\") fot debug\n}",
  6. "id": 847,
  7. "name": "test",
  8. "plan_circle": "P_FREE",
  9. "remark": "",
  10. "updated_at": 1537516164,
  11. "updated_by": "somebody"
  12. }

状态码说明

200: 成功。

401: 未授权,请检查请求头中的 Authorization 字段是否正确。

404: 云函数不存在。

修改云函数

接口地址

PATCH https://cloud.minapp.com/oserve/v1.3/cloud-function/:cloud-function-name/

其中 :cloud-function-name 为云函数名字。

请求参数说明

参数 类型 必填 说明
name String 云函数名,不能为空字符串
function_code String 云函数代码,不能为空字符串
plan_circle String 套餐类型,可能的值:P_FREE(128 MB 内存), P_PERSONAL(256 MB 内存)
remark String 备注

返回参数说明

参数 类型 说明
audit_status String 可能的值: approved, rejected, waiting
created_by String 创建者昵称
function_code String 云函数代码
id Integer 云函数 ID
name String 云函数名
plan_circle String 套餐类型,可能的值:P_FREE(128 MB 内存), P_PERSONAL(256 MB 内存)
remark String 备注
updated_at Integer 最近一次更新时间
updated_by String 更新者昵称

代码示例

{% tabs modifyCloudFunctionCurl=”Curl”, modifyCloudFunctionNode=”Node”, modifyCloudFunctionPHP=”PHP” %}

{% content “modifyCloudFunctionCurl” %}

  1. curl -X PATCH \
  2. https://cloud.minapp.com/oserve/v1.3/cloud-function/test/ \
  3. -H 'Authorization: Bearer 2323d124881bd3d63c9bb78458252454f676b' \
  4. -H "Content-Type: application/json" \
  5. -d '{
  6. "function_code": "exports.main = function functionName(event, callback) {\n callback(null, \"hello world\")\n}",
  7. "remark": "你好,云函数"
  8. }'

{% content “modifyCloudFunctionNode” %}

  1. var request = require("request");
  2. var cloudFunctionName = '';
  3. var options = {
  4. method: 'PATCH',
  5. url: 'https://cloud.minapp.com/oserve/v1.3/cloud-function/' + cloudFunctionName + '/',
  6. headers:
  7. {
  8. 'Content-Type': 'application/json',
  9. Authorization: 'Bearer 2323d124881bd3d63c9bb784582554f80a676b'
  10. },
  11. body:
  12. {
  13. remark: '你好,云函数',
  14. },
  15. json: true
  16. };
  17. request(options, function (error, response, body) {
  18. if (error) throw new Error(error);
  19. console.log(body);
  20. });

{% content “modifyCloudFunctionPHP” %}

  1. <?php
  2. $token = '';
  3. $cloud_function_name = '';
  4. $url = "https://cloud.minapp.com/oserve/v1.3/cloud-function/{$cloud_function_name}/";
  5. $ch = curl_init();
  6. $header = array(
  7. "Authorization: Bearer {$token}",
  8. 'Content-Type: application/json; charset=utf-8'
  9. );
  10. $payload = json_encode(
  11. array(
  12. 'function_code'=> "exports.main = function functionName(event, callback) {\n callback(null, \"hello world\")\n}",
  13. 'remark'=> '你好,云函数'
  14. )
  15. );
  16. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  17. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  18. curl_setopt($ch, CURLOPT_URL, $url);
  19. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
  20. curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  21. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  22. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  23. $response = curl_exec($ch);
  24. $err = curl_error($ch);
  25. curl_close($ch);
  26. if ($err) {
  27. echo "CURL Error #:" . $err;
  28. } else {
  29. echo $response;
  30. }

{% endtabs %}

返回示例

  1. {
  2. "audit_status": "approved",
  3. "created_at": 1535903214,
  4. "created_by": "somebody",
  5. "function_code": "exports.main = function functionName(event, callback) {\n callback(null, \"hello world\")\n}",
  6. "id": 847,
  7. "name": "modify_from_open_api",
  8. "plan_circle": "P_FREE",
  9. "remark": "",
  10. "updated_at": 1537710962,
  11. "updated_by": "somebody"
  12. }

状态码说明

202: 修改成功。

400: 参数不合法:云函数名或代码为空。

401: 未授权,请检查请求头中的 Authorization 字段是否正确。

401: 参数不合法:云函数名或代码为空

删除一个云函数

接口地址

DELETE https://cloud.minapp.com/oserve/v1.3/cloud-function/:cloud-function-name/

其中 :cloud-function-name 是云函数的名字。

代码示例

{% tabs deleteCloudFunctionCurl=”Curl”,deleteCloudFunctionNode=”Node”,deleteCloudFunctionPHP=”PHP” %}

{% content “deleteCloudFunctionCurl” %}

  1. curl -X DELETE \
  2. https://cloud.minapp.com/oserve/v1.3/cloud-function/test/ \
  3. -H 'Authorization: Bearer 2323d124881bd3d63c9bb78458252454f676b'

{% content “deleteCloudFunctionNode” %}

  1. var request = require("request");
  2. var cloudFunctionName = '';
  3. var options = {
  4. method: 'DELETE',
  5. url: 'https://cloud.minapp.com/oserve/v1.3/cloud-function/' + cloudFunctionName + '/',
  6. headers:
  7. {
  8. Authorization: 'Bearer 2323d124881bd3d63c9bb784582524540a676b'
  9. },
  10. json: true
  11. };
  12. request(options, function (error, response, body) {
  13. if (error) throw new Error(error);
  14. console.log(response.statusCode);
  15. });

{% content “deleteCloudFunctionPHP” %}

  1. <?php
  2. $token = '';
  3. $cloud_function_name = ''; // 云函数的名字
  4. $url = "https://cloud.minapp.com/oserve/v1.3/cloud-function/{$cloud_function_name}/";
  5. $ch = curl_init();
  6. $header = array(
  7. "Authorization: Bearer {$token}",
  8. 'Content-Type: application/json; charset=utf-8'
  9. );
  10. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  11. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  12. curl_setopt($ch, CURLOPT_URL, $url);
  13. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
  14. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  15. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  16. $response = curl_exec($ch);
  17. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  18. $err = curl_error($ch);
  19. curl_close($ch);
  20. if ($err) {
  21. echo "CURL Error #:" . $err;
  22. } else {
  23. echo $httpCode;
  24. }

{% endtabs %}

返回示例

返回体为空。

状态码说明

204: 删除成功。

401: 未授权,请检查请求头中的 Authorization 字段是否正确。

404: 没有找到对应的云函数。