Last works ...
This commit is contained in:
@@ -0,0 +1,13 @@
|
|||||||
|
namespace xApi.Constants
|
||||||
|
{
|
||||||
|
public struct AzureEndpoints
|
||||||
|
{
|
||||||
|
//
|
||||||
|
public const string ConnectionDataEndpoint = "/_apis/connectionData";
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Projects ...
|
||||||
|
public const string ProjectsEndpoint = "/_apis/projects";
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,5 +3,6 @@ namespace xApi.Constants
|
|||||||
public partial struct ConfigurationNodeNames
|
public partial struct ConfigurationNodeNames
|
||||||
{
|
{
|
||||||
public const string ApiConfigurationNodeName = "ApiConfiguration";
|
public const string ApiConfigurationNodeName = "ApiConfiguration";
|
||||||
|
public const string AzureServiceConfigurationNodeName = "AzureServiceConfiguration";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,5 +5,6 @@ namespace xApi.Constants
|
|||||||
//
|
//
|
||||||
// Common Params ...
|
// Common Params ...
|
||||||
public const string ApiVersionParam = "version";
|
public const string ApiVersionParam = "version";
|
||||||
|
public const string AzureHttpClient = "AzureHttpClient";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
using xApi.Interfaces;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace xApi.Controllers
|
||||||
|
{
|
||||||
|
[ApiController]
|
||||||
|
[ApiVersion("1.0")]
|
||||||
|
[Route("api/v{version:apiVersion}/[controller]")]
|
||||||
|
public class AzureController : ControllerBase
|
||||||
|
{
|
||||||
|
//
|
||||||
|
private readonly IAzureServices azureServices;
|
||||||
|
private readonly ILogger<AzureController> logger;
|
||||||
|
|
||||||
|
public AzureController(
|
||||||
|
IAzureServices azureServices,
|
||||||
|
ILogger<AzureController> logger
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
this.logger = logger;
|
||||||
|
this.azureServices = azureServices;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get connection data from Azure Services ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet("ConnectionData")]
|
||||||
|
public async Task<ActionResult<string>> GetConnectionData(
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var response = await azureServices
|
||||||
|
.GetConnectionDataAsync(cancellationToken);
|
||||||
|
|
||||||
|
//
|
||||||
|
return Ok(response);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
logger.LogError(ex, "Error occurred ...");
|
||||||
|
return StatusCode(500, "Internal server error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get projects from Azure Services ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet("GetProjects")]
|
||||||
|
public async Task<ActionResult<string>> GetProjects(
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var response = await azureServices
|
||||||
|
.GetProjectsAsync(cancellationToken);
|
||||||
|
|
||||||
|
//
|
||||||
|
return Ok(response);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
logger.LogError(ex, "Error occurred ...");
|
||||||
|
return StatusCode(500, "Internal server error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,9 @@
|
|||||||
using xApi.Models;
|
using xApi.Models;
|
||||||
using xApi.Constants;
|
using xApi.Constants;
|
||||||
|
using xApi.Providers;
|
||||||
|
using xApi.Interfaces;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace xApi.Extensions
|
namespace xApi.Extensions
|
||||||
{
|
{
|
||||||
@@ -34,6 +38,139 @@ namespace xApi.Extensions
|
|||||||
return result;
|
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>
|
/// <summary>
|
||||||
/// Inject Specific Registered Service from IServiceCollection
|
/// Inject Specific Registered Service from IServiceCollection
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -1,9 +1,105 @@
|
|||||||
using xApi.Models;
|
using xApi.Models;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace xApi.Extensions
|
namespace xApi.Extensions
|
||||||
{
|
{
|
||||||
public static class ModelExtensions
|
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>
|
/// <summary>
|
||||||
/// Validate Configuration Model ...
|
/// Validate Configuration Model ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -20,5 +116,49 @@ namespace xApi.Extensions
|
|||||||
//
|
//
|
||||||
return result;
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
using xApi.Models;
|
||||||
|
|
||||||
|
namespace xApi.Interfaces
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Implementing Azure Services Interfaces ...
|
||||||
|
/// </summary>
|
||||||
|
public interface IAzureServices
|
||||||
|
{
|
||||||
|
Task<AzureConnectionDataDto> GetConnectionDataAsync(
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
|
||||||
|
Task<string> GetProjectsAsync(
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace xApi.Models
|
||||||
|
{
|
||||||
|
public class AzureConnectionDataDto
|
||||||
|
{
|
||||||
|
[JsonPropertyName("instanceId")]
|
||||||
|
public string? InstanceId { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("deploymentId")]
|
||||||
|
public string? DeploymentId { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("deploymentType")]
|
||||||
|
public string? DeploymentType { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("authorizedUser")]
|
||||||
|
public AzureUserDetailsDto? AuthorizedUser { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("authenticatedUser")]
|
||||||
|
public AzureUserDetailsDto? AuthenticatedUser { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("locationServiceData")]
|
||||||
|
public AzureLocationServiceDto? LocationServiceData { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class AzurePropertyValueDto
|
||||||
|
{
|
||||||
|
[JsonPropertyName("$type")]
|
||||||
|
public string? Type { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("$value")]
|
||||||
|
public string? Value { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class AzureUserDetailsDto
|
||||||
|
{
|
||||||
|
[JsonPropertyName("id")]
|
||||||
|
public string? Id { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("isActive")]
|
||||||
|
public bool IsActive { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("metaTypeId")]
|
||||||
|
public int MetaTypeId { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("descriptor")]
|
||||||
|
public string? Descriptor { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("resourceVersion")]
|
||||||
|
public int ResourceVersion { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("subjectDescriptor")]
|
||||||
|
public string? SubjectDescriptor { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("providerDisplayName")]
|
||||||
|
public string? ProviderDisplayName { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("properties")]
|
||||||
|
public Dictionary<string, AzurePropertyValueDto>? Properties { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class AzureLocationServiceDto
|
||||||
|
{
|
||||||
|
[JsonPropertyName("lastChangeId")]
|
||||||
|
public int LastChangeId { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("lastChangeId64")]
|
||||||
|
public long LastChangeId64 { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("clientCacheTimeToLive")]
|
||||||
|
public int ClientCacheTimeToLive { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("defaultAccessMappingMoniker")]
|
||||||
|
public string? DefaultAccessMappingMoniker { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace xApi.Models
|
||||||
|
{
|
||||||
|
public class AzureServiceConfiguration
|
||||||
|
{
|
||||||
|
public string? Pat { get; set; }
|
||||||
|
public string? BaseUrl { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
+25
-2
@@ -3,6 +3,7 @@ using xApi.Attributes;
|
|||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
//
|
//
|
||||||
// Access Application Builder ...
|
// Access Application Builder ...
|
||||||
@@ -18,7 +19,7 @@ var xmlFilePath = Path.Combine(AppContext.BaseDirectory, xmlFilename);
|
|||||||
builder.Services.AddEndpointsApiExplorer();
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
|
|
||||||
//
|
//
|
||||||
// Register Swagger Generator ...
|
// Extract Api Configuration ...
|
||||||
var appConfiguration = builder.Configuration
|
var appConfiguration = builder.Configuration
|
||||||
.GetApiConfiguration() ??
|
.GetApiConfiguration() ??
|
||||||
throw new InvalidDataException("Api Configuration is not Found ...");
|
throw new InvalidDataException("Api Configuration is not Found ...");
|
||||||
@@ -26,9 +27,31 @@ if (!appConfiguration.IsValid())
|
|||||||
{
|
{
|
||||||
throw new InvalidDataException("Api Configuration is not Valid ...");
|
throw new InvalidDataException("Api Configuration is not Valid ...");
|
||||||
}
|
}
|
||||||
|
builder.Services.AddApiConfiguration(appConfiguration);
|
||||||
|
|
||||||
//
|
//
|
||||||
builder.Services.AddSingleton(appConfiguration);
|
// Register Azure Service ...
|
||||||
|
builder.Services.AddAzureApiServices(builder.Configuration);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Register Api Versioning ...
|
||||||
|
builder.Services.AddApiVersioning(opt =>
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Set Default Api Version ...
|
||||||
|
opt.DefaultApiVersion = new ApiVersion(1, 0);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Set Routing to Default API Version, if Version unspecified ...
|
||||||
|
opt.AssumeDefaultVersionWhenUnspecified = true;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Report All Available Api Versions on Response ...
|
||||||
|
opt.ReportApiVersions = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
//
|
||||||
|
// Register Swagger Generator ...
|
||||||
builder.Services.AddSwaggerGen(delegate (SwaggerGenOptions options)
|
builder.Services.AddSwaggerGen(delegate (SwaggerGenOptions options)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -0,0 +1,250 @@
|
|||||||
|
using xApi.Models;
|
||||||
|
using xApi.Constants;
|
||||||
|
using xApi.Interfaces;
|
||||||
|
using xApi.Extensions;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
|
||||||
|
namespace xApi.Providers
|
||||||
|
{
|
||||||
|
public class AzureServices : IAzureServices
|
||||||
|
{
|
||||||
|
//
|
||||||
|
private readonly ILogger<AzureServices> logger;
|
||||||
|
private readonly IHttpClientFactory httpClientFactory;
|
||||||
|
private readonly AzureServiceConfiguration configuration;
|
||||||
|
|
||||||
|
public AzureServices(
|
||||||
|
ILogger<AzureServices> logger,
|
||||||
|
IHttpClientFactory httpClientFactory,
|
||||||
|
AzureServiceConfiguration configuration
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
this.logger = logger;
|
||||||
|
this.configuration = configuration;
|
||||||
|
this.httpClientFactory = httpClientFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve Connection Info ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="InvalidOperationException"></exception>
|
||||||
|
public async Task<AzureConnectionDataDto> GetConnectionDataAsync(
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var json = await GetFromAzureAsync(
|
||||||
|
AzureEndpoints.ConnectionDataEndpoint,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = json.ParseJSON<AzureConnectionDataDto>();
|
||||||
|
if (result is null)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var message = "Failed to parse Azure connection data ...";
|
||||||
|
|
||||||
|
//
|
||||||
|
logger.LogError(message);
|
||||||
|
throw new InvalidOperationException(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<string> GetProjectsAsync(
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var json = await GetFromAzureAsync(
|
||||||
|
AzureEndpoints.ProjectsEndpoint,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// var result = json.ParseJSON<AzureConnectionDataDto>();
|
||||||
|
// if (result is null)
|
||||||
|
// {
|
||||||
|
// //
|
||||||
|
// var message = "Failed to parse Azure connection data ...";
|
||||||
|
|
||||||
|
// //
|
||||||
|
// logger.LogError(message);
|
||||||
|
// throw new InvalidOperationException(message);
|
||||||
|
// }
|
||||||
|
|
||||||
|
//
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Private ...
|
||||||
|
private async Task<string> HandleAzureRequest(
|
||||||
|
string path,
|
||||||
|
HttpMethod method,
|
||||||
|
IDictionary<string, string>? headers = null,
|
||||||
|
IDictionary<string, string>? queryParams = null,
|
||||||
|
IDictionary<string, string>? routeParams = null,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
var pat = configuration.Pat;
|
||||||
|
if (string.IsNullOrWhiteSpace(pat))
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var message = "Azure PAT is not configured ...";
|
||||||
|
logger.LogError(message);
|
||||||
|
throw new InvalidOperationException(message);
|
||||||
|
}
|
||||||
|
if (string.IsNullOrWhiteSpace(path))
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var message = "Request Path is not configured ...";
|
||||||
|
logger.LogError(message);
|
||||||
|
throw new InvalidOperationException(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Create Http Client ...
|
||||||
|
using (HttpClient client = new())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Add Accept ...
|
||||||
|
client.DefaultRequestHeaders.Accept.Add(
|
||||||
|
new MediaTypeWithQualityHeaderValue("application/json"));
|
||||||
|
|
||||||
|
//
|
||||||
|
// Add Personal Access Token ...
|
||||||
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
|
||||||
|
Convert.ToBase64String(
|
||||||
|
System.Text.Encoding.ASCII.GetBytes(
|
||||||
|
string.Format("{0}:{1}", "", pat))));
|
||||||
|
|
||||||
|
//
|
||||||
|
// Preparing Request Path ...
|
||||||
|
var url = configuration.BaseUrl;
|
||||||
|
if (!url!.EndsWith('/'))
|
||||||
|
{
|
||||||
|
url += "/";
|
||||||
|
}
|
||||||
|
if (path.StartsWith('/'))
|
||||||
|
{
|
||||||
|
path = path[1..];
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Handle Route Params ...
|
||||||
|
if (routeParams is not null &&
|
||||||
|
routeParams.Any())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var exists =
|
||||||
|
routeParams.Where(rp => path.Contains(rp.Key));
|
||||||
|
foreach (var irp in exists)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Finalize Url ...
|
||||||
|
url = $"{url}{path}";
|
||||||
|
|
||||||
|
//
|
||||||
|
// Preparing Request ...
|
||||||
|
var request = new HttpRequestMessage(
|
||||||
|
method: method,
|
||||||
|
requestUri: url
|
||||||
|
);
|
||||||
|
HttpResponseMessage? response = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<string> GetFromAzureAsync(
|
||||||
|
string relativePath,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate PAT ...
|
||||||
|
var pat = configuration.Pat;
|
||||||
|
if (string.IsNullOrWhiteSpace(pat))
|
||||||
|
{
|
||||||
|
//
|
||||||
|
logger.LogError("Azure PAT is not configured ...");
|
||||||
|
throw new InvalidOperationException("Azure PAT is not configured ...");
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Create Specified Http Client ...
|
||||||
|
var client = httpClientFactory.CreateClient(Params.AzureHttpClient);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Url ...
|
||||||
|
var url = $"{client.BaseAddress}{relativePath}";
|
||||||
|
logger.LogInformation("Url: {url}", url);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Handle Request and Response ...
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
using var cts = CancellationTokenSource
|
||||||
|
.CreateLinkedTokenSource(cancellationToken);
|
||||||
|
cts.CancelAfter(TimeSpan.FromSeconds(20));
|
||||||
|
|
||||||
|
//
|
||||||
|
var response = await client.GetAsync(relativePath, cts.Token);
|
||||||
|
if (!response.IsSuccessStatusCode)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var content = await response.Content.ReadAsStringAsync(cts.Token);
|
||||||
|
logger.LogError("Azure returned HTTP {StatusCode}. Content: {Content} ...", response.StatusCode, content);
|
||||||
|
throw new HttpRequestException($"Azure returned HTTP {(int)response.StatusCode} ...");
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Reading Content ...
|
||||||
|
var json = await response.Content.ReadAsStringAsync(cts.Token);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validating JSON ...
|
||||||
|
try
|
||||||
|
{
|
||||||
|
JsonDocument.Parse(json);
|
||||||
|
}
|
||||||
|
catch (JsonException ex)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
logger.LogError(ex, "Azure returned non-JSON data. Path: {Path} ...", relativePath);
|
||||||
|
throw new InvalidDataException("Azure returned non-JSON data ...");
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
catch (TaskCanceledException ex) when (ex.InnerException is TimeoutException || !cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
logger.LogError(ex, "Azure request timed out. Path: {Path} ...", relativePath);
|
||||||
|
throw new TimeoutException("Azure request timed out ...");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
logger.LogError(ex, "Could not connect to Azure. Path: {Path} ...", relativePath);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,10 @@
|
|||||||
"Email": "hkhazaeeasl@rpk.ir",
|
"Email": "hkhazaeeasl@rpk.ir",
|
||||||
"Url": "https://rpkavosh.ir"
|
"Url": "https://rpkavosh.ir"
|
||||||
},
|
},
|
||||||
|
"AzureServiceConfiguration": {
|
||||||
|
"Pat": "6zvwbdviuswlets7ovugq7nningtx4v6gqll7qkyqhnn75nrxh3q",
|
||||||
|
"BaseUrl": "http://192.168.10.22:8080/tfs/RPKavoshDevOps"
|
||||||
|
},
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
|
|||||||
Reference in New Issue
Block a user