summaryrefslogtreecommitdiff
path: root/internal/httpclient/client.go
blob: 5c57cdc2dddb3cdd1305a428a2acadc1d063ca55 (plain)
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
package httpclient

import (
	"context"
	"io"
	"net/http"
	"time"
)

const DefaultTimeout = 30 * time.Second

var defaultClient = &http.Client{
	Timeout: DefaultTimeout,
}

func Do(req *http.Request) (*http.Response, error) {
	return defaultClient.Do(req)
}

func NewRequest(method, url string, body io.Reader) (*http.Request, context.CancelFunc, error) {
	ctx, cancel := context.WithTimeout(context.Background(), DefaultTimeout)

	req, err := http.NewRequestWithContext(ctx, method, url, body)
	if err != nil {
		cancel()
		return nil, nil, err
	}

	return req, cancel, nil
}