Skip to content

Instantly share code, notes, and snippets.

@codingdawg
Last active May 7, 2017 00:02
Show Gist options
  • Save codingdawg/6039f7a919ffdfd40190d0e91041f400 to your computer and use it in GitHub Desktop.
Save codingdawg/6039f7a919ffdfd40190d0e91041f400 to your computer and use it in GitHub Desktop.
using DocuSign.eSign.Api;
using DocuSign.eSign.Client;
using DocuSign.eSign.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace CodingDawg.Sample
{
/*
Dependencies
Docusign C# SDK
https://github.com/docusign/docusign-csharp-client
DocuSign nuget Package
https://www.nuget.org/packages/DocuSign.eSign.dll/
*/
public class DocusignApiExamples
{
public void GetDocumentZip()
{
string envelopeId = "<Add your envelope Id here>";
string accountId = Init();
var envApi = new EnvelopesApi();
// GetDocument() API call returns a MemoryStream
var docStream = envApi.GetDocument(accountId, envelopeId, "archive");
// let's save the document to local file system
string zipName = Path.GetRandomFileName();
string zipfilePath = @"C:\temp\" + zipName + ".zip";
using (var fs = new FileStream(zipfilePath, FileMode.Create))
{
docStream.Seek(0, SeekOrigin.Begin);
docStream.CopyTo(fs);
}
string extractPath = @"c:\temp\" + zipName;
//Reference System.IO.Compression.FileSystem assembly for this
//http://stackoverflow.com/a/14635025/1219543
System.IO.Compression.ZipFile.ExtractToDirectory(zipfilePath, extractPath);
Console.WriteLine("Envelope Documents have been downloaded to: {0}", extractPath);
}
public void GetDocumentCombined()
{
string envelopeId = "<Add your envelope Id here>";
string accountId = Init();
var envApi = new EnvelopesApi();
var docStream = envApi.GetDocument(accountId, envelopeId, "combined");
string filePath = @"C:\temp\" + Path.GetRandomFileName() + ".pdf";
using (var stream = File.Create(filePath))
docStream.CopyTo(stream);
//using (var fs = new FileStream(filePath, FileMode.Create))
//{
// docStream.Seek(0, SeekOrigin.Begin);
// docStream.CopyTo(fs);
//}
}
public void GetDocumentRaw1()
{
string envelopeId = "<Add your envelope Id here>";
string accountId = "<Add your account Id here>";
string url = _baseUri + "/v2/accounts/" + accountId + "/envelopes/" + envelopeId + "/documents/combined";
using (var wc = new System.Net.WebClient())
{
wc.Headers.Add("X-DocuSign-Authentication", _authHeader);
wc.DownloadFile(url, @"C:\temp\" + Path.GetRandomFileName() + ".pdf");
}
}
public void GetDocumentRaw2()
{
string envelopeId = "<Add your envelope Id here>";
string accountId = "<Add your account Id here>";
string url = _baseUri + "/v2/accounts/" + accountId + "/envelopes/" + envelopeId + "/documents/combined";
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.Headers.Add("X-DocuSign-Authentication", _authHeader);
using (var response = (HttpWebResponse)request.GetResponse())
{
var fileName = @"C:\temp\" + Path.GetRandomFileName() + ".pdf";
using (var stream = File.Create(fileName))
response.GetResponseStream().CopyTo(stream);
}
}
string _username = "<Add your User Name>";
string _password = "<Add your Password>";
string _integratorKey = "<Add your Integrator Key>";
string _baseUri = "https://demo.docusign.net/restapi";
string _authHeader;
public DocusignApiExamples()
{
// configure 'X-DocuSign-Authentication' header
_authHeader = "{\"Username\":\"" + username + "\", \"Password\":\"" + password + "\", \"IntegratorKey\":\"" + integratorKey + "\"}";
}
public string Init()
{
// initialize client for desired environment (for production change to www)
ApiClient apiClient = new ApiClient(_baseUri);
Configuration.Default.ApiClient = apiClient;
Configuration.Default.AddDefaultHeader("X-DocuSign-Authentication", _authHeader);
// we will retrieve this from the login API call
string accountId = null;
/////////////////////////////////////////////////////////////////
// STEP 1: LOGIN API
/////////////////////////////////////////////////////////////////
// login call is available in the authentication api
AuthenticationApi authApi = new AuthenticationApi();
LoginInformation loginInfo = authApi.Login();
// parse the first account ID that is returned (user might belong to multiple accounts)
accountId = loginInfo.LoginAccounts[0].AccountId;
// Update ApiClient with the new base url from login call
apiClient = new ApiClient(loginInfo.LoginAccounts[0].BaseUrl);
return accountId;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment