-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrequest.go
59 lines (48 loc) · 1.39 KB
/
request.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
package mundipagg
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"github.com/lusantisuper/mundipagg/internal/utils"
)
// MakePostRequest do the low-level request
func MakePostRequest(data interface{}, secretKey string, indepotencyKey string, url string, requestType ...string) (*Response, error) {
postData, err := json.Marshal(data)
if err != nil {
panic(err)
}
requestMethod := "POST"
if len(requestType) > 0 {
requestMethod = requestType[0]
}
req, err := http.NewRequest(requestMethod, url, bytes.NewBuffer(postData))
// Setting the headers to make the request
req.Header.Set("Authorization", "Basic "+utils.ToBase64(secretKey+":"))
req.Header.Set("Content-Type", "application/json")
if !utils.IsStringEmpty(indepotencyKey) {
req.Header.Set("Idempotency-Key", indepotencyKey)
}
// Running the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// Results of the request
body, _ := ioutil.ReadAll(resp.Body)
if body == nil || string(body) == "" {
body = []byte("{}")
}
// Saving the result
response := Response{MundipaggJSONAnswer: string(body)}
if resp.StatusCode < 200 || resp.StatusCode >= 400 {
err = errors.New("Invalid Request:\nSended:\n" + string(postData) + "Received:\n" + string(body))
}
if err == nil {
err = json.Unmarshal(body, &response)
}
return &response, err
}