-
Notifications
You must be signed in to change notification settings - Fork 0
/
units.go
47 lines (37 loc) · 949 Bytes
/
units.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
38
39
40
41
42
43
44
45
46
47
package api
import (
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/evertoncolling/poc-requests-go/pkg/dto"
)
func (u *Units) List() (dto.UnitList, error) {
endpoint := fmt.Sprintf("/api/v1/projects/%s/units", u.Client.ClientConfig.Project)
url := u.Client.BaseURL + endpoint
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return dto.UnitList{}, err
}
for key, value := range u.Client.Headers {
req.Header.Set(key, value)
}
httpClient := &http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
return dto.UnitList{}, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return dto.UnitList{}, fmt.Errorf("failed to fetch units: %s", resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return dto.UnitList{}, err
}
var unitList dto.UnitList
if err := json.Unmarshal(body, &unitList); err != nil {
return dto.UnitList{}, err
}
return unitList, nil
}