-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclient.go
62 lines (47 loc) · 1.12 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
package orchestrate
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
const (
rootUri = "https://api.orchestrate.io/v0/"
)
type Client struct {
HttpClient *http.Client
AuthToken string
}
type OrchestrateError struct {
Status string
Message string `json:"message"`
Locator string `json:"locator"`
}
func NewClient(authToken string) *Client {
httpClient := &http.Client{}
return &Client{
HttpClient: httpClient,
AuthToken: authToken,
}
}
func newError(resp *http.Response) error {
decoder := json.NewDecoder(resp.Body)
orchestrateError := new(OrchestrateError)
decoder.Decode(orchestrateError)
orchestrateError.Status = resp.Status
return orchestrateError
}
func (e *OrchestrateError) Error() string {
return fmt.Sprintf(`%v: %v`, e.Status, e.Message)
}
func (client Client) doRequest(method, trailingPath string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(method, rootUri+trailingPath, body)
if err != nil {
return nil, err
}
req.SetBasicAuth(client.AuthToken, "")
if method == "PUT" {
req.Header.Add("Content-Type", "application/json")
}
return client.HttpClient.Do(req)
}