-
Notifications
You must be signed in to change notification settings - Fork 0
/
appstore_list.go
39 lines (31 loc) · 953 Bytes
/
appstore_list.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
package appstore
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
func doList[T any](c *Client, ctx context.Context, url string) ([]Resource[T], error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
token, err := c.tokenSource.Token()
if err != nil {
return nil, fmt.Errorf("failed to get token: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code %d", resp.StatusCode)
}
rp := new(response[[]Resource[T]])
if err := json.NewDecoder(resp.Body).Decode(rp); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return rp.Data, nil
}