Proyecto26.RestClient 1.2.1

Simple HTTP and REST client for Unity based on Promises, also support Callbacks!

There is a newer version of this package available.
See the version list below for details.
Install-Package Proyecto26.RestClient -Version 1.2.1
dotnet add package Proyecto26.RestClient --version 1.2.1
<PackageReference Include="Proyecto26.RestClient" Version="1.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Proyecto26.RestClient --version 1.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Codacy Badge
BCH compliance
Build Status

RestClient for Unity 🤘

Proyecto26.RestClient logo

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 root = "https://jsonplaceholder.typicode.com";
RestClient.GetArray<Post>(root + "/posts", (err, res) => {
  RestClient.GetArray<Todo>(root + "/todos", (errTodos, resTodos) => {
    RestClient.GetArray<User>(root + "/users", (errUsers, resUsers) => {
      if(err != null){
        EditorUtility.DisplayDialog ("Error", errTodos.Message, "Ok");
      }
    });
  });
});

But working with Promises we can improve our code, yay! 👏

RestClient.GetArray<Post>(root + "/posts").Then(response => {
  EditorUtility.DisplayDialog ("Success", JsonHelper.ArrayToJson<Post>(response, true), "Ok");
  return RestClient.GetArray<Todo>(root + "/todos");
}).Then(response => {
  EditorUtility.DisplayDialog ("Success", JsonHelper.ArrayToJson<Todo>(response, true), "Ok");
  return RestClient.GetArray<User>(root + "/users");
}).Then(response => {
  EditorUtility.DisplayDialog ("Success", JsonHelper.ArrayToJson<User>(response, true), "Ok");
}).Catch(err => EditorUtility.DisplayDialog ("Error", err.Message, "Ok"));

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) 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");
})

But we are going to create a class "User" 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 usersRoot = "https://jsonplaceholder.typicode.com/users"; 
RestClient.Get<User>(usersRoot + "/1").Then(firstUser => {
  EditorUtility.DisplayDialog("JSON", JsonUtility.ToJson(firstUser, true), "Ok");
})
  • GET Array
RestClient.GetArray<User>(usersRoot).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>(usersRoot, newUser).Then(customResponse => {
  EditorUtility.DisplayDialog("JSON", JsonUtility.ToJson(customResponse, true), "Ok");
})
  • PUT
RestClient.Put<CustomResponse>(usersRoot + "/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 requestOptions = new RequestHelper { 
  url = "https://jsonplaceholder.typicode.com/photos",
  headers = new Dictionary<string, string>{
    { "Authorization", "Other token..." }
  }
};
RestClient.GetArray<Photo>(requestOptions).Then(response => {
  EditorUtility.DisplayDialog("Header", requestOptions.GetRequestHeader("Authorization"), "Ok");
})

And later we can clean the default headers for all requests

RestClient.CleanDefaultHeaders();

Collaborators 🥇

Juan David Nicholls Diego Ossa

Credits 👍

Supporting 🍻

I believe in Unicorns 🦄
Support me, if you do too.

Happy coding 💯

Made with ❤️

Nicholls

Codacy Badge
BCH compliance
Build Status

RestClient for Unity 🤘

Proyecto26.RestClient logo

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 root = "https://jsonplaceholder.typicode.com";
RestClient.GetArray<Post>(root + "/posts", (err, res) => {
  RestClient.GetArray<Todo>(root + "/todos", (errTodos, resTodos) => {
    RestClient.GetArray<User>(root + "/users", (errUsers, resUsers) => {
      if(err != null){
        EditorUtility.DisplayDialog ("Error", errTodos.Message, "Ok");
      }
    });
  });
});

But working with Promises we can improve our code, yay! 👏

RestClient.GetArray<Post>(root + "/posts").Then(response => {
  EditorUtility.DisplayDialog ("Success", JsonHelper.ArrayToJson<Post>(response, true), "Ok");
  return RestClient.GetArray<Todo>(root + "/todos");
}).Then(response => {
  EditorUtility.DisplayDialog ("Success", JsonHelper.ArrayToJson<Todo>(response, true), "Ok");
  return RestClient.GetArray<User>(root + "/users");
}).Then(response => {
  EditorUtility.DisplayDialog ("Success", JsonHelper.ArrayToJson<User>(response, true), "Ok");
}).Catch(err => EditorUtility.DisplayDialog ("Error", err.Message, "Ok"));

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) 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");
})

But we are going to create a class "User" 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 usersRoot = "https://jsonplaceholder.typicode.com/users"; 
RestClient.Get<User>(usersRoot + "/1").Then(firstUser => {
  EditorUtility.DisplayDialog("JSON", JsonUtility.ToJson(firstUser, true), "Ok");
})
  • GET Array
RestClient.GetArray<User>(usersRoot).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>(usersRoot, newUser).Then(customResponse => {
  EditorUtility.DisplayDialog("JSON", JsonUtility.ToJson(customResponse, true), "Ok");
})
  • PUT
RestClient.Put<CustomResponse>(usersRoot + "/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 requestOptions = new RequestHelper { 
  url = "https://jsonplaceholder.typicode.com/photos",
  headers = new Dictionary<string, string>{
    { "Authorization", "Other token..." }
  }
};
RestClient.GetArray<Photo>(requestOptions).Then(response => {
  EditorUtility.DisplayDialog("Header", requestOptions.GetRequestHeader("Authorization"), "Ok");
})

And later we can clean the default headers for all requests

RestClient.CleanDefaultHeaders();

Collaborators 🥇

Juan David Nicholls Diego Ossa

Credits 👍

Supporting 🍻

I believe in Unicorns 🦄
Support me, if you do too.

Happy coding 💯

Made with ❤️

Nicholls

Release Notes

Fix GET requests to use RequestHelper class

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Proyecto26.RestClient:

Repository Stars
proyecto26/RestClient
🦄 Simple HTTP and REST client for Unity based on Promises, also supports Callbacks! 🎮

Version History

Version Downloads Last updated
2.6.1 63 6/10/2020
2.6.0 244 9/20/2019
2.5.9 120 9/9/2019
2.5.8 117 9/9/2019
2.5.7 169 6/22/2019
2.5.6 256 4/23/2019
2.5.5 194 4/20/2019
2.5.4 180 4/18/2019
2.5.2 224 1/23/2019
2.5.1 182 1/21/2019
2.4.2 224 1/14/2019
2.4.1 226 1/10/2019
2.2.1 271 1/2/2019
2.2.0 247 11/21/2018
2.1.1 269 8/26/2018
2.1.0 231 8/23/2018
2.0.1 278 7/27/2018
2.0.0 373 5/22/2018
1.2.2 381 3/4/2018
1.2.1 520 10/21/2017
1.2.0 365 10/20/2017
1.0.1 322 10/15/2017
Show less