-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathganboard.go
209 lines (170 loc) · 4.16 KB
/
ganboard.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
201
202
203
204
205
206
207
208
209
package ganboard
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"strconv"
)
// Client access to kanboard API methods
type Client struct {
Endpoint string
Username string
Password string
Project Project
}
// Request sends jsonrpc request to kanboard and returns *http.Response.
// FIXME set incremental id to track requests
// FIXME implement batch requests
func (c *Client) Request(request request) (*http.Response, error) {
// construct request
httpClient := &http.Client{}
request.JSONRPC = "2.0"
request.ID = 1
jsonrpc := new(bytes.Buffer)
json.NewEncoder(jsonrpc).Encode(request)
req, err := http.NewRequest(
"POST",
c.Endpoint,
jsonrpc,
)
if err != nil {
rsp := new(http.Response)
return rsp, err
}
// set auth and send to kanboard
req.SetBasicAuth(c.Username, c.Password)
//spew.Dump(req)
response, err := httpClient.Do(req)
if err != nil {
return response, err
}
if response.StatusCode != 200 {
return response, errors.New(response.Status)
}
return response, nil
}
type response struct {
JSONRPC string `json:"jsonrpc"`
ID FlexInt `json:"id"`
Result interface{} `json:"result"`
}
type request struct {
JSONRPC string `json:"jsonrpc"`
Method string `json:"method"`
ID int `json:"id,string"`
Params interface{} `json:"params,omitempty"`
Client *Client `json:"-"`
}
func (r *request) decodeInt() (int, error) {
rsp, err := r.Client.Request(*r)
if err != nil {
return 0, err
}
body := struct {
JSONRPC string `json:"jsonrpc"`
ID FlexInt `json:"id"`
Result FlexInt `json:"result"`
}{}
err = json.NewDecoder(rsp.Body).Decode(&body)
return int(body.Result), err
}
func (r *request) decodeInterface() (interface{}, error) {
rsp, err := r.Client.Request(*r)
if err != nil {
return nil, err
}
body := struct {
JSONRPC string `json:"jsonrpc"`
ID FlexInt `json:"id"`
Result interface{} `json:"result"`
}{}
err = json.NewDecoder(rsp.Body).Decode(&body)
return body.Result, err
}
func (r *request) decodeString() (string, error) {
rsp, err := r.Client.Request(*r)
if err != nil {
return "", err
}
body := struct {
JSONRPC string `json:"jsonrpc"`
ID FlexInt `json:"id"`
Result string `json:"result"`
}{}
err = json.NewDecoder(rsp.Body).Decode(&body)
return body.Result, err
}
func (r *request) decodeBoolean() (bool, error) {
rsp, err := r.Client.Request(*r)
if err != nil {
return false, err
}
body := struct {
JSONRPC string `json:"jsonrpc"`
ID FlexInt `json:"id"`
Result bool `json:"result"`
}{}
err = json.NewDecoder(rsp.Body).Decode(&body)
return body.Result, err
}
func (r *request) decodeFloat64() (float64, error) {
rsp, err := r.Client.Request(*r)
if err != nil {
return 0, err
}
body := struct {
JSONRPC string `json:"jsonrpc"`
ID FlexInt `json:"id"`
Result float64 `json:"result"`
}{}
err = json.NewDecoder(rsp.Body).Decode(&body)
return body.Result, err
}
func (r *request) decodeMapStringString() (map[string]string, error) {
rsp, err := r.Client.Request(*r)
if err != nil {
return nil, err
}
body := struct {
JSONRPC string `json:"jsonrpc"`
ID FlexInt `json:"id"`
Result map[string]string `json:"result"`
}{}
err = json.NewDecoder(rsp.Body).Decode(&body)
return body.Result, err
}
func (r *request) decodeMapIntString() (map[int]string, error) {
rsp, err := r.Client.Request(*r)
if err != nil {
return nil, err
}
body := struct {
JSONRPC string `json:"jsonrpc"`
ID FlexInt `json:"id"`
Result map[int]string `json:"result"`
}{}
err = json.NewDecoder(rsp.Body).Decode(&body)
return body.Result, err
}
// FlexInt unpredictable quoted int provided by JSON
type FlexInt int
// UnmarshalJSON decodes gracefully int from json even if it is surrounded by quotes
func (fi *FlexInt) UnmarshalJSON(b []byte) error {
if b[0] != '"' {
if string(b) != "false" {
return json.Unmarshal(b, (*int)(fi))
}
return errors.New("Waiting for Int, got false")
}
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
i, err := strconv.Atoi(s)
if err != nil {
return err
}
*fi = FlexInt(i)
return nil
}