Skip to content

Commit 0c2f258

Browse files
Merge pull request #2 from docusign/pr1
SDK v3.1.3, DoWork methods, and create SDK obj for each example
2 parents 0c28003 + e002e4c commit 0c2f258

19 files changed

+1065
-478
lines changed

eg-03-csharp-auth-code-grant-core/Common/IRequestItemsService.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
namespace eg_03_csharp_auth_code_grant_core
55
{
66
public interface IRequestItemsService
7-
{
8-
ApiClient DefaultApiClient { get; }
9-
10-
Configuration DefaultConfiguration { get; }
7+
{
118
string EgName { get; set; }
129

1310
Session Session { get; set; }

eg-03-csharp-auth-code-grant-core/Common/RequestItemService.cs

+2-37
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ public class RequestItemsService : IRequestItemsService
1010
{
1111
private readonly IHttpContextAccessor _httpContextAccessor;
1212
private readonly IMemoryCache _cache;
13-
private readonly string _id;
14-
private string API_CLIENT_KEY = "{0}_ApiClient";
15-
private string CONFIG_KEY = "{0}_DocusignConfig";
16-
private string BASE_URI = "https://demo.docusign.net/restapi";
13+
private readonly string _id;
1714
private string _accessToken;
1815

1916
public RequestItemsService(IHttpContextAccessor httpContextAccessor, IMemoryCache cache)
@@ -30,38 +27,6 @@ public RequestItemsService(IHttpContextAccessor httpContextAccessor, IMemoryCach
3027
}
3128
}
3229

33-
public ApiClient DefaultApiClient
34-
{
35-
get
36-
{
37-
var key = string.Format(API_CLIENT_KEY, _id);
38-
ApiClient apiClient = _cache.Get<ApiClient>("apiClient");
39-
if (apiClient == null)
40-
{
41-
apiClient = new ApiClient(BASE_URI);
42-
_cache.Set(key, apiClient);
43-
}
44-
45-
return apiClient;
46-
}
47-
}
48-
49-
public Configuration DefaultConfiguration
50-
{
51-
get
52-
{
53-
var key = string.Format(CONFIG_KEY, _id);
54-
var docuSignConfig = _cache.Get<Configuration>(key);
55-
56-
if (docuSignConfig == null)
57-
{
58-
docuSignConfig = new Configuration(new ApiClient(BASE_URI));
59-
_cache.Set(key, docuSignConfig);
60-
}
61-
docuSignConfig.AddDefaultHeader("Authorization", "Bearer " + _accessToken);
62-
return docuSignConfig;
63-
}
64-
}
6530
private string GetKey(string key)
6631
{
6732
return string.Format("{0}_{1}", _id, key);
@@ -70,7 +35,7 @@ public string EgName {
7035
get => _cache.Get<string>(GetKey("EgName"));
7136
set => _cache.Set(GetKey("EgName"), value);
7237
}
73-
38+
7439
public Session Session {
7540
get => _cache.Get<Session>(GetKey("Session"));
7641
set => _cache.Set(GetKey("Session"), value);

eg-03-csharp-auth-code-grant-core/Controllers/Eg001EmbeddedSigningController.cs

+69-14
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
using System;
22
using System.Collections.Generic;
33
using DocuSign.eSign.Api;
4+
using DocuSign.eSign.Client;
45
using DocuSign.eSign.Model;
5-
using eg_03_csharp_auth_code_grant_core.Common;
66
using eg_03_csharp_auth_code_grant_core.Controllers;
77
using eg_03_csharp_auth_code_grant_core.Models;
8-
using Microsoft.AspNetCore.Http;
98
using Microsoft.AspNetCore.Mvc;
109

1110
namespace eg_03_csharp_auth_code_grant_core.Views
@@ -25,19 +24,28 @@ public Eg001EmbeddedSigningController(DSConfiguration config, IRequestItemsServi
2524
ViewBag.title = "Embedded Signing Ceremony";
2625
}
2726

28-
[HttpPost]
29-
public IActionResult Create(string signerEmail, string signerName)
27+
private string DoWork(string signerEmail, string signerName,
28+
string accessToken, string basePath, string accountId)
3029
{
31-
var session = RequestItemsService.Session;
32-
var user = RequestItemsService.User;
30+
// Data for this method
31+
// signerEmail
32+
// signerName
33+
// accessToken
34+
// basePath
35+
// accountId
36+
37+
// dsPingUrl -- class global
38+
// signerClientId -- class global
39+
// dsReturnUrl -- class global
40+
3341
// Step 1. Create the envelope definition
3442
EnvelopeDefinition envelope = MakeEnvelope(signerEmail, signerName);
3543

36-
// Step 2. Call DocuSign to create the envelope
37-
//EnvelopesApi envelopesApi = new EnvelopesApi((Configuration)HttpContext.Items["docuSignConfig"]);
38-
EnvelopesApi envelopesApi = new EnvelopesApi(RequestItemsService.DefaultConfiguration);
39-
EnvelopeSummary results = envelopesApi.CreateEnvelope(session.AccountId, envelope);
40-
44+
// Step 2. Call DocuSign to create the envelope
45+
var config = new Configuration(new ApiClient(basePath));
46+
config.AddDefaultHeader("Authorization", "Bearer " + accessToken);
47+
EnvelopesApi envelopesApi = new EnvelopesApi(config);
48+
EnvelopeSummary results = envelopesApi.CreateEnvelope(accountId, envelope);
4149
string envelopeId = results.EnvelopeId;
4250

4351
// Save for future use within the example launcher
@@ -46,17 +54,26 @@ public IActionResult Create(string signerEmail, string signerName)
4654
// Step 3. create the recipient view, the Signing Ceremony
4755
RecipientViewRequest viewRequest = MakeRecipientViewRequest(signerEmail, signerName);
4856
// call the CreateRecipientView API
49-
ViewUrl results1 = envelopesApi.CreateRecipientView(session.AccountId, envelopeId, viewRequest);
57+
ViewUrl results1 = envelopesApi.CreateRecipientView(accountId, envelopeId, viewRequest);
5058

5159
// Step 4. Redirect the user to the Signing Ceremony
5260
// Don't use an iFrame!
5361
// State can be stored/recovered using the framework's session or a
5462
// query parameter on the returnUrl (see the makeRecipientViewRequest method)
55-
return Redirect(results1.Url);
63+
string redirectUrl = results1.Url;
64+
return redirectUrl;
5665
}
5766

5867
private RecipientViewRequest MakeRecipientViewRequest(string signerEmail, string signerName)
5968
{
69+
// Data for this method
70+
// signerEmail
71+
// signerName
72+
// dsPingUrl -- class global
73+
// signerClientId -- class global
74+
// dsReturnUrl -- class global
75+
76+
6077
RecipientViewRequest viewRequest = new RecipientViewRequest();
6178
// Set the url where you want the recipient to go once they are done signing
6279
// should typically be a callback route somewhere in your app.
@@ -93,8 +110,15 @@ private RecipientViewRequest MakeRecipientViewRequest(string signerEmail, string
93110

94111
private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName)
95112
{
96-
byte[] buffer = System.IO.File.ReadAllBytes(Config.docPdf);
113+
// Data for this method
114+
// signerEmail
115+
// signerName
116+
// signerClientId -- class global
117+
// Config.docPdf
97118

119+
120+
byte[] buffer = System.IO.File.ReadAllBytes(Config.docPdf);
121+
98122
EnvelopeDefinition envelopeDefinition = new EnvelopeDefinition();
99123
envelopeDefinition.EmailSubject = "Please sign this document";
100124
Document doc1 = new Document();
@@ -153,5 +177,36 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName)
153177
}
154178

155179
public override string EgName => "eg001";
180+
181+
[HttpPost]
182+
public IActionResult Create(string signerEmail, string signerName)
183+
{
184+
// Data for this method
185+
// signerEmail
186+
// signerName
187+
// dsPingUrl -- class global
188+
// signerClientId -- class global
189+
// dsReturnUrl -- class global
190+
string accessToken = RequestItemsService.User.AccessToken;
191+
string basePath = RequestItemsService.Session.BasePath + "/restapi";
192+
string accountId = RequestItemsService.Session.AccountId;
193+
194+
// Check the token with minimal buffer time.
195+
bool tokenOk = CheckToken(3);
196+
if (!tokenOk)
197+
{
198+
// We could store the parameters of the requested operation
199+
// so it could be restarted automatically.
200+
// But since it should be rare to have a token issue here,
201+
// we'll make the user re-enter the form data after
202+
// authentication.
203+
RequestItemsService.EgName = EgName;
204+
return Redirect("/ds/mustAuthenticate");
205+
}
206+
207+
string redirectUrl = DoWork(signerEmail, signerName, accessToken, basePath, accountId);
208+
// Redirect the user to the Signing Ceremony
209+
return Redirect(redirectUrl);
210+
}
156211
}
157212
}

eg-03-csharp-auth-code-grant-core/Controllers/Eg002SigningViaEmailController.cs

+56-19
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
53
using DocuSign.eSign.Api;
64
using DocuSign.eSign.Model;
75
using eg_03_csharp_auth_code_grant_core.Models;
86
using Microsoft.AspNetCore.Mvc;
9-
using System.IO;
107
using System.Text;
8+
using DocuSign.eSign.Client;
119

1210
namespace eg_03_csharp_auth_code_grant_core.Controllers
1311
{
@@ -22,21 +20,38 @@ public Eg002SigningViaEmailController(DSConfiguration config, IRequestItemsServi
2220

2321
public override string EgName => "eg002";
2422

25-
[HttpPost]
26-
public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName)
27-
{
23+
public EnvelopeSummary DoWork(string signerEmail, string signerName, string ccEmail, string ccName)
24+
{
25+
// Data for this method
26+
// signerEmail
27+
// signerName
28+
// ccEmail
29+
// ccName
30+
var accessToken = RequestItemsService.User.AccessToken;
31+
var basePath = RequestItemsService.Session.BasePath + "/restapi";
32+
var accountId = RequestItemsService.Session.AccountId;
33+
2834
EnvelopeDefinition env = MakeEnvelope(signerEmail, signerName, ccEmail, ccName);
29-
EnvelopesApi envelopesApi = new EnvelopesApi(RequestItemsService.DefaultConfiguration);
30-
EnvelopeSummary results = envelopesApi.CreateEnvelope(RequestItemsService.Session.AccountId, env);
35+
var config = new Configuration(new ApiClient(basePath));
36+
config.AddDefaultHeader("Authorization", "Bearer " + accessToken);
37+
EnvelopesApi envelopesApi = new EnvelopesApi(config);
38+
EnvelopeSummary results = envelopesApi.CreateEnvelope(accountId, env);
3139
RequestItemsService.EnvelopeId = results.EnvelopeId;
32-
ViewBag.h1 = "Envelope sent";
33-
ViewBag.message = "The envelope has been created and sent!<br />Envelope ID " + results.EnvelopeId + ".";
34-
//return results;
35-
return View("example_done");
40+
return results;
3641
}
3742

3843
private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, string ccEmail, string ccName)
3944
{
45+
// Data for this method
46+
// signerEmail
47+
// signerName
48+
// ccEmail
49+
// ccName
50+
// Config.docDocx
51+
// Config.docPdf
52+
// RequestItemsService.Status -- the envelope status ('created' or 'sent')
53+
54+
4055
// document 1 (html) has tag **signature_1**
4156
// document 2 (docx) has tag /sn1/
4257
// document 3 (pdf) has tag /sn1/
@@ -53,6 +68,8 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s
5368
// create the envelope definition
5469
EnvelopeDefinition env = new EnvelopeDefinition();
5570
env.EmailSubject = "Please sign this document set";
71+
72+
// Create document objects, one per document
5673
Document doc1 = new Document();
5774
string b64 = Convert.ToBase64String(document1(signerEmail, signerName, ccEmail, ccName));
5875
doc1.DocumentBase64 = b64;
@@ -65,16 +82,13 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s
6582
FileExtension = "docx",
6683
DocumentId = "2"
6784
};
68-
6985
Document doc3 = new Document
7086
{
7187
DocumentBase64 = doc3PdfBytes,
7288
Name = "Lorem Ipsum", // can be different from actual file name
7389
FileExtension = "pdf",
7490
DocumentId = "3"
7591
};
76-
77-
7892
// The order in the docs array determines the order in the envelope
7993
env.Documents = new List<Document> { doc1, doc2, doc3};
8094

@@ -124,12 +138,10 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s
124138
AnchorXOffset = "20"
125139
};
126140

127-
128141
// Tabs are set per recipient / signer
129142
Tabs signer1Tabs = new Tabs {
130143
SignHereTabs = new List<SignHere> { signHere1, signHere2}
131144
};
132-
133145
signer1.Tabs = signer1Tabs;
134146

135147
// Add the recipients to the envelope object
@@ -138,9 +150,7 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s
138150
Signers = new List<Signer> { signer1 },
139151
CarbonCopies = new List<CarbonCopy> { cc1 }
140152
};
141-
142153
env.Recipients = recipients;
143-
144154
// Request that the envelope be sent by setting |status| to "sent".
145155
// To request that the envelope be created as a draft, set to "created"
146156
env.Status = RequestItemsService.Status;
@@ -150,6 +160,12 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s
150160

151161
private byte[] document1(string signerEmail, string signerName, string ccEmail, string ccName)
152162
{
163+
// Data for this method
164+
// signerEmail
165+
// signerName
166+
// ccEmail
167+
// ccName
168+
153169
return Encoding.UTF8.GetBytes(
154170
" <!DOCTYPE html>\n" +
155171
" <html>\n" +
@@ -174,5 +190,26 @@ private byte[] document1(string signerEmail, string signerName, string ccEmail,
174190
" </html>"
175191
);
176192
}
193+
194+
[HttpPost]
195+
public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName)
196+
{
197+
// Check the token with minimal buffer time.
198+
bool tokenOk = CheckToken(3);
199+
if (!tokenOk)
200+
{
201+
// We could store the parameters of the requested operation
202+
// so it could be restarted automatically.
203+
// But since it should be rare to have a token issue here,
204+
// we'll make the user re-enter the form data after
205+
// authentication.
206+
RequestItemsService.EgName = EgName;
207+
return Redirect("/ds/mustAuthenticate");
208+
}
209+
EnvelopeSummary results = DoWork(signerEmail, signerName, ccEmail, ccName);
210+
ViewBag.h1 = "Envelope sent";
211+
ViewBag.message = "The envelope has been created and sent!<br />Envelope ID " + results.EnvelopeId + ".";
212+
return View("example_done");
213+
}
177214
}
178215
}

0 commit comments

Comments
 (0)