-
Notifications
You must be signed in to change notification settings - Fork 32
/
client.go
200 lines (171 loc) · 4.38 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Package api provides the daemon API.
package api
import (
"bytes"
"context"
"encoding/json"
"errors"
"net"
"net/http"
"net/url"
"github.com/donovanhide/eventsource"
"github.com/satori/go.uuid"
"github.com/arigatomachine/cli/apitypes"
"github.com/arigatomachine/cli/config"
)
// Client exposes the daemon API.
type Client struct {
client *http.Client
Orgs *OrgsClient
Users *UsersClient
Profiles *ProfilesClient
Teams *TeamsClient
Memberships *MembershipsClient
Invites *InvitesClient
Keypairs *KeypairsClient
Session *SessionClient
Services *ServicesClient
Policies *PoliciesClient
Environments *EnvironmentsClient
Projects *ProjectsClient
Credentials *CredentialsClient
Worklog *WorklogClient
Version *VersionClient
}
// NewClient returns a new Client.
func NewClient(cfg *config.Config) *Client {
c := &Client{
client: &http.Client{
Transport: &http.Transport{
Dial: func(network, address string) (net.Conn, error) {
return net.Dial("unix", cfg.SocketPath)
},
},
},
}
c.Orgs = &OrgsClient{client: c}
c.Users = &UsersClient{client: c}
c.Profiles = &ProfilesClient{client: c}
c.Teams = &TeamsClient{client: c}
c.Memberships = &MembershipsClient{client: c}
c.Invites = &InvitesClient{client: c}
c.Keypairs = &KeypairsClient{client: c}
c.Session = &SessionClient{client: c}
c.Projects = &ProjectsClient{client: c}
c.Services = &ServicesClient{client: c}
c.Environments = &EnvironmentsClient{client: c}
c.Credentials = &CredentialsClient{client: c}
c.Policies = &PoliciesClient{client: c}
c.Worklog = &WorklogClient{client: c}
c.Version = &VersionClient{client: c}
return c
}
const version = "v1"
// NewRequest constructs a new http.Request, with a body containing the json
// representation of body, if provided.
func (c *Client) NewRequest(method, path string, query *url.Values, body interface{}, proxied bool) (*http.Request, string, error) {
requestID := uuid.NewV4().String()
b := &bytes.Buffer{}
if body != nil {
enc := json.NewEncoder(b)
err := enc.Encode(body)
if err != nil {
return nil, requestID, err
}
}
if query == nil {
query = &url.Values{}
}
version := "v1"
if proxied {
version = "proxy"
}
fullPath := "http://localhost/" + version + path
if q := query.Encode(); q != "" {
fullPath += "?" + q
}
req, err := http.NewRequest(method, fullPath, b)
if err != nil {
return nil, requestID, err
}
req.Header.Set("Host", "localhost")
req.Header.Set("X-Request-ID", requestID)
req.Header.Set("Content-type", "application/json")
return req, requestID, nil
}
// Do executes an http.Request, populating v with the JSON response
// on success.
//
// If the request errors with a JSON formatted response body, it will be
// unmarshaled into the returned error.
func (c *Client) Do(ctx context.Context, r *http.Request, v interface{}, reqID *string, progress *ProgressFunc) (*http.Response, error) {
done := make(chan bool)
if progress != nil {
version := "v1"
req, err := http.NewRequest("GET", "http://localhost/"+version+"/observe", nil)
if err != nil {
return nil, err
}
stream, err := eventsource.SubscribeWith("", c.client, req)
if err != nil {
return nil, err
}
output := *progress
go func() {
for {
select {
case <-done:
return
case ev := <-stream.Events:
data := ev.Data()
raw := []byte(data)
event := Event{}
event.MessageType = "message"
err = json.Unmarshal(raw, &event)
if err != nil {
output(nil, err)
return
}
if event.ID == *reqID {
output(&event, nil)
}
case err := <-stream.Errors:
output(nil, err)
}
}
}()
}
resp, err := c.client.Do(r)
close(done)
if err != nil {
return nil, err
}
defer resp.Body.Close()
err = checkResponseCode(resp)
if err != nil {
return resp, err
}
if v != nil {
dec := json.NewDecoder(resp.Body)
err = dec.Decode(v)
if err != nil {
return nil, err
}
}
return resp, nil
}
func checkResponseCode(r *http.Response) error {
if r.StatusCode >= 200 && r.StatusCode < 300 {
return nil
}
rErr := &apitypes.Error{StatusCode: r.StatusCode}
if r.ContentLength != 0 {
dec := json.NewDecoder(r.Body)
err := dec.Decode(rErr)
if err != nil {
return errors.New("Malformed error response from daemon.")
}
return rErr
}
return errors.New("Error from daemon. Check status code.")
}