-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpUtils.go
142 lines (113 loc) · 3.23 KB
/
httpUtils.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
package common
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"go-graphql-mongo-server/config"
"go-graphql-mongo-server/logger"
"net/http"
)
func RespondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)
w.Header().Set("Content-Type", "application/json")
// Set HSTS header is HTTPS is enabled
if config.Store.HTTPSCert.HTTPSEnabled {
w.Header().Set("Strict-Transport-Security", "max-age=31536000; includeSubDomains")
}
if config.Store.CORSAllowOrigins != "" {
w.Header().Set("Access-Control-Allow-Origin", config.Store.CORSAllowOrigins)
}
w.WriteHeader(code)
if code != http.StatusNoContent {
_, err := w.Write(response)
if err != nil {
logger.Log.Errorf("Error in writing response %+v", err)
}
}
}
func RespondWithUnauthorized(w http.ResponseWriter) {
RespondWithJSON(w, http.StatusUnauthorized, map[string]string{"message": "Unauthorized"})
}
type HTTPClient struct {
url string
httpClient *http.Client
headers http.Header
}
func GetHTTPClient(useProxy bool) *http.Client {
tr := &http.Transport{}
if config.Store.InsecureSkipVerify {
// #nosec G402
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
if useProxy {
tr.Proxy = http.ProxyFromEnvironment
}
return &http.Client{
Transport: tr,
}
}
func NewHTTPClient(url string, token string, tlsSkipVerify bool) *HTTPClient {
client := &HTTPClient{
url: url,
httpClient: &http.Client{},
headers: http.Header{
"Content-Type": []string{"application/json"},
"Accept": []string{"application/json"},
},
}
//nolint:gosec
if tlsSkipVerify {
customTransport := http.DefaultTransport.(*http.Transport).Clone()
customTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
client.httpClient.Transport = customTransport
}
if token != "" {
client.headers.Add("Authorization", token)
}
return client
}
func (client *HTTPClient) ExecuteGraphQl(query string, variables map[string]any, response any) error {
var reqBody bytes.Buffer
reqBodyObj := struct {
Query string `json:"query"`
Variables map[string]any `json:"variables"`
}{
Query: query,
Variables: variables,
}
if err := json.NewEncoder(&reqBody).Encode(reqBodyObj); err != nil {
return fmt.Errorf("error encoding request body: %v", err)
}
req, err := http.NewRequest(http.MethodPost, client.url, &reqBody)
if err != nil {
return fmt.Errorf("error creating request: %v", err)
}
req.Header = client.headers
resp, err := client.httpClient.Do(req)
if err != nil {
return fmt.Errorf("error executing request: %v", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("error executing request: %v", resp.Status)
}
defer resp.Body.Close()
gqlResponse := struct {
Data any
Errors []struct {
Message string
}
}{
Data: response,
}
if err := json.NewDecoder(resp.Body).Decode(&gqlResponse); err != nil {
x := new(bytes.Buffer)
_, resError := x.ReadFrom(resp.Body)
logger.Log.Errorf("error decoding response body: %v", resError)
return fmt.Errorf("error decoding response body : %v : %v", err, x.String())
}
if len(gqlResponse.Errors) > 0 {
return fmt.Errorf("graphql error : %v", gqlResponse.Errors[0].Message)
}
return nil
}