Initial commit.
This commit is contained in:
92
ApiClient/MainWindowViewModel.cs
Normal file
92
ApiClient/MainWindowViewModel.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using ClientApiPoC.Shared;
|
||||
using ClientApiPoC.Shared.Models;
|
||||
using ClientApiPoC.Shared.Wpf;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using System.Net.Http;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace ClientApiPoC.ApiClient {
|
||||
public class MainWindowViewModel : BaseViewModel {
|
||||
#region Properties
|
||||
private string _apiServiceUrl = "http://localhost:5228/";
|
||||
public string ApiServiceUrl {
|
||||
get => _apiServiceUrl;
|
||||
set {
|
||||
_apiServiceUrl = value;
|
||||
OnPropertyChanged(nameof(ApiServiceUrl));
|
||||
}
|
||||
}
|
||||
|
||||
private bool _isFetchingData = false;
|
||||
public bool IsFetchingData {
|
||||
get => _isFetchingData;
|
||||
set {
|
||||
_isFetchingData = value;
|
||||
OnPropertyChanged(nameof(IsFetchingData));
|
||||
OnPropertyChanged(nameof(ControlsEnabled));
|
||||
}
|
||||
}
|
||||
|
||||
public bool ControlsEnabled => !this.IsFetchingData;
|
||||
|
||||
private ExampleRequestResultModel? _requestResultData = null;
|
||||
public ExampleRequestResultModel? RequestResultData {
|
||||
get => _requestResultData;
|
||||
set {
|
||||
_requestResultData = value;
|
||||
OnPropertyChanged(nameof(RequestResultData));
|
||||
OnPropertyChanged(nameof(RequestResultDataString));
|
||||
}
|
||||
}
|
||||
|
||||
public string RequestResultDataString {
|
||||
get {
|
||||
if (this.RequestResultData == null) return "NULL";
|
||||
var options = new JsonSerializerOptions() {
|
||||
IndentCharacter = ' ',
|
||||
IndentSize = 2,
|
||||
NewLine = "\n",
|
||||
WriteIndented = true
|
||||
};
|
||||
return JsonSerializer.Serialize(this.RequestResultData, options);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Commands
|
||||
public ICommand HttpGetButtonCommand { get; }
|
||||
#endregion
|
||||
|
||||
public MainWindowViewModel() {
|
||||
this.HttpGetButtonCommand = new AsyncRelayCommand(HttpGetButtonActionAsync);
|
||||
}
|
||||
|
||||
private async Task HttpGetButtonActionAsync() {
|
||||
if (this.IsFetchingData) return;
|
||||
try {
|
||||
this.IsFetchingData = true;
|
||||
var url = this.ApiServiceUrl;
|
||||
if (string.IsNullOrWhiteSpace(url)) throw new InvalidOperationException("No base url specified.");
|
||||
while (url.EndsWith('/')) url = url[..^1];
|
||||
url += Routes.EXAMPLE_REQUEST;
|
||||
using (var httpClient = Tools.CreateHttpClient()) {
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, url);
|
||||
var response = await httpClient.SendAsync(request);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
var options = new JsonSerializerOptions() {
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
var result = JsonSerializer.Deserialize<ExampleRequestResultModel>(responseString, options);
|
||||
this.RequestResultData = result;
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
await HandleErrorAsync(ex);
|
||||
} finally {
|
||||
this.IsFetchingData = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user