-
Notifications
You must be signed in to change notification settings - Fork 206
/
templates.go
91 lines (86 loc) · 2.3 KB
/
templates.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
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
package documize
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"github.com/documize/community/core/api/entity"
)
// GetTemplates returns the available templates; from the stock templates if useStock is set.
func (c *Client) GetTemplates(useStock bool) (temps []entity.Template, err error) {
url := fmt.Sprintf("%s/api/templates",
c.BaseURL)
if useStock {
url += "/stock"
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
resp, errDo := c.Client.Do(req)
if errDo != nil {
return nil, errDo
}
defer resp.Body.Close() // ignore error
b, errRA := ioutil.ReadAll(resp.Body)
if errRA != nil {
return nil, errRA
}
if isError(string(b)) {
return nil, errors.New(trimErrors(string(b)))
}
err = json.Unmarshal(b, &temps)
if err != nil {
return nil, err
}
return temps, nil
}
// StartDocumentFromTemplate returns the documentID created in the given folderID from the given templateID,
// using either a stock template if isStock==true or a saved template.
func (c *Client) StartDocumentFromTemplate(isStock bool, templateID, folderID string) (DocumentID string, err error) {
url := fmt.Sprintf("%s/api/templates/%s/folder/%s?type=",
c.BaseURL, templateID, folderID)
if isStock {
url += "stock"
} else {
url += "saved"
}
req, err := http.NewRequest("POST", url, nil)
if err != nil {
return "", err
}
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
resp, err := c.Client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close() // ignore error
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
if isError(string(b)) {
return "", errors.New(trimErrors(string(b)))
}
var model entity.Document
err = json.Unmarshal(b, &model)
if err != nil {
return "", err
}
if model.RefID == "" {
return "", errors.New("empty DocumentID returned")
}
return model.RefID, nil
}