-
Notifications
You must be signed in to change notification settings - Fork 250
/
Copy pathActuatorApiClient.cs
39 lines (31 loc) · 1.36 KB
/
ActuatorApiClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using Microsoft.AspNetCore.Http.Extensions;
using Steeltoe.Samples.ActuatorWeb.Pages;
namespace Steeltoe.Samples.ActuatorWeb;
public sealed class ActuatorApiClient(HttpClient httpClient)
{
public async Task<List<WeatherForecast>> GetWeatherForecastsAsync(string? fromDate, int? days, CancellationToken cancellationToken)
{
string requestUri = BuildWeatherForecastRequestUri(fromDate, days);
HttpResponseMessage response = await httpClient.GetAsync(requestUri, cancellationToken);
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadFromJsonAsync<List<WeatherForecast>>(cancellationToken) ?? [];
}
string responseBody = await response.Content.ReadAsStringAsync(cancellationToken);
string errorMessage = $"Failed to retrieve weather forecasts. Server response HTTP {(int)response.StatusCode}:{Environment.NewLine}{responseBody}";
throw new HttpRequestException(errorMessage);
}
private static string BuildWeatherForecastRequestUri(string? fromDate, int? days)
{
var builder = new QueryBuilder();
if (fromDate != null)
{
builder.Add("fromDate", fromDate);
}
if (days != null)
{
builder.Add("days", days.Value.ToString());
}
return $"weatherForecast{builder.ToQueryString()}";
}
}