Fluent REST client.
RestClient
The RestClient
is an abstract class that provides fluent methods to query REST services.
public class MyService : RestClient
{
public MyService(string baseUrl) : base(baseUrl)
{
}
public async Task<User[]> GetUsers(string searchText)
{
return await Get($"{BaseUrl}/users")
.QueryParameter("search", searchText)
.ReadJson<User[]>();
}
public async Task UpdateUser(User user, bool activate)
{
await Post($"{BaseUrl}/users/update")
.QueryParameter("activate", activate)
.JsonContent(user)
.ReadString();
}
}
- The fluent call starts with
Get
,Post
,Put
,Patch
, etc... - The following calls apply optional parameters to the HTTP request:
QueryParameter
StringContent
JsonContent
FormUrlEncodedContent
MultipartFileContent
andMultipartStringContent
- Finally, execute the REST request by calling either of the following:
ReadString
ReadByteArray
ReadJson
RestException
If a RestClient
encounters an error, a RestException
is thrown:
try
{
MyService myService = new("http://api.exmample.com");
User[] users = await myService.GetUsers();
}
catch (RestException ex)
{
Console.WriteLine(ex.StatusCode);
Console.WriteLine(ex.Content); // The HTML body
}
RestRequestOptions
Modify the properties in RestClient.RequestOptions
to configure formats, etc. of REST request:
public class MyService : RestClient
{
public MyService(string baseUrl) : base(baseUrl)
{
RequestOptions.QueryParameterDateTimeFormat = "dd.MM.yyyy HH:mm:ss";
RequestOptions.QueryParameterDateOnlyFormat = "dd.MM.yyyy";
RequestOptions.QueryParameterTimeOnlyFormat = "HH:mm:ss";
}
}
The configured formats are used in RestRequest.QueryParameter
.
- new:
RestRequest.Header
method
- new:
RestRequest.ReadFile
method
- new:
RestClient.RequestOptions
property - new:
RestRequest.MultipartFileContent
andMultipartStringContent
methods - new:
RestRequest.ReadByteArray
with progress callback - new:
RestRequestOptions
class
- Initial release