44using AStar . Dev . Api . HealthChecks ;
55using AStar . Dev . Functional . Extensions ;
66using AStar . Dev . Logging . Extensions ;
7+ using AStar . Dev . Technical . Debt . Reporting ;
8+ using AStar . Dev . Utilities ;
79using Microsoft . Identity . Web ;
810
911namespace AStar . Dev . Admin . Api . Client . Sdk . AdminApi ;
@@ -16,7 +18,7 @@ public sealed class AdminApiClient(HttpClient httpClient, ITokenAcquisition toke
1618 private static readonly JsonSerializerOptions JsonSerializerOptions = new ( JsonSerializerDefaults . Web ) ;
1719
1820 /// <inheritdoc />
19- public async Task < HealthStatusResponse > GetHealthAsync ( CancellationToken cancellationToken = default )
21+ [ Refactor ( 1 , 1 , "Remove from the Interface" ) ] public async Task < HealthStatusResponse > GetHealthAsync ( CancellationToken cancellationToken = default )
2022 {
2123 logger . LogHealthCheckStart ( Constants . ApiName ) ;
2224
@@ -26,7 +28,7 @@ public async Task<HealthStatusResponse> GetHealthAsync(CancellationToken cancell
2628
2729 return response . IsSuccessStatusCode
2830 ? ( await JsonSerializer . DeserializeAsync < HealthStatusResponse > ( await response . Content . ReadAsStreamAsync ( cancellationToken ) , JsonSerializerOptions , cancellationToken ) ) !
29- : ReturnLoggedFailure ( response ) ;
31+ : logger . ReturnLoggedFailure ( response , Constants . ApiName ) ;
3032 }
3133 catch ( HttpRequestException ex )
3234 {
@@ -38,55 +40,60 @@ public async Task<HealthStatusResponse> GetHealthAsync(CancellationToken cancell
3840
3941 /// <inheritdoc />
4042 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 ) ;
47-
48- return response . IsSuccessStatusCode
49- ? ( await JsonSerializer . DeserializeAsync < HealthStatusResponse > ( await response . Content . ReadAsStreamAsync ( cancellationToken ) , JsonSerializerOptions , cancellationToken ) ) !
50- : ReturnLoggedFailure ( response ) ;
51- }
52- catch ( HttpRequestException ex )
53- {
54- logger . LogException ( ex ) ;
55-
56- return new HealthStatusResponse { Status = $ "Could not get a response from the { Constants . ApiName } ." } ;
57- }
58- }
43+ => await GetSafelyAsync < HealthStatusResponse > ( "/health/ready?version=1.0" ) ;
5944
6045 /// <summary>
6146 /// The GetSiteConfigurationAsync method will get the User Configuration.
6247 /// </summary>
6348 /// <returns>The Site Configuration - populated or empty</returns>
6449 public async Task < Result < string , IEnumerable < SiteConfiguration > > > GetSiteConfigurationAsync ( )
65- {
66- var token = await tokenAcquisitionService . GetAccessTokenForUserAsync ( [ "api://2ca26585-5929-4aae-86a7-a00c3fc2d061/ToDoList.Write" ] ) ;
50+ => await GetSafelyAsync < IEnumerable < SiteConfiguration > > ( "site-configurations?version=1.0" ) ;
6751
68- httpClient . AddBearerToken ( token ) ;
69- var response = await GetSafelyAsync < IEnumerable < SiteConfiguration > > ( "site-configurations?version=1.0" ) ;
52+ /// <summary>
53+ /// The GetModelsToIgnoreAsync method will get the models to ignore.
54+ /// </summary>
55+ /// <returns>A collection of 0 or more models to ignore</returns>
56+ public async Task < Result < string , IEnumerable < ModelToIgnore > > > GetModelsToIgnoreAsync ( )
57+ => await GetSafelyAsync < IEnumerable < ModelToIgnore > > ( "models-to-ignore?version=1" ) ;
7058
71- return response . IsSuccess
72- ? response
73- : ReturnLoggedFailure ( "GetSiteConfiguration" , response . Error ) ;
74- }
59+ /// <summary>
60+ /// The GetScrapeDirectoriesAsync method will get the Scrape Directories.
61+ /// </summary>
62+ /// <returns>The Scrape Directories - populated or empty</returns>
63+ public async Task < Result < string , ScrapeDirectories > > GetScrapeDirectoriesAsync ( )
64+ => await GetSafelyAsync < ScrapeDirectories > ( "scrape-directories?version=1" ) ;
65+
66+ /// <summary>
67+ /// The GetSearchConfigurationAsync method will get the Search Configuration.
68+ /// </summary>
69+ /// <returns>The Search Configuration - populated or empty</returns>
70+ public async Task < Result < string , SearchConfiguration > > GetSearchConfigurationAsync ( )
71+ => await GetSafelyAsync < SearchConfiguration > ( "search-configuration?version=1" ) ;
72+
73+ /// <summary>
74+ /// The GetTagsToIgnoreAsync method will get the Tags to Ignore.
75+ /// </summary>
76+ /// <returns>A collection of 0 or more Tags to Ignore</returns>
77+ public async Task < Result < string , IEnumerable < TagToIgnore > > > GetTagsToIgnoreAsync ( )
78+ => await GetSafelyAsync < IEnumerable < TagToIgnore > > ( "search-configuration?version=1" ) ;
7579
7680 private async Task < Result < string , TResponse > > GetSafelyAsync < TResponse > ( string uri )
7781 {
7882 try
7983 {
84+ logger . LogApiCallStart ( Constants . ApiName , uri ) ;
85+ var token = await tokenAcquisitionService . GetAccessTokenForUserAsync ( [ "api://2ca26585-5929-4aae-86a7-a00c3fc2d061/ToDoList.Write" ] ) ;
86+
87+ _ = httpClient . AddBearerToken ( token ) ;
8088 var response = await httpClient . GetAsync ( uri ) ;
8189
82- if ( response . IsSuccessStatusCode )
90+ if ( ! response . IsSuccessStatusCode )
8391 {
84- return ( await response . Content . ReadFromJsonAsync < TResponse > ( ) ) ! ;
92+ return logger . ReturnLoggedFailure < TResponse > ( Constants . ApiName , uri , response . ReasonPhrase ?? response . StatusCode . ToString ( ) ) ;
8593 }
8694
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 ) } ") ! ;
95+ var result = ( await response . Content . ReadFromJsonAsync < TResponse > ( Utilities . Constants . WebDeserialisationSettings ) ) ! ;
96+ return logger . ReturnLoggedSuccess ( result , Constants . ApiName , "uri" ) ;
9097 }
9198 catch ( Exception ex )
9299 {
@@ -95,94 +102,4 @@ private async Task<Result<string, TResponse>> GetSafelyAsync<TResponse>( string
95102 return Result < string , TResponse > . Failure ( ex . Message ) ! ;
96103 }
97104 }
98-
99- private HealthStatusResponse ReturnLoggedFailure ( HttpResponseMessage response )
100- {
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" ) ;
109-
110- return $ "Call to { endpointName } failed - { reasonPhrase } .";
111- }
112-
113- //
114- // /// <summary>
115- // /// The GetModelsToIgnoreAsync method will get the models to ignore.
116- // /// </summary>
117- // /// <returns>A collection of 0 or more models to ignore</returns>
118- // public async Task<IEnumerable<ModelToIgnore>> GetModelsToIgnoreAsync()
119- // {
120- // logger.LogInformation("Getting the models-to-ignore.");
121- // var response = await httpClient.GetAsync("models-to-ignore?version=1");
122- //
123- // return response.IsSuccessStatusCode
124- // ? (await response.Content.ReadAsStringAsync()).FromJson<IEnumerable<ModelToIgnore>>(Utilities.Constants
125- // .WebDeserialisationSettings)
126- // : [];
127- // }
128- //
129- // /// <summary>
130- // /// The GetScrapeDirectoriesAsync method will get the Scrape Directories.
131- // /// </summary>
132- // /// <returns>The Scrape Directories - populated or empty</returns>
133- // public async Task<ScrapeDirectories> GetScrapeDirectoriesAsync()
134- // {
135- // logger.LogInformation("Getting the scrape-directories.");
136- // var response = await httpClient.GetAsync("scrape-directories?version=1");
137- //
138- // return response.IsSuccessStatusCode
139- // ? (await response.Content.ReadAsStringAsync()).FromJson<ScrapeDirectories>(Utilities.Constants
140- // .WebDeserialisationSettings)
141- // : new ScrapeDirectories();
142- // }
143- //
144- // /// <summary>
145- // /// The GetSearchConfigurationAsync method will get the Search Configuration.
146- // /// </summary>
147- // /// <returns>The Search Configuration - populated or empty</returns>
148- // public async Task<SearchConfiguration> GetSearchConfigurationAsync()
149- // {
150- // logger.LogInformation("Getting the search-configuration.");
151- // var response = await httpClient.GetAsync("search-configuration?version=1");
152- //
153- // return response.IsSuccessStatusCode
154- // ? (await response.Content.ReadAsStringAsync()).FromJson<SearchConfiguration>(Utilities.Constants
155- // .WebDeserialisationSettings)
156- // : new SearchConfiguration();
157- // }
158- //
159- // /// <summary>
160- // /// The GetTagsToIgnoreAsync method will get the Tags to Ignore.
161- // /// </summary>
162- // /// <returns>A collection of 0 or more Tags to Ignore</returns>
163- // public async Task<IEnumerable<TagToIgnore>> GetTagsToIgnoreAsync()
164- // {
165- // logger.LogInformation("Getting the Tags-to-ignore.");
166- // var response = await httpClient.GetAsync("tags-to-ignore?version=1");
167- //
168- // return response.IsSuccessStatusCode
169- // ? (await response.Content.ReadAsStringAsync()).FromJson<IEnumerable<TagToIgnore>>(Utilities.Constants
170- // .WebDeserialisationSettings)
171- // : [];
172- // }
173- //
174- // /// <summary>
175- // /// The GetUserConfigurationAsync method will get the User Configuration.
176- // /// </summary>
177- // /// <returns>The User Configuration - populated or empty</returns>
178- // public async Task<UserConfiguration> GetUserConfigurationAsync()
179- // {
180- // logger.LogInformation("Getting the User Configuration.");
181- // var response = await httpClient.GetAsync("user-configuration?version=1");
182- //
183- // return response.IsSuccessStatusCode
184- // ? (await response.Content.ReadAsStringAsync()).FromJson<UserConfiguration>(Utilities.Constants
185- // .WebDeserialisationSettings)
186- // : new UserConfiguration();
187- // }
188105}
0 commit comments