public async Task <List <Stack> > ListStacks(string cfTarget, string accessToken) { // trust any certificate ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => { return(true); }; var uri = new UriBuilder(cfTarget) { Path = ListStacksPath, }; HypertextReference firstPageHref = new HypertextReference() { Href = uri.ToString() }; return(await GetRemainingPagesForType(firstPageHref, accessToken, new List <Stack>())); }
/// <summary> /// Recursively requests all pages of results for spaces under the org specified by <paramref name="orgGuid"/>. /// <para> /// Exceptions: /// <para> /// Throws any exceptions encountered while creating/issuing request or deserializing response. /// </para> /// </para> /// </summary> /// <param name="cfApiAddress"></param> /// <param name="accessToken"></param> /// <param name="orgGuid"></param> /// <returns>List of <see cref="Space"/>s.</returns> public async Task <List <Space> > ListSpacesForOrg(string cfApiAddress, string accessToken, string orgGuid) { // trust any certificate ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => { return(true); }; var uri = new UriBuilder(cfApiAddress) { Path = ListSpacesPath, Query = $"organization_guids={orgGuid}", }; HypertextReference firstPageHref = new HypertextReference() { Href = uri.ToString() }; return(await GetRemainingPagesForType(firstPageHref, accessToken, new List <Space>())); }
public FakeRoutesResponse(string apiAddress, int pageNum, int totalResults, int totalPages, int resultsPerPage) : base() { bool isFirstPage = pageNum == 1; bool isLastPage = pageNum == totalPages; var firstHref = new HypertextReference() { Href = $"{apiAddress}{CfApiClient.ListRoutesPath}?page=1&per_page={resultsPerPage}" }; var lastHref = new HypertextReference() { Href = $"{apiAddress}{CfApiClient.ListRoutesPath}?page={totalPages}&per_page={resultsPerPage}" }; var nextHref = isLastPage ? null : new HypertextReference() { Href = $"{apiAddress}{CfApiClient.ListRoutesPath}?page={pageNum + 1}&per_page={resultsPerPage}" }; var previousHref = isFirstPage ? null : new HypertextReference() { Href = $"{apiAddress}{CfApiClient.ListRoutesPath}?page={pageNum - 1}&per_page={resultsPerPage}" }; Pagination = new Pagination { Total_results = totalResults, Total_pages = totalPages, First = firstHref, Last = lastHref, Next = nextHref, Previous = previousHref, }; Route[] routes; if (isLastPage) { int numResourcesInLastPage = totalResults % resultsPerPage; routes = new Route[numResourcesInLastPage]; for (int i = 0; i < numResourcesInLastPage; i++) { routes[i] = new Route { Guid = $"fakeRouteId-{i + 1}", }; } } else { routes = new Route[resultsPerPage]; for (int i = 0; i < resultsPerPage; i++) { routes[i] = new Route { Guid = $"fakeRouteId-{i + 1}", }; } } Routes = routes; }
public FakeBuildpacksResponse(string apiAddress, int pageNum, int totalResults, int totalPages, int resultsPerPage) { bool isFirstPage = pageNum == 1; bool isLastPage = pageNum == totalPages; var firstHref = new HypertextReference() { Href = $"{apiAddress}{CfApiClient.ListBuildpacksPath}?page=1&per_page={resultsPerPage}" }; var lastHref = new HypertextReference() { Href = $"{apiAddress}{CfApiClient.ListBuildpacksPath}?page={totalPages}&per_page={resultsPerPage}" }; var nextHref = isLastPage ? null : new HypertextReference() { Href = $"{apiAddress}{CfApiClient.ListBuildpacksPath}?page={pageNum + 1}&per_page={resultsPerPage}" }; var previousHref = isFirstPage ? null : new HypertextReference() { Href = $"{apiAddress}{CfApiClient.ListBuildpacksPath}?page={pageNum - 1}&per_page={resultsPerPage}" }; Pagination = new Pagination { Total_results = totalResults, Total_pages = totalPages, First = firstHref, Last = lastHref, Next = nextHref, Previous = previousHref, }; Buildpack[] buildpacks; var numPreviousResults = (pageNum - 1) * resultsPerPage; int numStackTypesPerBuildpack = 3; /* INTENTION: assign Buildpacks prop to contain a list like this: * bp.Name = fakeBuildpack1, bp.Stack = fakeStack1 * bp.Name = fakeBuildpack1, bp.Stack = fakeStack2 * bp.Name = fakeBuildpack1, bp.Stack = fakeStack3 * bp.Name = fakeBuildpack2, bp.Stack = fakeStack1 * bp.Name = fakeBuildpack2, bp.Stack = fakeStack2 * bp.Name = fakeBuildpack2, bp.Stack = fakeStack3 * bp.Name = fakeBuildpack3, bp.Stack = fakeStack1 * ... */ if (isLastPage) { int numResourcesInLastPage = totalResults % resultsPerPage; buildpacks = new Buildpack[numResourcesInLastPage]; for (int i = 0; i < numResourcesInLastPage; i++) { int buildpackId = i / numStackTypesPerBuildpack + 1 + numPreviousResults; int stackTypeId = i % numStackTypesPerBuildpack + 1 + numPreviousResults; buildpacks[i] = new Buildpack { Name = $"fakeBuildpack{buildpackId}", Stack = $"fakeStack{stackTypeId}", }; } } else { buildpacks = new Buildpack[resultsPerPage]; for (int i = 0; i < resultsPerPage; i++) { int buildpackId = i / numStackTypesPerBuildpack + 1 + numPreviousResults; int stackTypeId = i % numStackTypesPerBuildpack + 1 + numPreviousResults; buildpacks[i] = new Buildpack { Name = $"fakeBuildpack{buildpackId}", Stack = $"fakeStack{stackTypeId}", }; } } Buildpacks = buildpacks; }
private async Task <List <TResourceType> > GetRemainingPagesForType <TResourceType>(HypertextReference pageAddress, string accessToken, List <TResourceType> resultsSoFar) { if (pageAddress == null) { return(resultsSoFar); } var request = new HttpRequestMessage(HttpMethod.Get, pageAddress.Href); request.Headers.Add("Authorization", "Bearer " + accessToken); var response = await _httpClient.SendAsync(request); if (!response.IsSuccessStatusCode) { throw new Exception($"Response from GET `{pageAddress}` was {response.StatusCode}"); } string resultContent = await response.Content.ReadAsStringAsync(); HypertextReference nextPageHref; if (typeof(TResourceType) == typeof(Org)) { var results = JsonConvert.DeserializeObject <OrgsResponse>(resultContent); resultsSoFar.AddRange((IEnumerable <TResourceType>)results.Orgs.ToList()); nextPageHref = results.Pagination.Next; } else if (typeof(TResourceType) == typeof(Space)) { var results = JsonConvert.DeserializeObject <SpacesResponse>(resultContent); resultsSoFar.AddRange((IEnumerable <TResourceType>)results.Spaces.ToList()); nextPageHref = results.Pagination.Next; } else if (typeof(TResourceType) == typeof(App)) { var results = JsonConvert.DeserializeObject <AppsResponse>(resultContent); resultsSoFar.AddRange((IEnumerable <TResourceType>)results.Apps.ToList()); nextPageHref = results.Pagination.Next; } else if (typeof(TResourceType) == typeof(Buildpack)) { var results = JsonConvert.DeserializeObject <BuildpacksResponse>(resultContent); resultsSoFar.AddRange((IEnumerable <TResourceType>)results.Buildpacks.ToList()); nextPageHref = results.Pagination.Next; } else if (typeof(TResourceType) == typeof(Stack)) { var results = JsonConvert.DeserializeObject <StacksResponse>(resultContent); resultsSoFar.AddRange((IEnumerable <TResourceType>)results.Stacks.ToList()); nextPageHref = results.Pagination.Next; } else if (typeof(TResourceType) == typeof(Route)) { var results = JsonConvert.DeserializeObject <RoutesResponse>(resultContent); resultsSoFar.AddRange((IEnumerable <TResourceType>)results.Routes.ToList()); nextPageHref = results.Pagination.Next; } else { throw new Exception($"ResourceType unknown: {typeof(TResourceType).Name}"); } return(await GetRemainingPagesForType(nextPageHref, accessToken, resultsSoFar)); }