-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
181 lines (150 loc) · 4.79 KB
/
template.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
package rsw
import (
"encoding/xml"
"errors"
"net/url"
)
//Template represents Template entity.
//Notice: Tags implementation work only like a mock.
type Template struct {
Type string `xml:"type"`
GUID string `xml:"guid"`
CreatedAt string `xml:"created-at"`
Filename string `xml:"filename"`
Size int64 `xml:"size"`
ContentType string `xml:"content-type"`
PageCount string `xml:"page-count"`
Subject string `xml:"subject"`
Message string `xml:"message"`
Tags string `xml:"tags"`
ProcessingState string `xml:"processing-state"`
ThumbnailURL string `xml:"thumbnail-url"`
Roles []Role `xml:"roles>role"`
Pages []Page `xml:"pages>page"`
MergeFields []MergeField `xml:"merge-fields>merge-field"`
RedirectToken string `xml:"redirect-token"`
}
//PrefillTemplateReq represents arguments for Prefill Template API endpoint.
type PrefillTemplateReq struct {
XMLName xml.Name `xml:"template"`
GUID string `xml:"guid"`
Action string `xml:"action"`
Subject string `xml:"subject,omitempty"`
Roles []RoleReq `xml:"roles>role,omitempty"`
Description string `xml:"description,omitempty"`
ExpiresIn string `xml:"expires_in,omitempty"`
Tags []TagReq `xml:"tags>tag,omitempty"`
MergeFields []MergeFieldReq `xml:"merge_fields>merge_field,omitempty"`
CallbackLocation string `xml:"callback_location,omitempty"`
}
//PrepackageTemplateReq represents arguments for Prepackage Template API endpoint.
type PrepackageTemplateReq struct {
XMLName xml.Name `xml:"callback_location"`
CallbackLocation string `xml:",chardata"`
}
//PrefillAndSendTemplateResp represents Prefill Template (send) API endpoint response.
type PrefillAndSendTemplateResp struct {
Status string `xml:"status"`
GUID string `xml:"guid"`
}
//ListTemplatesResp represents List Templates API endpoint response.
type ListTemplatesResp struct {
Templates []Template `xml:"templates>template"`
TotalTemplates int64 `xml:"total-templates"`
TotalPages int64 `xml:"total-pages"`
PerPage int64 `xml:"per-page"`
CurrentPage int64 `xml:"current-page"`
}
//ListTemplates perform request to the same-name API endpoint.
//It has some optional arguments (see documentation).
//p - optional and can be missed.
func (a RightSignatureAPI) ListTemplates(p ...url.Values) (resp ListTemplatesResp, err error) {
params := url.Values{}
if len(p) > 0 {
params = p[0]
}
encodeParams := params.Encode()
if len(encodeParams) > 0 {
encodeParams = "?" + encodeParams
}
url := BaseURL + "/templates.xml" + encodeParams
method := MethodGet
body, err := a.Send(method, url, nil)
if err != nil {
return
}
err = xml.Unmarshal(body, &resp)
return
}
//PrepackageTemplate perform request to the same-name API endpoint.
//p - optional and can be missed.
func (a RightSignatureAPI) PrepackageTemplate(guid string, p ...PrepackageTemplateReq) (resp Template, err error) {
var data []byte
if len(p) > 0 {
data, err = xml.Marshal(p[0])
if err != nil {
return
}
}
url := BaseURL + "/templates/" + guid + "/prepackage.xml"
method := MethodPost
body, err := a.Send(method, url, data)
if err != nil {
return
}
err = xml.Unmarshal(body, &resp)
return
}
//PrefillTemplate perform 'prefill' action request to the Prefill Template API endpoint.
func (a RightSignatureAPI) PrefillTemplate(p PrefillTemplateReq) (resp Template, err error) {
err = a.prefillValidation(p)
if err != nil {
return
}
if p.Action != "prefill" {
err = errors.New("For this method action must be 'prefill'")
return
}
url := BaseURL + "/templates.xml"
method := MethodPost
xmlData, err := xml.Marshal(p)
if err != nil {
return
}
body, err := a.Send(method, url, xmlData)
if err != nil {
return
}
err = xml.Unmarshal(body, &resp)
return
}
//PrefillAndSendTemplate perform 'send' action request to the Prefill Template API endpoint.
func (a RightSignatureAPI) PrefillAndSendTemplate(p PrefillTemplateReq) (resp PrefillAndSendTemplateResp, err error) {
err = a.prefillValidation(p)
if err != nil {
return
}
if p.Action != "send" {
err = errors.New("For this method action must be 'send'")
return
}
url := BaseURL + "/templates.xml"
method := MethodPost
xmlData, err := xml.Marshal(p)
if err != nil {
return
}
body, err := a.Send(method, url, xmlData)
if err != nil {
return
}
err = xml.Unmarshal(body, &resp)
return
}
//prefillValidation represents validation for PrefillTemplateReq.
func (a RightSignatureAPI) prefillValidation(p PrefillTemplateReq) error {
if len(p.GUID) < 1 || len(p.Action) < 1 {
return errors.New("Requred arguments missed (check guid, action).")
}
return nil
}