Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/davidfowl/restsample
https://github.com/davidfowl/restsample
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/davidfowl/restsample
- Owner: davidfowl
- Created: 2017-08-15T08:41:22.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-08-15T17:35:31.000Z (over 7 years ago)
- Last Synced: 2024-10-15T05:28:15.927Z (2 months ago)
- Language: C#
- Size: 7.81 KB
- Stars: 42
- Watchers: 9
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## RestClient sample
This is a sample of an end to end client experience that we could build with ASP.NET Core using [refit](https://github.com/paulcbetts/refit) as as implementation detail.
The idea here is to make it easy to declare and inject an autogenerated proxy for an HTTP service. That is acheived by registering an
open generic RestClient\ that acts like a factory for a TClient. Each TClient must be configured in the ConfigureServices with the url (or http client). Other more advanced settings can also be set there.### Fundamentals
1. Define an interface for your rest service.
````csharp
public interface IConferencePlannerApi
{
[Get("/api/sessions")]
Task> GetSessionsAsync();
}
````1. Configure the URL for your service.
```csharp
public void ConfigureServices(IServiceCollection services)
{
services.AddRestClient(o =>
{
// Configure the URL for your service
o.Url = "https://conferenceplanner-api.azurewebsites.net/";
});
}
```
1. Consume the interface via RestClient\.
```csharp
public class HomeController : Controller
{
private readonly IConferencePlannerApi _client;public HomeController(RestClient api)
{
_client = api.Client;
}[HttpGet("/")]
public async Task Get()
{
var sessions = await _client.GetSessionsAsync();return Ok(sessions);
}
}
```### Future Ideas
- Integrate service discovery. Add the ability to name the service that a client represents via an attribute to avoid having to specify a URL in ConfigureServices.```csharp
[ServiceName("conference-api")]
public interface IConferencePlannerApi
{
[Get("/api/sessions")]
Task> GetSessionsAsync();
}
```
- Support the MVC attributes for consistency between client and server.
- Support returning ActionResult\. It would unify the story between the client and server even more. It could also bring efficiency gains by piping responses directly to the output when making outgoing calls.
- Integrate circuit breakers and other policy into the configuration.