-
Notifications
You must be signed in to change notification settings - Fork 2
/
list.go
40 lines (31 loc) · 858 Bytes
/
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
40
package kutt
import (
"encoding/json"
"fmt"
"net/http"
)
type listResponse struct {
URLs []*URL `json:"list"`
Count int `json:"countAll"`
}
func (cli *Client) List() ([]*URL, error) {
path := "/api/url/geturls"
reqURL := cli.BaseURL + path
req, err := http.NewRequest(http.MethodGet, reqURL, nil)
if err != nil {
return nil, fmt.Errorf("create HTTP request: %w", err)
}
resp, err := cli.do(req)
if err != nil {
return nil, fmt.Errorf("do HTTP request: %w", err)
}
defer resp.Body.Close()
if !(resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices) {
return nil, fmt.Errorf("HTTP response: %w", cli.error(resp.StatusCode, resp.Body))
}
var r listResponse
if err := json.NewDecoder(resp.Body).Decode(&r); err != nil {
return nil, fmt.Errorf("parse HTTP body: %w", err)
}
return r.URLs, nil
}