Add project files.

This commit is contained in:
Sergio Matias Urquin
2025-04-29 18:44:41 -06:00
parent c4ec91852f
commit b2635193dc
133 changed files with 4100 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
using Core.Blueprint.Service.External.Clients;
using Core.Blueprint.Service.External.GatewayConfigurations;
using Core.Blueprint.Service.External.Helpers.Token;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Refit;
namespace Core.Blueprint.External.ClientConfiguration
{
public static class RegisterClientConfiguration
{
public static IServiceCollection RegisterExternalLayer(this IServiceCollection services, IConfiguration configuration)
{
var gatewayConfiguration = new GatewayConfiguration();
var gatewaySettingsConfiguration = new GatewaySettingsConfiguration(configuration);
// Register GatewayConfiguration as a singleton
services.AddSingleton(gatewayConfiguration);
// Register IHttpContextAccessor
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
// Register ITokenProvider
services.AddSingleton<ITokenProvider, HttpContextTokenProvider>();
// Register the custom AuthenticatedHttpClientHandler
services.AddTransient(provider =>
{
var tokenProvider = provider.GetRequiredService<ITokenProvider>();
var handler = new AuthenticatedHttpClientHandler(tokenProvider)
{
InnerHandler = new HttpClientHandler() // Setting the InnerHandler manually
};
return handler;
});
var secretServiceApiUrl = GatewaySettingsConfiguration.GetBlueprintApiEndpoint.Endpoint.Uri;
// Register IBlueprintServiceClient with the manually created HttpClient
services.AddScoped<IBlueprintServiceClient>(provider =>
{
var handler = provider.GetRequiredService<AuthenticatedHttpClientHandler>();
var httpClient = new HttpClient(handler)
{
BaseAddress = secretServiceApiUrl,
Timeout = TimeSpan.FromMinutes(1)
};
return RestService.For<IBlueprintServiceClient>(httpClient);
});
services.AddScoped<IAuthenticationService, AuthenticationService>();
return services;
}
}
}

View File

@@ -0,0 +1,15 @@
namespace Core.Blueprint.Service.External.Clients.Adapters
{
public class BlueprintAdapter
{
public string Name { get; set; } = null!;
public string? Description { get; set; }
public string _Id { get; set; } = null!;
public string Id { get; init; } = null!;
public DateTime CreatedAt { get; set; }
public string? CreatedBy { get; set; }
public DateTime? UpdatedAt { get; set; }
public string? UpdatedBy { get; set; }
public StatusEnum Status { get; set; }
}
}

View File

@@ -0,0 +1,10 @@
using Core.Blueprint.KeyVault;
namespace Core.Blueprint.Service.External.Clients.Adapters
{
public class KeyVaultAdapter
{
public KeyVaultResponse Item1 { get; set; }
public string Item2 { get; set; }
}
}

View File

@@ -0,0 +1,15 @@
using System.Text.Json.Serialization;
namespace Core.Blueprint.Service.External.Clients.Adapters
{
[JsonConverter(typeof(JsonStringEnumConverter))]
public enum StatusEnum
{
Active = 0,
Inactive = 1,
Deleted = 2
}
}

View File

@@ -0,0 +1,16 @@
namespace Core.Blueprint.Service.External.Clients.Adapters
{
public class UserProjectAdapter
{
public string ProjectCode { get; set; } = null!;
public string ProjectDescription { get; set; } = null!;
public string UserId { get; set; } = null!;
public int Id { get; set; }
public string Guid { get; set; } = null!;
public DateTime CreatedAt { get; set; }
public string? CreatedBy { get; set; }
public DateTime? UpdatedAt { get; set; }
public string? UpdatedBy { get; set; }
public StatusEnum Status { get; set; }
}
}

View File

@@ -0,0 +1,67 @@
using Core.Blueprint.KeyVault;
using Core.Blueprint.Service.External.Clients.Adapters;
using Core.Blueprint.Service.External.Clients.Requests;
using Core.Blueprint.Storage;
using Core.Blueprint.Storage.Adapters;
using Microsoft.AspNetCore.Mvc;
using Refit;
namespace Core.Blueprint.Service.External.Clients
{
public interface IBlueprintServiceClient
{
[Post("/v1/MongoBlueprint/Create")]
Task<BlueprintAdapter> CreateBlueprintAsync([FromBody] BlueprintRequest newBlueprint, CancellationToken cancellationToken = default);
[Get("/v1/MongoBlueprint/GetAll")]
Task<IEnumerable<BlueprintAdapter>> GetAllBlueprintsAsync(CancellationToken cancellationToken = default);
[Get("/v1/MongoBlueprint/{_id}/GetBy_Id")]
Task<BlueprintAdapter> GetBlueprintByIdAsync([FromRoute] string _id, CancellationToken cancellationToken = default);
[Put("/v1/MongoBlueprint/{_id}/Update")]
Task<BlueprintAdapter> UpdateBlueprintAsync([FromRoute] string _id, [FromBody] BlueprintAdapter entity, CancellationToken cancellationToken = default);
[Delete("/v1/MongoBlueprint/{_id}/Delete")]
Task<BlueprintAdapter> DeleteBlueprintAsync([FromRoute] string _id, CancellationToken cancellationToken = default);
[Post("/v1/UserProject/Create")]
Task<UserProjectAdapter> CreateUserProjectAsync([FromBody] UserProjectRequest newUserProject, CancellationToken cancellationToken = default);
[Get("/v1/UserProject/GetAll")]
Task<IEnumerable<UserProjectAdapter>> GetAllUserProjectsAsync(CancellationToken cancellationToken = default);
[Get("/v1/UserProject/{id}/GetById")]
Task<UserProjectAdapter> GetUserProjectByIdAsync([FromRoute] int id, CancellationToken cancellationToken = default);
[Put("/v1/UserProject/{id}/Update")]
Task<UserProjectAdapter> UpdateUserProjectAsync([FromRoute] int id, [FromBody] UserProjectAdapter entity, CancellationToken cancellationToken = default);
[Delete("/v1/UserProject/{id}/Delete")]
Task<UserProjectAdapter> DeleteUserProjectAsync([FromRoute] int id, CancellationToken cancellationToken = default);
[Post("/v1/KeyVault/CreateSecret")]
Task<KeyVaultResponse> CreateSecretAsync([FromBody] KeyVaultRequest newKeyVault, CancellationToken cancellationToken = default);
[Get("/v1/KeyVault/{secretName}/GetSecret")]
Task<KeyVaultAdapter> GetSecretByNameAsync([FromRoute] string secretName, CancellationToken cancellationToken = default);
[Put("/v1/KeyVault/UpdateSecret")]
Task<KeyVaultAdapter> UpdateSecretAsync([FromBody] KeyVaultRequest entity, CancellationToken cancellationToken = default);
[Delete("/v1/KeyVault/{secretName}/DeleteSecret")]
Task<KeyVaultAdapter> DeleteSecretAsync([FromRoute] string secretName, CancellationToken cancellationToken = default);
[Post("/v1/BlobStorage/UploadBlob")]
Task<BlobFileAdapter> UploadBlobAsync([FromQuery] BlobAddDto request, CancellationToken cancellationToken = default);
[Get("/v1/BlobStorage/GetBlobList")]
Task<List<BlobFileAdapter>> GetBlobListAsync([FromQuery] string? prefix, CancellationToken cancellationToken = default);
[Get("/v1/BlobStorage/DownloadBlob")]
Task<BlobDownloadUriAdapter> DownloadBlobAsync([FromQuery] string blobName, CancellationToken cancellationToken = default);
[Delete("/v1/BlobStorage/DeleteBlob")]
Task<BlobFileAdapter> DeleteBlobAsync([FromQuery] string blobName, CancellationToken cancellationToken = default);
}
}

View File

@@ -0,0 +1,8 @@
namespace Core.Blueprint.Service.External.Clients.Requests
{
public class BlueprintRequest
{
public string Name { get; set; } = null!;
public string? Description { get; set; }
}
}

View File

@@ -0,0 +1,9 @@
namespace Core.Blueprint.Service.External.Clients.Requests
{
public class UserProjectRequest
{
public string ProjectCode { get; set; } = null!;
public string ProjectDescription { get; set; } = null!;
public string UserId { get; set; } = null!;
}
}

View File

@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" Version="12.23.0" />
<PackageReference Include="Core.Blueprint.KeyVault" Version="0.3.0-alpha0037" />
<PackageReference Include="Core.Blueprint.Storage" Version="0.3.0-alpha0049" />
<PackageReference Include="Lib.Architecture.BuildingBlocks" Version="0.9.0-alpha0008" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0" />
<PackageReference Include="Refit" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<Reference Include="LSA.Core.Adapters">
<HintPath>..\..\Dependencies\LSA.Core.Adapters.dll</HintPath>
</Reference>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,24 @@
using Core.Blueprint.External;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Core.Blueprint.Service.External.GatewayConfigurations
{
public record GatewayConfiguration
{
public GatewayConfiguration()
{
BlueprintServiceAPI = new BlueprintServiceApi();
}
public BlueprintServiceApi BlueprintServiceAPI { get; set; }
}
public record BlueprintServiceApi
{
public string Channel { get; set; }
public BaseEndpoint Endpoint { get; set; }
}
}

View File

@@ -0,0 +1,43 @@
using Core.Blueprint.External;
using Microsoft.Extensions.Configuration;
namespace Core.Blueprint.Service.External.GatewayConfigurations
{
public class GatewaySettingsConfiguration
{
private readonly IConfiguration _configuration;
public GatewaySettingsConfiguration(IConfiguration configuration)
{
_configuration = configuration;
this.SetBlueprintServiceApiEndpoint();
}
private static GatewayConfiguration GatewayConfiguration { get; set; } = new GatewayConfiguration();
public static BlueprintServiceApi GetBlueprintApiEndpoint => GatewayConfiguration.BlueprintServiceAPI;
private GatewayConfiguration SetBlueprintServiceApiEndpoint()
{
IConfigurationSection source;
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? string.Empty;
if (environment == "Local")
source = _configuration.GetSection("LocalGateways");
else
source = _configuration.GetSection("Gateways");
var endpoint = source["BlueprintDAL"];
GatewayConfiguration.BlueprintServiceAPI = new BlueprintServiceApi
{
Endpoint = new BaseEndpoint()
{
Uri = new Uri(endpoint),
Url = endpoint,
Token = string.Empty,
APIName = "Blueprint API"
}
};
return GatewayConfiguration;
}
}
}

View File

@@ -0,0 +1,32 @@
// ***********************************************************************
// <copyright file="AuthenticatedHttpClientHandler.cs">
// Heath
// </copyright>
// ***********************************************************************
namespace Core.Blueprint.Service.External.Helpers.Token
{
/// <summary>
/// Class to inject the token in all requests.
/// </summary>
public class AuthenticatedHttpClientHandler : DelegatingHandler
{
private readonly ITokenProvider _tokenProvider;
public AuthenticatedHttpClientHandler(ITokenProvider tokenProvider)
{
_tokenProvider = tokenProvider;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var token = _tokenProvider.GetToken();
if (!string.IsNullOrEmpty(token))
{
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
}
return await base.SendAsync(request, cancellationToken);
}
}
}

View File

@@ -0,0 +1,31 @@
// ***********************************************************************
// <copyright file="HttpContextTokenProvider.cs">
// Heath
// </copyright>
// ***********************************************************************
using Microsoft.AspNetCore.Http;
namespace Core.Blueprint.Service.External.Helpers.Token
{
/// <summary>
/// Class to return the access token to controllers.
/// </summary>
public class HttpContextTokenProvider : ITokenProvider
{
private readonly IHttpContextAccessor _httpContextAccessor;
public HttpContextTokenProvider(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
/// <summary>
/// Get token from headers.
/// </summary>
public string GetToken()
{
return _httpContextAccessor.HttpContext?.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last();
}
}
}

View File

@@ -0,0 +1,19 @@
// ***********************************************************************
// <copyright file="ITokenProvider.cs">
// Heath
// </copyright>
// ***********************************************************************
namespace Core.Blueprint.Service.External.Helpers.Token
{
/// <summary>
/// Interface for token provider.
/// </summary>
public interface ITokenProvider
{
/// <summary>
/// Get token from headers.
/// </summary>
string GetToken();
}
}