oven-exporter/api/request.go

34 lines
600 B
Go
Raw Normal View History

2021-07-21 00:15:57 +02:00
package api
2021-07-20 12:18:00 +02:00
import (
"encoding/json"
2021-07-20 12:25:05 +02:00
"fmt"
2021-07-20 12:18:00 +02:00
"net/http"
"time"
)
2021-07-21 00:15:57 +02:00
// Request to API and unmarshal result
func (c *Client) Request(url string, value interface{}) error {
2021-07-20 12:18:00 +02:00
netClient := &http.Client{
Timeout: time.Second * 20,
}
req, err := http.NewRequest(http.MethodGet, c.URL+url, nil)
2021-07-20 12:18:00 +02:00
if err != nil {
return err
}
req.Header = map[string][]string{
"authorization": {fmt.Sprintf("Basic %s", c.Token)},
}
resp, err := netClient.Do(req)
if err != nil {
return err
}
err = json.NewDecoder(resp.Body).Decode(&value)
resp.Body.Close()
if err != nil {
return err
}
return nil
}