2 Star 4 Fork 5

联犀/中台模块

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
tenantOpenWebhook.go 1.29 KB
一键复制 编辑 原始数据 按行查看 历史
杨磊 提交于 2024-10-10 21:56 . feat: 更新mod
package sysExport
import (
"context"
"gitee.com/unitedrhino/core/service/syssvr/client/tenantmanage"
"gitee.com/unitedrhino/core/service/syssvr/pb/sys"
"gitee.com/unitedrhino/share/caches"
"gitee.com/unitedrhino/share/errors"
"gitee.com/unitedrhino/share/eventBus"
"github.com/parnurzeal/gorequest"
"net/http"
"time"
)
const (
CodeDmDeviceConn = "dmDeviceConn"
CodeDmDevicePropertyReport = "devicePropertyReport"
)
type Webhook struct {
*caches.Cache[sys.TenantOpenWebHook, string]
}
func NewTenantOpenWebhook(pm tenantmanage.TenantManage, fastEvent *eventBus.FastEvent) (*Webhook, error) {
c, err := NewTenantOpenWebhookCache(pm, fastEvent)
if err != nil {
return nil, err
}
return &Webhook{Cache: c}, nil
}
func (i *Webhook) Publish(ctx context.Context, code string, in any) error {
hook, err := i.GetData(ctx, GenWebhookCacheKey(ctx, code))
if err != nil {
if errors.Cmp(err, errors.NotFind) {
return nil
}
return err
}
req := gorequest.New().Retry(3, time.Second*2)
url := hook.Hosts[0] + hook.Uri
req.Post(url)
for k, v := range hook.Handler {
req.Set(k, v)
}
resp, body, errs := req.Type("json").Send(in).End()
if errs != nil {
return errors.System.AddDetail(errs)
}
if resp.StatusCode != http.StatusOK {
return errors.System.AddDetail(body)
}
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/unitedrhino/core.git
git@gitee.com:unitedrhino/core.git
unitedrhino
core
中台模块
v1.0.0

搜索帮助