-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
57 lines (51 loc) · 1.2 KB
/
client.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
package httpclient
import (
"crypto/tls"
"io"
"net/http"
"time"
"github.com/gojek/heimdall/httpclient"
"github.com/valyala/fasthttp"
)
type ReqStruct struct {
Req fasthttp.Request
Method string
Url string
Body io.Reader
Timeout int
Headers http.Header
}
type ReqResponse struct {
Response *http.Response
Error error
}
func (req *ReqStruct) SetHeader(headers map[string]string) *ReqStruct {
headershttp := http.Header{}
for key, value := range headers {
// maybe will add function for anti spoof
headershttp.Set(key, value)
}
req.Headers = headershttp
return req
}
func (req *ReqStruct) DoRequests() *ReqResponse {
client := &http.Client{
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
}
httpClient := httpclient.NewClient(
httpclient.WithHTTPClient(client),
httpclient.WithHTTPTimeout(time.Duration(time.Duration(req.Timeout).Seconds())),
)
request, err := http.NewRequest(req.Method, req.Url, req.Body)
if err != nil {
return &ReqResponse{Error: err}
}
request.Header = req.Headers
response, err := httpClient.Do(request)
if err != nil {
return &ReqResponse{Error: err}
}
return &ReqResponse{
Response: response,
}
}