-
Notifications
You must be signed in to change notification settings - Fork 23
/
client.go
89 lines (77 loc) · 2.1 KB
/
client.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package enterpriseclient
import (
"bytes"
"crypto/ecdsa"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/pkg/errors"
)
const apiOrigin = "https://api.replicated.com/enterprise"
// An HTTPClient communicates with the Replicated Enterprise HTTP API.
type HTTPClient struct {
privateKey *ecdsa.PrivateKey
apiOrigin string
}
// New returns a new HTTP client.
func New(privateKeyContents []byte) *HTTPClient {
return NewHTTPClient(apiOrigin, privateKeyContents)
}
func NewHTTPClient(origin string, privateKeyContents []byte) *HTTPClient {
c := &HTTPClient{
apiOrigin: origin,
}
if privateKeyContents != nil {
privateKey, err := decodePrivateKeyPEM(privateKeyContents)
if err != nil {
privateKey = nil
}
c.privateKey = privateKey
}
return c
}
func (c *HTTPClient) doJSON(method, path string, successStatus int, reqBody interface{}, respBody interface{}) error {
endpoint := fmt.Sprintf("%s%s", c.apiOrigin, path)
var bodyBytes []byte
if reqBody != nil {
var err error
bodyBytes, err = json.Marshal(reqBody)
if err != nil {
return err
}
}
req, err := http.NewRequest(method, endpoint, bytes.NewBuffer(bodyBytes))
if err != nil {
return err
}
if c.privateKey != nil {
sigWithNonce, sig, fingerprint, err := sigAndFingerprint(c.privateKey, bodyBytes)
if err != nil {
return err
}
req.Header.Set("Signature", sig)
req.Header.Set("Authorization", fingerprint)
req.Header.Set("SignatureNonce", sigWithNonce)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return errors.Wrap(err, "failed to do request")
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return errors.New("not found")
}
if resp.StatusCode != successStatus {
body, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("%s %s %d: %s", method, endpoint, resp.StatusCode, body)
}
if respBody != nil {
if err := json.NewDecoder(resp.Body).Decode(respBody); err != nil {
return fmt.Errorf("%s %s response decoding: %w", method, endpoint, err)
}
}
return nil
}