static async Task Main(string[] args) { Console.WriteLine("Hello World!"); await Task.Delay(TimeSpan.FromSeconds(10)); using var channel = GrpcChannel.ForAddress("https://localhost:5001"); var client = new Pricing.PricingClient(channel); var req = new StockPriceRequest { Context = Context.Query, Symbol = "TSLA" }; var res = client.GetPriceStream(req); int i = 0; while (await res.ResponseStream.MoveNext()) { Console.WriteLine($"Stock: Tesla, Price: {res.ResponseStream.Current}"); if (i++ == 10) { break; } } res.Dispose(); Console.ReadLine(); }
public List<StockPrice> GetPriceFromAPI(ICollection<StockSymbol> symbols) { if(symbols != null && symbols.Count > 0) { StockDataSoapClient client = new StockDataSoapClient(); var request = new StockPriceRequest(); request.Body = new StockPriceRequestBody(); request.Body.pubicKey = ConfigurationManager.AppSettings["PublicKey"]; request.Body.stockCodes = new ArrayOfString(); foreach (var symbol in symbols) { request.Body.stockCodes.Add(symbol.Ticker); } request.Body.hash = new HashHelper().ComputeHash(JsonConvert.SerializeObject(request.Body.stockCodes), ConfigurationManager.AppSettings["PrivateKey"]); var response = client.StockPrice(request); List<StockPrice> prices = JsonConvert.DeserializeObject<List<StockPrice>>(response.Body.StockPriceResult); return prices; } return null; }
public override async Task GetPriceStream(StockPriceRequest request, IServerStreamWriter <StockPriceResponse> responseStream, ServerCallContext context) { while (!context.CancellationToken.IsCancellationRequested) { await responseStream.WriteAsync(await _client.RequestPriceAsync(request)); } }
public async Task <StockPriceResponse> RequestPriceAsync(StockPriceRequest request) { var lookUp = new LookupRequest { LookUpId = Guid.NewGuid(), Symbol = request.Symbol, ApiKey = request.ApiKey, }; lookUp.Context = request.Context switch { Context.Query => LookUpContext.Query, Context.Buy => LookUpContext.Buy, Context.Sell => LookUpContext.Sell, _ => throw new NotSupportedException() }; var options = new BoundedChannelOptions(1); options.FullMode = BoundedChannelFullMode.DropNewest; options.SingleReader = true; options.SingleWriter = true; var channel = Channel.CreateBounded <LookUpResponse>(options); while (!_writers.TryAdd(lookUp.LookUpId, channel.Writer)) { ; } _queue.Enqueue(lookUp, (int)lookUp.Context); if (!await channel.Reader.WaitToReadAsync()) { throw new NotSupportedException(); } return(new StockPriceResponse { Price = (await channel.Reader.ReadAsync()).Price }); } }
public List <StockPrice> GetPriceFromAPI(ICollection <StockSymbol> symbols) { if (symbols != null && symbols.Count > 0) { StockDataSoapClient client = new StockDataSoapClient(); var request = new StockPriceRequest(); request.Body = new StockPriceRequestBody(); request.Body.pubicKey = ConfigurationManager.AppSettings["PublicKey"]; request.Body.stockCodes = new ArrayOfString(); foreach (var symbol in symbols) { request.Body.stockCodes.Add(symbol.Ticker); } request.Body.hash = new HashHelper().ComputeHash(JsonConvert.SerializeObject(request.Body.stockCodes), ConfigurationManager.AppSettings["PrivateKey"]); var response = client.StockPrice(request); List <StockPrice> prices = JsonConvert.DeserializeObject <List <StockPrice> >(response.Body.StockPriceResult); return(prices); } return(null); }
public override async Task <StockPriceResponse> GetCurrentPrice(StockPriceRequest request, ServerCallContext context) { return(await _client.RequestPriceAsync(request)); }