public void Run() { using PaprikaApiClient client = new PaprikaApiClient(); using zcryptoContext ctx = new zcryptoContext(); List <Buy> activeBuys = DataConsistentController.GetActiveBuys(); foreach (Buy b in activeBuys) { API_Market_Price price = client.GetMarketPrice(b.CoinId); if (price is not null) { if (ctx.ReportedExchangeRates.Where(x => x.BuyId == b.Id).Count() > 1000) { ctx.ReportedExchangeRates.RemoveRange(ctx.ReportedExchangeRates.Where(x => x.BuyId == b.Id)); ctx.SaveChanges(); } ctx.ReportedExchangeRates.Add(new ReportedExchangeRate() { BuyId = b.Id, PlnExchange = price.quotes.PLN.price, ApiUpdateTime = price.last_updated, ExchangeDiff = (price.quotes.PLN.price * (double)b.Count) - (double)(b.ExchangeRatePln * b.Count) }); ctx.SaveChanges(); } } }
public void GetMarketPrices() { using (zcryptoContext ctx = new zcryptoContext()) { List <Coin> coins = ctx.Coins.Where(x => x.IsActive).ToList(); using (PaprikaApiClient client = new PaprikaApiClient()) { foreach (Coin c in coins) { API_Market_Price marketPrice = client.GetMarketPrice(c.Id); if (marketPrice != null) { Assert.IsTrue(marketPrice.quotes.EUR != null); Assert.IsTrue(marketPrice.quotes.EUR.price != 0); Assert.IsTrue(marketPrice.quotes.PLN != null); Assert.IsTrue(marketPrice.quotes.PLN.price != 0); } } } } }
public API_Market_Price GetMarketPrice(string coinId) { using zcryptoContext dbCtx = new zcryptoContext(); if (DataConsistentController.IsCoinBlacklisted(coinId)) { return(null); } System.Diagnostics.Debug.WriteLine(coinId); IRestResponse response = restClient.Get(new RestRequest($"v1/coins/{coinId}/markets?quotes=PLN,EUR", Method.GET)); try { ValidateResponse(response); }catch (Exception) { if (response.StatusCode == System.Net.HttpStatusCode.NotFound) { dbCtx.CoinBlacklistIntegrations.Add(new CoinBlacklistIntegration() { CoinId = coinId, Reason = "Not found by markets endpoint" }); dbCtx.SaveChanges(); return(null); } else { throw; } } string jsonCoinList = response.Content; API_Market_Price foundMarketPrice = JsonConvert.DeserializeObject <List <API_Market_Price> >(jsonCoinList) .Where(x => x.exchange_id.Equals(DEFAULT_EXCHANGE_ID)).FirstOrDefault(); return(foundMarketPrice); }