-
Notifications
You must be signed in to change notification settings - Fork 0
/
clientBuilder.go
133 lines (117 loc) · 3.25 KB
/
clientBuilder.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package eventuate
import loglib "github.com/eventuate-clients/eventuate-client-golang/logger"
type ClientBuilderInstance struct {
overrideCredentials bool
overrideUrl bool
overrideSpace bool
overrideStompUrl bool
ll loglib.LogLevelEnum
lg loglib.Logger
apiKeyId string
apiKeySecret string
url string
space string
stompUrl string
typeHints typeHintsMap
}
func ClientBuilder() *ClientBuilderInstance {
return &ClientBuilderInstance{
false,
false,
false,
false,
0,
loglib.NewNilLogger(),
"",
"",
"https://api.eventuate.io",
"default",
"https://api.eventuate.io:61614",
nil}
}
func (bldr *ClientBuilderInstance) WithUrl(serverUrl string) *ClientBuilderInstance {
bldr.overrideUrl = true
bldr.url = serverUrl
return bldr
}
func (bldr *ClientBuilderInstance) WithStompUrl(serverUrl string) *ClientBuilderInstance {
(*bldr).overrideStompUrl = true
(*bldr).stompUrl = serverUrl
return bldr
}
func (bldr *ClientBuilderInstance) WithSpace(space string) *ClientBuilderInstance {
bldr.overrideSpace = true
bldr.space = space
return bldr
}
func (bldr *ClientBuilderInstance) WithCredentials(apiKeyId string,
apiKeySecret string) *ClientBuilderInstance {
bldr.overrideCredentials = true
bldr.apiKeyId = apiKeyId
bldr.apiKeySecret = apiKeySecret
return bldr
}
func (bldr *ClientBuilderInstance) WithNewTypeHints() *ClientBuilderInstance {
bldr.typeHints = NewTypeHintsMap()
return bldr
}
//func (bldr *ClientBuilderInstance) WithTypeHintPair(name string, typeInstance interface{}) *ClientBuilderInstance {
//
//}
func (bldr *ClientBuilderInstance) WithTypeHintPair(name string, typeInstance interface{}) *ClientBuilderInstance {
if bldr.typeHints == nil {
bldr.typeHints = NewTypeHintsMap()
}
err := bldr.typeHints.RegisterEventType(name, typeInstance)
if err != nil {
panic(err) // we need to panic as it is for the developers to see
}
return bldr
}
func (bldr *ClientBuilderInstance) SetLogLevel(level loglib.LogLevelEnum) *ClientBuilderInstance {
bldr.ll = level
bldr.lg = loglib.NewLogger(level)
return bldr
}
func (bldr *ClientBuilderInstance) BuildREST() (*RESTClient, error) {
var (
credentials *Credentials
err error
)
if (*bldr).overrideCredentials {
credentials, err = NewCredentials(bldr.apiKeyId, bldr.apiKeySecret, bldr.space)
} else {
credentials, err = NewCredentialsFromEnvAndSpace(bldr.space)
}
if err != nil {
return nil, err
}
result, clientErr := NewRESTClient(credentials, bldr.url)
if clientErr == nil {
result.ll = bldr.ll
result.lg = bldr.lg
}
result.typeHints = bldr.typeHints.MakeCopy()
return result, clientErr
}
func (bldr *ClientBuilderInstance) BuildSTOMP() (*StompClient, error) {
var (
credentials *Credentials
err error
)
if bldr.overrideCredentials {
credentials, err = NewCredentials(bldr.apiKeyId, bldr.apiKeySecret, bldr.space)
} else {
credentials, err = NewCredentialsFromEnvAndSpace(bldr.space)
}
if err != nil {
return nil, err
}
result, clientErr := NewStompClient(credentials, bldr.stompUrl)
if clientErr == nil {
result.ll = bldr.ll
result.lg = bldr.lg
}
result.typeHints = bldr.typeHints.MakeCopy()
return result, clientErr
}