Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
@page "/callwebapi"

@using Microsoft.Identity.Web

@inject IDownstreamWebApi downstreamAPI
@inject MicrosoftIdentityConsentAndConditionalAccessHandler ConsentHandler

<h1>Call an API</h1>

<p>This component demonstrates fetching data from a Web API.</p>

@if (apiResult == null)
{
<p><em>Loading...</em></p>
}
else
{
<h2>API Result</h2>
@apiResult
}

@code {
private HttpResponseMessage response;
private string apiResult;

protected override async Task OnInitializedAsync()
{
try
{
response = await downstreamAPI.CallWebApiForUserAsync(
"DownstreamApi",
options => options.RelativePath = "");

if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
apiResult = await response.Content.ReadAsStringAsync();
}
else
{
apiResult = "Failed to call the web API";
}
}
catch (Exception ex)
{
ConsentHandler.HandleException(ex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"Files": [
{
"FileName": "Program.cs",
"Options": [ "MinimalApp" ],
"Methods": {
"Global": {
"CodeChanges": [
Expand Down Expand Up @@ -153,6 +154,13 @@
"Options": [
"MicrosoftGraph"
]
},
{
"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>",
"ReplaceSnippet": "</NavLink>\r\n </div>\r\n </nav>\r\n</div>",
"Options": [
"DownstreamApi"
]
}
]
},
Expand All @@ -162,6 +170,13 @@
"Options": [
"MicrosoftGraph"
]
},
{
"FileName": "CallWebApi.razor",
"AddFilePath": "Pages/CallWebApi.razor",
"Options": [
"DownstreamApi"
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"Files": [
{
"FileName": "Startup.cs",
"Options": [ "NonMinimalApp" ],
"Methods": {
"Configure": {
"Parameters": [ "IApplicationBuilder", "IWebHostEnvironment" ],
Expand Down Expand Up @@ -36,6 +37,7 @@
},
{
"FileName": "Program.cs",
"Options": [ "MinimalApp" ],
"Methods": {
"Global": {
"CodeChanges": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"Files": [
{
"FileName": "Startup.cs",
"Options": [ "NonMinimalApp" ],
"Methods": {
"Configure": {
"Parameters": [ "IApplicationBuilder", "IWebHostEnvironment" ],
Expand Down Expand Up @@ -70,6 +71,7 @@
},
{
"FileName": "Program.cs",
"Options": [ "MinimalApp" ],
"Methods": {
"Global": {
"CodeChanges": [
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@
<value>Adding package {0} . . .</value>
</data>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="add_CallWebApi_razor" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\CodeReaderWriter\CodeFiles\Blazor\Server\CallWebApi.razor;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="add_ShowProfile_razor" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\CodeReaderWriter\CodeFiles\Blazor\Server\ShowProfile.razor;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class CodeChangeOptionStrings
public const string DownstreamApi = nameof(DownstreamApi);
public const string Skip = nameof(Skip);
public const string NonMinimalApp = nameof(NonMinimalApp);
public const string MinimalApp = nameof(MinimalApp);
}

public class CodeChangeOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ internal static async Task<bool> IsMinimalApp(IModelTypesLocator modelTypesLocat
return startupType == null;
}

// Returns true when there is no Startup.cs or equivalent
internal static async Task<bool> IsMinimalApp(CodeAnalysis.Project project)
{
if (project != null)
Expand Down Expand Up @@ -110,6 +111,7 @@ node is MemberAccessExpressionSyntax maes &&
}
}
}

return string.Empty;
}

Expand Down Expand Up @@ -343,6 +345,11 @@ internal static bool FilterOptions(string[] options, CodeChangeOptions codeChang
{
return false;
}
// for example, program.cs is only modified when codeChangeOptions.IsMinimalApp is true
if (options.Contains(CodeChangeOptionStrings.MinimalApp) && !codeChangeOptions.IsMinimalApp)
{
return false;
}
//if its a minimal app and options have a "NonMinimalApp", don't add the CodeBlock
if (options.Contains(CodeChangeOptionStrings.NonMinimalApp) && codeChangeOptions.IsMinimalApp)
{
Expand Down Expand Up @@ -375,6 +382,7 @@ internal static bool FilterOptions(string[] options, CodeChangeOptions codeChang
return true;
}
}

return false;
}

Expand Down Expand Up @@ -420,14 +428,14 @@ internal static async Task<Document> ModifyDocumentText(Document fileDoc, IEnume
return null; // TODO generate README
}

var sourceTextToAdd = SourceText.From(sourceFileString);
return fileDoc.WithText(sourceTextToAdd);
var updatedSourceText = SourceText.From(sourceFileString);
return fileDoc.WithText(updatedSourceText);
}

internal static async Task UpdateDocument(Document fileDoc, Document editedDocument, IConsoleLogger consoleLogger)
{
var classFileTxt = await editedDocument.GetTextAsync();
File.WriteAllText(fileDoc.Name, classFileTxt.ToString());
File.WriteAllText(fileDoc.FilePath, classFileTxt.ToString());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double check the value for Document.Name vs Document.FilePath. I remember distinctly using Document.Name but blanking rn.
Maybe add a comment showing the difference between their values.

consoleLogger.LogMessage($"Modified {fileDoc.Name}.\n");
}

Expand Down