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