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.
- Can reflection be used to access the method passed to a Func<>? In my example above, simply modifying the ‘skip’ parameter would solve this problem, but from what I can tell I can only access the Func<>’s params; not the params of one of its params.
- If the answer to #1 is no, is there a better approach to centralizing this logic that I haven’t considered? Additional notes below:
- Methods making API requests are generated using an OpenApi spec. I believe I can extend them, but I do not have direct access to modify them.
- I can guarantee that parameter naming is always the same; i.e.
skip
for page start andtake
for page size.
officeSpacePirate is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.