Skip to content

Commit 16b049a

Browse files
zahalzeldeepchoudhery
authored andcommitted
Code updates for blazor server Downstream API option (#1767)
* Add blazor server, and some code refactors
1 parent 75892f6 commit 16b049a

9 files changed

Lines changed: 99 additions & 8 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
@page "/callwebapi"
2+
3+
@using Microsoft.Identity.Web
4+
5+
@inject IDownstreamWebApi downstreamAPI
6+
@inject MicrosoftIdentityConsentAndConditionalAccessHandler ConsentHandler
7+
8+
<h1>Call an API</h1>
9+
10+
<p>This component demonstrates fetching data from a Web API.</p>
11+
12+
@if (apiResult == null)
13+
{
14+
<p><em>Loading...</em></p>
15+
}
16+
else
17+
{
18+
<h2>API Result</h2>
19+
@apiResult
20+
}
21+
22+
@code {
23+
private HttpResponseMessage response;
24+
private string apiResult;
25+
26+
protected override async Task OnInitializedAsync()
27+
{
28+
try
29+
{
30+
response = await downstreamAPI.CallWebApiForUserAsync(
31+
"DownstreamApi",
32+
options => options.RelativePath = "");
33+
34+
if (response.StatusCode == System.Net.HttpStatusCode.OK)
35+
{
36+
apiResult = await response.Content.ReadAsStringAsync();
37+
}
38+
else
39+
{
40+
apiResult = "Failed to call the web API";
41+
}
42+
}
43+
catch (Exception ex)
44+
{
45+
ConsentHandler.HandleException(ex);
46+
}
47+
}
48+
}

src/MSIdentityScaffolding/Microsoft.DotNet.MSIdentity/CodeReaderWriter/CodeModifierConfigs/cm_dotnet_blazorserver.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"Files": [
44
{
55
"FileName": "Program.cs",
6+
"Options": [ "MinimalApp" ],
67
"Methods": {
78
"Global": {
89
"CodeChanges": [
@@ -153,6 +154,13 @@
153154
"Options": [
154155
"MicrosoftGraph"
155156
]
157+
},
158+
{
159+
"Block": "</NavLink>\r\n </div>\r\n <div class=\"nav-item px-3\">\r\n <NavLink class=\"nav-link\" href=\"callwebapi\">\r\n <span class=\"oi oi-list-rich\" aria-hidden=\"true\"></span> Call Web API\r\n </NavLink>\r\n </div>\r\n </nav>\r\n</div>",
160+
"ReplaceSnippet": "</NavLink>\r\n </div>\r\n </nav>\r\n</div>",
161+
"Options": [
162+
"DownstreamApi"
163+
]
156164
}
157165
]
158166
},
@@ -162,6 +170,13 @@
162170
"Options": [
163171
"MicrosoftGraph"
164172
]
173+
},
174+
{
175+
"FileName": "CallWebApi.razor",
176+
"AddFilePath": "Pages/CallWebApi.razor",
177+
"Options": [
178+
"DownstreamApi"
179+
]
165180
}
166181
]
167182
}

src/MSIdentityScaffolding/Microsoft.DotNet.MSIdentity/CodeReaderWriter/CodeModifierConfigs/cm_dotnet_webapi.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"Files": [
44
{
55
"FileName": "Startup.cs",
6+
"Options": [ "NonMinimalApp" ],
67
"Methods": {
78
"Configure": {
89
"Parameters": [ "IApplicationBuilder", "IWebHostEnvironment" ],
@@ -36,6 +37,7 @@
3637
},
3738
{
3839
"FileName": "Program.cs",
40+
"Options": [ "MinimalApp" ],
3941
"Methods": {
4042
"Global": {
4143
"CodeChanges": [

src/MSIdentityScaffolding/Microsoft.DotNet.MSIdentity/CodeReaderWriter/CodeModifierConfigs/cm_dotnet_webapp.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"Files": [
44
{
55
"FileName": "Startup.cs",
6+
"Options": [ "NonMinimalApp" ],
67
"Methods": {
78
"Configure": {
89
"Parameters": [ "IApplicationBuilder", "IWebHostEnvironment" ],
@@ -70,6 +71,7 @@
7071
},
7172
{
7273
"FileName": "Program.cs",
74+
"Options": [ "MinimalApp" ],
7375
"Methods": {
7476
"Global": {
7577
"CodeChanges": [

src/MSIdentityScaffolding/Microsoft.DotNet.MSIdentity/CodeReaderWriter/ProjectModifier.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ internal async Task ModifyRazorFile(string fileName, CodeFile file, CodeAnalysis
167167

168168
var razorChanges = file?.RazorChanges.Where(cc => ProjectModifierHelper.FilterOptions(cc.Options, toolOptions));
169169
var editedDocument = await ProjectModifierHelper.ModifyDocumentText(document, razorChanges);
170-
await ProjectModifierHelper.UpdateDocument(document, editedDocument, _consoleLogger);
170+
await ProjectModifierHelper.UpdateDocument(editedDocument, _consoleLogger);
171171
}
172172

173173
internal async Task ModifyCshtmlFile(string fileName, CodeFile cshtmlFile, CodeAnalysis.Project project, CodeChangeOptions options)
@@ -188,7 +188,7 @@ internal async Task ModifyCshtmlFile(string fileName, CodeFile cshtmlFile, CodeA
188188
var filteredCodeFiles = globalMethod.CodeChanges.Where(cc => ProjectModifierHelper.FilterOptions(cc.Options, options));
189189
var editedDocument = await ProjectModifierHelper.ModifyDocumentText(fileDoc, filteredCodeFiles);
190190
//replace the document
191-
await ProjectModifierHelper.UpdateDocument(fileDoc, editedDocument, _consoleLogger);
191+
await ProjectModifierHelper.UpdateDocument(editedDocument, _consoleLogger);
192192
}
193193
}
194194
}

src/MSIdentityScaffolding/Microsoft.DotNet.MSIdentity/Properties/Resources.Designer.cs

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/MSIdentityScaffolding/Microsoft.DotNet.MSIdentity/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@
124124
<value>Adding package {0} . . .</value>
125125
</data>
126126
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
127+
<data name="add_CallWebApi_razor" type="System.Resources.ResXFileRef, System.Windows.Forms">
128+
<value>..\CodeReaderWriter\CodeFiles\Blazor\Server\CallWebApi.razor;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
129+
</data>
127130
<data name="add_ShowProfile_razor" type="System.Resources.ResXFileRef, System.Windows.Forms">
128131
<value>..\CodeReaderWriter\CodeFiles\Blazor\Server\ShowProfile.razor;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
129132
</data>

src/Shared/Microsoft.DotNet.Scaffolding.Shared/CodeModifier/CodeChange/CodeChangeHelper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class CodeChangeOptionStrings
1515
public const string DownstreamApi = nameof(DownstreamApi);
1616
public const string Skip = nameof(Skip);
1717
public const string NonMinimalApp = nameof(NonMinimalApp);
18+
public const string MinimalApp = nameof(MinimalApp);
1819
}
1920

2021
public class CodeChangeOptions

src/Shared/Microsoft.DotNet.Scaffolding.Shared/Project/ProjectModifierHelper.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ internal static async Task<bool> IsMinimalApp(IModelTypesLocator modelTypesLocat
3838
return startupType == null;
3939
}
4040

41+
// Returns true when there is no Startup.cs or equivalent
4142
internal static async Task<bool> IsMinimalApp(CodeAnalysis.Project project)
4243
{
4344
if (project != null)
@@ -110,6 +111,7 @@ node is MemberAccessExpressionSyntax maes &&
110111
}
111112
}
112113
}
114+
113115
return string.Empty;
114116
}
115117

@@ -343,6 +345,11 @@ internal static bool FilterOptions(string[] options, CodeChangeOptions codeChang
343345
{
344346
return false;
345347
}
348+
// for example, program.cs is only modified when codeChangeOptions.IsMinimalApp is true
349+
if (options.Contains(CodeChangeOptionStrings.MinimalApp) && !codeChangeOptions.IsMinimalApp)
350+
{
351+
return false;
352+
}
346353
//if its a minimal app and options have a "NonMinimalApp", don't add the CodeBlock
347354
if (options.Contains(CodeChangeOptionStrings.NonMinimalApp) && codeChangeOptions.IsMinimalApp)
348355
{
@@ -375,6 +382,7 @@ internal static bool FilterOptions(string[] options, CodeChangeOptions codeChang
375382
return true;
376383
}
377384
}
385+
378386
return false;
379387
}
380388

@@ -420,15 +428,17 @@ internal static async Task<Document> ModifyDocumentText(Document fileDoc, IEnume
420428
return null; // TODO generate README
421429
}
422430

423-
var sourceTextToAdd = SourceText.From(sourceFileString);
424-
return fileDoc.WithText(sourceTextToAdd);
431+
var updatedSourceText = SourceText.From(sourceFileString);
432+
return fileDoc.WithText(updatedSourceText);
425433
}
426434

427-
internal static async Task UpdateDocument(Document fileDoc, Document editedDocument, IConsoleLogger consoleLogger)
435+
internal static async Task UpdateDocument(Document document, IConsoleLogger consoleLogger)
428436
{
429-
var classFileTxt = await editedDocument.GetTextAsync();
430-
File.WriteAllText(fileDoc.Name, classFileTxt.ToString());
431-
consoleLogger.LogMessage($"Modified {fileDoc.Name}.\n");
437+
var classFileTxt = await document.GetTextAsync();
438+
439+
// Note: Here, document.Name is the full filepath
440+
File.WriteAllText(document.Name, classFileTxt.ToString());
441+
consoleLogger.LogMessage($"Modified {document.Name}.\n");
432442
}
433443

434444
// Filter out CodeBlocks that are invalid using FilterOptions

0 commit comments

Comments
 (0)