/// <summary> /// Gets all starships from SWApi endpoint by calling <see cref="IApiService"/> /// </summary> /// <returns>All starships contained in SWApi endpoint</returns> public async Task <List <Starship> > GetAllStarships() { var response = new StarshipsResponse() { NextPageUrl = starshipsUrl }; var result = new List <Starship>(); do { response = await service.GetRequestAsync <StarshipsResponse>(response.NextPageUrl); if (response == null) { return(result); } if (response.Starships != null) { result.AddRange(response.Starships); } } while (!string.IsNullOrEmpty(response.NextPageUrl)); return(result); }
public async Task TestApiMockService() { Assert.NotNull(StarshipApiServiceMock.Starships); Assert.NotEmpty(StarshipApiServiceMock.Starships); var mockService = new StarshipApiServiceMock(); var items = new List <Starship>(); var response = new StarshipsResponse() { NextPageUrl = "localhost" }; do { var result = await mockService.GetRequestAsync(response.NextPageUrl); Assert.NotNull(result); response = JsonConvert.DeserializeObject <StarshipsResponse>(result); Assert.NotNull(response); Assert.NotNull(response.Starships); items.AddRange(response.Starships); } while (response.NextPageUrl != null); Assert.Equal(StarshipApiServiceMock.Starships.Count, items.Count); for (int i = 0; i < StarshipApiServiceMock.Starships.Count; i++) { Assert.True(StarshipsEqual(items[i], StarshipApiServiceMock.Starships[i])); } }
public async Task <List <Starship> > RefreshDataAsync() { starships = new List <Starship>(); try { var originalStarships = await GetStarshipsAsync(_client); int pages = originalStarships.Count / originalStarships.Results.Count; if (originalStarships.Count % originalStarships.Results.Count != 0) { pages++; } originalStarships.Results.ForEach(starship => { starships.Add(starship); }); for (int i = 2; i < pages + 1; i++) { StarshipsResponse starshipsResponse = await GetStarshipsAsync(_client, i); starshipsResponse.Results.ForEach(starship => { starships.Add(starship); }); } } catch (Exception ex) { Debug.WriteLine(@"\tERROR {0}", ex.Message); } return(starships); }
public async Task <StarshipsResponse> GetStarshipsAsync(HttpClient client, int page = 1) { StarshipsResponse starships = null; HttpResponseMessage response = await client.GetAsync($"{BaseUri}/?page={page}"); if (response.IsSuccessStatusCode) { starships = await response.Content.ReadAsAsync <StarshipsResponse>(); } return(starships); }
private async Task TestStarshipsCollection(StarshipsResponse response, Action <List <Starship> > test) { var mock = new Mock <IApiService>(); mock.Setup(x => x.GetRequestAsync(It.IsNotNull <string>())).Returns(Task.FromResult(JsonConvert.SerializeObject(response))); mock.Setup(x => x.GetRequestAsync <StarshipsResponse>(It.IsNotNull <string>())).Returns(Task.FromResult(response)); var mockService = new SWApiService(mock.Object); var starships = await mockService.GetAllStarships(); var starships2 = await mockService.GetAllStarshipsParallelly(); test.Invoke(starships); test.Invoke(starships2); }