refit icon indicating copy to clipboard operation
refit copied to clipboard

Shared URL prefix and placeholder values

Open thomaslevesque opened this issue 7 years ago • 3 comments

I have an interface with methods like this:

interface IItemsClient
{
    [Get("/api/tenant/{tenantKey}/item/{itemId}")]
    Task<FileSystemItem> GetItemAsync(string tenantKey, string itemId, CancellationToken cancellationToken = default);

    [Get("/api/tenant/{tenantKey}/item/{parentId}/children")]
    Task<CollectionResponse<FileSystemItem>> GetChildrenAsync(string tenantKey, string parentId, CancellationToken cancellationToken = default);

    ...
}

The tenantKey is passed in ALL methods, and the /api/tenant/{tenantId} prefix is common to all methods.

Is there a way to somehow share the prefix and placeholder value? e.g. doing something like this:

[Prefix("/api/tenant/{tenantKey}/item"]
interface IItemsClient
{
    [Get("/{itemId}")]
    Task<FileSystemItem> GetItemAsync(string itemId, CancellationToken cancellationToken = default);

    [Get("/{parentId}/children")]
    Task<CollectionResponse<FileSystemItem>> GetChildrenAsync(string parentId, CancellationToken cancellationToken = default);

    ...
}

var itemsClient = RestService.For<IItemsClient>(httpClient, refitSettings, new { tenantKey = "foo" });

This way I wouldn't have to pass the tenantKey explicitly to all methods.

(I realize I could just pass an HttpClient with a different BaseAddress, but I split my API into separate interfaces, and I'd rather use the same HttpClient for all of them)

If it's not supported, I'd be willing to submit a PR to add it.

thomaslevesque avatar Apr 20 '18 11:04 thomaslevesque

I think this would be great.

tibitoth avatar Apr 20 '18 15:04 tibitoth

This might be better achieved with the property support described in #163. If/when that's implemented, I think this makes sense.

bennor avatar Jun 11 '19 03:06 bennor

Since the 'property support' has not been implemented yet and this is such an old post, I have implemented a PR to add this functionality - https://github.com/reactiveui/refit/pull/1685

I think this would be very helpful.

JesseKlaasse avatar May 08 '24 17:05 JesseKlaasse