I am using Refit library in my Xamarin App, I want to set 10 seconds timeout for the request. Is there any way to do this in refit?
Interface:
interface IDevice
{
[Get("/app/device/{id}")]
Task<Device> GetDevice(string id, [Header("Authorization")] string authorization);
}
Invoking the API
var device = RestService.For<IDevice>("http://localhost");
var dev = await device.GetDevice("15e2a691-06df-4741-b26e-87e1eecc6bd7", "Bearer OAUTH_TOKEN");
I finally found a way of setting the timeout for a request in Refit. I used CancelationToken
. Here is the modified code after adding CancelationToken
Interface:
interface IDevice
{
[Get("/app/device/{id}")]
Task<Device> GetDevice(string id, [Header("Authorization")] string authorization, CancellationToken cancellationToken);
}
Invoking the API:
var device = RestService.For<IDevice>("http://localhost");
CancellationTokenSource tokenSource = new CancellationTokenSource();
tokenSource.CancelAfter(10000); // 10000 ms
CancellationToken token = tokenSource.Token;
var dev = await device.GetDevice("15e2a691-06df-4741-b26e-87e1eecc6bd7", "Bearer OAUTH_TOKEN", token);
It works properly for me. I don't know whether it is the proper way or not. If it is a wrong, kindly suggest the correct way.