-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding the capability of calling a Web API to the webapi2 template (#226
) * Updating the outstanding project templates * Fixing template.json * Update * Update * Fix indentation * Fixing the templates * Fix the mvc2 and webapp2 templates which were generating the "call an API" even without the flags to enable it (--called-api-url and --called-api-scopes) Fixing the AuthorizeForScopes attribute in the Razor pages (which was added even when the webapp2 was not calling an API Fixing the identity of the Web API 2. Updating the test-templates.bat file to: - add test for webapp2 and mvc2 calling Web APIs - replace msbuild by dotnet build * Adding "calls a web api" to the Web API2 template
- Loading branch information
Showing
7 changed files
with
195 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
ProjectTemplates/templates/WebApi-CSharp/Services/DownstreamWebApi.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Identity.Web; | ||
|
||
namespace Company.WebApplication1.Services | ||
{ | ||
public interface IDownstreamWebApi | ||
{ | ||
Task<string> CallWebApi(string relativeEndpoint = "", string[] requiredScopes = null); | ||
} | ||
|
||
public static class DownstreamWebApiExtensions | ||
{ | ||
public static void AddDownstreamWebApiService(this IServiceCollection services, IConfiguration configuration) | ||
{ | ||
// https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests | ||
services.AddHttpClient<IDownstreamWebApi, DownstreamWebApi>(); | ||
} | ||
} | ||
|
||
public class DownstreamWebApi : IDownstreamWebApi | ||
{ | ||
private readonly ITokenAcquisition _tokenAcquisition; | ||
|
||
private readonly IConfiguration _configuration; | ||
|
||
private readonly HttpClient _httpClient; | ||
|
||
public DownstreamWebApi( | ||
ITokenAcquisition tokenAcquisition, | ||
IConfiguration configuration, | ||
HttpClient httpClient) | ||
{ | ||
_tokenAcquisition = tokenAcquisition; | ||
_configuration = configuration; | ||
_httpClient = httpClient; | ||
} | ||
|
||
/// <summary> | ||
/// Calls the Web API with the required scopes | ||
/// </summary> | ||
/// <param name="requireScopes">[Optional] Scopes required to call the Web API. If | ||
/// not specified, uses scopes from the configuration</param> | ||
/// <param name="relativeEndpoint">Endpoint relative to the CalledApiUrl configuration</param> | ||
/// <returns>A Json string representing the result of calling the Web API</returns> | ||
public async Task<string> CallWebApi(string relativeEndpoint = "", string[] requiredScopes = null) | ||
{ | ||
string[] scopes = requiredScopes ?? _configuration["CalledApi:CalledApiScopes"]?.Split(' '); | ||
string apiUrl = (_configuration["CalledApi:CalledApiUrl"] as string)?.TrimEnd('/') + $"/{relativeEndpoint}"; | ||
|
||
string accessToken = await _tokenAcquisition.GetAccessTokenForUserAsync(scopes); | ||
_httpClient.DefaultRequestHeaders.Add("Authorization", $"bearer {accessToken}"); | ||
|
||
string apiResult; | ||
var response = await _httpClient.GetAsync($"{apiUrl}"); | ||
if (response.StatusCode == HttpStatusCode.OK) | ||
{ | ||
apiResult = await response.Content.ReadAsStringAsync(); | ||
} | ||
else | ||
{ | ||
apiResult = $"Error calling the API '{apiUrl}'"; | ||
} | ||
|
||
return apiResult; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters