//private void TDAApiService_OnStatusesChanged() //{ // throw new NotImplementedException(); //} public static async Task <TDAStockQuote> GetQuote(string sSymbol) { /// TODO: TDA Streaming quotes vs using Thread.Sleep 2000. /// TODO: Save option quotes with spreads or spreads to files for sparkline? /// DONE: Need to understand TDA Auths and how Refresh Token is used NEEDS WORK /// DONE: Need a way to store the Auth info on server and refresh it as needed /// var quote = new TDAStockQuote(); HttpResponseMessage response = new HttpResponseMessage(); try { response = await GetQuoteResponseAsync(sSymbol); TDANotifications.quoteStatus = response.StatusCode.ToString(); response.EnsureSuccessStatusCode(); var content = await response.Content?.ReadAsStringAsync(); using JsonDocument document = JsonDocument.Parse(content); var sJson = document.RootElement.GetProperty(sSymbol).ToString(); quote = JsonSerializer.Deserialize <TDAStockQuote>(sJson); return(quote); } catch (Exception ex) { Debug.Print(ex.Message); if (response.StatusCode.ToString() == "Unauthorized" || response.StatusCode.ToString() == "BadRequest") { await GetAuthenticationAsync(); } return(quote); } }
public async Task <TDAStockQuote> GetStaticQuote(string sSymbol) { /// TODO: TDA Streaming quotes vs using Thread.Sleep 2000. /// TODO: Save option quotes with spreads or spreads to files for sparkline? /// DONE: Need to understand TDA Auths and how Refresh Token is used NEEDS WORK /// DONE: Need a way to store the Auth info on server and refresh it as needed /// var quote = new TDAStockQuote(); HttpResponseMessage response = new HttpResponseMessage(); try { using var httpClient = new HttpClient(); using var request = new HttpRequestMessage(new HttpMethod("GET"), $"https://api.tdameritrade.com/v1/marketdata/{sSymbol}/quotes?apikey={TDAConstants._apiKey}"); request.Headers.TryAddWithoutValidation("Authorization", $"Bearer {TDATokens._TDAAccessToken}"); response = await httpClient.SendAsync(request); TDANotifications.quoteStatus = response.StatusCode.ToString(); response.EnsureSuccessStatusCode(); var content = await response.Content?.ReadAsStringAsync(); using JsonDocument document = JsonDocument.Parse(content); var sJson = document.RootElement.GetProperty(sSymbol).ToString(); quote = JsonSerializer.Deserialize <TDAStockQuote>(sJson); return(quote); } catch (Exception ex) { Debug.Print(ex.Message); if (response.StatusCode.ToString() == "Unauthorized" || response.StatusCode.ToString() == "BadRequest") { await GetAuthenticationAsync(); } return(quote); } }