/
client.go
65 lines (55 loc) · 1.88 KB
/
client.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
package data_messages
import (
"fmt"
log "github.com/sirupsen/logrus"
types "github.com/amyllz/my-go-dots/dots_common/types/data"
)
type ClientRequest struct {
DotsClient []types.DotsClient `json:"ietf-dots-data-channel:dots-client"`
}
type ClientResponse struct {
DotsClient types.DotsClient `json:"ietf-dots-data-channel:dots-client"`
}
func (r *ClientRequest) Validate() (bool, string) {
errorMsg := ""
if len(r.DotsClient) <= 0 {
log.WithField("len", len(r.DotsClient)).Error("'dots-client' is not exist.")
errorMsg = fmt.Sprintf("Body Data Error : 'dots-client' is not exist")
return false, errorMsg
}
if len(r.DotsClient) > 1 {
log.WithField("len", len(r.DotsClient)).Error("multiple 'dots-client'.")
errorMsg = fmt.Sprintf("Body Data Error : Have multiple 'dots-client' (%d)", len(r.DotsClient))
return false, errorMsg
}
client := r.DotsClient[0]
if client.Cuid == "" {
log.Error("Missing required 'cuid' attribute.")
errorMsg = fmt.Sprintf("Body Data Error : Missing 'cuid'")
return false, errorMsg
}
if client.Aliases != nil {
log.WithField("alias", client.Aliases).Error("'alias' found.")
errorMsg = fmt.Sprintf("Body Data Error : Found 'alias'")
return false, errorMsg
}
if client.ACLs != nil {
log.WithField("acls", client.ACLs).Error("'acls' found.")
errorMsg = fmt.Sprintf("Body Data Error : Found 'acls'")
return false, errorMsg
}
return true, ""
}
func (r *ClientRequest) ValidateWithCuid(cuid string) (bool, string) {
bValid, errorMsg := r.Validate()
if !bValid {
return false, errorMsg
}
client := r.DotsClient[0]
if client.Cuid != cuid {
log.WithField("cuid(req)", client.Cuid).WithField("cuid(URI)", cuid).Error("request/URI cuid mismatch.")
errorMsg = fmt.Sprintf("Request/URI cuid mismatch : (%v) / (%v)", client.Cuid, cuid)
return false, errorMsg
}
return true, ""
}