forked from docusign/code-examples-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEg007EnvelopeGetDocController.cs
110 lines (99 loc) · 4.12 KB
/
Eg007EnvelopeGetDocController.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
using System.Collections.Generic;
using System.Linq;
using DocuSign.eSign.Api;
using DocuSign.eSign.Client;
using eg_03_csharp_auth_code_grant_core.Models;
using Microsoft.AspNetCore.Mvc;
namespace eg_03_csharp_auth_code_grant_core.Controllers
{
[Route("eg007")]
public class Eg007EnvelopeGetDocController : EgController
{
public Eg007EnvelopeGetDocController(DSConfiguration config, IRequestItemsService requestItemsService)
: base(config, requestItemsService)
{
ViewBag.title = "Download a document";
}
public override string EgName => "eg007";
// ***DS.snippet.0.start
private FileStreamResult DoWork(string accessToken, string basePath, string accountId,
string envelopeId, List<EnvelopeDocItem> documents, string docSelect)
{
// Data for this method
// accessToken
// basePath
// accountId
// envelopeId
// docSelect -- the requested documentId
// documents -- from eg 6
var config = new Configuration(new ApiClient(basePath));
config.AddDefaultHeader("Authorization", "Bearer " + accessToken);
EnvelopesApi envelopesApi = new EnvelopesApi(config);
// Step 1. EnvelopeDocuments::get.
// Exceptions will be caught by the calling function
System.IO.Stream results = envelopesApi.GetDocument(accountId,
envelopeId, docSelect);
// Step 2. Look up the document from the list of documents
EnvelopeDocItem docItem = documents.FirstOrDefault(d => docSelect.Equals(d.DocumentId));
// Process results. Determine the file name and mimetype
string docName = docItem.Name;
bool hasPDFsuffix = docName.ToUpper().EndsWith(".PDF");
bool pdfFile = hasPDFsuffix;
// Add .pdf if it's a content or summary doc and doesn't already end in .pdf
string docType = docItem.Type;
if (("content".Equals(docType) || "summary".Equals(docType)) && !hasPDFsuffix)
{
docName += ".pdf";
pdfFile = true;
}
// Add .zip as appropriate
if ("zip".Equals(docType))
{
docName += ".zip";
}
// Return the file information
// See https://stackoverflow.com/a/30625085/64904
string mimetype;
if (pdfFile)
{
mimetype = "application/pdf";
}
else if ("zip".Equals(docType))
{
mimetype = "application/zip";
}
else
{
mimetype = "application/octet-stream";
}
return File(results, mimetype, docName);
}
// ***DS.snippet.0.end
[HttpPost]
public ActionResult Create(string docSelect)
{
// Data for this method
// docSelect -- argument
var accessToken = RequestItemsService.User.AccessToken;
var basePath = RequestItemsService.Session.BasePath + "/restapi";
var accountId = RequestItemsService.Session.AccountId;
var envelopeId = RequestItemsService.EnvelopeId;
// documents data for the envelope. See example EG006
List<EnvelopeDocItem> documents = RequestItemsService.EnvelopeDocuments.Documents;
bool tokenOk = CheckToken(3);
if (!tokenOk)
{
// We could store the parameters of the requested operation
// so it could be restarted automatically.
// But since it should be rare to have a token issue here,
// we'll make the user re-enter the form data after
// authentication.
RequestItemsService.EgName = EgName;
return Redirect("/ds/mustAuthenticate");
}
FileStreamResult result = DoWork(accessToken, basePath, accountId,
envelopeId, documents, docSelect);
return result;
}
}
}