forked from exercism/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asset.go
37 lines (32 loc) · 825 Bytes
/
asset.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
package cli
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
)
// Asset is a build for a particular system, uploaded to a GitHub release.
type Asset struct {
ID int `json:"id"`
Name string `json:"name"`
ContentType string `json:"content_type"`
}
func (a *Asset) download() (*bytes.Reader, error) {
downloadURL := fmt.Sprintf("%s/assets/%d", ReleaseURL, a.ID)
req, err := http.NewRequest("GET", downloadURL, nil)
if err != nil {
return nil, err
}
// https://developer.github.com/v3/repos/releases/#get-a-single-release-asset
req.Header.Set("Accept", "application/octet-stream")
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
bs, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
return bytes.NewReader(bs), nil
}