Пример #1
0
        public async Task <List <T> > GetAllAsync <T, TY>(string path, ICollectionRequest request)
            where T : ExchangeDto
            where TY : IResponse
        {
            var stopwatch = new Stopwatch();
            var total     = 0;
            var count     = 0;

            var returnModels = new List <T>();

            do
            {
                using (var client = new ExchangeHttpClient(ExchangeConfig.RestUrl))
                {
                    stopwatch.Restart();
                    total          += count;
                    request.StartAt = total;

                    var query      = $"?{request.AsDictionary().BuildQueryString()}";
                    var authHeader = await GetHttpAuthHeader(HttpMethod.GET, path, query);

                    var response = await client.GetAsync <TY>($"{path}{query}", authHeader);

                    if (response == null)
                    {
                        throw new NullReferenceException();
                    }

                    var models = response.Select(x => Mapper.Map <T>(x)).ToArray();

                    returnModels.AddRange(models);

                    count = response.Length;

                    if (count < request.Count)
                    {
                        count = 0;
                    }

                    if (stopwatch.Elapsed < TimeSpan.FromSeconds(1.01))
                    {
                        await Task.Delay(TimeSpan.FromSeconds(1.5).Subtract(stopwatch.Elapsed));
                    }
                }
            } while (count > 0);

            return(returnModels);
        }
Пример #2
0
        public async Task <T> GetAsync <T, TY>(string path, IRequest request)
            where T : ExchangeDto
            where TY : IResponse
        {
            using (var client = new ExchangeHttpClient(ExchangeConfig.RestUrl))
            {
                var query      = $"?{request.AsDictionary().BuildQueryString()}";
                var authHeader = await GetHttpAuthHeader(HttpMethod.GET, path, query);

                var response = await client.GetAsync <TY>($"{path}{query}", authHeader);

                if (response == null)
                {
                    throw new NullReferenceException();
                }

                var model = response.Select(x => Mapper.Map <T>(x)).FirstOrDefault();

                return(model);
            }
        }