示例#1
0
        private async Task <List <object> > GetAsync(string resourceName, MessageNumberRange range)
        {
            var url = new Uri(new Uri(_host), relativeUri: Path.Combine("events", resourceName, $"{range.Start}-{range.End}"));

            Console.WriteLine($"Getting events from {url}");
            var message =
                new HttpRequestMessage(
                    method: HttpMethod.Get,
                    requestUri: url);

            var responseMessage = await _httpClientWrapper.SendAsync(message);

            if (!responseMessage.IsSuccessStatusCode)
            {
                throw new Exception();
            }

            if (!responseMessage.IsSuccessStatusCode)
            {
                throw new EventApiRequestFailedException(url, statusCode: responseMessage.StatusCode);
            }

            if (responseMessage.Content == null)
            {
                return(new List <object>());
            }

            var content = await responseMessage.Content.ReadAsStringAsync();

            return(MapJsonContentToObjects(content));
        }
示例#2
0
        public async Task <IList <object> > GetEventsAfterAsync(string resourceName, MessageNumber lastMessageNumber)
        {
            Console.WriteLine($"GetEventsAfterAsync with Last message number: {lastMessageNumber}");
            var allEvents = new List <object>();

            List <object>      retrievedEventsList;
            MessageNumberRange numberRange;

            if (lastMessageNumber == MessageNumber.NotSet)
            {
                numberRange = new MessageNumberRange(MessageNumber.New(0), _batchSize);

                retrievedEventsList = await GetAsync(resourceName, numberRange);

                allEvents.AddRange(retrievedEventsList);
            }
            else
            {
                var skippedBatches = lastMessageNumber.Value / _batchSize;
                numberRange = new MessageNumberRange(MessageNumber.New(skippedBatches * _batchSize), _batchSize);

                retrievedEventsList = await GetAsync(resourceName, numberRange);

                var skippedBatchSize = lastMessageNumber.Value % _batchSize;
                var eventsListStartingFromLastMessageNumber = retrievedEventsList.Skip(skippedBatchSize + 1).ToList();

                allEvents.AddRange(eventsListStartingFromLastMessageNumber);
            }

            while (retrievedEventsList.Count == _batchSize)
            {
                numberRange         = new MessageNumberRange(MessageNumber.New(numberRange.End.Value + 1), _batchSize);
                retrievedEventsList = await GetAsync(resourceName, numberRange);

                allEvents.AddRange(retrievedEventsList);
            }

            return(allEvents);
        }