Once again pushing broken code

This commit is contained in:
alphafetus committed 2023-11-30 16:56:24 -05:00
1 parent fab011b898
commit 683a2cf87b
2 files changed
+43 -4

No files matched your search

+18 -3
View File
@@ -50,8 +50,17 @@ public class CoinDatabase {
using (var reader = cmd.ExecuteReader()) { using (var reader = cmd.ExecuteReader()) {
if (reader.Read()) { if (reader.Read()) {
var apiKey = reader.GetString(0); 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(); var insertCmd = Connection.CreateCommand();
insertCmd.CommandText = $"INSERT INTO ApiTable (API_KEY, REQUESTS_REMAINING) VALUE ({apiKey}, 1000)"; insertCmd.CommandText = $"INSERT INTO ApiTable (API_KEY, REQUESTS_REMAINING) VALUE ({apiKey}, 1000)";
insertCmd.ExecuteNonQuery(); insertCmd.ExecuteNonQuery();
@@ -69,12 +78,18 @@ public class CoinDatabase {
public bool TryInitApiClient(string apiKey) { public bool TryInitApiClient(string apiKey) {
Console.Write("Attempting to initialize API client with key param... "); 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. // 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(); var cmd = Connection.CreateCommand();
cmd.CommandText = $"INSERT INTO ApiTable (API_KEY, REQUESTS_REMAINING) VALUES ({apiKey}, 1000)"; cmd.CommandText = $"INSERT INTO ApiTable (API_KEY, REQUESTS_REMAINING) VALUES ({apiKey}, 1000)";
// cmd.ExecuteNonQuery(); // cmd.ExecuteNonQuery();
IsClientConnected = true;
Console.WriteLine("Done!"); Console.WriteLine("Done!");
return IsClientConnected; return IsClientConnected;
} }
+25 -1
View File
@@ -25,11 +25,35 @@ public class PcgsClient : ReactiveObject {
private readonly string _apiKey; private readonly string _apiKey;
private int _numApiRequests; private int _numApiRequests;
public PcgsClient(string apiKey) { /// <summary>
/// Creates a new instance of the PcgsClient class.
/// Although this is a constructor, it's meant to be called only from the CreateClient method.
/// </summary>
private PcgsClient(string apiKey) {
_apiKey = apiKey; _apiKey = apiKey;
// "E8xivTVN_k4Q8ubk8Xv_uU11U91WX0wDht4S4m4BAf2HPmKnGrZ0J2f9nccM7Ns1VkGoT1SXYR74I7aNaMKqXlJGEy3oobseKV0nGiqu1IKvFkOTVjhV37SWUVExSDFMC6FLzs7xJQIHwkcxr5JFOQNmc4ZytEh_-hmCN3NL-z0fEk98tzBmPJ_F7bq_xNru86QMD2A9yalu_dWe-lCysfyE2bCA2EDqwvWenRYf-0XnNtp6qlxxCMlCd-TDqqRKLC3qefCwpXSemwBiNSP5gbC9pwcKrZugkruuJ0nyxxThZymm"; // "E8xivTVN_k4Q8ubk8Xv_uU11U91WX0wDht4S4m4BAf2HPmKnGrZ0J2f9nccM7Ns1VkGoT1SXYR74I7aNaMKqXlJGEy3oobseKV0nGiqu1IKvFkOTVjhV37SWUVExSDFMC6FLzs7xJQIHwkcxr5JFOQNmc4ZytEh_-hmCN3NL-z0fEk98tzBmPJ_F7bq_xNru86QMD2A9yalu_dWe-lCysfyE2bCA2EDqwvWenRYf-0XnNtp6qlxxCMlCd-TDqqRKLC3qefCwpXSemwBiNSP5gbC9pwcKrZugkruuJ0nyxxThZymm";
} }
/// <summary>
/// Creates a new instance of the PcgsClient class.
/// </summary>
/// <param name="apiKey">The api key to use for requests.</param>
/// <returns>A new instance of the PcgsClient class, or null if the key is invalid.</returns>
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) { public async Task<(ErrorType, Coin?)> GetCoinFactsByGrade(int pcgsNumber, string grade, bool plusGrade = false) {
using (HttpClient client = new HttpClient()) { using (HttpClient client = new HttpClient()) {
var gradeParam = GetGradeFromString(grade); var gradeParam = GetGradeFromString(grade);