diff --git a/PcgsInvUi/Services/CoinDatabase.cs b/PcgsInvUi/Services/CoinDatabase.cs
index 98fa5a6..decc7b6 100644
--- a/PcgsInvUi/Services/CoinDatabase.cs
+++ b/PcgsInvUi/Services/CoinDatabase.cs
@@ -50,8 +50,17 @@ public class CoinDatabase {
using (var reader = cmd.ExecuteReader()) {
if (reader.Read()) {
var apiKey = reader.GetString(0);
- _pcgsClient = new PcgsClient(apiKey);
+ _pcgsClient = PcgsClient.CreateClient(apiKey);
+ // Check that the client is valid.
+ if (_pcgsClient is null) {
+ IsClientConnected = false;
+ Console.WriteLine("Failed. API key was invalid.");
+ return IsClientConnected;
+ }
+
+ // This only runs if the key is valid. Insert the key into the database.
+ // TODO Create checks to ensure that the database doesn't already contain a key.
var insertCmd = Connection.CreateCommand();
insertCmd.CommandText = $"INSERT INTO ApiTable (API_KEY, REQUESTS_REMAINING) VALUE ({apiKey}, 1000)";
insertCmd.ExecuteNonQuery();
@@ -69,12 +78,18 @@ public class CoinDatabase {
public bool TryInitApiClient(string apiKey) {
Console.Write("Attempting to initialize API client with key param... ");
- _pcgsClient = new PcgsClient(apiKey);
+ _pcgsClient = PcgsClient.CreateClient(apiKey);
// TODO Check if the API key is valid and if it is, then add to the database.
- IsClientConnected = true;
+ if (_pcgsClient is null) {
+ IsClientConnected = false;
+ Console.WriteLine("Failed. API key was invalid.");
+ return IsClientConnected;
+ }
+
var cmd = Connection.CreateCommand();
cmd.CommandText = $"INSERT INTO ApiTable (API_KEY, REQUESTS_REMAINING) VALUES ({apiKey}, 1000)";
// cmd.ExecuteNonQuery();
+ IsClientConnected = true;
Console.WriteLine("Done!");
return IsClientConnected;
}
diff --git a/PcgsInvUi/Services/PcgsClient.cs b/PcgsInvUi/Services/PcgsClient.cs
index 244bf21..21752dd 100644
--- a/PcgsInvUi/Services/PcgsClient.cs
+++ b/PcgsInvUi/Services/PcgsClient.cs
@@ -25,10 +25,34 @@ public class PcgsClient : ReactiveObject {
private readonly string _apiKey;
private int _numApiRequests;
- public PcgsClient(string apiKey) {
+ ///
+ /// Creates a new instance of the PcgsClient class.
+ /// Although this is a constructor, it's meant to be called only from the CreateClient method.
+ ///
+ private PcgsClient(string apiKey) {
_apiKey = apiKey;
// "E8xivTVN_k4Q8ubk8Xv_uU11U91WX0wDht4S4m4BAf2HPmKnGrZ0J2f9nccM7Ns1VkGoT1SXYR74I7aNaMKqXlJGEy3oobseKV0nGiqu1IKvFkOTVjhV37SWUVExSDFMC6FLzs7xJQIHwkcxr5JFOQNmc4ZytEh_-hmCN3NL-z0fEk98tzBmPJ_F7bq_xNru86QMD2A9yalu_dWe-lCysfyE2bCA2EDqwvWenRYf-0XnNtp6qlxxCMlCd-TDqqRKLC3qefCwpXSemwBiNSP5gbC9pwcKrZugkruuJ0nyxxThZymm";
}
+
+ ///
+ /// Creates a new instance of the PcgsClient class.
+ ///
+ /// The api key to use for requests.
+ /// A new instance of the PcgsClient class, or null if the key is invalid.
+ public static PcgsClient? CreateClient(string apiKey) {
+ // Check if the api key is valid.
+ var testClient = new PcgsClient(apiKey);
+ if (!testClient.VerifyCredentials()) return null;
+ return testClient;
+ }
+
+ public bool VerifyCredentials() {
+ // Create a way to check if the api key given is valid
+ // I think the best way to do this is to make a request to the api and see if it returns the expected response.
+ var result = this.GetCoinFactsByGrade(1, "1"); // This is a known invalid coin id - this is because the api is very fast with invalid requests.
+ if (result.Result.Item1 == ErrorType.ApiKeyInvalid) return false;
+ return true;
+ }
public async Task<(ErrorType, Coin?)> GetCoinFactsByGrade(int pcgsNumber, string grade, bool plusGrade = false) {
using (HttpClient client = new HttpClient()) {