public async Task<List<RealTimeStockModel>> QueryStockAsync(List<StockInfoModel> queryStockList, SessionData session) { List<RealTimeStockModel> allRealTimeResult = new List<RealTimeStockModel>(); // Build all of the stock parameter StringBuilder stockParm = new StringBuilder(); foreach (var stock in queryStockList) stockParm.Append(stock.ExchangeTpeyKey).Append("_").Append(stock.Id).Append(".tw|"); try { string queryStr = string.Format(QUERY_URI, stockParm, GetJavaScriptTimeTick()); Debug.WriteLine(DateTime.Now + " : " + queryStr); string responseJson = string.Empty; responseJson = await Http.GetAsync(queryStr, true, session.Cookie); var results = JsonConverter.DeserializeFrom<RootObject<StockData>>(responseJson); if (results.Result == SUCCESS) { foreach (var item in results.StockMessage) allRealTimeResult.Add(item.Convert()); return allRealTimeResult; } else throw new Exception("Stock Query fail, error result : " + results.Result); } catch (WebException e) { // retry to get session and try to query stock data throw new QueryDataTimeOutException(e.InnerException.Message); } }
private async Task<SessionData> GetSession() { //SessionData clientSession; if (_clientSession == null) { await _syncLock.WaitAsync(); if (_clientSession == null) { _clientSession = await TwseStockClient.CreateSessionAsync(); } _syncLock.Release(); } return _clientSession; }
private void ReleaseSession() { _clientSession = null; }
public async Task <List <RealTimeStockModel> > QueryStockAsync(List <StockInfoModel> queryStockList, SessionData session) { List <RealTimeStockModel> allRealTimeResult = new List <RealTimeStockModel>(); // Build all of the stock parameter StringBuilder stockParm = new StringBuilder(); foreach (var stock in queryStockList) { stockParm.Append(stock.ExchangeTpeyKey).Append("_").Append(stock.Id).Append(".tw|"); } try { string queryStr = string.Format(QUERY_URI, stockParm, GetJavaScriptTimeTick()); Debug.WriteLine(DateTime.Now + " : " + queryStr); string responseJson = string.Empty; responseJson = await Http.GetAsync(queryStr, true, session.Cookie); var results = JsonConverter.DeserializeFrom <RootObject <StockData> >(responseJson); if (results.Result == SUCCESS) { foreach (var item in results.StockMessage) { allRealTimeResult.Add(item.Convert()); } return(allRealTimeResult); } else { throw new Exception("Stock Query fail, error result : " + results.Result); } } catch (WebException e) { // retry to get session and try to query stock data throw new QueryDataTimeOutException(e.InnerException.Message); } }