Activout.RestClient 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Activout.RestClient --version 1.0.0
NuGet\Install-Package Activout.RestClient -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Activout.RestClient" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Activout.RestClient --version 1.0.0
#r "nuget: Activout.RestClient, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Activout.RestClient as a Cake Addin
#addin nuget:?package=Activout.RestClient&version=1.0.0

// Install Activout.RestClient as a Cake Tool
#tool nuget:?package=Activout.RestClient&version=1.0.0

Activout Rest Client

Build Status

Create a REST(ish) API client only by defining the C# interface you want

Shamelessly inspired by Rest Client for MicroProfile.

Rationale

The Activout Rest Client provides a type-safe approach to invoke RESTful services over HTTP. As much as possible the Rest Client attempts to use .NET Core MVC APIs for consistency and easier re-use.

Example

Here is an example - let’s say that you want to use a movie review service. The remote service might provide APIs to view users' reviews and allow you to post and modify your own reviews. You might start with an interface to represent the remote service like this:

[InterfaceRoute("movies")]
[ErrorResponse(typeof(ErrorResponse))]
[InterfaceConsumes("application/json")]
public interface IMovieReviewService
{
    [HttpGet]
    Task<IEnumerable<Movie>> GetAllMovies();

    [HttpGet]
    [Route("/{movieId}/reviews")]
    Task<IEnumerable<Review>> GetAllReviews([RouteParam] string movieId);

    [HttpGet("/{movieId}/reviews/{reviewId}")]
    Review GetReview([RouteParam] string movieId, [RouteParam] string reviewId);

    [HttpPost]
    [Route("/{movieId}/reviews")]
    Task<Review> SubmitReview([RouteParam] string movieId, Review review);

    [HttpPut]
    [Route("/{movieId}/reviews/{reviewId}")]
    Review UpdateReview([RouteParam] string movieId, [RouteParam] string reviewId, Review review);

    [HttpPost("/import.csv")]
    [Consumes("text/csv")]
    Task Import(string csv);

    [HttpGet]
    Task<IEnumerable<Movie>> QueryMoviesByDate(
        [QueryParam] DateTime begin,
        [QueryParam] DateTime end);
}

Now we can use this interface as a means to invoke the actual remote review service like this:

var restClientFactory = Services.CreateRestClientFactory();
var movieReviewService = restClientFactory
            .HttpClient(_httpClient)
            .CreateBuilder()
            .BaseUri(new Uri("http://localhost:9080/movieReviewService"))
            .Build<IMovieReviewService>();

Review review = new Review(stars: 3, "This was a delightful comedy, but not terribly realistic.");
await movieReviewService.SubmitReview(movieId, review);

This allows for a much more natural coding style, and the underlying implementation handles the communication between the client and service - it makes the HTTP connection, serializes the Review object to JSON/etc. so that the remote service can process it.

Usage notes

  • Exceptions will be wrapped in AggregatedException
  • Both synchronous and asynchronous calls are supported. Asynchronous is recommended.
  • Additional serializers and deserializers can be added at will.
  • Support for custom error objects via [ErrorResponse] attribute. These will be included in a RestClientException that is thrown if the API call fails.

TODO

  • Caching of RequestHandler instances
  • Support for cookie parameters
  • Support for header parameters
  • Support for default headers
  • More real-life testing πŸ˜ƒ

Collaborate

This project is still under development - participation welcome!

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 is compatible.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.1.0 495 5/10/2022
3.0.5-beta 318 12/11/2020
3.0.4-beta2 273 12/7/2020
3.0.3 1,061 12/1/2020
2.4.0 586 2/5/2020
2.3.0 587 10/27/2019
2.0.0 664 4/26/2019
1.6.0 937 9/14/2018
1.5.0 869 9/13/2018
1.4.0 880 9/11/2018
1.3.0 878 9/11/2018
1.2.0 960 8/16/2018
1.1.0 911 7/21/2018
1.0.2 930 7/21/2018
1.0.1 863 7/21/2018
1.0.0 886 7/20/2018