Ejemplo n.º 1
0
Archivo: Api.cs Proyecto: DeanDM/Funda
 private static IEnumerable <GroupedResult> SquashedAndCountedList(MakelaarResults listOfMakelaars)
 => listOfMakelaars
 .Objects
 .GroupBy(p => p.MakelaarId)
 .Select(p => new GroupedResult
 {
     MakelaarId = p.Key,
     Count      = p.Count()
 });
Ejemplo n.º 2
0
Archivo: Api.cs Proyecto: DeanDM/Funda
        public static async Task <MakelaarResults> For(Uri baseUri)
        {
            //Initalize all the values
            var makelaarResults  = new MakelaarResults();
            var client           = new HttpClient();
            var counter          = 1;
            var isThereANextPage = true;

            // I use Polly to handle retries. If this would fail for some reason it would retry once but we
            // can configure it to retry multiple times with a timeout if we want. Thanks to Polly that's quite easy
            var retryPolicy = Policy
                              .Handle <Exception>()
                              .Retry();

            do
            {
                //Build the request
                var request = new HttpRequestMessage
                {
                    RequestUri = new Uri(baseUri + counter.ToString()),
                    Method     = HttpMethod.Get,
                };

                // I like working with json a bit more so I opted for json
                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                // do "get a page" while "there is a next page"
                await retryPolicy.Execute(() => client.SendAsync(request)
                                          .ContinueWith(async getMakelaarsTask =>
                {
                    var res = await getMakelaarsTask;
                    if (res.IsSuccessStatusCode == false)
                    {
                        isThereANextPage = false;
                        // Ideally this would log somewhere too.
                        throw new Exception($"Something went wrong calling the api: result = {await res.Content.ReadAsStringAsync()}");
                    }

                    // If we made it here that means our request was a success and we have some data.
                    // deserialize it and add it to the previous results
                    var rawJsonResult = await res.Content.ReadAsStringAsync();

                    var listOfMakelaars = JsonConvert.DeserializeObject <MakelaarResults>(rawJsonResult);
                    makelaarResults.Objects.AddRange(listOfMakelaars.Objects);

                    // I know this is a bit weird but the result of volgende url looks like
                    // /~/koop/amsterdam/p2/ which is a bit annoying because i can't just paste
                    // that at the end of the request which has the format ?type=koop&zo=/amsterdam
                    // so I just add the page at the end and if there's a value in here, just
                    // go to the next page, else set nextpage to empty string so the loop ends
                    if (string.IsNullOrEmpty(listOfMakelaars.Paging?.VolgendeUrl) == false)
                    {
                        counter++;
                    }
                    else
                    {
                        isThereANextPage = false;
                    }
                }));
            } while (isThereANextPage);

            return(makelaarResults);
        }