Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specifying timeout with attribute #1590

Open
glb-cblin opened this issue Nov 6, 2023 · 3 comments
Open

Specifying timeout with attribute #1590

glb-cblin opened this issue Nov 6, 2023 · 3 comments

Comments

@glb-cblin
Copy link

Is your feature request related to a problem? Please describe.
Sometimes, for an API with multiple routes, we'd like different timeouts for each route

At the moment, the timeout can only be specified at the HTTP client level like this :

     services.AddRefitClient<MyClient>()
            .ConfigureHttpClient(c =>
            {
                c.BaseAddress = new Uri(options.BaseUrl);
                c.Timeout = new TimeSpan(0, 0, 5);
            });

Describe the solution you'd like

public interface MyClient
{
    [Get("/xxx")]
    [Timeout(5)]
    Task<IApiResponse<ADto>> GetXxx();

or

public interface MyClient
{
    [Get("/xxx", timeout = 5)]
    Task<IApiResponse<ADto>> GetXxx();

Describe alternatives you've considered
I tried to use a DelegatingHandler to acheve this BUT inside a delegating handler we have no idea about which method is called on the MyClient interface ....

In the end, we are currently using XxxClient with a single method (so we have 1 client for each route)

@bennor
Copy link
Contributor

bennor commented Nov 6, 2023

There are a few options for specifying timeouts at the request level shown here:

https://stackoverflow.com/questions/43315934/how-to-set-timeout-in-refit-library

Seems like a reasonable feature request though.

Stack Overflow
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}")]
Ta...

@glb-cblin
Copy link
Author

@bennor Thanks for the pointer ! I would prefer the attribute, do you have an idea about a possible implementation I could try on my own ?

For future reader, here is my workaround to make it "transparent" to the caller (i.e it does not break compilation with previous method)

It is not really beautiful but it does the job ...

public interface MyClient
{
    [Get("/xxx")]    
    Task<IApiResponse<ADto>> GetXxxInternal(CancellationToken cancellationToken);
}

public static class MyClientExtensions
{
    public static Task<IApiResponse<ADto>> GetXxx()
    {
        return c.GetXxxInternal(Timeout(5));
    }

    public static CancellationToken Timeout(long timeout)
    {
        var tokenSource = new CancellationTokenSource();
        tokenSource.CancelAfter(TimeSpan.FromSeconds(timeout));
        return tokenSource.Token;
    }
}

@bennor
Copy link
Contributor

bennor commented Nov 7, 2023

@glb-cblin There was a time where I would have known, but I haven't done any dev on this library (or any .NET) for years. 🙃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants