I am using the RestSharp library to access the REST API.
I want all API requests to go through the same method, so I can add headers, handle errors, and do other things in a central place.
So, I made a method that accepts a generic Func<> and that solves most of my problems, but I donβt know how to handle the case when I donβt have a return type.
private T PerformApiCall<T>(RestRequest restRequest, Func<RestRequest, IRestResponse<T>> restMethod) { var response = restMethod.Invoke(restRequest);
I call it this way:
var apples = PerformApiCall(new RestRequest('/api/apples'), req => Client.Execute<List<Apple>>(req));
But I ran into a problem, a bunch of API calls have no return type, because they do not return data. So I used Client.Execute(req) , and I got an error saying that type arguments could not be deduced, I tried to pass, but this failed because it could not convert the non-generic IRestResponse to printed.
Any ideas on how to handle this in a good way?
source share