RestBuilder is a C# library designed to simplify HTTP requests in Unity using UnityWebRequest. It supports asynchronous requests (GET, POST, PUT, PATCH, DELETE), JSON serialization/deserialization, header configuration, and dynamic URL building.
- Asynchronous HTTP requests using
UniTask(Cysharp.Threading.Tasks). - Support for methods: GET, POST, PUT, PATCH, DELETE.
- JSON serialization/deserialization via
Newtonsoft.Json. - Flexible URL construction with path and query parameters.
- Configuration of headers and cancellation tokens (
CancellationToken). - Error handling through
ApiException.
-
Dependencies:
- Install
Newtonsoft.Json(available via NuGet or Unity Package Manager; if you prefer not to use Newtonsoft, you can remove theNewtonsoftSerializerfile). - Install
Cysharp.Threading.Tasks(via NuGet or GitHub: UniTask). - Ensure
UnityWebRequestis available (built into Unity).
- Install
-
Adding RestBuilder:
- Use the
.unitypackagefrom the releases page.
- Use the
-
Initialization:
using RestBuilder; var serializer = new NewtonsoftSerializer(); var apiService = new ApiService(serializer);
The ApiService class provides methods for executing HTTP requests. Example:
using RestBuilder;
using Cysharp.Threading.Tasks;
async UniTask GetUserDataAsync()
{
var serializer = new NewtonsoftSerializer();
var apiService = new ApiService(serializer);
try
{
var response = await apiService.GetAsync<User>("https://api.example.com/users/123");
Console.WriteLine($"User name: {response.Name}");
}
catch (ApiException ex)
{
Console.WriteLine($"Error: {ex.StatusCode}, {ex.Body}");
}
}For POST, PUT, and PATCH methods, you can send data:
var userData = new { Name = "John", Age = 30 };
var response = await apiService.PostAsync<object, User>("https://api.example.com/users", userData);The EndpointBuilder class allows constructing URLs with dynamic parameters:
var builder = new EndpointBuilder("https://api.example.com/users/{userId}/profile");
builder.AddPathParam("userId", 123);
builder.AddQueryParam("format", "json");
or
var builder = new EndpointBuilder("https://api.example.com/users/{userId}/profile")
.AddPathParam("userId", 123)
.AddQueryParam("format", "json");
string url = builder.Build(); // https://api.example.com/users/123/profile?format=jsonUse RequestOptions to add headers and a cancellation token:
var options = new RequestOptions
{
Headers = new Dictionary<string, string>
{
{ "Authorization", "Bearer token123" },
{ "Content-Type", "application/json" }
}
};
var response = await apiService.GetAsync<User>("https://api.example.com/users/123", options);HTTP request errors are handled via ApiException:
try
{
var response = await apiService.GetAsync<User>("https://api.example.com/invalid");
}
catch (ApiException ex)
{
Console.WriteLine($"HTTP Error {ex.StatusCode}: {ex.Body}");
}async UniTask FetchUserProfileAsync()
{
var serializer = new NewtonsoftSerializer();
var apiService = new ApiService(serializer);
var builder = new EndpointBuilder("https://api.example.com/users/{userId}/profile");
builder.AddPathParam("userId", 123);
builder.AddQueryParam("details", "full");
var options = new RequestOptions
{
Headers = new Dictionary<string, string> { { "Authorization", "Bearer token123" } }
};
try
{
var profile = await apiService.GetAsync<UserProfile>(builder.Build(), options);
Console.WriteLine($"Profile: {profile.Name}");
}
catch (ApiException ex)
{
Console.WriteLine($"Error: {ex.StatusCode}, {ex.Body}");
}
}- Dependency on
UnityWebRequest, limiting usage outside of Unity. - JSON serialization is supported only via
Newtonsoft.Json. For other formats, implement a customISerializer. - Requires external libraries (
UniTask,Newtonsoft.Json).
MIT License. See the LICENSE file for details.