refit icon indicating copy to clipboard operation
refit copied to clipboard

Configurable base paths for one API

Open hiraldesai opened this issue 8 years ago • 5 comments

I am trying to use Refit for an API that has weird routing/paths that are not very RESTful. It has the same base url but each function has its own unique Guid and couple of other things. I want to make these things configurable. Is it doable?

public interface ISomeApi
{
    [Get("{some-configurable-value-1}/{id}")]
    Task<Something> GetSomething(string id);

    [Get("{some-configurable-value-2}/{id}")]
    Task<SomethingElse> GetSomethingElse(string id);
}

So far - the only option I've been able to think of is to have one Refit interface per path and set a different base url for each of those interfaces which are configurable.

hiraldesai avatar Aug 03 '18 04:08 hiraldesai

I'm not sure if this covers it, but you can provide those arguments as parameters:

public interface ISomeApi
{
    [Get("{prefix}/{id}")]
    Task<Something> GetSomething(string prefix, string id);

    [Get("{prefix}/{id}")]
    Task<SomethingElse> GetSomethingElse(string prefix, string id);
}

The other option is the one you suggested -- using a different base URL for two different interfaces.

Did you have any other ideas for how an alternative might work?

bennor avatar Aug 03 '18 04:08 bennor

Also, if your API is really that simple, you could make it generic:

public interface ISomeApi<T>
{
    [Get("{prefix}/{id}")]
    Task<T> Get(string prefix, string id);
}

bennor avatar Aug 03 '18 04:08 bennor

Great - the parameter option will work for me! And no, the API isn't as simple to put a generic on it.

hiraldesai avatar Aug 03 '18 08:08 hiraldesai

Is there an attribute I can use to Uri.UnescapeDataString(path)?

 [Get("{path}/{id}")]
 Task<Something> GetSomething(string path, string id);

My path value here is of the format /path/to/object/{id}.

Edit: I just checked the underlying implementation and it looks like splitting the value of path into multiple parameters is the only option. Although it'd be great if you can confirm that as I may not have looked hard enough.

hiraldesai avatar Aug 07 '18 01:08 hiraldesai

Closing because this was a trivial change for me. Also it's more readable anyway since the actual API parameters for me were more along the lines of /{apiName}/{apiVersion}/{apiPath}/{id} and so on.

hiraldesai avatar Aug 07 '18 01:08 hiraldesai