-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_document.go
More file actions
112 lines (94 loc) · 2.9 KB
/
api_document.go
File metadata and controls
112 lines (94 loc) · 2.9 KB
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
package squeeze_go_client
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"strconv"
)
type DocumentApi struct {
client *Client
}
func newDocumentApi(client *Client) *DocumentApi {
return &DocumentApi{client: client}
}
func (api *DocumentApi) ProcessDocument(batchClassId, documentClassId int, externalId string, fields map[string]string, file io.Reader, filename string) (*CreatedIntDto, *Error) {
body := bytes.NewBuffer([]byte{})
writer := multipart.NewWriter(body)
// request params
{
if err := writer.WriteField("batchClassId", strconv.Itoa(batchClassId)); err != nil {
return nil, newErr(fmt.Errorf("writing batchClassId failed: %s", err))
}
if documentClassId > 0 {
if err := writer.WriteField("documentClassId", strconv.Itoa(documentClassId)); err != nil {
return nil, newErr(fmt.Errorf("writing documentClassId failed: %s", err))
}
}
if externalId != "" {
if err := writer.WriteField("externalId", externalId); err != nil {
return nil, newErr(fmt.Errorf("writing externalId failed: %s", err))
}
}
if fields != nil {
fieldsJson, err := json.Marshal(fields)
if err != nil {
return nil, newErr(fmt.Errorf("marshaling fields failed: %s", err))
}
if err := writer.WriteField("fields", string(fieldsJson)); err != nil {
return nil, newErr(fmt.Errorf("writing fields failed: %s", err))
}
}
}
// file
{
formFile, err := writer.CreateFormFile("file", filename)
if err != nil {
return nil, newErr(fmt.Errorf("creating form file failed: %s", err))
}
_, err = io.Copy(formFile, file)
if err != nil {
return nil, newErr(fmt.Errorf("copying file failed: %s", err))
}
err = writer.Close()
if err != nil {
return nil, newErr(fmt.Errorf("closing writer failed: %s", err))
}
}
req, err := api.client.newRequest("POST", "/documents", body)
if err != nil {
return nil, newErr(err)
}
req.Header.Set("Content-Type", writer.FormDataContentType())
res, err := api.client.http.Do(req)
if err != nil {
return nil, newApiErr(err, res)
}
if res.StatusCode != 200 {
return nil, newApiErr(fmt.Errorf("unexpected status code: %d", res.StatusCode), res)
}
var data CreatedIntDto
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, newApiErr(fmt.Errorf("unmarshaling json failed: %s", err), res)
}
return &data, nil
}
func (api *DocumentApi) GetDocumentById(id int) (*Document, *Error) {
req, err := api.client.newRequest("GET", fmt.Sprintf("/documents/%d", id), nil)
if err != nil {
return nil, newErr(err)
}
res, err := api.client.http.Do(req)
if err != nil {
return nil, newApiErr(err, res)
}
if res.StatusCode != 200 {
return nil, newApiErr(fmt.Errorf("unexpected status code: %d", res.StatusCode), res)
}
var data Document
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, newApiErr(fmt.Errorf("unmarshaling json failed: %s", err), res)
}
return &data, nil
}