Last works ...
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
using xApi.Models;
|
||||
using xApi.Constants;
|
||||
using xApi.Providers;
|
||||
using xApi.Interfaces;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
|
||||
namespace xApi.Extensions
|
||||
{
|
||||
@@ -34,6 +38,139 @@ namespace xApi.Extensions
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extract Azure Service Configuration based on appSetting file ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
public static AzureServiceConfiguration? GetAzureServiceConfiguration(
|
||||
this IConfiguration source
|
||||
)
|
||||
{
|
||||
//
|
||||
var section = source
|
||||
.GetSection(ConfigurationNodeNames.AzureServiceConfigurationNodeName);
|
||||
var result = section.Get<AzureServiceConfiguration>();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add Application Configuration to Service Collection ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="configuration"></param>
|
||||
public static void AddApiConfiguration(
|
||||
this IServiceCollection source,
|
||||
IConfiguration configuration
|
||||
)
|
||||
{
|
||||
//
|
||||
var config = configuration.GetApiConfiguration();
|
||||
source.AddApiConfiguration(config);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add Application Configuration to Service Collection ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="configuration"></param>
|
||||
public static void AddApiConfiguration(
|
||||
this IServiceCollection source,
|
||||
ApiConfiguration? configuration
|
||||
)
|
||||
{
|
||||
//
|
||||
if (configuration is null ||
|
||||
!configuration.IsValid())
|
||||
{
|
||||
throw new InvalidDataException("Api Configuration is not Valid ...");
|
||||
}
|
||||
|
||||
//
|
||||
source.AddSingleton(configuration);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add Azure Service Configuration to Service Collection ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="configuration"></param>
|
||||
public static void AddAzureServiceConfiguration(
|
||||
this IServiceCollection source,
|
||||
IConfiguration configuration
|
||||
)
|
||||
{
|
||||
//
|
||||
var config = configuration.GetAzureServiceConfiguration();
|
||||
source.AddAzureServiceConfiguration(config);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add Azure Service Configuration to Service Collection ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="configuration"></param>
|
||||
/// <exception cref="InvalidDataException"></exception>/
|
||||
public static void AddAzureServiceConfiguration(
|
||||
this IServiceCollection source,
|
||||
AzureServiceConfiguration? configuration
|
||||
)
|
||||
{
|
||||
//
|
||||
if (configuration is null ||
|
||||
!configuration.IsValid())
|
||||
{
|
||||
throw new InvalidDataException("Azure Service Configuration is not Valid ...");
|
||||
}
|
||||
|
||||
//
|
||||
source.AddSingleton(configuration);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add Azure Service Configuration to Service Collection ...
|
||||
/// and Register Azure Services ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="configuration"></param>
|
||||
public static void AddAzureApiServices(
|
||||
this IServiceCollection source,
|
||||
IConfiguration configuration
|
||||
)
|
||||
{
|
||||
//
|
||||
// Retrieve Configuration and Add to Service Collection ...
|
||||
var config = configuration.GetAzureServiceConfiguration();
|
||||
source.AddAzureServiceConfiguration(config);
|
||||
|
||||
//
|
||||
// Register Specified HttpClient for Azure DevOps Service ...
|
||||
source.AddHttpClient(Params.AzureHttpClient, (sp, client) =>
|
||||
{
|
||||
//
|
||||
client.BaseAddress = new Uri(config!.BaseUrl!);
|
||||
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
|
||||
//
|
||||
client.Timeout = TimeSpan.FromSeconds(20);
|
||||
|
||||
//
|
||||
var pat = config.Pat;
|
||||
if (!string.IsNullOrWhiteSpace(pat))
|
||||
{
|
||||
//
|
||||
var authToken = Convert.ToBase64String(Encoding.ASCII.GetBytes($":{pat}"));
|
||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authToken);
|
||||
}
|
||||
});
|
||||
|
||||
//
|
||||
// Register Azure Service ...
|
||||
source.AddScoped<IAzureServices, AzureServices>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inject Specific Registered Service from IServiceCollection
|
||||
/// </summary>
|
||||
|
||||
@@ -1,9 +1,105 @@
|
||||
using xApi.Models;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace xApi.Extensions
|
||||
{
|
||||
public static class ModelExtensions
|
||||
{
|
||||
//
|
||||
#region Common ...
|
||||
/// <summary>
|
||||
/// Parse Json String to Specified Type of Object ...
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="json"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="InvalidOperationException"></exception>
|
||||
public static T? ParseJSON<T>(
|
||||
this string json,
|
||||
JsonSerializerOptions? options = null
|
||||
)
|
||||
{
|
||||
//
|
||||
if (string.IsNullOrWhiteSpace(json))
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
//
|
||||
// Normalize ...
|
||||
options = GetDefaultJsonSerializerOptions(options);
|
||||
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
return JsonSerializer
|
||||
.Deserialize<T>(json, options);
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
throw new InvalidOperationException($"Failed to deserialize JSON to {typeof(T).Name}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert to Json String ...
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="InvalidOperationException"></exception>
|
||||
public static string? ToJSON(
|
||||
this object? obj,
|
||||
JsonSerializerOptions? options = null
|
||||
)
|
||||
{
|
||||
//
|
||||
if (obj is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
//
|
||||
options = GetDefaultJsonSerializerOptions(options);
|
||||
try
|
||||
{
|
||||
//
|
||||
return JsonSerializer
|
||||
.Serialize(obj, options);
|
||||
}
|
||||
catch (NotSupportedException ex)
|
||||
{
|
||||
throw new InvalidOperationException($"Failed to serialize object of type {obj.GetType().Name}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert to Pretty Json String ...
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
public static string? ToPrettyJSON(
|
||||
this object? obj,
|
||||
JsonSerializerOptions? options = null
|
||||
)
|
||||
{
|
||||
//
|
||||
if (obj is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
//
|
||||
options = GetDefaultJsonSerializerOptions(options);
|
||||
options.WriteIndented = true;
|
||||
return JsonSerializer.Serialize(obj, options);
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Validate Configuration Model ...
|
||||
/// </summary>
|
||||
@@ -20,5 +116,49 @@ namespace xApi.Extensions
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validate Configuration Model ...
|
||||
/// </summary>
|
||||
/// <param name="configuration"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsValid(this AzureServiceConfiguration? configuration)
|
||||
{
|
||||
//
|
||||
var result = configuration != null
|
||||
&& !string.IsNullOrWhiteSpace(configuration.Pat)
|
||||
&& !string.IsNullOrWhiteSpace(configuration.BaseUrl);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
#region Private ...
|
||||
/// <summary>
|
||||
/// Retrieve Default JsonSerializerOptions ...
|
||||
/// </summary>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
private static JsonSerializerOptions GetDefaultJsonSerializerOptions(
|
||||
JsonSerializerOptions? options = null
|
||||
)
|
||||
{
|
||||
//
|
||||
if (options is null)
|
||||
{
|
||||
options = new()
|
||||
{
|
||||
WriteIndented = false,
|
||||
PropertyNameCaseInsensitive = true,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
||||
};
|
||||
}
|
||||
|
||||
//
|
||||
return options;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user