Comments (14)
This isn't possible right now, the best you can do currently is use partial classes (Partial interfaces? Is that a thing?) The idea of nesting objects is kind of interesting though
from refit.
Yeah. I'm new to this library, and i'm experimenting with it. Maybe one singe big interface is a better solution, having all the endpoints in one place, it's easier to navigate and understand. I'll play around more. Thanks.
from refit.
It seems reasonable for interface-returning properties on API interfaces to return Refit proxies for the returned interface.
from refit.
I've begun adding support for it (see here).
Currently, a refit interface can be composed of other refit interfaces like this:
public interface IServicePart
{
[Get("/")]
Task<List<string>> GetUsers();
[Get("/{user}")]
Task<string> GetUser(string user);
}
public interface IService
{
[Compose("/users")]
IServicePart Users { get; }
The attribute is mandatory here, unless we have all the compile time types (requiring another part of Roslyn).
Of course, we can compose multiple interface, even generic interfaces.
What do you think of it ?
from refit.
I'm not opposed to it but I'm not sure we need the compose attribute to generate the stubs - as long as all of the interfaces you're composing are in the same project, you should be able to pick up any wrapper interfaces with a second pass:
- Run through interfaces and detect all Refit interfaces (as we currently do)
- Run through interfaces again and look for any that have properties which are in the first list
We could run into problems without an attribute though, because it would mean having to pass the relative paths in when calling RestService.For()
which could get a little clunky. I think I prefer the attribute.
@paulcbetts?
I think it looks good so far. What else is there still to do on it (other than tests)? I think anything that we're building into the stubs that will throw an exception should raise a warning at build time too, so you'll want to do that as soon as we get #79 merged.
from refit.
Am I understanding the semantics right here?
public interface IServicePart
{
// Actually makes a request to /users
[Get("/")]
Task<List<string>> GetUsers();
// Actually makes a request to /users/{user}
[Get("/{user}")]
Task<string> GetUser(string user);
}
public interface IService
{
[Compose("/users")]
IServicePart Users { get; }
}
How is this better (this is not a rhetorical question, it very may well be!) than:
public interface IService
{
IServicePart Users { get; }
}
public class Service
{
IServicePart Users {
get { return RestService.For<IServicePart>("https://myservice.com/users"); }
}
}
from refit.
It still allows you to have refit method inside your interface and compose it.
This will work:
public interface IServicePart
{
// Actually makes a request to /users
[Get("/")]
Task<List<string>> GetUsers();
// Actually makes a request to /users/{user}
[Get("/{user}")]
Task<string> GetUser(string user);
}
public interface IService
{
[Compose("/users")]
IServicePart Users { get; }
[Get("/")]
Task<List<string>> Get();
}
Maybe it is too much and interface composition shouldn't be mixed with refit service definition.
from refit.
How is this better (this is not a rhetorical question, it very may well be!)...
I think it's potentially worse actually - you lose the control over the HttpClient
that you get with a direct call to RestService.For<IServicePart>(someClient)
.
If we go ahead with it, we will need to make sure we allow for a single HttpClient
instance to be shared by all nested calls to RestService.For()
, which could get complicated.
from refit.
Sorry, accidentally clicked the button.
from refit.
I think you could just do this better with Extension Methods:
public interface IService
{
[Get("/")]
Task<List<string>> Get();
}
public static class UsersExtension
{
public static IServicePart Users(this IService This) {
return RestService.For<IServicePart>("https://myservice.com/users");
}
}
from refit.
Looks good.
You lose the garantee that both services will use the same base address but it will probably be easier to use.
Currently, I don't have [Header(...)] inheritance in sub-service, so you have to specify it everywhere.
If it isn't needed, no need to support the whole sub-service generation.
from refit.
Let's close this out then, unless someone comes up with a super cool use-case
from refit.
Maybe we could update the Readme to include your solution ?
from refit.
@nekresh Can you submit a PR to do that?
from refit.
Related Issues (20)
- [Bug]: Naming the interface to end with "Client" will result in an error during injection.
- Generated code should use `Array.Empty` or constant field where possible HOT 1
- [Bug]: Failed to generate generated classes in Azure Devops with Refit version 6.3.2 and 7.0.0
- Suggestion: run code cleanup/formatter HOT 1
- Target .NET 8 in Refit HOT 4
- Make fullstack C# development easier
- Add handlers to the interface method parameters and headers before generating the HttpRequestMessage
- [Bug]: POST request sent as GET HOT 9
- [Bug]: Long-lived HttpClient instance despite use of IHttpClientFactory HOT 1
- [Bug]: HttpClients not disposed
- [Bug]: Request streams get disposed and there is no option to avoid it
- Source generator for minimal API
- [Bug]: Cannot set the Content-Type header for a POST request HOT 2
- [Bug]: Refit Does Not Generate Implementations if 3+ Interfaces Share a Name HOT 1
- [Bug]: exception with TrimMode full: "URL /path/{id} has parameter id, but no method parameter matches"
- Dependency Dashboard
- [NiceToHave]: Interface `IApiResponse<out T>` misses `[MemberNotNullWhen(true, nameof(Content))]` attribute
- [Bug]: Using `Refit.HttpClientFactory` with a .Net 8 Blazor Web App HOT 5
- Ability to more simply configure a scoped authorization token
- Support [ValueConvertion] attribute HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from refit.