-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.go
More file actions
151 lines (127 loc) · 3.36 KB
/
client.go
File metadata and controls
151 lines (127 loc) · 3.36 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package api
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"mime"
"net/http"
"github.com/RackHD/ipam/controllers/helpers"
"github.com/RackHD/ipam/interfaces"
"github.com/RackHD/ipam/resources/factory"
"github.com/hashicorp/go-cleanhttp"
)
// Client struct is used to configure the creation of a client
type Client struct {
Address string
Scheme string
}
// NewClient returns a new client
func NewClient(address string) *Client {
// bootstrap the config
c := &Client{
Address: address,
Scheme: "http",
}
return c
}
//Leases returns a handle to the Leases routes
func (c *Client) Leases() *Leases {
return &Leases{c}
}
//Reservations returns a handle to the Reservations routes
func (c *Client) Reservations() *Reservations {
return &Reservations{c}
}
//Subnets returns a handle to the Subnets routes
func (c *Client) Subnets() *Subnets {
return &Subnets{c}
}
//Pools returns a handle to the Pools routes
func (c *Client) Pools() *Pools {
return &Pools{c}
}
// SendResource is used to send a generic resource type
func (c *Client) SendResource(method, path string, in interfaces.Resource) (string, error) {
body, err := encodeBody(in)
if err != nil {
return "", err
}
req, err := http.NewRequest(method, c.Scheme+"://"+c.Address+path, body)
if err != nil {
return "", err
}
req.Header.Set(
"Content-Type",
mime.FormatMediaType(
fmt.Sprintf("%s+%s", in.Type(), "json"),
map[string]string{"version": in.Version()},
),
)
client := cleanhttp.DefaultClient()
resp, err := client.Do(req)
if err != nil {
return "", err
}
if resp.StatusCode < 200 || resp.StatusCode > 300 {
return "", errors.New(resp.Status)
}
return resp.Header.Get("Location"), nil
}
// ReceiveResource is used to receive the passed reasource type
func (c *Client) ReceiveResource(method, path, resourceType, resourceVersion string) (interfaces.Resource, error) {
req, err := http.NewRequest(method, c.Scheme+"://"+c.Address+path, nil)
req.Header.Set(
"Content-Type",
mime.FormatMediaType(
fmt.Sprintf("%s+%s", resourceType, "json"),
map[string]string{"version": resourceVersion},
),
)
client := cleanhttp.DefaultClient()
resp, err := client.Do(req)
if err != nil {
return nil, err
}
mediaType, err := helpers.NewMediaType(resp.Header.Get("Content-Type"))
if err != nil {
return nil, err
}
resource, err := factory.Require(mediaType.Type, mediaType.Version)
if err != nil {
return nil, err
}
err = decodeBody(resp, &resource)
if err != nil {
return nil, err
}
return resource, nil
}
// SendReceiveResource is used to send a resource type and then
// upon success, fetch and recieve that resource type
func (c *Client) SendReceiveResource(methodSend, methodReceive, path string, in interfaces.Resource) (interfaces.Resource, error) {
location, err := c.SendResource(methodSend, path, in)
if err != nil {
return nil, err
}
out, err := c.ReceiveResource(methodReceive, location, "", "")
return out, err
}
// decodeBody is used to JSON decode a body
func decodeBody(resp *http.Response, out interface{}) error {
dec := json.NewDecoder(resp.Body)
return dec.Decode(out)
}
// encodeBody is used to encode a request body
func encodeBody(obj interface{}) (io.Reader, error) {
if obj == nil {
return nil, nil
}
buf := bytes.NewBuffer(nil)
enc := json.NewEncoder(buf)
if err := enc.Encode(obj); err != nil {
return nil, err
}
return buf, nil
}