-
Notifications
You must be signed in to change notification settings - Fork 3
/
TemplateApi.cs
executable file
·143 lines (130 loc) · 6.48 KB
/
TemplateApi.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace RightSignature
{
class TemplateApi : ITemplate
{
private OAuthRightSignature _oauth = new OAuthRightSignature();
// Get all templates created in your account
public string GetTemplates(string query = null, int? page = 1, int? perPage = 10, Dictionary<string, string> tags = null)
{
string urlPath = "/api/templates.xml";
string requestPath;
List<string> queryParams = new List<string>();
// Build up the URL request path parameters
if (query != null)
queryParams.Add("search=" + query);
if (page.HasValue)
queryParams.Add("page=" + page.ToString());
if (perPage.HasValue)
queryParams.Add("per_page=" + perPage.ToString());
// Creates parameter string for tags
if (tags != null)
queryParams.Add(ApiHelper.CreateTagsParameter(tags));
// Creates URL path with query parameters in it
requestPath = ApiHelper.CreateRequestPath(urlPath, queryParams);
return _oauth.APIWebRequest("GET", requestPath);
}
public string GetTemplateDetails(string guid)
{
return _oauth.APIWebRequest("GET", "/api/templates" + guid + ".xml");
}
//This allows you to build a template
//The output document contains a redirect token which should be used in the RightSignature API to complete building the template
public string BuildTemplate(Dictionary<string, string> tags = null, List<string> acceptableRoleNames = null, List<string> acceptableMergeFieldNames = null, string callback_location = null, string redirect_location = null)
{
XElement templateNode = new XElement("template");
if (acceptableRoleNames != null)
{
templateNode.Add(ApiHelper.acceptableRoleNamesXML(acceptableRoleNames));
}
if (acceptableMergeFieldNames != null)
{
templateNode.Add(ApiHelper.acceptableMergeFieldsXML(acceptableMergeFieldNames));
}
if (tags != null)
{
templateNode.Add(ApiHelper.CreateTagsXML(tags));
}
if (callback_location != null)
{
templateNode.Add(new XElement("callback_location", callback_location));
}
if (redirect_location != null)
{
templateNode.Add(new XElement("redirect_location", redirect_location));
}
return _oauth.APIWebRequest("POST", "/api/templates/generate_build_token.xml", templateNode.ToString());
}
//provide a single or comma seperated string of guids to prepackage the templates into a single document to send
public string PrepackageTemplate(string guidsString)
{
return(_oauth.APIWebRequest("POST", "/api/templates/" + guidsString + "/prepackage.xml", null));
}
public string GetPrepackagedGuid(string document)
{
return ApiHelper.getGuid(document, "template");
}
//Send/Prefill the prepackaged template as a document using the guid that was generated during the prepackage process.
public string PreFillORSendAsDocument(string guid, string subject, string action = "send", List<Structs.Recipient> roles = null, List<Structs.MergeField> mergeFields = null, Dictionary<string, string> tags = null, int? expires_in = null, string description = null, string callbackURL = null, bool embedded_signing = false)
{
string urlPath = "/api/templates.xml";
XElement rootNode = new XElement("template");
XDocument xml = new XDocument(rootNode);
// Creates the xml body to send to API
rootNode.Add(new XElement("guid", guid));
rootNode.Add(new XElement("subject", subject));
rootNode.Add(new XElement("action", action)); // Action can be 'send' or 'prefill'
if (description != null)
rootNode.Add(new XElement("description", description));
if (expires_in != null)
rootNode.Add(new XElement("expires_in", expires_in.ToString())); // Must be 2, 5, 15, or 30. Otherwise, API will default it to 30 days
// Create Roles XML
XElement rolesNode = new XElement("roles");
foreach (Structs.Recipient role in roles)
{
XElement roleNode = new XElement("role");
roleNode.SetAttributeValue("role_name", role.role);
roleNode.Add(new XElement("name", role.name));
roleNode.Add(new XElement("email", role.email));
roleNode.Add(new XElement("locked", role.locked.ToString().ToLower()));
rolesNode.Add(roleNode);
}
rootNode.Add(rolesNode);
// Create mergefields XML
if (mergeFields != null)
{
XElement mfsNode = new XElement("merge_fields");
foreach (Structs.MergeField mergeField in mergeFields)
{
XElement mfNode = new XElement("merge_field");
mfNode.SetAttributeValue("merge_field_name", mergeField.name);
mfNode.Add(new XElement("value", mergeField.value));
mfNode.Add(new XElement("locked", mergeField.locked.ToString().ToLower()));
mfsNode.Add(mfNode);
}
rootNode.Add(mfsNode);
}
if (tags != null)
rootNode.Add(ApiHelper.CreateTagsXML(tags));
if (callbackURL != null)
rootNode.Add(new XElement("callback_location", callbackURL));
string templateXml = _oauth.APIWebRequest("POST", urlPath, xml.ToString());
if (!embedded_signing && action == "redirect")
{
// Creates HTTP Request and parses it as XDocument
return templateXml;
}
else
{
string templateGuid = ApiHelper.getGuid(templateXml, "document");
return ApiHelper.GetSignerLinks(templateGuid);
}
}
}
}