-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhttp_client.go
More file actions
64 lines (57 loc) · 1.66 KB
/
http_client.go
File metadata and controls
64 lines (57 loc) · 1.66 KB
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 confluent
import (
"crypto/tls"
"encoding/base64"
"io"
"io/ioutil"
"net/http"
)
type HttpClient interface {
DoRequest(method string, uri string, reqBody io.Reader) (responseBody []byte, statusCode int, status string, err error)
}
type DefaultHttpClient struct {
// BaseURL : https://localhost:8090
// API endpoint of Confluent platform
BaseUrl string
// Define the user-agent would be sent to confluent api
// Default: confluent-client-go-sdk
Username string
Password string
Token string
UserAgent string
}
func NewDefaultHttpClient(baseUrl string, username string, password string) *DefaultHttpClient {
return &DefaultHttpClient{
BaseUrl: baseUrl,
Username: username,
Password: password,
UserAgent: userAgent,
}
}
func (c *DefaultHttpClient) DoRequest(method string, uri string, reqBody io.Reader) (responseBody []byte, statusCode int, status string, err error) {
client := http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
req, err := http.NewRequest(method, c.BaseUrl+uri, reqBody)
if err != nil {
return nil, 0, "", err
}
if c.Token != "" {
req.Header.Set("Authorization", "Bearer "+c.Token)
} else {
auth := c.Username + ":" + c.Password
token := base64.StdEncoding.EncodeToString([]byte(auth))
req.Header.Set("Authorization", "Basic " + token)
}
req.Header.Set("User-Agent", c.UserAgent)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
res, respErr := client.Do(req)
if respErr != nil {
return nil, 0, "", respErr
}
respBody, bodyErr := ioutil.ReadAll(res.Body)
return respBody, res.StatusCode, res.Status, bodyErr
}