summary refs log tree commit diff stats
path: root/fetch/request.go
blob: a3c6f23bec0803260b1c248616ec02f9cc2428b4 (plain) (blame)
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
package fetch

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"time"
)

func getRes(api string) ([]byte, error) {
	var body []byte

	c := http.Client{
		// TODO: timeout should be configurable by the user
		Timeout: time.Second * 64,
	}

	req, err := http.NewRequest(http.MethodGet, api, nil)
	if err != nil {
		return body, err
	}

	req.Header.Set("User-Agent", "Andinus / Indus - framagit.org/andinus/indus")

	res, err := c.Do(req)
	if err != nil {
		return body, err
	}
	defer res.Body.Close()

	if res.StatusCode != 200 {
		return body, fmt.Errorf("Unexpected response status code received: %d %s",
			res.StatusCode, http.StatusText(res.StatusCode))
	}

	body, err = ioutil.ReadAll(res.Body)
	if err != nil {
		return body, err
	}
	return body, err
}