码迷,mamicode.com
首页 > Web开发 > 详细

cloudevents js sdk 简单试用

时间:2019-10-07 11:29:24      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:load   log   eve   tor   处理   demo   tps   ret   header   

cloudevents 目前官方提供了不同语言的sdk,以下是js 的简单学习试用,从目前来说更新不是很好

clone 代码

git clone https://github.com/cloudevents/sdk-javascript.git

demo 代码运行

cd express-ex
yarn
yarn  start 

测试

demo 的readme 已经提供了比较全的操作说明,我们可以结合这个学习指南

  • 简单json 格式
curl -X POST \
     -d‘@../payload/v03/structured-event-0.json‘ \
     -H‘Content-Type:application/cloudevents+json‘ \
     http://localhost:3000/v03

效果

curl -X POST \
     -d‘@../payload/v03/structured-event-0.json‘ \
     -H‘Content-Type:application/cloudevents+json‘ \
     http://localhost:3000/v03
{"specversion":"0.3","id":"b25e2717-a470-45a0-8231-985a99aa9416","type":"com.github.pull.create","source":"https://github.com/cloudevents/spec/pull/123","time":"2019-07-04T17:31:00.000Z","datacontenttype":"application/json","data":{"much":"wow"}}% 
 
  • 包含扩展的操作
curl -X POST \
     -d‘@../payload/v03/structured-event-1.json‘ \
     -H‘Content-Type:application/cloudevents+json‘ \
     http://localhost:3000/v03

效果

 curl -X POST \
     -d‘@../payload/v03/structured-event-1.json‘ \
     -H‘Content-Type:application/cloudevents+json‘ \
     http://localhost:3000/v03
{"specversion":"0.3","id":"70d3c768-63f8-40e7-aa9d-d197d530586b","type":"com.github.pull.create","source":"https://github.com/cloudevents/spec/pull/123","time":"2019-07-04T17:31:00.000Z","datacontenttype":"application/json","data":{"much":"wow"},"my-extension":{"some":"thing"}}% 
  • 二进制base64 处理
curl -X POST \
     -d‘@../payload/data-1.txt‘ \
     -H‘Content-Type:application/json‘ \
     -H‘ce-specversion:0.3‘ \
     -H‘ce-type:com.github.pull.create‘ \
     -H‘ce-source:https://github.com/cloudevents/spec/pull/123‘ \
     -H‘ce-id:45c83279-c8a1-4db6-a703-b3768db93887‘ \
     -H‘ce-time:2019-06-21T17:31:00Z‘ \
     -H‘ce-datacontentencoding:base64‘ \
     http://localhost:3000/v03

效果

curl -X POST \
     -d‘@../payload/data-1.txt‘ \
     -H‘Content-Type:application/json‘ \
     -H‘ce-specversion:0.3‘ \
     -H‘ce-type:com.github.pull.create‘ \
     -H‘ce-source:https://github.com/cloudevents/spec/pull/123‘ \
     -H‘ce-id:45c83279-c8a1-4db6-a703-b3768db93887‘ \
     -H‘ce-time:2019-06-21T17:31:00Z‘ \
     -H‘ce-datacontentencoding:base64‘ \
     http://localhost:3000/v03
{"specversion":"0.3","id":"45c83279-c8a1-4db6-a703-b3768db93887","type":"com.github.pull.create","source":"https://github.com/cloudevents/spec/pull/123","time":"2019-06-21T17:31:00.000Z","datacontenttype":"application/json","datacontentencoding":"base64","data":{"much":"wow"}}% 
 

api 使用说明

  • 解析代码
const v03 = require("cloudevents-sdk/v03");
var unmarshaller03 = new v03.HTTPUnmarshaller();
?
const v02 = require("cloudevents-sdk/v02");
var unmarshaller02 = new v02.HTTPUnmarshaller();
?
app.post("/v03", function (req, res) {
  console.log(req.headers);
  console.log(req.body);
?
  unmarshaller03.unmarshall(req.body, req.headers)
    .then(cloudevent => {
      // pretty print
      console.log("Accepted event:");
      console.log(JSON.stringify(cloudevent.format(), null, 2));
?
      res.status(201)
            .json(cloudevent.format());
  })
  .catch(err => {
    console.error(err);
    res.status(415)
          .header("Content-Type", "application/json")
          .send(JSON.stringify(err));
  });
});
  • 生成event
    sdk 提供了方便的event 创建方法
let ce =
  v03.event()
       .type("com.github.pull.create")
       .source("urn:event:from:myapi/resourse/123");
 
 
  • 说明
    对于event的解码处理,我们可以看到直接使用 HTTPUnmarshaller,然后通过请求body 以及header 进行数据处理,还是比较简单的
    同时github文档还是比较全的,提供了比较完整的api 使用说明

说明

目前js sdk 分装了部分简单编码处理,比如二进制,base64 。。,其他格式的暂时还没有,

参考资料

https://github.com/cloudevents/sdk-javascript

cloudevents js sdk 简单试用

标签:load   log   eve   tor   处理   demo   tps   ret   header   

原文地址:https://www.cnblogs.com/rongfengliang/p/11629646.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!