-
Notifications
You must be signed in to change notification settings - Fork 3
/
http.go
142 lines (122 loc) · 3.39 KB
/
http.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
// Copyright (c) ClaceIO, LLC
// SPDX-License-Identifier: Apache-2.0
package utils
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"strings"
"time"
)
const (
ApplicationJson = "application/json"
ClaceServiceLocation = "clace"
)
type HttpClient struct {
client *http.Client
serverUri string
user string
password string
}
// NewHttpClient creates a new HttpClient instance
func NewHttpClient(serverUri, user, password string, skipCertCheck bool) *HttpClient {
serverUri = os.ExpandEnv(serverUri)
var client *http.Client
if !strings.HasPrefix(serverUri, "http://") && !strings.HasPrefix(serverUri, "https://") {
transport := &Transport{}
// Using unix domain sockets
transport.RegisterLocation(ClaceServiceLocation, serverUri)
t := &http.Transport{}
t.RegisterProtocol(Scheme, transport)
client = &http.Client{
Transport: transport,
Timeout: time.Duration(180) * time.Second,
}
serverUri = fmt.Sprintf("%s://%s", Scheme, ClaceServiceLocation)
} else {
customTransport := http.DefaultTransport.(*http.Transport).Clone()
customTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: skipCertCheck}
client = &http.Client{
Transport: customTransport,
Timeout: time.Duration(180) * time.Second,
}
}
return &HttpClient{
client: client,
serverUri: serverUri,
user: user,
password: password,
}
}
func (h *HttpClient) Get(url string, params url.Values, output any) error {
return h.request(http.MethodGet, url, params, nil, output)
}
func (h *HttpClient) Post(url string, params url.Values, input any, output any) error {
return h.request(http.MethodPost, url, params, input, output)
}
func (h *HttpClient) Delete(url string, params url.Values, output any) error {
return h.request(http.MethodDelete, url, params, nil, output)
}
func (h *HttpClient) request(method, apiPath string, params url.Values, input any, output any) error {
var resp *http.Response
var payloadBuf bytes.Buffer
if input != nil {
if err := json.NewEncoder(&payloadBuf).Encode(input); err != nil {
return fmt.Errorf("error encoding request: %w", err)
}
}
u, err := url.Parse(h.serverUri)
if err != nil {
return err
}
u.Path = path.Join(u.Path, apiPath)
if params != nil {
u.RawQuery = params.Encode()
}
request, err := http.NewRequest(method, u.String(), &payloadBuf)
if err != nil {
return fmt.Errorf("error creating request: %w", err)
}
request.SetBasicAuth(h.user, h.password)
request.Header.Set("Accept", ApplicationJson)
if method == http.MethodPost || method == http.MethodPut || method == http.MethodPatch {
request.Header.Set("Content-Type", ApplicationJson)
}
resp, err = h.client.Do(request)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
errBody, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
var errResp RequestError
parseErr := json.Unmarshal(errBody, &errResp)
if parseErr != nil || errResp.Code == 0 {
errResp.Code = resp.StatusCode
errResp.Message = string(errBody)
}
return errResp
}
if resp.StatusCode == http.StatusNoContent {
return nil
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if output != nil {
if err := json.Unmarshal(body, output); err != nil {
return fmt.Errorf("error parsing response: %w", err)
}
}
return nil
}