refit icon indicating copy to clipboard operation
refit copied to clipboard

feature: Separating resources in generated client

Open hognevevle opened this issue 3 years ago • 0 comments

Is your feature request related to a problem? Please describe. Currently, it seems one can only generate an API client which defines all resources on the same "level". When defining the API interface, one can use inheritance to split the API definitions, but for the client, they will still appear together.

E.g.

public interface IMyApi : IFeatureOneApi, IFeatureTwoApi
{
    
}

public interface IFeatureOneApi
{
    [Post("/featureone/foo")]
    Task Foo([Body] SomeData data);
}

public interface IFeatureTwoApi
{
    [Post("/featuretwo/bar")]
    Task Bar([Body] SomeData data);
}

Now, it would be great if we could also separate these on the generated API client, in such a way that Foo would be accessed as FeatureOne.Foo, and Bar would be accessed as FeatureTwo.Bar. This would allow me to reuse resource naming, such as Add, Delete, etc. for multiple entities, e.g. FeatureOne.Add, without having to come up with creative namings that don't collide (like .AddFeatureOneEntity).

Describe the solution you'd like Already explained above. Perhaps it could be used like this:

public interface IMyApi
{
    public IFeatureOneApi FeatureOne { get; } 
    public IFeatureTwoApi FeatureTwo { get; } 
}

public interface IFeatureOneApi
{
    [Post("/featureone/foo")]
    Task Foo([Body] SomeData data);
}

public interface IFeatureTwoApi
{
    [Post("/featuretwo/bar")]
    Task Bar([Body] SomeData data);
}

While this solution already compiles and generates the right types, it throws an exception when trying to use it.

System.InvalidOperationException: IMyApi doesn't look like a Refit interface. Make sure it has at least one method with a Refit HTTP method attribute and Refit is installed in the project.
   at Refit.RestService.GetGeneratedType(Type refitInterfaceType) in /_/Refit/RestService.cs:line 173
   at Refit.RestService.For(Type refitInterfaceType, HttpClient client, IRequestBuilder builder) in /_/Refit/RestService.cs:line 76
   at Refit.RestService.For[T](HttpClient client, IRequestBuilder`1 builder) in /_/Refit/RestService.cs:line 20
   at Refit.RestService.For[T](HttpClient client, RefitSettings settings) in /_/Refit/RestService.cs:line 34
   at Refit.RestService.For[T](String hostUrl, RefitSettings settings) in /_/Refit/RestService.cs:line 54
   at Refit.RestService.For[T](String hostUrl) in /_/Refit/RestService.cs:line 65

I hope this made sense 🤞

hognevevle avatar May 04 '22 08:05 hognevevle