/
pool.go
66 lines (55 loc) · 1.33 KB
/
pool.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/olxbr/network-api/pkg/types"
)
func (c *Client) ListPools(ctx context.Context) (*types.PoolListResponse, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseUrl("api/v1/pools"), nil)
if err != nil {
return nil, err
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
d := json.NewDecoder(resp.Body)
p := &types.PoolListResponse{}
if err := d.Decode(p); err != nil {
return nil, err
}
return p, nil
}
func (c *Client) CreatePool(ctx context.Context, r *types.PoolRequest) (*types.Pool, error) {
buf := &bytes.Buffer{}
e := json.NewEncoder(buf)
if err := e.Encode(r); err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseUrl("api/v1/pools"), buf)
if err != nil {
return nil, err
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
d := json.NewDecoder(resp.Body)
if resp.StatusCode != http.StatusCreated {
e := &types.ErrorResponse{}
if err := d.Decode(e); err != nil {
return nil, err
}
return nil, fmt.Errorf("request failed %d: %+v", resp.StatusCode, e)
}
p := &types.Pool{}
if err := d.Decode(p); err != nil {
return nil, err
}
return p, nil
}