Search code examples
c#.netreflectionpagingfunc

Can I access params of method execution passed to a Func<> in C#/.NET?


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.

  1. 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.
  2. 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 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);

Solution

  • 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);