Skip to content

Latest commit

 

History

History
110 lines (84 loc) · 2.39 KB

README.md

File metadata and controls

110 lines (84 loc) · 2.39 KB

BytecodeApi.Rest

Fluent REST client.

Examples

BytecodeApi.Rest

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 and MultipartStringContent
  • 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.

Changelog

3.0.3 (28.11.2024)

  • new: RestRequest.Header method

3.0.2 (27.07.2024)

  • new: RestRequest.ReadFile method

3.0.1 (10.12.2023)

  • new: RestClient.RequestOptions property
  • new: RestRequest.MultipartFileContent and MultipartStringContent methods
  • new: RestRequest.ReadByteArray with progress callback
  • new: RestRequestOptions class

3.0.0 (08.09.2023)

  • Initial release