-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapi.go
More file actions
61 lines (55 loc) · 1.28 KB
/
api.go
File metadata and controls
61 lines (55 loc) · 1.28 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package forecast
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/cookiejar"
)
// API provides access to the forecastapp.com API
type API struct {
URL string
AccountID string
token string `ignored:"true"`
client *http.Client `ignored:"true"`
}
// New returns a API that is authenticated with Forecast
func New(url string, accountID string, accessToken string) *API {
return &API{
URL: url,
AccountID: accountID,
token: accessToken,
}
}
func get[T any](api *API, path string) (T, error) {
var result T
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/%s", api.URL, path), nil)
if err != nil {
return result, err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", api.token))
req.Header.Set("Forecast-Account-ID", api.AccountID)
if api.client == nil {
jar, err := cookiejar.New(nil)
if err != nil {
return result, err
}
api.client = &http.Client{
Jar: jar,
}
}
r, err := api.client.Do(req)
if err != nil {
return result, err
}
defer r.Body.Close()
if r.StatusCode >= http.StatusBadRequest {
body, err := io.ReadAll(r.Body)
if err != nil {
return result, err
}
return result, fmt.Errorf("%s: %s", r.Status, string(body))
}
err = json.NewDecoder(r.Body).Decode(&result)
return result, err
}