I am attempting to make a method that can iteratively call RESTful API endpoints that implement paging until a specific JSON object is found. I'll outline the intended pattern below and then describe the issue in more detail.
// ApiClient.cs
// Example methods that call REST API
public async Task<ICollection<PetsApiResonse>> GetPets(string searchQuery, int skip = 0, int take = 20, bool includeAdopted = false)
{
// Logic that makes HTTP request to something like 'GET {baseUri}/pets'
}
public async Task<ICollection<EmployeesApiResonse>> GetEmployees(string searchQuery, int skip = 0, int take = 20)
{
// Logic that makes HTTP request to something like 'GET {baseUri}/employees'
}
// RequestHandler.cs
// Example method expecting Func<> that doesn't work yet
public static async Task<T> PagedLookupAsync<T>(Func<Task<PetShopApiResponse<ICollection<T>>>> getResponse, string propertyToCompare, object valueToCompare, int maxIterations = 10)
{
Type type = typeof(T);
PropertyInfo propertyInfo = type.GetProperty(propertyToCompare);
ICollection<T> response;
do
{
// This is where I want to alter the 'skip' parameter's value so that each iteration observes a different page from the API
response = await getResponse();
maxIterations--;
}
while (maxIterations >= 0 && !response.Any(o => valueToCompare.Equals(propertyInfo.GetValue(o))));
return response.FirstOrDefault(o => valueToCompare.Equals(propertyInfo.GetValue(o)));
}
// Example code that might be in the body of an NUnit test case
var petIdToFind = "123";
var response = RequestHandler.PagedLookupAsync(ApiClient.GetPetsAsync("sparky", take: 50), nameof(PetsApiResonse.Id), petIdToFind);
response.Result.Should().NotBeNull();
response.Result.Id.Should().BeEquivalentTo(petIdToFind);
As you may have guessed, the goal is to test for a record existing and being searchable. The issue is that searches do not always return the desired record in the first page of results, which makes for flaky testing. The obvious route would be to simply write loops in every test case that needs this functionality, but I'm hoping there's a way to centralize the logic.
skip
for page start and take
for page size.Update
My solution differs slightly from marsze's suggestion, but is based on the same approach. I ended up dropping the reflection entirely since it shouldn't be needed anymore:
// RequestHandler.cs
public static async Task<T> PagedLookupAsync<T>(
Func<(int SkipState, int TakeState), Task<PetShopApiResponse<ICollection<T>>>> getResponse,
Func<T, bool> lookupFunction,
int maxIterations = 10)
{
int skip = 0;
int take = 20;
ICollection<T> response;
bool isFound = false;
do
{
response = await getResponse(skip, take);
isFound = response.Any(o => lookupFunction(o));
skip += take;
maxIterations--;
}
while (maxIterations >= 0 && !isFound);
return response.FirstOrDefault(o => lookupFunction(o));
}
var response = RequestHandler.PagedLookupAsync((s, t) =>
ApiClient.GetPetsAsync("sparky", skip: s, take: t),
(lookup) => lookup.Id == petIdToFind);
One way would be simply including the pagination parameters skip
and take
in your callback:
public delegate Task<ICollection<T>> GetResponse<T>(int skip, int take);
public static async Task<T> PagedLookupAsync<T>(
GetResponse<T> getResponse,
string propertyToCompare,
object valueToCompare,
int maxIterations = 10,
take = 20
)
{
Type type = typeof(T);
PropertyInfo propertyInfo = type.GetProperty(propertyToCompare);
ICollection<T> response;
int skip = 0;
do
{
// This is where I want to alter the 'skip' parameter's value so that each iteration observes a different page from the API
response = await getResponse(skip, take);
skip += take;
maxIterations--;
}
while (maxIterations >= 0 && !response.Any(o => valueToCompare.Equals(propertyInfo.GetValue(o))));
return response.FirstOrDefault(o => valueToCompare.Equals(propertyInfo.GetValue(o)));
}
Then call it like this:
var response = RequestHandler.PagedLookupAsync((skip, take) => ApiClient.GetPetsAsync("sparky", skip, take), nameof(PetsApiResonse.Id), petIdToFind, take: 50);