Add API key management. #10
@@ -9,7 +9,11 @@ using ReactiveUI;
|
|||||||
|
|
||||||
namespace PcgsInvUi.Services;
|
namespace PcgsInvUi.Services;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Provides an interface into the PCGS Public API.
|
||||||
|
/// </summary>
|
||||||
public class PcgsClient : ReactiveObject {
|
public class PcgsClient : ReactiveObject {
|
||||||
|
|
||||||
public int NumApiRequests {
|
public int NumApiRequests {
|
||||||
get => _numApiRequests;
|
get => _numApiRequests;
|
||||||
set => this.RaiseAndSetIfChanged(ref _numApiRequests, value);
|
set => this.RaiseAndSetIfChanged(ref _numApiRequests, value);
|
||||||
@@ -20,7 +24,7 @@ public class PcgsClient : ReactiveObject {
|
|||||||
private int _numApiRequests;
|
private int _numApiRequests;
|
||||||
|
|
||||||
public PcgsClient(string apiKey) {
|
public PcgsClient(string apiKey) {
|
||||||
// _apiKey = apiKey;
|
// I'd like to be able to test the API key upon initialization.
|
||||||
_apiKey =
|
_apiKey =
|
||||||
"E8xivTVN_k4Q8ubk8Xv_uU11U91WX0wDht4S4m4BAf2HPmKnGrZ0J2f9nccM7Ns1VkGoT1SXYR74I7aNaMKqXlJGEy3oobseKV0nGiqu1IKvFkOTVjhV37SWUVExSDFMC6FLzs7xJQIHwkcxr5JFOQNmc4ZytEh_-hmCN3NL-z0fEk98tzBmPJ_F7bq_xNru86QMD2A9yalu_dWe-lCysfyE2bCA2EDqwvWenRYf-0XnNtp6qlxxCMlCd-TDqqRKLC3qefCwpXSemwBiNSP5gbC9pwcKrZugkruuJ0nyxxThZymm";
|
"E8xivTVN_k4Q8ubk8Xv_uU11U91WX0wDht4S4m4BAf2HPmKnGrZ0J2f9nccM7Ns1VkGoT1SXYR74I7aNaMKqXlJGEy3oobseKV0nGiqu1IKvFkOTVjhV37SWUVExSDFMC6FLzs7xJQIHwkcxr5JFOQNmc4ZytEh_-hmCN3NL-z0fEk98tzBmPJ_F7bq_xNru86QMD2A9yalu_dWe-lCysfyE2bCA2EDqwvWenRYf-0XnNtp6qlxxCMlCd-TDqqRKLC3qefCwpXSemwBiNSP5gbC9pwcKrZugkruuJ0nyxxThZymm";
|
||||||
}
|
}
|
||||||
@@ -38,12 +42,15 @@ public class PcgsClient : ReactiveObject {
|
|||||||
Console.WriteLine("Got response.");
|
Console.WriteLine("Got response.");
|
||||||
|
|
||||||
if (response.IsSuccessStatusCode) {
|
if (response.IsSuccessStatusCode) {
|
||||||
|
// TODO Check for errors indicated by a successful response.
|
||||||
string jsonResponse = await response.Content.ReadAsStringAsync();
|
string jsonResponse = await response.Content.ReadAsStringAsync();
|
||||||
newCoin = ParseJsonResponse(jsonResponse);
|
newCoin = ParseJsonResponse(jsonResponse);
|
||||||
Console.WriteLine("Coin created.");
|
Console.WriteLine("Coin created.");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Console.WriteLine($"Error: {response.StatusCode} - {response.ReasonPhrase}");
|
// TODO Throw HTTP exception.
|
||||||
|
Console.WriteLine("Error getting coin.");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,4 +75,36 @@ public class PcgsClient : ReactiveObject {
|
|||||||
|
|
||||||
return newCoin;
|
return newCoin;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Thrown when the API response indicates that the request was invalid.
|
||||||
|
/// Generally used when the request was formatted incorrectly.
|
||||||
|
/// </summary>
|
||||||
|
public class InvalidRequestException : Exception {
|
||||||
|
public InvalidRequestException() { }
|
||||||
|
public InvalidRequestException(string message) : base(message) { }
|
||||||
|
public InvalidRequestException(string message, Exception inner) : base(message, inner) { }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Thrown when the API response indicates that the coin was not found.
|
||||||
|
/// Generally used when the PCGS number and grade combo that was requested does not exist in the PCGS database.
|
||||||
|
/// </summary>
|
||||||
|
public class CoinNotFoundException : Exception {
|
||||||
|
public CoinNotFoundException() { }
|
||||||
|
public CoinNotFoundException(string message) : base(message) { }
|
||||||
|
public CoinNotFoundException(string message, Exception inner) : base(message, inner) { }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Thrown when the API returns an HTTP error 401.
|
||||||
|
/// Generally indicates that the API key is invalid.
|
||||||
|
/// </summary>
|
||||||
|
public class ApiAuthorizationException : Exception {
|
||||||
|
public ApiAuthorizationException() { }
|
||||||
|
public ApiAuthorizationException(string message) : base(message) { }
|
||||||
|
public ApiAuthorizationException(string message, Exception inner) : base(message, inner) { }
|
||||||
|
}
|
||||||
|
|
||||||
|
public
|
||||||
Reference in new issue
Block a user