forked from rwestlund/quickbooks-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
attachable.go
357 lines (324 loc) · 9.19 KB
/
attachable.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package quickbooks
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/textproto"
"net/url"
"strconv"
)
type ContentType string
const (
AI ContentType = "application/postscript"
CSV ContentType = "text/csv"
DOC ContentType = "application/msword"
DOCX ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
EPS ContentType = "application/postscript"
GIF ContentType = "image/gif"
JPEG ContentType = "image/jpeg"
JPG ContentType = "image/jpg"
ODS ContentType = "application/vnd.oasis.opendocument.spreadsheet"
PDF ContentType = "application/pdf"
PNG ContentType = "image/png"
RTF ContentType = "text/rtf"
TIF ContentType = "image/tif"
TXT ContentType = "text/plain"
XLS ContentType = "application/vnd/ms-excel"
XLSX ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
XML ContentType = "text/xml"
)
type Attachable struct {
ID string `json:"Id,omitempty"`
SyncToken string `json:",omitempty"`
FileName string `json:",omitempty"`
Note string `json:",omitempty"`
Category string `json:",omitempty"`
ContentType ContentType `json:",omitempty"`
PlaceName string `json:",omitempty"`
AttachableRef []AttachableRef `json:",omitempty"`
Long string `json:",omitempty"`
Tag string `json:",omitempty"`
Lat string `json:",omitempty"`
MetaData MetaData `json:",omitempty"`
FileAccessUri string `json:",omitempty"`
Size json.Number `json:",omitempty"`
ThumbnailFileAccessUri string `json:",omitempty"`
TempDownloadUri string `json:",omitempty"`
ThumbnailTempDownloadUri string `json:",omitempty"`
}
type AttachableRef struct {
IncludeOnSend bool `json:",omitempty"`
LineInfo string `json:",omitempty"`
NoRefOnly bool `json:",omitempty"`
// CustomField[0..n]
Inactive bool `json:",omitempty"`
EntityRef ReferenceType `json:",omitempty"`
}
// CreateAttachable creates the attachable
func (c *Client) CreateAttachable(attachable *Attachable) (*Attachable, error) {
var u, err = url.Parse(string(c.Endpoint))
if err != nil {
return nil, err
}
u.Path = "/v3/company/" + c.RealmID + "/attachable"
var v = url.Values{}
v.Add("minorversion", minorVersion)
u.RawQuery = v.Encode()
var j []byte
j, err = json.Marshal(attachable)
if err != nil {
return nil, err
}
var req *http.Request
req, err = http.NewRequest("POST", u.String(), bytes.NewBuffer(j))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
var res *http.Response
res, err = c.Client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, parseFailure(res)
}
var r struct {
Attachable Attachable
Time Date
}
err = json.NewDecoder(res.Body).Decode(&r)
return &r.Attachable, err
}
// DeleteAttachable deletes the attachable
func (c *Client) DeleteAttachable(attachable *Attachable) error {
var u, err = url.Parse(string(c.Endpoint))
if err != nil {
return err
}
u.Path = "/v3/company/" + c.RealmID + "/attachable"
var v = url.Values{}
v.Add("minorversion", minorVersion)
v.Add("operation", "delete")
u.RawQuery = v.Encode()
var j []byte
j, err = json.Marshal(attachable)
if err != nil {
return err
}
var req *http.Request
req, err = http.NewRequest("POST", u.String(), bytes.NewBuffer(j))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
var res *http.Response
res, err = c.Client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return parseFailure(res)
}
return nil
}
// DownloadAttachable downloads the attachable
func (c *Client) DownloadAttachable(attachableId string) (string, error) {
var u, err = url.Parse(string(c.Endpoint))
if err != nil {
return "", err
}
u.Path = "/v3/company/" + c.RealmID + "/download/" + attachableId
var v = url.Values{}
v.Add("minorversion", minorVersion)
u.RawQuery = v.Encode()
var req *http.Request
req, err = http.NewRequest("GET", u.String(), nil)
if err != nil {
return "", err
}
var res *http.Response
res, err = c.Client.Do(req)
if err != nil {
return "", err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return "", parseFailure(res)
}
url, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
return string(url), err
}
// GetAttachables gets the attachables
func (c *Client) GetAttachables(startpos int) ([]Attachable, error) {
var r struct {
QueryResponse struct {
Attachable []Attachable
StartPosition int
MaxResults int
}
}
q := "SELECT * FROM Attachable ORDERBY Id STARTPOSITION " +
strconv.Itoa(startpos) + " MAXRESULTS " + strconv.Itoa(queryPageSize)
err := c.query(q, &r)
if err != nil {
return nil, err
}
if r.QueryResponse.Attachable == nil {
r.QueryResponse.Attachable = make([]Attachable, 0)
}
return r.QueryResponse.Attachable, nil
}
// GetAttachable gets the attachable
func (c *Client) GetAttachable(attachableId string) (*Attachable, error) {
var u, err = url.Parse(string(c.Endpoint))
if err != nil {
return nil, err
}
u.Path = "/v3/company/" + c.RealmID + "/attachable/" + attachableId
var v = url.Values{}
v.Add("minorversion", minorVersion)
u.RawQuery = v.Encode()
var req *http.Request
req, err = http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
var res *http.Response
res, err = c.Client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, parseFailure(res)
}
var r struct {
Attachable Attachable
Time Date
}
err = json.NewDecoder(res.Body).Decode(&r)
return &r.Attachable, err
}
// UpdateAttachable updates the attachable
func (c *Client) UpdateAttachable(attachable *Attachable) (*Attachable, error) {
var u, err = url.Parse(string(c.Endpoint))
if err != nil {
return nil, err
}
u.Path = "/v3/company/" + c.RealmID + "/attachable"
var v = url.Values{}
v.Add("minorversion", minorVersion)
u.RawQuery = v.Encode()
var d = struct {
*Attachable
Sparse bool `json:"sparse"`
}{
Attachable: attachable,
Sparse: true,
}
var j []byte
j, err = json.Marshal(d)
if err != nil {
return nil, err
}
var req *http.Request
req, err = http.NewRequest("POST", u.String(), bytes.NewBuffer(j))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
var res *http.Response
res, err = c.Client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, parseFailure(res)
}
var r struct {
Attachable Attachable
Time Date
}
err = json.NewDecoder(res.Body).Decode(&r)
return &r.Attachable, err
}
// UploadAttachable uploads the attachable
func (c *Client) UploadAttachable(attachable *Attachable, data io.Reader) (*Attachable, error) {
var u, err = url.Parse(string(c.Endpoint))
if err != nil {
return nil, err
}
u.Path = "/v3/company/" + c.RealmID + "/upload"
var v = url.Values{}
v.Add("minorversion", minorVersion)
u.RawQuery = v.Encode()
var buffer bytes.Buffer
mWriter := multipart.NewWriter(&buffer)
// Add file metadata
metadataHeader := make(textproto.MIMEHeader)
metadataHeader.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`, "file_metadata_01", "attachment.json"))
metadataHeader.Set("Content-Type", "application/json")
var metadataContent io.Writer
if metadataContent, err = mWriter.CreatePart(metadataHeader); err != nil {
return nil, err
}
var j []byte
j, err = json.Marshal(attachable)
if err != nil {
return nil, err
}
if _, err = metadataContent.Write(j); err != nil {
return nil, err
}
// Add file content
fileHeader := make(textproto.MIMEHeader)
fileHeader.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`, "file_content_01", attachable.FileName))
fileHeader.Set("Content-Type", string(attachable.ContentType))
var fileContent io.Writer
if fileContent, err = mWriter.CreatePart(fileHeader); err != nil {
return nil, err
}
if _, err = io.Copy(fileContent, data); err != nil {
return nil, err
}
mWriter.Close()
var req *http.Request
req, err = http.NewRequest("POST", u.String(), &buffer)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", mWriter.FormDataContentType())
req.Header.Add("Accept", "application/json")
var res *http.Response
res, err = c.Client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, parseFailure(res)
}
var r struct {
AttachableResponse []struct {
Attachable Attachable
}
Time Date
}
err = json.NewDecoder(res.Body).Decode(&r)
return &r.AttachableResponse[0].Attachable, err
}