-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
36 lines (28 loc) · 894 Bytes
/
helpers.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
package main
import (
"encoding/json"
"fmt"
"net/http"
)
func doHTTP(url, key string, target interface{}) error {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return fmt.Errorf("failed to issue HTTP request: %w", err)
}
req.Header.Set("Authorization", "Bearer "+key)
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", "Incident.io Prometheus Exporter - https://github.com/dirsigler/incidentio-exporter")
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to run HTTP request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("received HTTP statuscode not 200: %v", resp.StatusCode)
}
if err = json.NewDecoder(resp.Body).Decode(&target); err != nil {
return fmt.Errorf("failed to unmarshal JSON: %w", err)
}
return nil
}