-
Notifications
You must be signed in to change notification settings - Fork 13
/
endpoint.go
74 lines (60 loc) · 1.54 KB
/
endpoint.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package http
import (
"context"
"github.com/MainfluxLabs/mainflux/webhooks"
"github.com/go-kit/kit/endpoint"
)
func createWebhooksEndpoint(svc webhooks.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(createWebhooksReq)
if err := req.validate(); err != nil {
return nil, err
}
whs := []webhooks.Webhook{}
for _, wReq := range req.Webhooks {
wh := webhooks.Webhook{
ThingID: req.ThingID,
Name: wReq.Name,
Format: wReq.Format,
Url: wReq.Url,
}
whs = append(whs, wh)
}
_, err := svc.CreateWebhooks(ctx, req.Token, whs...)
if err != nil {
return nil, err
}
res := webhookRes{
created: true,
}
return res, nil
}
}
func listWebhooksByThing(svc webhooks.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(listWebhooksReq)
if err := req.validate(); err != nil {
return nil, err
}
webhooks, err := svc.ListWebhooksByThing(ctx, req.Token, req.ThingID)
if err != nil {
return nil, err
}
return buildWebhooksResponse(webhooks), nil
}
}
func buildWebhooksResponse(webhooks []webhooks.Webhook) webhooksRes {
res := webhooksRes{Webhooks: []webhookResponse{}}
for _, wh := range webhooks {
webhook := webhookResponse{
ThingID: wh.ThingID,
Name: wh.Name,
Format: wh.Format,
Url: wh.Url,
}
res.Webhooks = append(res.Webhooks, webhook)
}
return res
}