Skip to content

Commit

Permalink
update cloudfunction demo
Browse files Browse the repository at this point in the history
  • Loading branch information
yycccc91 committed Feb 8, 2022
1 parent cd8a5f8 commit 0f537c7
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 0 deletions.
45 changes: 45 additions & 0 deletions cloudfunction/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# AGCServerFunction_Nodejs 快速入门


## 简介
通过该demo演示用户如何在自己的工程中集成Server端云函数SDK,使用云函数互调功能。

## 环境要求
* Node.js v10.12.0及以上环境

## 快速入门
在运行quickstart前,您需要
1. 如果没有华为开发者联盟帐号,需要先[注册账号](https://developer.huawei.com/consumer/cn/doc/start/registration-and-verification-0000001053628148) 并通过实名认证。
2. 使用申请的帐号登录[AppGallery Connect](https://developer.huawei.com/consumer/cn/service/josp/agc/index.html#/) 网站创建一个项目。
3. 开通云函数服务后,在“项目设置”页面中点击“Server SDK”页签,点击认证凭据中的“创建”,创建完后生成认证凭据,点击“下载认证凭据”。
4. 将下载后的认证凭据文件agc-apiclient-*.json放置到您指定路径下,在[index.js](./index.js)中初始化SDK时将会使用到该文件.
5. 在命令行中依次运行如下命令以运行demo:
``` bash
# 1.1 安装 AGC function-server sdk
npm install --save @agconnect/[email protected]
npm install --save @agconnect/[email protected]
# 1.2 或向package.json文件添加如下配置,再执行npm install命令
"dependencies": {
"@agconnect/common-server": "1.0.0",
"@agconnect/function-server": "1.0.0"
}

# 2 启动demo
npm run build
```
6. 更多细节请查看以下链接: [Function-Server](https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-Guides/agc-cloudfunction-functioncall-server-nodejs-0000001192526140)

## 示例代码

Sample code: index.js

## 技术支持

如果您对使用AppGallery Connect示例代码有疑问,请通过如下途径寻求帮助:
- 访问[Stack Overflow](https://stackoverflow.com/) , 在`AppGallery`标签下提问,有华为研发专家在线一对一解决您的问题。
- 访问[华为开发者论坛](https://forums.developer.huawei.com/forumPortal/en/home) AppGallery Connect板块与其他开发者进行交流。

如果您在尝试示例代码中遇到问题,请向仓库提交[issue](https://github.com/AppGalleryConnect/agc-demos/issues) ,也欢迎您提交[Pull Request](https://github.com/AppGalleryConnect/agc-demos/pulls) 。

## 授权许可
该示例代码经过[Apache 2.0 授权许可](http://www.apache.org/licenses/LICENSE-2.0) 。
9 changes: 9 additions & 0 deletions cloudfunction/agc-apiclient-test-test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"type":"project_client_id",
"developer_id":"xxxx",
"project_id":"xxxx",
"client_id":"xxxx",
"client_secret":"xxxx",
"configuration_version":"3.0",
"region":"CN"
}
81 changes: 81 additions & 0 deletions cloudfunction/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const path = require('path');
const {AGCClient} = require("@agconnect/common-server");
const {CredentialParser} = require("@agconnect/common-server");
const {AGCFunction} = require("@agconnect/function-server");

// 加载凭证文件
try {
console.log("--------agc-------")
let client_name = "./agc-apiclient-test-test.json";
let client_path = path.join(__dirname, client_name);
let credential = CredentialParser.toCredential(client_path);
AGCClient.initialize(credential);
} catch (error) {
console.error(error);
return;
}
let agcFunction = new AGCFunction();

// 本地调用测试
let myHandlerTest = async function() {
console.log("-------call tag 1--------");

// 构造云函数实例
try {
let value = agcFunction.wrap("callback", "$latest");
value.setTimeout(20000);

console.log("-------调用------");

// 无参调用
let res1 = await value.call();
console.log("res1: " + res1.getValue());

// string类型传参调用
let str = "test s string";
let res2 = await value.call(str);
console.log("res2: " + res2.getValue());

// Buffer类型传参调用
const buf = Buffer.alloc(10, 1);
let res3 = await value.call(buf);
console.log("res3: " + res3.getValue());

console.log("-------成功--------");
} catch (error) {
console.log("-------调用异常-------");
console.log(error);
}
}

myHandlerTest();

// 上传AGC网站调用
let myHandler = async function(event, context, callback, logger) {
logger.info("-------call tag 1--------");

// 返回结果内容
let good_res = new context.HTTPResponse({"good": "good"},{
"res-type": "simple example",
"faas-content-type": "json"
}, "application/json", "200");
let bad_res = new context.HTTPResponse({"bad": "bad"},{
"res-type": "simple example",
"faas-content-type": "json"
}, "application/json", "200");

try {
let res = await agcFunction.wrap("callback", "$latest").call();
logger.info("---------调用--------");
logger.info("res: " + res.getValue());

logger.info("-------成功--------");
callback(good_res);
} catch (error) {
logger.error("-------调用异常-------");
logger.error(error);
callback(bad_res);
}
}

module.exports.myHandler = myHandler;
15 changes: 15 additions & 0 deletions cloudfunction/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "agcserverfunction_nodejs_demo",
"version": "1.0.0",
"description": "AGC Server端云函数互调demo",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@agconnect/common-server": "1.0.0",
"@agconnect/function-server": "1.0.0"
}
}

0 comments on commit 0f537c7

Please sign in to comment.