-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
131 lines (98 loc) · 2.66 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
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
package utmconfig
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"sync"
"github.com/utmstack/config-client-go/enum"
"github.com/utmstack/config-client-go/types"
"github.com/utmstack/config-client-go/util"
)
type UTMConfigClient struct {
ConnectionKey string `json:"connectionKey"`
MasterLocation string `json:"masterLocation"`
}
var utmClient *UTMConfigClient
var one sync.Once
func NewUTMClient(connectionKey, masterLocation string) *UTMConfigClient {
one.Do(func() {
// fmt.Println("Creating UTMConfigClient instance")
utmClient = &UTMConfigClient{
ConnectionKey: connectionKey,
MasterLocation: masterLocation,
}
})
return utmClient
}
func (s *UTMConfigClient) doRequest(req *http.Request) ([]byte, error) {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
client := new(http.Client)
req.Header.Set(util.UTMInternalKeyHeaderName, s.ConnectionKey)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("status: %d; body: %s; header: %s", resp.StatusCode, body, resp.Header.Get("X-UtmStack-error"))
}
return body, nil
}
func (s *UTMConfigClient) CreateUTMConfig(config *types.ConfigurationSection) error {
url := fmt.Sprintf(util.RegisterConfigURL, s.MasterLocation)
j, err := json.Marshal(config)
if err != nil {
return err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(j))
if err != nil {
return err
}
_, err = s.doRequest(req)
return err
}
func (s *UTMConfigClient) GetUTMConfig(module enum.UTMModule) (*types.ConfigurationSection, error) {
url := fmt.Sprintf(util.GetConfigURL, s.MasterLocation, module)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
response, err := s.doRequest(req)
if err != nil {
return nil, err
}
var data types.ConfigurationSection
err = json.Unmarshal(response, &data)
if err != nil {
return nil, err
}
return &data, nil
}
func (s *UTMConfigClient) GetAllUTMConfig() ([]*types.ConfigurationSection, error) {
confs := []*types.ConfigurationSection{}
for _, module := range enum.AllV10Integrations {
url := fmt.Sprintf(util.GetConfigURL, s.MasterLocation, module)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
response, err := s.doRequest(req)
if err != nil {
return nil, err
}
var data types.ConfigurationSection
err = json.Unmarshal(response, &data)
if err != nil {
return nil, err
}
confs = append(confs, &data)
}
return confs, nil
}