Skip to content

Commit 6127121

Browse files
committed
finished up work on the code example
1 parent ba311a3 commit 6127121

File tree

8 files changed

+215
-170
lines changed

8 files changed

+215
-170
lines changed

launcher-csharp/Startup.cs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ namespace DocuSign.CodeExamples
77
{
88
using System;
99
using System.Collections.Generic;
10+
using System.Linq;
1011
using System.Net.Http;
1112
using System.Net.Http.Headers;
1213
using System.Security.Claims;
1314
using System.Text.Json;
15+
using System.Text.RegularExpressions;
1416
using System.Threading.Tasks;
17+
using System.Web;
1518
using DocuSign.CodeExamples.Common;
1619
using DocuSign.CodeExamples.Models;
1720
using DocuSign.Rooms.Api;
@@ -35,7 +38,7 @@ public Startup(IConfiguration configuration)
3538
{
3639
this.Configuration = configuration;
3740

38-
this.apiTypes.Add(ExamplesApiType.ESignature, new List<string> { "signature" });
41+
this.apiTypes.Add(ExamplesApiType.ESignature, new List<string> { "signature", });
3942

4043
this.apiTypes.Add(ExamplesApiType.Rooms, new List<string>
4144
{
@@ -180,28 +183,8 @@ public void ConfigureServices(IServiceCollection services)
180183
{
181184
List<string> scopesForCurrentApi = this.apiTypes.GetValueOrDefault(Enum.Parse<ExamplesApiType>(this.Configuration["API"]));
182185

183-
foreach (var api in this.apiTypes)
184-
{
185-
if (this.Configuration["API"] != api.Key.ToString())
186-
{
187-
foreach (var scope in api.Value)
188-
{
189-
if (scopesForCurrentApi != null && !scopesForCurrentApi.Contains(scope))
190-
{
191-
var scopeWithSeperator = scope + "%20";
192-
193-
if (redirectContext.RedirectUri.Contains(scopeWithSeperator))
194-
{
195-
redirectContext.RedirectUri = redirectContext.RedirectUri.Replace(scopeWithSeperator, string.Empty);
196-
}
197-
else
198-
{
199-
redirectContext.RedirectUri = redirectContext.RedirectUri.Replace(scope, string.Empty);
200-
}
201-
}
202-
}
203-
}
204-
}
186+
187+
redirectContext.RedirectUri = this.UpdateRedirectUriScopes(redirectContext.RedirectUri, scopesForCurrentApi);
205188

206189
redirectContext.HttpContext.Response.Redirect(redirectContext.RedirectUri);
207190
return Task.FromResult(0);
@@ -332,5 +315,13 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
332315

333316
return keyValue;
334317
}
318+
319+
private string UpdateRedirectUriScopes(string uri, List<string> wantedScopes)
320+
{
321+
const string pattern = @"(?:&|\?)scope=([^&]+)";
322+
323+
var encodedScopes = string.Join(" ", wantedScopes);
324+
return Regex.Replace(uri, pattern, $"&scope={HttpUtility.UrlPathEncode(encodedScopes)}");
325+
}
335326
}
336327
}

launcher-csharp/WebForms/Controllers/CreateAndEmbedForm.cs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ namespace DocuSign.CodeExamples.WebForms.Controllers
2121
[Route("web001")]
2222
public class CreateAndEmbedForm : EgController
2323
{
24+
public const string TemplateName = "Web Form Example Template";
25+
2426
private IConfiguration configuration;
2527

2628
public CreateAndEmbedForm(
@@ -31,7 +33,7 @@ public CreateAndEmbedForm(
3133
: base(config, launcherTexts, requestItemsService)
3234
{
3335
this.configuration = configuration;
34-
this.CodeExampleText = this.GetExampleText(this.EgName, ExamplesApiType.Connect);
36+
this.CodeExampleText = this.GetExampleText(this.EgName, ExamplesApiType.WebForms);
3537
this.ViewBag.title = this.CodeExampleText.ExampleName;
3638
}
3739

@@ -49,13 +51,18 @@ public ActionResult CheckTheTemplates()
4951
var docuSignClient = new DocuSignClient(basePath);
5052
docuSignClient.Configuration.DefaultHeader.Add("Authorization", "Bearer " + accessToken);
5153

52-
List<EnvelopeTemplate> templates = CreateAndEmbedFormService.CheckIfTemplateExists(docuSignClient, accountId);
54+
List<EnvelopeTemplate> templates = CreateAndEmbedFormService.CheckIfTemplateExists(
55+
docuSignClient,
56+
accountId,
57+
TemplateName);
58+
5359
if (templates == null || templates.Count == 0)
5460
{
55-
var template = CreateAndEmbedFormService.CreateTemplate(
61+
TemplateSummary template = CreateAndEmbedFormService.CreateTemplate(
5662
docuSignClient,
5763
accountId,
58-
this.Config.DocumentTemplatePdf);
64+
this.Config.DocumentTemplatePdf,
65+
TemplateName);
5966

6067
this.RequestItemsService.WebFormsTemplateId = template.TemplateId;
6168
}
@@ -68,6 +75,10 @@ public ActionResult CheckTheTemplates()
6875
this.Config.WebFormConfig,
6976
this.RequestItemsService.WebFormsTemplateId);
7077

78+
this.ViewBag.CodeExampleText = this.CodeExampleText;
79+
this.ViewBag.Description = this.CodeExampleText.AdditionalPages
80+
.First(x => x.Name == "create_web_form").ResultsPageText;
81+
7182
return this.View("embedForm");
7283
}
7384

@@ -76,7 +87,7 @@ public ActionResult CheckTheTemplates()
7687
[Route("EmbedForm")]
7788
[HttpPost]
7889
[ValidateAntiForgeryToken]
79-
public ActionResult EmbedForm(string formId)
90+
public ActionResult EmbedForm()
8091
{
8192
string basePath = this.RequestItemsService.Session.WebFormsBasePath;
8293
string accessToken = this.RequestItemsService.User.AccessToken;
@@ -85,6 +96,20 @@ public ActionResult EmbedForm(string formId)
8596
var docuSignClient = new DocuSign.WebForms.Client.DocuSignClient(basePath);
8697
docuSignClient.Configuration.DefaultHeader.Add("Authorization", "Bearer " + accessToken);
8798

99+
WebFormSummaryList forms = CreateAndEmbedFormService.GetForms(
100+
docuSignClient,
101+
Guid.Parse(accountId));
102+
103+
if (forms.Items == null || forms.Items.Count == 0)
104+
{
105+
this.ViewBag.CodeExampleText = this.CodeExampleText;
106+
this.ViewBag.Description = this.CodeExampleText.AdditionalPages
107+
.First(x => x.Name == "create_web_form").ResultsPageText;
108+
109+
return this.View("embedForm");
110+
}
111+
112+
Guid? formId = forms.Items.First(x => x.FormProperties.Name == TemplateName).Id;
88113
WebFormInstance form = CreateAndEmbedFormService.CreateInstance(
89114
docuSignClient,
90115
Guid.Parse(accountId),

0 commit comments

Comments
 (0)