-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathApiHelper.cs
executable file
·139 lines (128 loc) · 5.25 KB
/
ApiHelper.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
namespace RightSignature
{
public class ApiHelper
{
public static void Initialize()
{
OAuthRightSignature _oauth = new OAuthRightSignature();
if (Configuration.AuthType == "oauthtoken")
_oauth._initialize();
}
public static XElement createRolesXML(List<Structs.Recipient> recipients)
{
// Create Roles XML
XElement rolesNode = new XElement("recipients");
foreach (Structs.Recipient recipient in recipients)
{
XElement roleNode = new XElement("recipient");
roleNode.Add(new XElement("role", recipient.role));
roleNode.Add(new XElement("name", recipient.name));
roleNode.Add(new XElement("email", recipient.email));
if (recipient.is_sender)
{
roleNode.Add(new XElement("is_sender", "true"));
}
rolesNode.Add(roleNode);
}
return rolesNode;
}
// Converts Dictionary into tags XML Element
public static XElement CreateTagsXML(Dictionary<string, string> tags)
{
XElement tagsNode = new XElement("tags");
foreach (KeyValuePair<string, string> tag in tags)
{
XElement tagNode = new XElement("tag");
tagNode.Add(new XElement("name", tag.Key));
if (tag.Value != null)
tagNode.Add(new XElement("value", tag.Value));
tagsNode.Add(tagNode);
}
return tagsNode;
}
// Converts a List and request path into one request path with paramters
public static string CreateRequestPath(string path, List<string> parameters)
{
for (int i = 0; i < parameters.Count; i++)
{
path += i == 0 ? "?" : "&";
path += parameters[i];
}
return path;
}
public static string CreateTagsParameter(Dictionary<string, string> tags)
{
string tagsParam = "tags=";
int i = 0;
foreach (KeyValuePair<string, string> tag in tags)
{
if (i > 0)
tagsParam += ',';
if (tag.Value == null)
tagsParam += tag.Key;
else
tagsParam += tag.Key + ":" + tag.Value;
i++;
}
return tagsParam;
}
public static XElement acceptableRoleNamesXML(List<string> roleNames)
{
XElement roleNamesXml = new XElement("acceptable_role_names");
foreach (string roleName in roleNames)
{
roleNamesXml.Add(new XElement("name", roleName));
}
return roleNamesXml;
}
public static XElement acceptableMergeFieldsXML(List<string> mergeFields)
{
XElement mergeFieldNamesXml = new XElement("acceptable_merge_field_names");
foreach (string mergeField in mergeFields)
{
mergeFieldNamesXml.Add(new XElement("name", mergeField));
}
return mergeFieldNamesXml;
}
//extract the guid from the document
//type = document or template
public static string getGuid(string document, string type)
{
XDocument xmlDocument = XDocument.Parse(document);
return xmlDocument.Element(type).Element("guid").Value;
}
public static string GetSignerLinks(string guid)
{
string urlPath = "/api/documents/" + guid + "/signer_links.xml";
OAuthRightSignature _oauth = new OAuthRightSignature();
string signerLinksXml = _oauth.APIWebRequest("GET", urlPath, null);
XDocument doc = XDocument.Parse(signerLinksXml);
XElement rootNode = new XElement("document");
XDocument returnXml = new XDocument(rootNode);
rootNode.Add(new XElement("guid", guid));
XElement signer_links = new XElement("signer-links");
foreach (XElement element in doc.Element("document").Element("signer-links").Elements("signer-link"))
{
Console.WriteLine("Name: {0}; Value: {1}",
(string)element.Attribute("name"),
(string)element.Element("role"));
XElement signer_link = new XElement("signer-link");
signer_link.Add(new XElement("name", (string)element.Element("name")));
signer_link.Add(new XElement("role", (string)element.Element("role")));
signer_link.Add(new XElement("link", Configuration.BaseUrl + "/signatures/embedded?rt=" + (string)element.Element("signer-token")));
signer_links.Add(signer_link);
}
rootNode.Add(signer_links);
return rootNode.ToString();
}
}
}