-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
207 lines (188 loc) · 4.2 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package client
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
)
type Client struct {
http *http.Client
username string
password string
endpoint string
key string
cert string
cacert string
insecure bool
}
var (
ErrNotFound = errors.New("Not found")
)
func loadEnv(client *Client) {
if os.Getenv("APPCLACKS_USERNAME") != "" {
client.username = os.Getenv("APPCLACKS_USERNAME")
}
if os.Getenv("APPCLACKS_PASSWORD") != "" {
client.password = os.Getenv("APPCLACKS_PASSWORD")
}
if os.Getenv("APPCLACKS_API_ENDPOINT") != "" {
client.endpoint = os.Getenv("APPCLACKS_API_ENDPOINT")
}
if os.Getenv("APPCLACKS_TLS_KEY") != "" {
client.key = os.Getenv("APPCLACKS_TLS_KEY")
}
if os.Getenv("APPCLACKS_TLS_CERT") != "" {
client.cert = os.Getenv("APPCLACKS_TLS_CERT")
}
if os.Getenv("APPCLACKS_TLS_CACERT") != "" {
client.cacert = os.Getenv("APPCLACKS_TLS_CACERT")
}
insecure := os.Getenv("APPCLACKS_TLS_INSECURE")
if insecure == "true" {
client.insecure = true
}
}
type ClientOption func(c *Client) error
func New(options ...ClientOption) (*Client, error) {
client := &Client{
http: &http.Client{},
}
loadEnv(client)
for _, option := range options {
err := option(client)
if err != nil {
return nil, err
}
}
if client.cert != "" || client.key != "" || client.cacert != "" || client.insecure {
tlsConfig, err := getTLSConfig(client.key, client.cert, client.cacert, "", client.insecure)
if err != nil {
return nil, err
}
transport := &http.Transport{
TLSClientConfig: tlsConfig,
}
client.http.Transport = transport
}
return client, nil
}
func WithUsername(username string) ClientOption {
return func(c *Client) error {
c.username = username
return nil
}
}
func WithPassword(password string) ClientOption {
return func(c *Client) error {
c.password = password
return nil
}
}
func WithEndpoint(endpoint string) ClientOption {
return func(c *Client) error {
c.endpoint = endpoint
return nil
}
}
func WithKey(key string) ClientOption {
return func(c *Client) error {
c.key = key
return nil
}
}
func WithCert(cert string) ClientOption {
return func(c *Client) error {
c.cert = cert
return nil
}
}
func WithCacert(cacert string) ClientOption {
return func(c *Client) error {
c.cacert = cacert
return nil
}
}
func WithInsecure(insecure bool) ClientOption {
return func(c *Client) error {
c.insecure = insecure
return nil
}
}
func (c *Client) sendRequest(ctx context.Context, url string, method string, body any, result any, queryParams map[string]string) (*http.Response, error) {
var reqBody io.Reader
if body != nil {
json, err := json.Marshal(body)
if err != nil {
return nil, err
}
reqBody = bytes.NewBuffer(json)
}
request, err := http.NewRequestWithContext(
ctx,
method,
fmt.Sprintf("%s%s", c.endpoint, url),
reqBody)
if err != nil {
return nil, err
}
if len(queryParams) != 0 {
q := request.URL.Query()
for k, v := range queryParams {
q.Add(k, v)
}
request.URL.RawQuery = q.Encode()
}
request.Header.Add("content-type", "application/json")
if c.username != "" {
authString := fmt.Sprintf("%s:%s", c.username, c.password)
creds := base64.StdEncoding.EncodeToString([]byte(authString))
request.Header.Add("Authorization", fmt.Sprintf("Basic %s", creds))
}
response, err := c.http.Do(request)
if err != nil {
return nil, err
}
b, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode >= 400 {
if response.StatusCode == 404 {
return nil, ErrNotFound
}
return nil, fmt.Errorf("the API returned an error: status %d\n%s", response.StatusCode, string(b))
}
if result != nil {
err = json.Unmarshal(b, result)
if err != nil {
return nil, err
}
}
return response, nil
}
func jsonMerge(s1 any, s2 any) (map[string]any, error) {
result := make(map[string]any)
str1, err := json.Marshal(s1)
if err != nil {
return nil, err
}
str2, err := json.Marshal(s2)
if err != nil {
return nil, err
}
err = json.Unmarshal(str1, &result)
if err != nil {
return nil, err
}
err = json.Unmarshal(str2, &result)
if err != nil {
return nil, err
}
return result, nil
}