public async Task GetRows(IGetRowsParams getParams)
        {
            try
            {
                var url = "https://jsonplaceholder.typicode.com/photos?";

                if (AlbumIds?.Length > 0)
                {
                    foreach (var aid in AlbumIds)
                    {
                        url += $"&albumId={aid}";
                    }
                }
                else
                {
                    url += "&albumId=-1";
                }

                // https://github.com/typicode/json-server#slice
                url += $"&_start={getParams.StartRow}";
                url += $"&_end={getParams.EndRow}";

                if (getParams.SortModel?.Length > 0)
                {
                    // https://github.com/typicode/json-server#sort
                    url += $"&_sort={string.Join(",", getParams.SortModel.Select(sm => sm.ColumnId))}";
                    url += $"&_order={string.Join(",", getParams.SortModel.Select(sm => sm.Direction))}";
                }

                // TODO: FILTER

                using var http = HttpFactory.CreateClient();

                Console.WriteLine("Fetching from [{0}]", url);
                var resp = await http.GetAsync(url);

                resp.EnsureSuccessStatusCode();

                // https://github.com/typicode/json-server#slice
                resp.Headers.TryGetValues("X-Total-Count", out var totalCountHeader);
                var totalCount = int.TryParse(totalCountHeader?.FirstOrDefault(), out var totalCountInt)
                    ? (int?)totalCountInt
                    : null;

                var photos = await http.GetJsonAsync <Photo[]>(url);

                //Console.WriteLine("From [{0}:{1}], got [{2}] row(s) out of [{3}]",
                //    getParams.StartRow, getParams.EndRow, photos.Length, totalCount);

                await getParams.SuccessCallback(photos, totalCount);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to retrieve!");
                Console.WriteLine(ex.ToString());
                await getParams.FailCallback();
            }
        }
Пример #2
0
 public MockedEncompassHttpClientService(ILogger <MockedEncompassHttpClientService> logger)
 {
     _logger                  = logger;
     MockedHandler            = new Mock <HttpMessageHandler>();
     HttpFactory              = MockedHandler.CreateClientFactory();
     MockedClient             = HttpFactory.CreateClient("EncompassClient");
     BaseAddress              = new Uri(Faker.Internet.Url());
     MockedClient.BaseAddress = BaseAddress;
     MockedEncompassClient    = new EncompassApiService(MockedClient, new ClientParameters());
 }