Skip to content

Commit 3b719d2

Browse files
committed
Add the initial code from local
1 parent 78c9c07 commit 3b719d2

File tree

39 files changed

+530
-52
lines changed

39 files changed

+530
-52
lines changed

nuget.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<packageSources>
4+
<add key="local-nuget" value="c:/local-nuget"/>
5+
</packageSources>
6+
</configuration>

src/AStar.Dev.Admin.Api.Client.Sdk/AStar.Dev.Admin.Api.Client.Sdk.csproj

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<Authors>AStar Developement, Jason Barden</Authors>
66
<Company>AStar Development</Company>
77
<Copyright>AStar Developement, 2025</Copyright>
8-
<Description>Update.</Description>
8+
<Description>This package contains the currently supported methods for interacting with the Admin API whilst abstracting the details..</Description>
99
<DocumentationFile>$(AssemblyName).xml</DocumentationFile>
1010
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
1111
<GenerateDocumentationFile>True</GenerateDocumentationFile>
@@ -24,27 +24,32 @@
2424
<RepositoryUrl>https://github.com/astar-development/</RepositoryUrl>
2525
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
2626
<TargetFramework>net9.0</TargetFramework>
27-
<Title>AStar.Dev.Update</Title>
27+
<Title>AStar.Dev.Admin.Api.Client.Sdk</Title>
2828
<UserSecretsId>3a340cb0-60ee-4e38-93ac-0f8829c7193d</UserSecretsId>
2929
<Version>0.1.0</Version>
3030
</PropertyGroup>
3131

3232
<ItemGroup>
3333
<None Include="LICENSE" Pack="true" PackagePath=""/>
3434
<None Include="Readme.md" Pack="true" PackagePath=""/>
35+
<None Include="..\..\AStar.png" Pack="true" PackagePath=""/>
3536
</ItemGroup>
3637

3738
<ItemGroup>
3839
<PackageReference Include="AStar.Dev.Api.Client.Sdk.Shared" Version="0.1.0"/>
39-
<PackageReference Include="AStar.Dev.Api.HealthChecks" Version="0.3.1"/>
40-
<PackageReference Include="AStar.Dev.Utilities" Version="1.6.0"/>
41-
<PackageReference Include="Microsoft.Identity.Web" Version="3.8.3"/>
42-
<PackageReference Include="Microsoft.Identity.Web.DownstreamApi" Version="3.8.3"/>
43-
<PackageReference Include="Microsoft.Identity.Web.UI" Version="3.8.3"/>
40+
<PackageReference Include="AStar.Dev.Api.HealthChecks" Version="0.4.0"/>
41+
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions" Version="9.0.4"/>
42+
<PackageReference Include="AStar.Dev.Functional.Extensions" Version="0.2.0"/>
43+
<PackageReference Include="AStar.Dev.Logging.Extensions" Version="0.5.1" />
44+
<PackageReference Include="AStar.Dev.Utilities" Version="1.6.1"/>
45+
<PackageReference Include="AStar.Dev.Technical.Debt.Reporting" Version="0.1.0"/>
46+
<PackageReference Include="Microsoft.Identity.Web" Version="3.8.4"/>
47+
<PackageReference Include="Microsoft.Identity.Web.DownstreamApi" Version="3.8.4"/>
48+
<PackageReference Include="Microsoft.Identity.Web.UI" Version="3.8.4"/>
4449
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.4"/>
4550
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.4"/>
4651
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="9.4.0"/>
47-
<PackageReference Include="Microsoft.Identity.Web.TokenAcquisition" Version="3.8.3"/>
52+
<PackageReference Include="Microsoft.Identity.Web.TokenAcquisition" Version="3.8.4"/>
4853
</ItemGroup>
4954

5055
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

src/AStar.Dev.Admin.Api.Client.Sdk/AStar.Dev.Admin.Api.Client.Sdk.xml

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
-12.2 KB
Binary file not shown.

src/AStar.Dev.Admin.Api.Client.Sdk/AdminApi/AdminApiClient.cs

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,112 @@
22
using System.Text.Json;
33
using AStar.Dev.Admin.Api.Client.Sdk.Models;
44
using AStar.Dev.Api.HealthChecks;
5-
using Microsoft.Extensions.Logging;
5+
using AStar.Dev.Functional.Extensions;
6+
using AStar.Dev.Logging.Extensions;
67
using Microsoft.Identity.Web;
78

89
namespace AStar.Dev.Admin.Api.Client.Sdk.AdminApi;
910

1011
/// <summary>
1112
/// The <see href="AdminApiClient"></see> class.
1213
/// </summary>
13-
public sealed class AdminApiClient(HttpClient httpClient, ITokenAcquisition tokenAcquisitionService, ILogger<AdminApiClient> logger) : IApiClient
14+
public sealed class AdminApiClient(HttpClient httpClient, ITokenAcquisition tokenAcquisitionService, ILoggerAstar<AdminApiClient> logger) : IApiClient
1415
{
1516
private static readonly JsonSerializerOptions JsonSerializerOptions = new(JsonSerializerDefaults.Web);
1617

1718
/// <inheritdoc />
1819
public async Task<HealthStatusResponse> GetHealthAsync(CancellationToken cancellationToken = default)
1920
{
20-
logger.LogInformation("Checking the {ApiName} Health Status.", Constants.ApiName);
21+
logger.LogHealthCheckStart(Constants.ApiName);
2122

2223
try
2324
{
24-
logger.LogInformation("Checking the {ApiName} Health Status.", Constants.ApiName);
25+
var response = await httpClient.GetAsync("/health/ready", cancellationToken);
2526

26-
HttpResponseMessage response = await httpClient.GetAsync("/health/ready", cancellationToken);
27+
return response.IsSuccessStatusCode
28+
? (await JsonSerializer.DeserializeAsync<HealthStatusResponse>(await response.Content.ReadAsStreamAsync(cancellationToken), JsonSerializerOptions, cancellationToken))!
29+
: ReturnLoggedFailure(response);
30+
}
31+
catch (HttpRequestException ex)
32+
{
33+
logger.LogException(ex);
34+
35+
return new() { Status = $"Could not get a response from the {Constants.ApiName}." };
36+
}
37+
}
38+
39+
/// <inheritdoc />
40+
public async Task<Result<string, HealthStatusResponse>> GetHealthCheckAsync(CancellationToken cancellationToken = new ())
41+
{
42+
logger.LogHealthCheckStart(Constants.ApiName);
43+
44+
try
45+
{
46+
var response = await httpClient.GetAsync("/health/ready", cancellationToken);
2747

2848
return response.IsSuccessStatusCode
2949
? (await JsonSerializer.DeserializeAsync<HealthStatusResponse>(await response.Content.ReadAsStreamAsync(cancellationToken), JsonSerializerOptions, cancellationToken))!
3050
: ReturnLoggedFailure(response);
3151
}
3252
catch (HttpRequestException ex)
3353
{
34-
logger.LogError(500, ex, "Error: {ErrorMessage}", ex.Message);
54+
logger.LogException(ex);
3555

36-
return new() { Status = $"Could not get a response from the {Constants.ApiName}.", };
56+
return new HealthStatusResponse { Status = $"Could not get a response from the {Constants.ApiName}." };
3757
}
3858
}
3959

4060
/// <summary>
4161
/// The GetSiteConfigurationAsync method will get the User Configuration.
4262
/// </summary>
4363
/// <returns>The Site Configuration - populated or empty</returns>
44-
public async Task<IEnumerable<SiteConfiguration>> GetSiteConfigurationAsync()
64+
public async Task<Result<string, IEnumerable<SiteConfiguration>>> GetSiteConfigurationAsync()
4565
{
46-
string token = await tokenAcquisitionService.GetAccessTokenForUserAsync(["api://2ca26585-5929-4aae-86a7-a00c3fc2d061/ToDoList.Write",]);
66+
var token = await tokenAcquisitionService.GetAccessTokenForUserAsync(["api://2ca26585-5929-4aae-86a7-a00c3fc2d061/ToDoList.Write"]);
4767

48-
// logger.LogDebug("Token: {Token}", token);
49-
httpClient.DefaultRequestHeaders.Authorization = new("Bearer", token);
50-
HttpResponseMessage response = await httpClient.GetAsync("site-configurations?version=1.0");
68+
httpClient.AddBearerToken(token);
69+
var response = await GetSafelyAsync<IEnumerable<SiteConfiguration>>("site-configurations?version=1.0");
5170

52-
return (await response.Content.ReadFromJsonAsync<IEnumerable<SiteConfiguration>>())!;
71+
return response.IsSuccess
72+
? response
73+
: ReturnLoggedFailure("GetSiteConfiguration", response.Error);
74+
}
75+
76+
private async Task<Result<string, TResponse>> GetSafelyAsync<TResponse>( string uri)
77+
{
78+
try
79+
{
80+
var response = await httpClient.GetAsync(uri);
81+
82+
if(response.IsSuccessStatusCode)
83+
{
84+
return (await response.Content.ReadFromJsonAsync<TResponse>())!;
85+
}
86+
87+
logger.LogApiCallFailed("AStar.Dev.Admin.Api", uri, $"StatusCode: {response.StatusCode}, ResponseCode: {response.ReasonPhrase}");
88+
89+
return Result<string, TResponse>.Failure($"Call to {uri} failed with {(response.ReasonPhrase ?? response?.ReasonPhrase)}")!;
90+
}
91+
catch(Exception ex)
92+
{
93+
logger.LogException(ex);
94+
95+
return Result<string, TResponse>.Failure(ex.Message)!;
96+
}
5397
}
5498

5599
private HealthStatusResponse ReturnLoggedFailure(HttpResponseMessage response)
56100
{
57-
logger.LogInformation("The {ApiName} Health failed - {FailureReason}.", Constants.ApiName, response.ReasonPhrase);
101+
logger.LogHealthCheckFailure(Constants.ApiName, response.ReasonPhrase ?? "Failure reason not known");
102+
103+
return new() { Status = $"Health Check failed - {response.ReasonPhrase}." };
104+
}
105+
106+
private string ReturnLoggedFailure(string endpointName, string? reasonPhrase)
107+
{
108+
logger.LogApiCallFailed(Constants.ApiName, endpointName,reasonPhrase ?? "Failure reason not known");
58109

59-
return new() { Status = $"Health Check failed - {response.ReasonPhrase}.", };
110+
return $"Call to {endpointName} failed - {reasonPhrase}.";
60111
}
61112

62113
//
@@ -134,4 +185,4 @@ private HealthStatusResponse ReturnLoggedFailure(HttpResponseMessage response)
134185
// .WebDeserialisationSettings)
135186
// : new UserConfiguration();
136187
// }
137-
}
188+
}

src/AStar.Dev.Admin.Api.Client.Sdk/AdminApi/AdminApiConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ public sealed class AdminApiConfiguration : IApiConfiguration
2020
/// <inheritdoc />
2121
[Required]
2222
public required string[] Scopes { get; set; }
23-
}
23+
}

src/AStar.Dev.Admin.Api.Client.Sdk/Constants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ public static class Constants
99
/// Gets the Api Name.
1010
/// </summary>
1111
public const string ApiName = "AStar.Dev.Admin.Api";
12-
}
12+
}

src/AStar.Dev.Admin.Api.Client.Sdk/Models/ModelToIgnore.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ public sealed class ModelToIgnore
1818
/// <returns>
1919
/// This object serialized as a JSON object.
2020
/// </returns>
21-
public override string ToString() =>
22-
this.ToJson();
23-
}
21+
public override string ToString()
22+
=> this.ToJson();
23+
}

src/AStar.Dev.Admin.Api.Client.Sdk/Models/ScrapeDirectories.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ public sealed class ScrapeDirectories
3434
/// Gets or sets the default subdirectory name for the save - appended to the root directory.
3535
/// </summary>
3636
public string SubDirectoryName { get; set; } = string.Empty;
37-
}
37+
}

src/AStar.Dev.Admin.Api.Client.Sdk/Models/SearchCategory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ public sealed class SearchCategory
3434
/// Gets or sets the Total Pages for the results.
3535
/// </summary>
3636
public int TotalPages { get; set; }
37-
}
37+
}

0 commit comments

Comments
 (0)