This repository has been archived by the owner on Apr 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
98 lines (80 loc) · 2.5 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
90
91
92
93
94
95
96
97
98
package ghost
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)
type Client struct {
Username string
Password string
Endpoint string
}
type errorObject struct {
Code int `json:"code,omitempty"`
Message string `json:"message,omitempty"`
Errors []string `json:"errors,omitempty"`
}
var netClient = &http.Client{
Timeout: time.Second * 10,
}
func (c *Client) decodeJSON(resp *http.Response, payload interface{}) error {
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
return decoder.Decode(payload)
}
func (c *Client) getErrorFromResponse(resp *http.Response) (*errorObject, error) {
var result map[string]errorObject
if err := c.decodeJSON(resp, &result); err != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", err)
}
s, ok := result["error"]
if !ok {
return nil, fmt.Errorf("JSON response does not have error field")
}
return &s, nil
}
func (c *Client) checkResponse(resp *http.Response, err error) (*http.Response, error) {
if err != nil {
return resp, fmt.Errorf("Error calling the API endpoint: %v", err)
}
if 199 >= resp.StatusCode || 300 <= resp.StatusCode {
return resp, fmt.Errorf("Failed call API endpoint. HTTP response code: %v", resp.StatusCode)
}
return resp, nil
}
func (c *Client) do(method, path string, payload interface{}, headers map[string]string) (*http.Response, error) {
url := c.Endpoint + path
var body bytes.Buffer
if payload != nil {
data, err := json.Marshal(payload)
if err == nil {
body = *bytes.NewBuffer(data)
}
}
req, _ := http.NewRequest(method, url, &body)
for k, v := range headers {
req.Header.Set(k, v)
}
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(c.Username, c.Password)
resp, err := netClient.Do(req)
return c.checkResponse(resp, err)
}
func (c *Client) delete(path string, headers map[string]string) (*http.Response, error) {
return c.do("DELETE", path, nil, headers)
}
func (c *Client) patch(path string, payload interface{}, headers map[string]string) (res *http.Response, err error) {
return c.do("PATCH", path, payload, headers)
}
func (c *Client) post(path string, payload interface{}) (res *http.Response, err error) {
return c.do("POST", path, payload, nil)
}
func (c *Client) get(path string) (*http.Response, error) {
return c.do("GET", path, nil, nil)
}
// NewClient Return a Cloud Deploy client
func NewClient(endpoint string, username string, password string) *Client {
return &Client{Endpoint: endpoint, Username: username, Password: password}
}