RestClient

About RestClient: simple REST and HTTP API Client for .NET

RestSharp Repo stars is one of the most powerful and performant REST client libraries in the .NET ecosystem.

Unlike other widely used libraries, Refit has been using a concept of automatic source code generation of the REST client at development time (build time) for years. This reduces runtime overhead, increases performance and improves reliability.
For this purpose, Refit used to use so-called stubs, but now Refit uses source code generators).

The documentation of Refit is excellent.

Info Link
License GitHub
Downloads Nuget
Latest Version GitHub release (latest by date)
Issues GitHub issues
Contributors GitHub contributors

RestSharp is a very simple and very basic library for REST communication with .NET. The library abstracts based on very simple HTTP calls, which is very similar to native HttpClient programming.
No code is generated automatically, but you work manually with an instance that is enriched with parameters. The call must be done manually, the deserialization is supported in a simple way - again similar to HttpClient.

Client sample

// Create options
RestClientOptions options = new RestClientOptions("my-rest-api.com")
{
    ThrowOnAnyError = true,
    Timeout = 30
};

// Create client
RestClient client = new RestClient(options);

// Get single task
{
    RestRequest request = new RestRequest("tasks/1234");
    TaskApiModel? task = await client.GetAsync<TaskApiModel>(request, cancellationToken);
}

// Get all tasks
{
    RestRequest request = new RestRequest("tasks");
    TaskApiModel[]? tasks = await client.GetAsync<TaskApiModel[]>(request, cancellationToken);
}

// Add new task
{
    TaskApiCreateModel newTask = new TaskApiCreateModel { Title = "Buy flowers" };
    RestRequest request = new RestRequest("tasks").AddJsonBody(newTask);
    TaskApiModel? createdTask = await client.GetAsync<TaskApiModel?>(request, cancellationToken);
}

Wrapper sample

Since RestSharp is really very simple, it is recommended to abstract the entire RestClient. For this purpose a wrapper pattern is useful.

/*** Wrapper Sample ***/

public class MyTaskApiClient
{
    private readonly RestClient _client;

    public MyTaskApiClient(string baseUrl)
    {
        _client = new RestClient(baseUrl);
    }

    public Task<TaskApiModel?> GetTask(int id, CancellationToken cancellationToken = default)
        => _client.GetAsync<TaskApiModel>(new RestRequest($"task/{id}"), cancellationToken);

    public Task<List<TaskApiModel>?> GetAllTasks(CancellationToken cancellationToken = default) =>
        _client.GetAsync<List<TaskApiModel>>(new RestRequest("tasks"), cancellationToken);

    public Task<TaskApiModel?> AddTask(string title, CancellationToken cancellationToken = default)
    {
        TaskApiCreateModel newTask = new TaskApiCreateModel { Title = title };
        RestRequest request = new RestRequest("tasks").AddJsonBody(newTask);
        return _client.GetAsync<TaskApiModel>(request, cancellationToken);
    }
}

Comparison

Pros

  • Very simple client
  • Good serialization.
  • Good for slim szenarios.

Cons

  • Relatively many allocations necessary and therefore more inefficient in comparison.
  • More implementation effort, since a wrapper is always necessary in real applications.
  • If it goes beyond simple scenarios, you have to do a lot of programming yourself.

Full Sample

See the full sample on GitHub: https://github.com/BenjaminAbt/dotnet.rest-samples