Skip to content

Instantly share code, notes, and snippets.

@lite
Created February 9, 2017 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lite/1b5806b6557404b8b3ebf0af8ec5be07 to your computer and use it in GitHub Desktop.
Save lite/1b5806b6557404b8b3ebf0af8ec5be07 to your computer and use it in GitHub Desktop.
Webhook

webhook

接口设计

// POST
{
 "source": {
 "platform": "A Niubility Platform",
 "other": "other infomation"
 },
 "target": {
 "name": "Barret Lee",
 "version": "1.0.0",
 "other": "other infomation"
 },
 "data": {
 //...
 },
 "needCallback": true,
 "serverTime": "2016-12-26 11:55:45"
}
  • 在接口中需要详细说明,数据源从哪里来(source),
  • 数据需要穿给谁(target),
  • 传递那些数据(data),
  • 是否需要对方发送回执(needCallback),以及其他信息。

2、多 Hook 设计

WebHook 的设计一定要支持多 Hook,你永远都不知道下一个系统对接需求会在什么时候到来。

对于复杂的 Hook 设计,表格中可能还有:是否需要回执、是否停用、安全 Token、数据配置等项。

3、安全性设计

这里的安全性是为 Client 考虑的,Client 可能对 refer 或者 origin 做了限制,但这远远不够。当用户在 Server 端注册 WebHook 时,就应该开始考虑 Hook 的安全性了:

// POST response.setHeader("x-webhook-sign", SHA1(webhook));

4、retry 机制

极有可能因为 Client 的不稳定,导致 Hook 调用失败,此时可以考虑多次尝试:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment