From 6399693e090c48bbe728e750b5822e2345a79667 Mon Sep 17 00:00:00 2001 From: Robert Belcher Date: Sat, 6 Jul 2024 21:01:07 -0700 Subject: [PATCH] Get credits remaining at startup and after changing the API key --- KeywordsEverywhereClient/ClientForm.cs | 37 +++++++++++++++++-- .../KeywordsEverywhereApi.cs | 24 ++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/KeywordsEverywhereClient/ClientForm.cs b/KeywordsEverywhereClient/ClientForm.cs index 0a7a94f..3a39ee3 100644 --- a/KeywordsEverywhereClient/ClientForm.cs +++ b/KeywordsEverywhereClient/ClientForm.cs @@ -20,6 +20,27 @@ public partial class ClientForm : Form this.clientFormConfiguration = this.clientFormConfigurationManager.Load(); this.apiKeyTextBox.Text = this.clientFormConfiguration.ApiKey; + + this.UpdateCreditBalanceAsync(); + } + + protected async void UpdateCreditBalanceAsync() + { + this.SetEnabled(false); + + try + { + var creditBalance = await this.keywordsEverywhereApi.GetCreditBalanceAsync(this.clientFormConfiguration.ApiKey, default); + this.UpdateCreditsRemaining(creditBalance); + } + catch + { + creditsRemainingLabel.Text = "Credits remaining: TBD"; + } + finally + { + this.SetEnabled(true); + } } protected override void OnShown(EventArgs e) @@ -32,8 +53,13 @@ public partial class ClientForm : Form private void apiKeyTextBox_Leave(object sender, EventArgs e) { - this.clientFormConfiguration.ApiKey = this.apiKeyTextBox.Text; - this.clientFormConfigurationManager.Save(this.clientFormConfiguration); + if (this.clientFormConfiguration.ApiKey != this.apiKeyTextBox.Text) + { + this.clientFormConfiguration.ApiKey = this.apiKeyTextBox.Text; + + this.clientFormConfigurationManager.Save(this.clientFormConfiguration); + this.UpdateCreditBalanceAsync(); + } } private async void getResultsButton_Click(object sender, EventArgs e) @@ -89,7 +115,12 @@ public partial class ClientForm : Form this.resultsDataGrid.DataSource = rows; - creditsRemainingLabel.Text = $"Credits remaining: {responseData.Credits}"; + UpdateCreditsRemaining(responseData.Credits); + } + + private void UpdateCreditsRemaining(long creditsRemaining) + { + creditsRemainingLabel.Text = $"Credits remaining: {creditsRemaining}"; } private void HandleError(GetKeywordDataError errorData) diff --git a/KeywordsEverywhereClient/KeywordsEverywhereApi.cs b/KeywordsEverywhereClient/KeywordsEverywhereApi.cs index 454433a..3160698 100644 --- a/KeywordsEverywhereClient/KeywordsEverywhereApi.cs +++ b/KeywordsEverywhereClient/KeywordsEverywhereApi.cs @@ -14,6 +14,30 @@ internal class KeywordsEverywhereApi }); } + public async Task GetCreditBalanceAsync(string apiKey, CancellationToken cancellationToken) + { + HttpRequestMessage request = new(HttpMethod.Get, "https://api.keywordseverywhere.com/v1/account/credits"); + + request.Headers.Add("Accept", "application/json"); + request.Headers.Add("Authorization", $"Bearer {apiKey}"); + + using var response = await this.httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken); + + var responseBody = await response.Content.ReadAsStringAsync(cancellationToken); + + if (response.IsSuccessStatusCode) + { + var result = JsonSerializer.Deserialize>(responseBody) + ?? throw new InvalidOperationException("Success response is null."); + + return result.Single(); + } + else + { + throw new InvalidOperationException("Failed to get credit balance. " + responseBody); + } + } + public async Task GetKeywordDataAsync(string apiKey, GetKeywordDataRequest requestData, CancellationToken cancellationToken) { HttpRequestMessage request = new(HttpMethod.Post, "https://api.keywordseverywhere.com/v1/get_keyword_data");