public bool PublishMessage(JobsityMessage message) { try { using (var conn = connectionFactory.CreateConnection()) { using (var channel = conn.CreateModel()) { channel.QueueDeclare( queue: QueueName, durable: true, exclusive: false, autoDelete: false, arguments: null ); var jsonPayload = JsonConvert.SerializeObject(message); var body = Encoding.UTF8.GetBytes(jsonPayload); channel.BasicPublish(exchange: "", routingKey: QueueName, basicProperties: null, body: body ); return(true); } } } catch (Exception) { throw; } }
public async Task <IActionResult> PublishMessage([FromBody] JobsityMessage message) { try { var r = _amqp.PublishMessage(message); if (r) { return(Ok(new { Success = true, Data = string.Empty, Message = "Success" })); } return(Ok(new { Success = false, Data = string.Empty, Message = "Cannot Connect to service!" })); } catch (Exception eX) { return(BadRequest(eX)); } }
public void BeforeEach() { _fixture = new Fixture().Customize(new AutoMoqCustomization()); _fixture.Customize <JobsityMessage>(ob => ob .With(t => t.User, "ashuerta") .With(t => t.Msg, "/stock=aapl.us") .With(t => t.Date, DateTime.Now) .WithAutoProperties()); _mockEntity = _fixture.Create <JobsityMessage>(); _mockLogger = new Mock <ILogger <BotController> >(); _mockMemoryCache = new Mock <IMemoryCache>(); _mockWebHosting = new Mock <IWebHostEnvironment>(); var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location.Replace("Jobsity.ServiceTests\\bin\\Debug\\netcoreapp3.0", "\\Jobsity.Service\\wwwroot")); _mockWebHosting.Setup(s => s.WebRootPath).Returns(path); }
public async Task <IActionResult> ResponseMsgAsync([FromQuery] JobsityMessage message) { try { // Look for cache key. if (!_cache.TryGetValue(CacheKeys.Aapl, out List <Aapl> cacheEntry)) { // Key not in cache, so get data. cacheEntry = ReadCsv("aapl.us.csv", ','); // Set cache options. var cacheEntryOptions = new MemoryCacheEntryOptions() // Keep in cache for this time, reset time if accessed. .SetSlidingExpiration(TimeSpan.FromDays(3)); // Save data in cache. _cache.Set(CacheKeys.Aapl, cacheEntry, cacheEntryOptions); } if (cacheEntry.Any()) { var sym = message.Msg.Substring(7); var item = cacheEntry.SingleOrDefault(x => x.Symbol.Equals(sym, StringComparison.InvariantCultureIgnoreCase)); if (item == null) { return(Ok(new { Success = true, From = "bot", Data = $"Hello {message.User}, I can't help you with {sym.ToUpper()}." })); } //apply fx var value = $"{item.Symbol.ToUpper()} quote is ${item.Open} per share."; return(Ok(new { Success = true, From = "bot", Data = value })); } return(Ok(null)); } catch (Exception eX) { _logger.LogError(eX, eX.Message); return(BadRequest(eX)); } }
public async Task <IActionResult> StooqSourceAsync([FromQuery] JobsityMessage message) { try { // Change : Jobcity request to read data directly from api. // Change Date : 2020/01/21 var stockName = message.Msg.Substring(7); var client = new RestClient($"https://stooq.com/q/l"); var request = new RestRequest("?s=" + stockName + "&f=sd2t2ohlcv&h&e=csv", DataFormat.None); var response = await client.ExecuteGetAsync(request); if (response.IsSuccessful) { if (!string.IsNullOrEmpty(response.Content)) { var stooqs = new List <StooqDetail>(); using (var stream = new MemoryStream(response.RawBytes)) using (var sr = new StreamReader(stream)) { var isHeader = true; while (!sr.EndOfStream) { var row = sr.ReadLine(); if (isHeader) { isHeader = false; continue; } var l = row.Split(',').ToArray(); var item = new StooqDetail { Symbol = l[0], Date = l[1], Time = l[2], Open = double.Parse(l[3]), High = double.Parse(l[4]), Low = double.Parse(l[5]), Close = double.Parse(l[6]), Volume = int.Parse(l[7]) }; stooqs.Add(item); } } var stooq = stooqs.First(); var value = $"{stooq.Symbol.ToUpper()} quote is ${stooq.Open} per share."; return(Ok(new { Success = true, From = "bot", Data = value })); } return(Ok(new { Success = true, From = "bot", Data = $"Hello {message.User}, I can't help you with {stockName.ToUpper()}." })); } return(Ok(new { Success = true, From = "bot", Data = $"Hello {message.User}, I can't help you with {stockName.ToUpper()}." })); } catch (Exception eX) { _logger.LogError(eX, eX.Message); return(BadRequest(eX)); } }