-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequests.go
108 lines (82 loc) · 2.77 KB
/
requests.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
package rester
import (
"bytes"
"encoding/json"
"net/http"
)
func PostRequest(url string, request interface{}, conversionType interface{}, headers map[string]string, customTransport http.RoundTripper) Requester {
bty, err := json.Marshal(request)
if err != nil {
panic(err)
}
r := func() (*http.Response, error) {
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(bty))
req.Header.Add("content-type", "application/json")
for key, value := range headers {
req.Header.Add(key, value)
}
client := getHttpClient(customTransport)
res, err := client.Do(req)
if err != nil {
return res, ErrRequestNotComplete
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&conversionType)
return res, err
}
return Requester{r, url, string(bty)}
}
func PutRequest(url string, request interface{}, conversionType interface{}, headers map[string]string, customTransport http.RoundTripper) Requester {
return GenericRequest(url, request, conversionType, headers, "PUT", customTransport)
}
func DeleteRequest(url string, request interface{}, conversionType interface{}, headers map[string]string, customTransport http.RoundTripper) Requester {
return GenericRequest(url, request, conversionType, headers, "DELETE", customTransport)
}
func GetRequest(url string, conversionType interface{}, headers map[string]string, customTransport http.RoundTripper) Requester {
r := func() (*http.Response, error) {
req, _ := http.NewRequest("GET", url, nil)
for key, value := range headers {
req.Header.Add(key, value)
}
client := getHttpClient(customTransport)
res, err := client.Do(req)
if err != nil {
return res, ErrRequestNotComplete
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&conversionType)
return res, err
}
return Requester{r, url, ""}
}
func GenericRequest(url string, request interface{}, conversionType interface{}, headers map[string]string, requestMethod string, customTransport http.RoundTripper) Requester {
bty, err := json.Marshal(request)
if err != nil {
panic(err)
}
r := func() (*http.Response, error) {
req, _ := http.NewRequest(requestMethod, url, bytes.NewBuffer(bty))
req.Header.Add("content-type", "application/json")
for key, value := range headers {
req.Header.Add(key, value)
}
client := getHttpClient(customTransport)
res, err := client.Do(req)
if err != nil {
return res, ErrRequestNotComplete
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&conversionType)
return res, err
}
return Requester{r, url, string(bty)}
}
func getHttpClient(customTransport http.RoundTripper) (client *http.Client) {
client = &http.Client{}
if customTransport != nil {
client.Transport = customTransport
} else {
client.Transport = http.DefaultTransport
}
return
}