private IList <GetMinuteBarsRequest> ConsolidatedMinuteBars(IList <GetMinuteBarsRequest> requests)
        {
            if (requests == null || !requests.Any())
            {
                return(new List <GetMinuteBarsRequest>());
            }

            var result = new List <GetMinuteBarsRequest>();

            var groupedByFigi = requests.GroupBy(req => req.Figi);

            foreach (var grp in groupedByFigi)
            {
                var from       = grp.Min(x => x.From);
                var to         = grp.Max(x => x.To);
                var newRequest = new GetMinuteBarsRequest
                {
                    Figi     = grp.FirstOrDefault()?.Figi,
                    From     = from,
                    To       = to,
                    Interval = grp.FirstOrDefault()?.Interval
                };

                result.Add(newRequest);
            }

            return(result);
        }
Exemplo n.º 2
0
 public GetTimeBarPair(GetMinuteBarsRequest request, GetMinuteBarsResponse response)
 {
     this.Request  = request;
     this.Response = response;
 }
Exemplo n.º 3
0
        /// <summary>
        /// The get minute bars async.
        /// </summary>
        /// <param name="request">
        /// The request.
        /// </param>
        /// <returns>
        /// The <see cref="Task"/>.
        /// </returns>
        public async Task <GetMinuteBarsResponse> GetMinuteBarsAsync(GetMinuteBarsRequest request)
        {
            if (request == null)
            {
                this.logger?.LogInformation(
                    "BmllTimeBarApiRepository Get received a null request. Returning an empty response");
                return(new GetMinuteBarsResponse());
            }

            this.logger?.LogInformation(
                $"BmllTimeBarApiRepository Get received a get minute bars request for {request?.Figi} at {request?.From} to {request?.To}");

            try
            {
                using (var httpClient =
                           this.httpClientFactory.GenericHttpClient(this.apiClientConfiguration.BmllServiceUrl))
                {
                    var json   = JsonConvert.SerializeObject(request);
                    var policy = this.policyFactory.PolicyTimeoutGeneric <HttpResponseMessage>(
                        TimeSpan.FromMinutes(30),
                        i => !i.IsSuccessStatusCode,
                        1,
                        TimeSpan.FromSeconds(30));

                    HttpResponseMessage response = null;
                    await policy
                    .ExecuteAsync(async() =>
                    {
                        response = await httpClient.PostAsync(
                            MinuteBarRoute,
                            new StringContent(json, Encoding.UTF8, "application/json"));
                        this.logger.LogInformation("GetMinuteBars policy received post response or timed out");
                        return(response);
                    })
                    ;

                    if (response == null || !response.IsSuccessStatusCode)
                    {
                        this.logger.LogError(
                            $"BmllTimeBarApiRepository Unsuccessful bmll time bar api repository GET request. {response?.StatusCode}");

                        return(new GetMinuteBarsResponse());
                    }

                    var jsonResponse = await response.Content.ReadAsStringAsync();

                    var deserialisedResponse = JsonConvert.DeserializeObject <GetMinuteBarsResponse>(jsonResponse);

                    if (deserialisedResponse == null)
                    {
                        this.logger.LogError("BmllTimeBarApiRepository was unable to deserialise the response");
                        return(new GetMinuteBarsResponse());
                    }

                    this.logger.LogInformation("BmllTimeBarApiRepository returning deserialised GET response");

                    return(deserialisedResponse);
                }
            }
            catch (Exception e)
            {
                this.logger?.LogError(e, "BmllTimeBarApiRepository Get encountered an exception");
            }

            this.logger?.LogInformation("BmllTimeBarApiRepository Get received a response from the client. Returning result.");

            return(new GetMinuteBarsResponse());
        }