In my project I'm calling a lot of WebApi
with Refit
(link). Basically, I define the WebApi
as an interface
. For example:
public interface ICustomer
{
[Get("/v1/customer")]
Task<CustomerResponse> GetDetails([Header("ApiKey")] string apikey,
[Header("Authorization")] string token,
[Header("Referer")] string referer);
}
For each WebApi
, I create a client
like that:
public async Task<CustomerResponse> GetDetails(string apikey, string token)
{
CustomerResponse rsl = new CustomerResponse();
rsl.Success = false;
var customer = RestService.For<ICustomer>(apiUrl);
try
{
rsl = await customer.GetDetails(apikey, token, apiUrl);
rsl.Success = true;
}
catch (ApiException ax)
{
rsl.ErrorMessage = ax.Message;
}
catch (Exception ex)
{
rsl.ErrorMessage = ex.Message;
}
return rsl;
}
The only difference between clients are the interface (in the above example code ICustomer
), the return structure (in the example CustomerResponse
derives from BaseResponse
), and the function I have to call (in the example GetDetails
with params).
I should have a base class to avoid duplicated code. Thanks in advance.