Proyecto26.RestClient
2.2.1
See the version list below for details.
dotnet add package Proyecto26.RestClient --version 2.2.1
NuGet\Install-Package Proyecto26.RestClient -Version 2.2.1
<PackageReference Include="Proyecto26.RestClient" Version="2.2.1" />
paket add Proyecto26.RestClient --version 2.2.1
#r "nuget: Proyecto26.RestClient, 2.2.1"
// Install Proyecto26.RestClient as a Cake Addin #addin nuget:?package=Proyecto26.RestClient&version=2.2.1 // Install Proyecto26.RestClient as a Cake Tool #tool nuget:?package=Proyecto26.RestClient&version=2.2.1
RestClient for Unity 🤘
Supported Unity versions 2017.2 or higher
This HTTP/REST Client is based on Promises to avoid the Callback Hell ☠️ and the Pyramid of doom 💩 working with Coroutines in Unity 🎮, example:
var api = "https://jsonplaceholder.typicode.com";
RestClient.GetArray<Post>(api + "/posts", (err, res) => {
RestClient.GetArray<Todo>(api + "/todos", (errTodos, resTodos) => {
RestClient.GetArray<User>(api + "/users", (errUsers, resUsers) => {
//Missing validations to catch errors!
});
});
});
But working with Promises we can improve our code, yay! 👏
RestClient.GetArray<Post>(api + "/posts").Then(response => {
EditorUtility.DisplayDialog ("Success", JsonHelper.ArrayToJson<Post>(response, true), "Ok");
return RestClient.GetArray<Todo>(api + "/todos");
}).Then(response => {
EditorUtility.DisplayDialog ("Success", JsonHelper.ArrayToJson<Todo>(response, true), "Ok");
return RestClient.GetArray<User>(api + "/users");
}).Then(response => {
EditorUtility.DisplayDialog ("Success", JsonHelper.ArrayToJson<User>(response, true), "Ok");
}).Catch(err => EditorUtility.DisplayDialog ("Error", err.Message, "Ok"));
Supported platforms
The UnityWebRequest system supports most Unity platforms:
- All versions of the Editor and Standalone players
- WebGL
- Mobile platforms: iOS, Android
- Universal Windows Platform
- PS4 and PSVita
- XboxOne
- HoloLens
- Nintendo Switch
Demo ⏯
Do you want to see this beautiful package in action? Download the demo here
Installation 👨💻
Unity package
Download and install the .unitypackage file of the latest release published here.
Nuget package
Other option is download this package from NuGet with Visual Studio or using the nuget-cli, a NuGet.config file is required at the root of your Unity Project, for example:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositoryPath" value="./Assets/Packages" />
</config>
</configuration>
The package to search for is Proyecto26.RestClient.
Getting Started 📚
The default methods (GET, POST, PUT, DELETE, HEAD) are:
RestClient.Get("https://jsonplaceholder.typicode.com/posts/1").Then(response => {
EditorUtility.DisplayDialog("Response", response.Text, "Ok");
});
RestClient.Post("https://jsonplaceholder.typicode.com/posts", newPost).Then(response => {
EditorUtility.DisplayDialog("Status", response.StatusCode.ToString(), "Ok");
});
RestClient.Put("https://jsonplaceholder.typicode.com/posts/1", updatedPost).Then(response => {
EditorUtility.DisplayDialog("Status", response.StatusCode.ToString(), "Ok");
});
RestClient.Delete("https://jsonplaceholder.typicode.com/posts/1").Then(response => {
EditorUtility.DisplayDialog("Status", response.StatusCode.ToString(), "Ok");
});
RestClient.Head("https://jsonplaceholder.typicode.com/posts").Then(response => {
EditorUtility.DisplayDialog("Status", response.StatusCode.ToString(), "Ok");
});
And we have a generic method to create any type of request:
RestClient.Request(new RequestHelper {
Uri = "https://jsonplaceholder.typicode.com/photos",
Method = "POST",
Timeout = 10000,
Headers = new Dictionary<string, string> {
{ "Authorization", "Bearer JWT_token..." }
},
Body = newPost, //Serialize object using JsonUtility by default
BodyString = SerializeObject(newPost), // Use it instead of 'Body' to serialize objects to JSON string using other tools
BodyRaw = CompressToRawData(newPost), // Use it instead of 'Body' to send raw data directly
SimpleForm = new Dictionary<string, string> {}, //Content-Type: application/x-www-form-urlencoded
FormSections = new List<IMultipartFormSection>() {}, //Content-Type: multipart/form-data
ContentType = "application/json", //JSON is used by default
ChunkedTransfer = true,
IgnoreHttpException = true, //Prevent to catch http exceptions
}).Then(response => {
EditorUtility.DisplayDialog("Status", response.StatusCode.ToString(), "Ok");
});
With all the methods we have the possibility to indicate the type of response, in the following example we're going to create a class and the HTTP requests to load JSON data easily:
[Serializable]
public class User
{
public int id;
public string name;
public string username;
public string email;
public string phone;
public string website;
}
- GET JSON
var usersRoute = "https://jsonplaceholder.typicode.com/users";
RestClient.Get<User>(usersRoute + "/1").Then(firstUser => {
EditorUtility.DisplayDialog("JSON", JsonUtility.ToJson(firstUser, true), "Ok");
});
- GET Array (JsonHelper is an extension to manage arrays)
RestClient.GetArray<User>(usersRoute).Then(allUsers => {
EditorUtility.DisplayDialog("JSON Array", JsonHelper.ArrayToJsonString<User>(allUsers, true), "Ok");
});
Also we can create different classes for custom responses:
[Serializable]
public class CustomResponse
{
public int id;
}
- POST
RestClient.Post<CustomResponse>(usersRoute, newUser).Then(customResponse => {
EditorUtility.DisplayDialog("JSON", JsonUtility.ToJson(customResponse, true), "Ok");
});
- PUT
RestClient.Put<CustomResponse>(usersRoute + "/1", updatedUser).Then(customResponse => {
EditorUtility.DisplayDialog("JSON", JsonUtility.ToJson(customResponse, true), "Ok");
});
Custom HTTP Headers and Options 💥
HTTP Headers, such as Authorization
, can be set in the DefaultRequestHeaders object for all requests
RestClient.DefaultRequestHeaders["Authorization"] = "Bearer ...";
Also we can add specific options and override default headers for a request
var currentRequest = new RequestHelper {
Uri = "https://jsonplaceholder.typicode.com/photos",
Headers = new Dictionary<string, string> {
{ "Authorization", "Other token..." }
}
};
RestClient.GetArray<Photo>(currentRequest).Then(response => {
EditorUtility.DisplayDialog("Header", currentRequest.GetHeader("Authorization"), "Ok");
});
currentRequest.UploadProgress; //To know the progress by uploading data to the server
currentRequest.DownloadProgress; //To know the progress by downloading data from the server
currentRequest.Abort(); //Abort the request manually
And later we can clean the default headers for all requests
RestClient.CleanDefaultHeaders();
Collaborators 🥇
Credits 👍
- Promises library for C#: Real Serious Games/C-Sharp-Promise
Supporting 🍻
I believe in Unicorns 🦄 Support me, if you do too.
Happy coding 💯
Made with ❤️
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net35 is compatible. net40 was computed. net403 was computed. net45 was computed. net451 was computed. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
-
- RSG.Promise (>= 3.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on Proyecto26.RestClient:
Repository | Stars |
---|---|
proyecto26/RestClient
🦄 A Promise based REST and HTTP client for Unity 🎮
|
Version | Downloads | Last updated |
---|---|---|
2.6.2 | 723 | 12/28/2021 |
2.6.1 | 749 | 6/10/2020 |
2.6.0 | 689 | 9/20/2019 |
2.5.9 | 586 | 9/9/2019 |
2.5.8 | 572 | 9/9/2019 |
2.5.7 | 596 | 6/22/2019 |
2.5.6 | 676 | 4/23/2019 |
2.5.5 | 618 | 4/20/2019 |
2.5.4 | 621 | 4/18/2019 |
2.5.2 | 719 | 1/23/2019 |
2.5.1 | 680 | 1/21/2019 |
2.4.2 | 746 | 1/14/2019 |
2.4.1 | 724 | 1/10/2019 |
2.2.1 | 813 | 1/2/2019 |
2.2.0 | 773 | 11/21/2018 |
2.1.1 | 849 | 8/26/2018 |
2.1.0 | 816 | 8/23/2018 |
2.0.1 | 846 | 7/27/2018 |
2.0.0 | 989 | 5/22/2018 |
1.2.2 | 1,017 | 3/4/2018 |
1.2.1 | 1,248 | 10/21/2017 |
1.2.0 | 974 | 10/20/2017 |
1.0.1 | 888 | 10/15/2017 |
Add more HTTP methods, handle exceptions and support more options