Add API key management. #10
No files matched your search
@@ -43,6 +43,7 @@ public class CoinDatabase {
|
||||
/// </summary>
|
||||
/// <returns>True if the API key was found, false otherwise.</returns>
|
||||
public bool TryInitApiClient() {
|
||||
Console.Write("Attempting to initialize API client... ");
|
||||
SQLiteCommand cmd = Connection.CreateCommand();
|
||||
cmd.CommandText = "SELECT API_KEY FROM ApiTable";
|
||||
|
||||
@@ -56,14 +57,28 @@ public class CoinDatabase {
|
||||
insertCmd.ExecuteNonQuery();
|
||||
|
||||
IsClientConnected = true;
|
||||
Console.WriteLine("Done!");
|
||||
}
|
||||
else {
|
||||
IsClientConnected = false;
|
||||
Console.WriteLine("Failed. API key not found.");
|
||||
}
|
||||
}
|
||||
return IsClientConnected;
|
||||
}
|
||||
|
||||
public bool TryInitApiClient(string apiKey) {
|
||||
Console.Write("Attempting to initialize API client with key param... ");
|
||||
_pcgsClient = new PcgsClient(apiKey);
|
||||
// TODO Check if the API key is valid and if it is, then add to the database.
|
||||
IsClientConnected = true;
|
||||
var cmd = Connection.CreateCommand();
|
||||
cmd.CommandText = $"INSERT INTO ApiTable (API_KEY, REQUESTS_REMAINING) VALUES ({apiKey}, 1000)";
|
||||
// cmd.ExecuteNonQuery();
|
||||
Console.WriteLine("Done!");
|
||||
return IsClientConnected;
|
||||
}
|
||||
|
||||
private SQLiteConnection CreateConnection() {
|
||||
// Check if database exists.
|
||||
if (!System.IO.File.Exists("coin_collection")) {
|
||||
|
||||
@@ -31,7 +31,6 @@ public class PcgsClient : ReactiveObject {
|
||||
}
|
||||
|
||||
public async Task<(ErrorType, Coin?)> GetCoinFactsByGrade(int pcgsNumber, string grade, bool plusGrade = false) {
|
||||
Coin newCoin = null;
|
||||
using (HttpClient client = new HttpClient()) {
|
||||
var gradeParam = GetGradeFromString(grade);
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using ReactiveUI;
|
||||
using System.Reactive;
|
||||
using System;
|
||||
|
||||
namespace PcgsInvUi.ViewModels;
|
||||
|
||||
|
||||
@@ -39,23 +39,22 @@ public class MainWindowViewModel : ViewModelBase {
|
||||
public MainWindowViewModel(CoinDatabase coins, bool askForApiKey) {
|
||||
ConnectedCoinDatabase = coins;
|
||||
var newViewModel = new NewViewModel();
|
||||
var apiKeyViewModel = new ApiKeyViewModel();
|
||||
// DisplayedList = CoinCollection = new ObservableCollection<Coin>(coins.GetItems());
|
||||
DisplayedList = ConnectedCoinDatabase.Collection;
|
||||
TotalValue = DisplayedList.Sum(x => x.TotalPrice);
|
||||
|
||||
// Setup interaction to show ApiKeyWindow
|
||||
apiKeyViewModel.OkCommand.Subscribe(apiKey => {
|
||||
if (ConnectedCoinDatabase.TryInitApiClient(apiKey))
|
||||
SidebarContent = newViewModel; // Must assign this to the observable property.
|
||||
});
|
||||
|
||||
// Check if API key must be entered.
|
||||
if (askForApiKey) {
|
||||
Console.WriteLine("API key must be entered by user.");
|
||||
// Show the API key input view.
|
||||
var apiKeyViewModel = new ApiKeyViewModel();
|
||||
_sideContent = apiKeyViewModel;
|
||||
// Subscribe to the OkCommand in the ApiKeyViewModel.
|
||||
apiKeyViewModel.OkCommand
|
||||
.Subscribe(apiKey => {
|
||||
// Attempt to connect to the PCGS API and set view accordingly.
|
||||
if (ConnectedCoinDatabase.TryInitApiClient()) _sideContent = newViewModel;
|
||||
});
|
||||
} else _sideContent = newViewModel;
|
||||
|
||||
// Set up the ability to show the error window.
|
||||
@@ -115,7 +114,8 @@ public class MainWindowViewModel : ViewModelBase {
|
||||
break;
|
||||
}
|
||||
|
||||
// TODO Clear the text boxes.
|
||||
// Clear the text boxes.
|
||||
newViewModel.ClearText();
|
||||
});
|
||||
|
||||
// Update TotalValue anytime a coin's quantity changes.
|
||||
|
||||
@@ -40,9 +40,7 @@ public class NewViewModel : ViewModelBase {
|
||||
okEnabled);
|
||||
|
||||
ClearCommand = ReactiveCommand.Create(() => {
|
||||
PcgsNumber = "";
|
||||
Grade = "";
|
||||
Quantity = "";
|
||||
ClearText();
|
||||
});
|
||||
|
||||
// Initialize grade values for use in the view.
|
||||
@@ -83,4 +81,13 @@ public class NewViewModel : ViewModelBase {
|
||||
|
||||
public ReactiveCommand<Unit, (int, string, int)> OkCommand { get; }
|
||||
public ReactiveCommand<Unit, Unit> ClearCommand { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears the text fields.
|
||||
/// </summary>
|
||||
public void ClearText() {
|
||||
PcgsNumber = "";
|
||||
Grade = "";
|
||||
Quantity = "";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user