/// <summary>
/// Add subject and blurb information for an envelope one is sending
/// </summary>
/// <param name="subject">Email subject</param>
/// <param name="blurb">Email body</param>
/// <returns>true if successful, false otherwise</returns>
public bool AddEmailInformation(string subject, string blurb)
{
// for now just support adding all email details. We can expand if needed.
if (String.IsNullOrEmpty(subject) || String.IsNullOrEmpty(blurb))
{
return false;
}
try
{
RequestInfo req = new RequestInfo();
req.RequestContentType = "application/json";
req.AcceptContentType = "application/json";
req.BaseUrl = Login.BaseUrl;
req.LoginEmail = Login.Email;
req.LoginPassword = Login.Password;
req.ApiPassword = Login.ApiPassword;
req.Uri = String.Format("/envelopes/{0}", EnvelopeId);
req.HttpMethod = "PUT";
req.IntegratorKey = RestSettings.Instance.IntegratorKey;
RequestBuilder builder = new RequestBuilder();
builder.Proxy = Proxy;
builder.Request = req;
List<RequestBody> requestBodies = new List<RequestBody>();
RequestBody rb = new RequestBody();
var emailDetails = new Dictionary<string, string>(){
{"emailBlurb", blurb},
{"emailSubject", subject}
};
rb.Text = JsonConvert.SerializeObject(emailDetails);
requestBodies.Add(rb);
req.RequestBody = requestBodies.ToArray();
builder.Request = req;
ResponseInfo response = builder.MakeRESTRequest();
this.Trace(builder, response);
if (response.StatusCode != HttpStatusCode.OK)
{
this.ParseErrorResponse(response);
}
return response.StatusCode == HttpStatusCode.OK;
}
catch (Exception ex)
{
if (ex is WebException || ex is NotSupportedException || ex is InvalidOperationException || ex is ProtocolViolationException)
{
// Once we get the debugging logger integrated into this project, we should write a log entry here
return false;
}
throw;
}
}
/// <summary>
/// Update recipients in the envelope
/// </summary>
/// <param name="recipients"></param>
/// <param name="resendEnvelope">True or false setting that defaults to false.
/// Setting this to true will resend the envelope to the recipient.
/// The resend_envelope flag is only used to resend an In Process envelope.</param>
/// <returns>true if successful, false otherwise</returns>
public bool UpdateRecipients(Recipients recipients, bool resendEnvelope = false)
{
try
{
RequestInfo req = new RequestInfo();
req.RequestContentType = "application/json";
req.AcceptContentType = "application/json";
req.BaseUrl = Login.BaseUrl;
req.LoginEmail = Login.Email;
req.LoginPassword = Login.Password;
req.ApiPassword = Login.ApiPassword;
req.Uri = String.Format(resendEnvelope ? "/envelopes/{0}/recipients?resend_envelope=true" : "/envelopes/{0}/recipients", EnvelopeId);
req.HttpMethod = "PUT";
req.IntegratorKey = RestSettings.Instance.IntegratorKey;
RequestBuilder builder = new RequestBuilder();
builder.Proxy = Proxy;
builder.Request = req;
List<RequestBody> requestBodies = new List<RequestBody>();
RequestBody rb = new RequestBody();
rb.Text = JsonConvert.SerializeObject(recipients);
requestBodies.Add(rb);
req.RequestBody = requestBodies.ToArray();
builder.Request = req;
ResponseInfo response = builder.MakeRESTRequest();
this.Trace(builder, response);
if (response.StatusCode != HttpStatusCode.OK)
{
this.ParseErrorResponse(response);
}
return response.StatusCode == HttpStatusCode.OK;
}
catch (Exception ex)
{
if (ex is WebException || ex is NotSupportedException || ex is InvalidOperationException || ex is ProtocolViolationException)
{
// Once we get the debugging logger integrated into this project, we should write a log entry here
return false;
}
throw;
}
}
/// <summary>
/// Get the list of Templates Matched with the envelope
/// </summary>
/// <exception cref="ArgumentNullException">If we find a null or empty envelopeId</exception>
/// <returns>object with information about the envelope's documents</returns>
public EnvelopeTemplates GetEnvelopeMatchingTemplates()
{
try
{
RequestBuilder builder = new RequestBuilder();
RequestInfo req = new RequestInfo();
List<RequestBody> requestBodies = new List<RequestBody>();
req.RequestContentType = "application/json";
req.AcceptContentType = "application/json";
req.HttpMethod = "GET";
req.LoginEmail = this.Login.Email;
req.ApiPassword = this.Login.ApiPassword;
req.DistributorCode = RestSettings.Instance.DistributorCode;
req.DistributorPassword = RestSettings.Instance.DistributorPassword;
req.IntegratorKey = RestSettings.Instance.IntegratorKey;
req.Uri = string.Format("{0}/envelopes/{1}/templates?include=matching%2Capplied", this.Login.BaseUrl, this.EnvelopeId);
builder.Request = req;
builder.Proxy = this.Proxy;
ResponseInfo response = builder.MakeRESTRequest();
this.Trace(builder, response);
if (response.StatusCode != HttpStatusCode.OK)
{
this.ParseErrorResponse(response);
return null;
}
else
{
return EnvelopeTemplates.FromJson(response.ResponseText);
}
}
catch (Exception ex)
{
if (ex is WebException || ex is NotSupportedException || ex is InvalidOperationException || ex is ProtocolViolationException)
{
// Once we get the debugging logger integrated into this project, we should write a log entry here
return null;
}
throw;
}
}
/// <summary>
/// updates the envelope's status to the one provided
/// </summary>
/// <param name="voidedReason">voided reason required when status is being updated to voided</param>
/// <returns>true if successful, false otherwise</returns>
public bool UpdateStatus(string voidedReason = null)
{
try
{
RequestBuilder builder = new RequestBuilder();
RequestInfo req = new RequestInfo();
List<RequestBody> requestBodies = new List<RequestBody>();
req.RequestContentType = "application/json";
req.AcceptContentType = "application/json";
req.HttpMethod = "PUT";
req.LoginEmail = this.Login.Email;
req.ApiPassword = this.Login.ApiPassword;
req.DistributorCode = RestSettings.Instance.DistributorCode;
req.DistributorPassword = RestSettings.Instance.DistributorPassword;
req.IntegratorKey = RestSettings.Instance.IntegratorKey;
req.Uri = string.Format("{0}/envelopes/{1}", this.Login.BaseUrl, this.EnvelopeId);
RequestBody rb = new RequestBody();
StringBuilder sb = new StringBuilder();
sb.Append("{");
sb.AppendFormat("\"status\":\"{0}\"", this.Status);
if (this.Status == "voided") {
if (String.IsNullOrEmpty(voidedReason))
throw new ArgumentException("The voided reason is required to change status to voided.");
sb.AppendFormat(", \"voidedReason\":\"{0}\"", voidedReason);
}
sb.Append("}");
rb.Text = sb.ToString();
requestBodies.Add(rb);
req.RequestBody = requestBodies.ToArray();
builder.Request = req;
builder.Proxy = this.Proxy;
ResponseInfo response = builder.MakeRESTRequest();
this.Trace(builder, response);
if (response.StatusCode != HttpStatusCode.OK)
{
this.ParseErrorResponse(response);
return false;
}
return true;
}
catch (Exception ex)
{
if (ex is WebException || ex is NotSupportedException || ex is InvalidOperationException || ex is ProtocolViolationException)
{
// Once we get the debugging logger integrated into this project, we should write a log entry here
return false;
}
throw;
}
}
/// <summary>
/// Updates an envelope status
/// </summary>
/// <param name="envelopeId">The envelopeId for this envelope</param>
/// <returns>Date/Time when the status was set</returns>
public DateTime GetStatus(string envelopeId)
{
try
{
RequestBuilder builder = new RequestBuilder();
RequestInfo req = new RequestInfo();
List<RequestBody> requestBodies = new List<RequestBody>();
req.RequestContentType = "application/json";
req.AcceptContentType = "application/json";
req.HttpMethod = "GET";
req.LoginEmail = this.Login.Email;
req.ApiPassword = this.Login.ApiPassword;
req.DistributorCode = RestSettings.Instance.DistributorCode;
req.DistributorPassword = RestSettings.Instance.DistributorPassword;
req.IntegratorKey = RestSettings.Instance.IntegratorKey;
req.Uri = string.Format("{0}/envelopes/{1}", this.Login.BaseUrl, envelopeId);
builder.Request = req;
builder.Proxy = this.Proxy;
ResponseInfo response = builder.MakeRESTRequest();
this.Trace(builder, response);
if (response.StatusCode != HttpStatusCode.OK)
{
this.ParseErrorResponse(response);
return DateTime.MinValue;
}
JObject json = JObject.Parse(response.ResponseText);
this.Status = (string)json["status"];
this.EmailSubject = (string)json["emailSubject"];
this.EmailBlurb = (string)json["emailBlurb"];
this.Created = DateTime.Parse((string)json["createdDateTime"]);
return (DateTime)json["statusChangedDateTime"];
}
catch (Exception ex)
{
if (ex is WebException || ex is NotSupportedException || ex is InvalidOperationException || ex is ProtocolViolationException)
{
// Once we get the debugging logger integrated into this project, we should write a log entry here
return DateTime.MinValue;
}
throw;
}
}
/// <summary>
/// Returns a list of names of documents used to create this envelope
/// </summary>
/// <returns></returns>
public List<EnvelopeDocument> GetDocuments()
{
try
{
RequestBuilder builder = new RequestBuilder();
RequestInfo req = new RequestInfo();
List<RequestBody> requestBodies = new List<RequestBody>();
req.RequestContentType = "application/json";
req.AcceptContentType = "application/json";
req.HttpMethod = "GET";
req.LoginEmail = this.Login.Email;
req.ApiPassword = this.Login.ApiPassword;
req.DistributorCode = RestSettings.Instance.DistributorCode;
req.DistributorPassword = RestSettings.Instance.DistributorPassword;
req.IntegratorKey = RestSettings.Instance.IntegratorKey;
req.Uri = string.Format("{0}/envelopes/{1}/documents", this.Login.BaseUrl, EnvelopeId);
builder.Request = req;
builder.Proxy = this.Proxy;
ResponseInfo response = builder.MakeRESTRequest();
this.Trace(builder, response);
if (response.StatusCode != HttpStatusCode.OK)
{
this.ParseErrorResponse(response);
return null;
}
JObject json = JObject.Parse(response.ResponseText);
var docs = json["envelopeDocuments"];
var res = new List<EnvelopeDocument>();
if (docs != null)
foreach (var jsonDoc in docs)
if ((string)jsonDoc["type"] == "content")
res.Add(new EnvelopeDocument { documentId = (string)jsonDoc["documentId"], name = (string)jsonDoc["name"] });
return res;
}
catch (Exception ex)
{
if (ex is WebException || ex is NotSupportedException || ex is InvalidOperationException || ex is ProtocolViolationException)
{
// Once we get the debugging logger integrated into this project, we should write a log entry here
return null;
}
throw;
}
}
/// <summary>
/// Returns all recipients that have lowest routing value (meaning they need to sign first)
/// </summary>
/// <returns></returns>
public IEnumerable<JToken> GetFirstRecipients()
{
try
{
RequestBuilder builder = new RequestBuilder();
RequestInfo req = new RequestInfo();
List<RequestBody> requestBodies = new List<RequestBody>();
req.RequestContentType = "application/json";
req.AcceptContentType = "application/json";
req.HttpMethod = "GET";
req.LoginEmail = this.Login.Email;
req.ApiPassword = this.Login.ApiPassword;
req.DistributorCode = RestSettings.Instance.DistributorCode;
req.DistributorPassword = RestSettings.Instance.DistributorPassword;
req.IntegratorKey = RestSettings.Instance.IntegratorKey;
req.Uri = string.Format("{0}/envelopes/{1}/recipients", this.Login.BaseUrl, EnvelopeId);
builder.Request = req;
builder.Proxy = this.Proxy;
ResponseInfo response = builder.MakeRESTRequest();
this.Trace(builder, response);
if (response.StatusCode != HttpStatusCode.OK)
{
this.ParseErrorResponse(response);
return new List<JToken>();
}
JObject json = JObject.Parse(response.ResponseText);
var signers = json["signers"];
// return the first signer
if (signers.Count() > 0)
{
int minRoute = signers.Min(s => (int)s["routingOrder"]);
return signers.Where(s => (int)s["routingOrder"] == minRoute);
}
else
{
return signers;
}
}
catch (Exception ex)
{
if (ex is WebException || ex is NotSupportedException || ex is InvalidOperationException || ex is ProtocolViolationException)
{
// Once we get the debugging logger integrated into this project, we should write a log entry here
return null;
}
throw;
}
}
请发表评论