-
Notifications
You must be signed in to change notification settings - Fork 2
/
http.go
40 lines (32 loc) · 955 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
package updater
import (
"context"
"fmt"
"io"
"net/http"
)
// HTTPDownloader represents http downloader client
type HTTPDownloader struct {
client *http.Client
}
// NewHTTPDownloader creates new http downloader client instance.
// If the passed client is nil http.DefaultClient is used.
func NewHTTPDownloader(client *http.Client) *HTTPDownloader {
if client == nil {
client = http.DefaultClient
}
return &HTTPDownloader{client: client}
}
// Fetch downloads GH release
func (d *HTTPDownloader) Fetch(ctx context.Context, r Release) (io.ReadCloser, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, r.URL, nil)
if err != nil {
return nil, fmt.Errorf("could not create a request for the release download URL: %w", err)
}
req.Header.Add("Accept", "application/octet-stream")
resp, err := d.client.Do(req)
if err != nil {
return nil, fmt.Errorf("unable to download release: %w", err)
}
return resp.Body, nil
}