Пример #1
0
        public async Task When_StooqApiReturns200_With_InvalidContent_Should_ReturnNotFoundMessage()
        {
            // arrange
            var handler    = GetMockedHttpHandler(InvalidContent);
            var httpClient = new HttpClient(handler.Object);
            var stooqApi   = new StooqApi(_loggerMock.Object, httpClient, _configurationMock.Object);

            // act
            var result = await stooqApi.GetStockQuote("STOCK");

            // assert
            result.ShouldBe($"Stock STOCK not found");

            handler.Protected().Verify(
                "SendAsync",
                Times.Exactly(1),
                ItExpr.Is <HttpRequestMessage>(req => req.Method == HttpMethod.Get),
                ItExpr.IsAny <CancellationToken>());
        }
Пример #2
0
        public async Task When_ThrowAnyException_Should_ReturnFriendlyErrorMessage()
        {
            // arrange
            var handler    = GetMockedHttpHandler("exception content");
            var httpClient = new HttpClient(handler.Object);
            var stooqApi   = new StooqApi(_loggerMock.Object, httpClient, _configurationMock.Object);

            // act
            var result = await stooqApi.GetStockQuote("STOCK");

            // assert
            result.ShouldBe($"Oops, Something Went Wrong Please Try Again!");

            handler.Protected().Verify(
                "SendAsync",
                Times.Exactly(1),
                ItExpr.Is <HttpRequestMessage>(req => req.Method == HttpMethod.Get),
                ItExpr.IsAny <CancellationToken>());
        }
Пример #3
0
        public IQuotesSourceGetQuotesResult GetQuotes(IQuotesSourceGetQuotesParams getQuotesParams)
        {
            StooqApi api = new StooqApi();

            IQuotesSourceGetQuotesResult result = new StooqSourceGetQuotesResult();

            foreach (var t in getQuotesParams.Tickers)
            {
                IQuotesSourceCanImportParams canImportParams = CreateCanImportParams();
                canImportParams.Tickers.Add(t);
                IQuotesSourceCanImportResult canImportRes = CanImport(canImportParams);
                if (canImportRes.Success)
                {
                    try
                    {
                        IQuotesData qd = new BaseQuotesData();

                        var quotes = api.Download(t,
                                                  getQuotesParams.PeriodStart,
                                                  getQuotesParams.PeriodEnd,
                                                  getQuotesParams.Country,
                                                  getQuotesParams.TimeFrame == ETimeFrame.Daily ? StooqApi.ETimeFrame.Daily : (getQuotesParams.TimeFrame == ETimeFrame.Weekly ? StooqApi.ETimeFrame.Weekly : StooqApi.ETimeFrame.Monthly));

                        foreach (var q in quotes.Quotes)
                        {
                            ITimeSeriesRecord newRec = qd.CreateQuotesRecord();
                            newRec["Close"]  = q.Close;
                            newRec["High"]   = q.High;
                            newRec["Open"]   = q.Open;
                            newRec["Low"]    = q.Low;
                            newRec["Volume"] = q.Volume;
                            newRec.Time      = ToPeriodStart(q.PeriodEnd, getQuotesParams.TimeFrame);

                            qd.AddRecord(newRec);
                        }

                        qd.Country   = getQuotesParams.Country;
                        qd.Ticker    = t;
                        qd.Name      = this.TickerName(t);
                        qd.TimeFrame = getQuotesParams.TimeFrame;
                        qd.Type      = this.TickerType(t);
                        qd.Unit      = this.TickerUnit(t);

                        result.QuotesData.Add(qd);
                    }
                    catch (Exception ex)
                    {
                        result.Success = false;
                        result.AddError(Interfaces.EErrorCodes.QuotesSourceFail, Interfaces.EErrorType.Error, ex.Message);
                    }
                }
            }

            result.Success = result.QuotesData.Count > 0;
            if (result.Success && result.QuotesData.Count != getQuotesParams.Tickers.Count)
            {
                result.AddError(Interfaces.EErrorCodes.QuotesNotFound, Interfaces.EErrorType.Warning, "Not all quotes were found");
            }
            else if (!result.Success)
            {
                result.AddError(Interfaces.EErrorCodes.QuotesNotFound, Interfaces.EErrorType.Error, "Requested tickers are not supported or quotes for them not found");
            }

            return(result);
        }