-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
54 lines (45 loc) · 1.56 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package routing
import (
"fmt"
"github.com/scrapnode/kanthor/internal/entities"
"github.com/scrapnode/kanthor/pkg/identifier"
"github.com/scrapnode/kanthor/pkg/timer"
"github.com/scrapnode/kanthor/pkg/utils"
"github.com/scrapnode/kanthor/project"
)
func NewRequest(
timer timer.Timer,
msg *entities.Message,
ep *entities.Endpoint,
epr *entities.EndpointRule,
) *entities.Request {
// construct request
req := &entities.Request{
MsgId: msg.Id,
Tier: msg.Tier,
AppId: msg.AppId,
Type: msg.Type,
EpId: ep.Id,
Metadata: entities.Metadata{},
Headers: entities.Header{},
Body: msg.Body,
Uri: ep.Uri,
Method: ep.Method,
}
// must use merge function otherwise you will edit the original data
req.Headers.Merge(msg.Headers)
req.Metadata.Merge(msg.Metadata)
req.Id = identifier.New(entities.IdNsReq)
req.SetTS(timer.Now())
req.Headers.Set("User-Agent", fmt.Sprintf("Kanthor/%s", project.GetVersion()))
req.Metadata.Set(entities.MetaEprId, epr.Id)
req.Headers.Set(entities.HeaderIdempotencyKey, msg.Id)
// https://github.com/standard-webhooks/standard-webhooks/blob/main/spec/standard-webhooks.md
req.Headers.Set(entities.HeaderWebhookId, msg.Id)
req.Headers.Set(entities.HeaderWebhookTs, fmt.Sprintf("%d", req.Timestamp))
signature := utils.SignatureSign(ep.SecretKey, fmt.Sprintf("%s.%d.%s", msg.Id, req.Timestamp, msg.Body))
req.Headers.Set(entities.HeaderWebhookSign, fmt.Sprintf("v1,%s", signature))
// custom headers
req.Headers.Set(entities.HeaderWebhookRef, fmt.Sprintf("%s/%s", msg.AppId, ep.Id))
return req
}