-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathhttp.go
53 lines (43 loc) · 1009 Bytes
/
http.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
package shared
import (
"errors"
"net/http"
"strings"
"time"
"github.com/thomaspoignant/go-feature-flag/internal"
"golang.org/x/net/context"
)
// CallHTTPAPI is the default function to make an HTTP call in the retrievers.
func CallHTTPAPI(
ctx context.Context,
url string, method string,
body string,
timeout time.Duration,
header http.Header,
httpClient internal.HTTPClient) (*http.Response, error) {
if timeout <= 0 {
timeout = 10 * time.Second
}
if url == "" {
return nil, errors.New("URL is a mandatory parameter when using httpretriever.Retriever")
}
if method == "" {
method = http.MethodGet
}
if ctx == nil {
ctx = context.Background()
}
req, err := http.NewRequestWithContext(ctx, method, url, strings.NewReader(body))
if err != nil {
return nil, err
}
// Add header if some are passed
if len(header) > 0 {
req.Header = header
}
if httpClient == nil {
httpClient = internal.HTTPClientWithTimeout(timeout)
}
// API call
return httpClient.Do(req)
}