/
endpoint.go
64 lines (52 loc) · 1.8 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
package communications
import (
"context"
"encoding/json"
cs "github.com/cloudtrust/common-service"
errrorhandler "github.com/cloudtrust/common-service/errors"
api "github.com/cloudtrust/keycloak-bridge/api/communications"
msg "github.com/cloudtrust/keycloak-bridge/internal/constants"
"github.com/go-kit/kit/endpoint"
)
// Endpoints wraps a service behind a set of endpoints.
type Endpoints struct {
GetActions endpoint.Endpoint
SendEmail endpoint.Endpoint
SendSMS endpoint.Endpoint
}
// MakeGetActionsEndpoint creates an endpoint for GetActions
func MakeGetActionsEndpoint(component Component) cs.Endpoint {
return func(ctx context.Context, _ interface{}) (interface{}, error) {
return component.GetActions(ctx)
}
}
// MakeSendEmailEndpoint makes the SendEmail Endpoint
func MakeSendEmailEndpoint(component Component) cs.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
var m = req.(map[string]string)
var body api.EmailRepresentation
err := json.Unmarshal([]byte(m[reqBody]), &body)
if err != nil {
return nil, errrorhandler.CreateBadRequestError(msg.MsgErrInvalidParam + "." + msg.Body)
}
if err = body.Validate(); err != nil {
return nil, err
}
return nil, component.SendEmail(ctx, m[prmRealm], body)
}
}
// MakeSendSMSEndpoint makes the SendSMS Endpoint
func MakeSendSMSEndpoint(component Component) cs.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
var m = req.(map[string]string)
var body api.SMSRepresentation
err := json.Unmarshal([]byte(m[reqBody]), &body)
if err != nil {
return nil, errrorhandler.CreateBadRequestError(msg.MsgErrInvalidParam + "." + msg.Body)
}
if err = body.Validate(); err != nil {
return nil, err
}
return nil, component.SendSMS(ctx, m[prmRealm], body)
}
}