-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.go
61 lines (52 loc) · 1.38 KB
/
func.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
package utils
import (
"bytes"
"encoding/json"
"net/http"
"github.com/Datosystem/go_api_core/message"
)
// Convert the input data to a byte array
func ConvertArrayByte(data interface{}) []byte {
reqBodyBytes := new(bytes.Buffer)
json.NewEncoder(reqBodyBytes).Encode(data)
return reqBodyBytes.Bytes()
}
/*
It makes an HTTP call and returns the response body:
* ${ @method } The method to use in the call, e.g., http.MethodGet
* ${ @url } URL to call
* ${ @header } Headers to set in the request
* ${ @jsonData } The JSON to send to the destination
*/
func MakeHTTPCall(method, url string, headers *http.Header, jsonData []byte) ([]byte, error) {
client := &http.Client{}
req, _ := http.NewRequest(method, url, bytes.NewBuffer(jsonData))
if headers != nil {
req.Header = *headers
}
if headers == nil || headers.Get("Content-Type") == "" {
req.Header.Set("Content-Type", "application/json")
}
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(res.Body)
if err != nil {
return nil, err
}
if res.StatusCode >= 400 && res.StatusCode <= 500 {
return nil, &message.Msg{Status: res.StatusCode, Message: buf.String()}
}
return buf.Bytes(), nil
}
func StringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}