Skip to content

Commit

Permalink
Refactored Configuration binding
Browse files Browse the repository at this point in the history
  • Loading branch information
bcgov-hl committed Oct 16, 2023
1 parent 7f49601 commit 315723f
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 15 deletions.
8 changes: 4 additions & 4 deletions OFM.Infrastructure.WebAPI/Extensions/ApiKeyMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public ApiKeyMiddleware(RequestDelegate next)
_next = next;
}
public async Task InvokeAsync(HttpContext context,
IOptionsMonitor<AuthenticationSettings> options)
IOptions<AuthenticationSettings> options)
{
var apiKeys = options.CurrentValue.Schemes.ApiKeyScheme.Keys;
var apiKeyPresentInHeader = context.Request.Headers.TryGetValue(options.CurrentValue.Schemes.ApiKeyScheme.ApiKeyName ?? "", out var extractedApiKey);
var apiKeys = options.Value.Schemes.ApiKeyScheme.Keys;
var apiKeyPresentInHeader = context.Request.Headers.TryGetValue(options.Value.Schemes.ApiKeyScheme.ApiKeyName ?? "", out var extractedApiKey);

if ((apiKeyPresentInHeader && apiKeys.Any(k => k.Value == extractedApiKey))
|| context.Request.Path.StartsWithSegments("/swagger"))
Expand All @@ -35,7 +35,7 @@ public async Task InvokeAsync(HttpContext context,
}

context.Response.StatusCode = StatusCodes.Status401Unauthorized;
await context.Response.WriteAsync(options.CurrentValue.Schemes.ApiKeyScheme.ApiKeyErrorMesssage);
await context.Response.WriteAsync(options.Value.Schemes.ApiKeyScheme.ApiKeyErrorMesssage);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static void RegisterProviderProfileEndpoints(this IEndpointRouteBuilder e
{
var searchesEndpoints = endpointRouteBuilder.MapGroup("/api/providerprofile");

searchesEndpoints.MapGet("", ProviderProfileHandlers.GetProfileAsync).WithTags("Providers").Produces(200).ProducesProblem(404);
searchesEndpoints.MapGet("", ProviderProfilesHandlers.GetProfileAsync).WithTags("Providers").Produces(200).ProducesProblem(404);
}

public static void RegisterOperationsEndpoints(this IEndpointRouteBuilder endpointRouteBuilder)
Expand Down
8 changes: 4 additions & 4 deletions OFM.Infrastructure.WebAPI/Handlers/DocumentsHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static async Task<Results<BadRequest<string>, ProblemHttpResult, Ok<JsonO
}

public static async Task<Results<ProblemHttpResult, BadRequest<string>, Ok<string>>> PostAsync(
IOptionsMonitor<AppSettings> appSettings,
IOptions<AppSettings> appSettings,
ID365DocumentService documentService,
[FromBody] dynamic? jsonBody)
{
Expand All @@ -45,7 +45,7 @@ public static async Task<Results<ProblemHttpResult, BadRequest<string>, Ok<strin
if (jsonDom.Count == 0) return TypedResults.BadRequest("Invalid request.");

// Validate file size limit
if (JsonSizeCalculator.Estimate(jsonDom, true) > appSettings.CurrentValue.MaxFileSize)
if (JsonSizeCalculator.Estimate(jsonDom, true) > appSettings.Value.MaxFileSize)
{
return TypedResults.Problem("The file size exceeds the limit allowed.", statusCode: StatusCodes.Status500InternalServerError);
};
Expand All @@ -55,8 +55,8 @@ public static async Task<Results<ProblemHttpResult, BadRequest<string>, Ok<strin
string fileName = fileNameNode?.GetValue<string>() ?? throw new InvalidEnumArgumentException("The filename is missing.");
string[] fileNames = fileName.Split('.');
string fileExt = fileNames[^1].ToLower();
string[] acceptedFileFormats = appSettings.CurrentValue.FileFommats.Split(",").Select(ext => ext.Trim()).ToArray();
if (Array.IndexOf(acceptedFileFormats, fileExt) == -1) return TypedResults.BadRequest(appSettings.CurrentValue.FileFormatErrorMessage);
string[] acceptedFileFormats = appSettings.Value.FileFommats.Split(",").Select(ext => ext.Trim()).ToArray();
if (Array.IndexOf(acceptedFileFormats, fileExt) == -1) return TypedResults.BadRequest(appSettings.Value.FileFormatErrorMessage);

var response = await documentService.UploadAsync(jsonDom);

Expand Down
4 changes: 2 additions & 2 deletions OFM.Infrastructure.WebAPI/Handlers/EnvironmentHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ namespace OFM.Infrastructure.WebAPI.Handlers;
public static class EnvironmentHandlers
{
public static Results<ProblemHttpResult, Ok<JsonObject>> Get(
IOptionsMonitor<D365AuthSettings> options)
IOptions<D365AuthSettings> options)
{
var _authConfig = options.CurrentValue;
var _authConfig = options.Value;
_authConfig.AZAppUsers = new List<AZAppUser>();

string jsonContent = JsonSerializer.Serialize<D365AuthSettings>(_authConfig, D365AuthSettingsSerializationContext.Default.D365AuthSettings);
Expand Down
4 changes: 2 additions & 2 deletions OFM.Infrastructure.WebAPI/Handlers/OperationsHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class OperationsHandlers
static readonly string pageSizeParam = "pageSize";
public static async Task<Results<BadRequest<string>, ProblemHttpResult, Ok<JsonObject>>> GetAsync(
HttpContext context,
IOptionsMonitor<AppSettings> appSettings,
IOptions<AppSettings> appSettings,
ID365AppUserService appUserService,
ID365WebApiService d365WebApiService,
ILogger<string> logger,
Expand All @@ -41,7 +41,7 @@ public static async Task<Results<BadRequest<string>, ProblemHttpResult, Ok<JsonO
statement = statementFormatted;
}

int pagerTake = (pageSize > 0 && pageSize <= appSettings.CurrentValue.MaxPageSize) ? pageSize : appSettings.CurrentValue.MaxPageSize;
int pagerTake = (pageSize > 0 && pageSize <= appSettings.Value.MaxPageSize) ? pageSize : appSettings.Value.MaxPageSize;
var response = await d365WebApiService.SendRetrieveRequestAsync(appUserService.AZPortalAppUser, statement, formatted: true, pagerTake);

if (response.IsSuccessStatusCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace OFM.Infrastructure.WebAPI.Handlers;

public static class ProviderProfileHandlers
public static class ProviderProfilesHandlers
{
public static async Task<Results<BadRequest<string>, NotFound<string>, ProblemHttpResult, Ok<BusinessBCeID>>> GetProfileAsync(
ID365WebApiService d365WebApiService,
Expand Down
2 changes: 1 addition & 1 deletion OFM.Infrastructure.WebAPI/Models/DataverseModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class BusinessBCeID
public string? ofm_last_name { get; set; }
public Organization? organization { get; set; }
public string? ofm_portal_role { get; set; }
public List<FacilityPermission>? facility_permission { get; set; }
public IList<FacilityPermission>? facility_permission { get; set; }

public void MapBusinessBCeIDFacilityPermissions(IEnumerable<BCeIDFacility> permissions)
{
Expand Down

0 comments on commit 315723f

Please sign in to comment.