forked from jheth/hellosign-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhellosign_docsignature_template.go
429 lines (383 loc) · 11.4 KB
/
hellosign_docsignature_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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package hellosign
import (
"bytes"
"encoding/json"
"fmt"
"github.com/DeputyApp/hellosign-go-sdk/model"
"io"
"mime/multipart"
"net/http"
"os"
"reflect"
"strconv"
)
const (
ClientIDKey string = "client_id"
TestModeKey string = "test_mode"
TitleKey string = "title"
SubjectKey string = "subject"
MessageKey string = "message"
ShowPreviewKey string = "show_preview"
PreviewOnlyKey string = "preview_only"
MetadataKey string = "metadata"
SignerRolesKey string = "signer_roles"
FileURLKey string = "file_url"
)
// CreateEmbeddedTemplate creates a new embedded Template
func (m *Client) CreateEmbeddedTemplate(req model.CreateEmbeddedTemplateRequest) (*model.EmbeddedTemplate, error) {
params, writer, err := m.marshalMultipartCreateEmbeddedTemplateRequest(req)
if err != nil {
return nil, err
}
response, err := m.post("template/create_embedded_draft", params, *writer)
if err != nil {
return nil, err
}
defer response.Body.Close()
resp := &model.CreateEmbeddedTemplateResponse{}
err = json.NewDecoder(response.Body).Decode(resp)
return resp.GetTemplate(), err
}
// ListTemplates retrieves a list that are accessible by your account
func (m *Client) ListTemplates() (*model.ListTemplatesResponse, error) {
path := fmt.Sprintf("template/list")
response, err := m.get(path)
if err != nil {
return nil, err
}
defer response.Body.Close()
listResponse := &model.ListTemplatesResponse{}
err = json.NewDecoder(response.Body).Decode(listResponse)
if err != nil {
return nil, err
}
return listResponse, err
}
// DeleteTemplate completely deletes the template specified from the account and is irreversible
func (m *Client) DeleteTemplate(templateID string) (*http.Response, error) {
path := fmt.Sprintf("template/delete/%s", templateID)
response, err := m.nakedPost(path)
if err != nil {
return nil, err
}
return response, err
}
// GetEmbeddedTemplateEditURL - Retrieves an embedded template object to edit.
func (m *Client) GetEmbeddedTemplateEditURL(templateID, customFields string, enableEdit, testMode bool) (*model.EmbeddedTemplateEditURL, error) {
if templateID == "" {
return nil, fmt.Errorf("invalid argument: %s", templateID)
}
req := model.EditEmbeddedTemplateRequest{}
req.ShowPreview = enableEdit
req.CustomFields = customFields
req.TestMode = testMode
params, writer, err := m.marshalMultipartEditEmbeddedTemplateRequest(req)
if err != nil {
return nil, err
}
path := fmt.Sprintf("embedded/edit_url/%s", templateID)
response, err := m.post(path, params, *writer)
if err != nil {
return nil, err
}
data := &model.EmbeddedTemplateResponse{}
err = json.NewDecoder(response.Body).Decode(data)
if err != nil {
return nil, err
}
return data.GetEmbedded(), nil
}
// GetEmbeddedTemplateEditURLForPreview - Retrieves an embedded template object for preview.
// This method uses model.CreateEmbeddedTemplateRequest under the hood which probably needs to be
// renamed so that it can be reused across different methods.
func (m *Client) GetEmbeddedTemplateEditURLForPreview(templateID string, testMode bool) (*model.EmbeddedTemplateEditURL, error) {
if templateID == "" {
return nil, fmt.Errorf("invalid argument: %s", templateID)
}
req := model.EditEmbeddedTemplateRequest{}
req.PreviewOnly = true
req.TestMode = testMode
params, writer, err := m.marshalMultipartEditEmbeddedTemplateRequest(req)
if err != nil {
return nil, err
}
path := fmt.Sprintf("embedded/edit_url/%s", templateID)
response, err := m.post(path, params, *writer)
if err != nil {
return nil, err
}
data := &model.EmbeddedTemplateResponse{}
err = json.NewDecoder(response.Body).Decode(data)
if err != nil {
return nil, err
}
return data.GetEmbedded(), nil
}
func (m *Client) GetTemplate(templateID string) (*model.Template, error) {
if templateID == "" {
return nil, fmt.Errorf("invalid argument: %s", templateID)
}
path := fmt.Sprintf("template/%s", templateID)
response, err := m.get(path)
if err != nil {
return nil, err
}
if response.StatusCode == 404 {
return nil, fmt.Errorf("template not found")
} else if response.StatusCode != 200 {
return nil, fmt.Errorf("error occurred when retrieving template details")
}
data := &model.GetTemplateResponse{}
err = json.NewDecoder(response.Body).Decode(data)
if err != nil {
return nil, err
}
return data.GetTemplate(), nil
}
func (m *Client) marshalMultipartCreateEmbeddedTemplateRequest(embRequest model.CreateEmbeddedTemplateRequest) (*bytes.Buffer, *multipart.Writer, error) {
var b bytes.Buffer
w := multipart.NewWriter(&b)
structType := reflect.TypeOf(embRequest)
val := reflect.ValueOf(embRequest)
for i := 0; i < val.NumField(); i++ {
valueField := val.Field(i)
f := valueField.Interface()
val := reflect.ValueOf(f)
field := structType.Field(i)
fieldTag := field.Tag.Get(FormFieldKey)
switch val.Kind() {
case reflect.Map:
if fieldTag == MetadataKey {
for k, v := range embRequest.GetMetadata() {
formField, err := w.CreateFormField(fmt.Sprintf("%s[%v]", MetadataKey, k))
if err != nil {
return nil, nil, err
}
formField.Write([]byte(v))
}
}
case reflect.Slice:
switch fieldTag {
case TestModeKey:
tm, err := w.CreateFormField(TestModeKey)
if err != nil {
return nil, nil, err
}
tm.Write([]byte(m.boolToIntString(embRequest.GetTestMode())))
case ClientIDKey:
c, err := w.CreateFormField(ClientIDKey)
if err != nil {
return nil, nil, err
}
if embRequest.GetClientID() != "" {
c.Write([]byte(embRequest.GetClientID()))
}
case SignerRolesKey:
for i, sr := range embRequest.GetSignerRoles() {
name, err := w.CreateFormField(fmt.Sprintf("%s[%v][name]", SignerRolesKey, i))
if err != nil {
return nil, nil, err
}
name.Write([]byte(sr.GetName()))
if sr.GetOrder() != 0 {
order, err := w.CreateFormField(fmt.Sprintf("%s[%v][order]", SignerRolesKey, i))
if err != nil {
return nil, nil, err
}
order.Write([]byte(strconv.Itoa(sr.GetOrder())))
}
}
case FileKey:
for i, path := range embRequest.GetFile() {
file, _ := os.Open(path)
formField, err := w.CreateFormFile(fmt.Sprintf("%s[%v]", FileKey, i), file.Name())
if err != nil {
return nil, nil, err
}
_, err = io.Copy(formField, file)
}
case FileURLKey:
for i, fileURL := range embRequest.GetFileURL() {
formField, err := w.CreateFormField(fmt.Sprintf("%s[%v]", FileURLKey, i))
if err != nil {
return nil, nil, err
}
formField.Write([]byte(fileURL))
}
case TitleKey:
f, err := w.CreateFormField(TitleKey)
if err != nil {
return nil, nil, err
}
if embRequest.GetTitle() != "" {
f.Write([]byte(embRequest.GetTitle()))
}
case SubjectKey:
f, err := w.CreateFormField(SubjectKey)
if err != nil {
return nil, nil, err
}
if embRequest.GetSubject() != "" {
f.Write([]byte(embRequest.GetSubject()))
}
case MessageKey:
f, err := w.CreateFormField(MessageKey)
if err != nil {
return nil, nil, err
}
if embRequest.GetMessage() != "" {
f.Write([]byte(embRequest.GetMessage()))
}
case ShowPreviewKey:
tm, err := w.CreateFormField(ShowPreviewKey)
if err != nil {
return nil, nil, err
}
tm.Write([]byte(m.boolToIntString(embRequest.IsShowingPreview())))
}
case reflect.Bool:
formField, err := w.CreateFormField(fieldTag)
if err != nil {
return nil, nil, err
}
formField.Write([]byte(m.boolToIntString(val.Bool())))
default:
if val.String() != "" {
formField, err := w.CreateFormField(fieldTag)
if err != nil {
return nil, nil, err
}
formField.Write([]byte(val.String()))
}
}
}
w.Close()
return &b, w, nil
}
// TODO Abhishek: merge this with the create and use common parsing code
func (m *Client) marshalMultipartEditEmbeddedTemplateRequest(embRequest model.EditEmbeddedTemplateRequest) (*bytes.Buffer, *multipart.Writer, error) {
var b bytes.Buffer
w := multipart.NewWriter(&b)
structType := reflect.TypeOf(embRequest)
val := reflect.ValueOf(embRequest)
for i := 0; i < val.NumField(); i++ {
valueField := val.Field(i)
f := valueField.Interface()
val := reflect.ValueOf(f)
field := structType.Field(i)
fieldTag := field.Tag.Get(FormFieldKey)
switch val.Kind() {
case reflect.Map:
if fieldTag == MetadataKey {
for k, v := range embRequest.GetMetadata() {
formField, err := w.CreateFormField(fmt.Sprintf("%s[%v]", MetadataKey, k))
if err != nil {
return nil, nil, err
}
formField.Write([]byte(v))
}
}
case reflect.Slice:
switch fieldTag {
case TestModeKey:
tm, err := w.CreateFormField(TestModeKey)
if err != nil {
return nil, nil, err
}
tm.Write([]byte(m.boolToIntString(embRequest.GetTestMode())))
case ClientIDKey:
c, err := w.CreateFormField(ClientIDKey)
if err != nil {
return nil, nil, err
}
if embRequest.GetClientID() != "" {
c.Write([]byte(embRequest.GetClientID()))
}
case SignerRolesKey:
for i, sr := range embRequest.GetSignerRoles() {
name, err := w.CreateFormField(fmt.Sprintf("%s[%v][name]", SignerRolesKey, i))
if err != nil {
return nil, nil, err
}
name.Write([]byte(sr.GetName()))
if sr.GetOrder() != 0 {
order, err := w.CreateFormField(fmt.Sprintf("%s[%v][order]", SignerRolesKey, i))
if err != nil {
return nil, nil, err
}
order.Write([]byte(strconv.Itoa(sr.GetOrder())))
}
}
case FileKey:
for i, path := range embRequest.GetFile() {
file, _ := os.Open(path)
formField, err := w.CreateFormFile(fmt.Sprintf("%s[%v]", FileKey, i), file.Name())
if err != nil {
return nil, nil, err
}
_, err = io.Copy(formField, file)
}
case FileURLKey:
for i, fileURL := range embRequest.GetFileURL() {
formField, err := w.CreateFormField(fmt.Sprintf("%s[%v]", FileURLKey, i))
if err != nil {
return nil, nil, err
}
formField.Write([]byte(fileURL))
}
case TitleKey:
f, err := w.CreateFormField(TitleKey)
if err != nil {
return nil, nil, err
}
if embRequest.GetTitle() != "" {
f.Write([]byte(embRequest.GetTitle()))
}
case SubjectKey:
f, err := w.CreateFormField(SubjectKey)
if err != nil {
return nil, nil, err
}
if embRequest.GetSubject() != "" {
f.Write([]byte(embRequest.GetSubject()))
}
case MessageKey:
f, err := w.CreateFormField(MessageKey)
if err != nil {
return nil, nil, err
}
if embRequest.GetMessage() != "" {
f.Write([]byte(embRequest.GetMessage()))
}
case ShowPreviewKey:
tm, err := w.CreateFormField(ShowPreviewKey)
if err != nil {
return nil, nil, err
}
tm.Write([]byte(m.boolToIntString(embRequest.IsShowingPreview())))
case PreviewOnlyKey:
tm, err := w.CreateFormField(PreviewOnlyKey)
if err != nil {
return nil, nil, err
}
tm.Write([]byte(m.boolToIntString(embRequest.IsPreviewOnly())))
}
case reflect.Bool:
formField, err := w.CreateFormField(fieldTag)
if err != nil {
return nil, nil, err
}
formField.Write([]byte(m.boolToIntString(val.Bool())))
default:
if val.String() != "" {
formField, err := w.CreateFormField(fieldTag)
if err != nil {
return nil, nil, err
}
formField.Write([]byte(val.String()))
}
}
}
w.Close()
return &b, w, nil
}