Skip to content

UniRetrofit is a lightweight wrapper around Proyecto26/RestClient, inspired by Retrofit (Android). It simplifies working with HTTP requests in Unity, adds support for interceptors, authorization, and convenient integration with Promises (RSG) and Reactive Extensions (R3).

License

Notifications You must be signed in to change notification settings

UDFSoft/UniRetrofit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

UniRetrofit

UniRetrofit is a lightweight wrapper around Proyecto26/RestClient, inspired by Retrofit (Android).
It simplifies working with HTTP requests in Unity, adds support for interceptors, authorization, and convenient integration with Promises (RSG) and Reactive Extensions (R3).


📂 Structure

Assets/

└── Scripts/

└── UniRetrofit/

├── RestClientWrapper.cs // Promise-based version (RSG.Promise)

└── RestClientRxWrapper.cs // Observable-based version (R3.Observable)


🚀 Features

  • ✨ Retrofit-like interface
  • 🔄 Support for interceptors (IRestInterceptor)
    • OnRequest — modify the request before sending
    • OnResponse — handle the response
    • OnError — handle errors
  • 🔑 Authorization support via IAuthHandler
    • automatic token refresh on 401 Unauthorized
  • ⚡ Two implementations:
    • RestClientWrapper — based on RSG.Promise
    • RestClientRxWrapper — based on R3.Observable

🔧 Installation

  1. Download and install Proyecto26 RestClient.
  2. Install RSG Promises.
  3. If you need Rx-style, install R3 (Reactive Extensions for Unity).

Place UniRetrofit in your project:

Assets/Scripts/UniRetrofit


🛠️ Usage Examples

1. Basic usage (Promise)

using Proyecto26;
using UnityEngine;

public class Example : MonoBehaviour
{
    private RestClientWrapper client;

    void Start()
    {
        client = new RestClientWrapper();

        var request = new RequestHelper
        {
            Uri = "https://jsonplaceholder.typicode.com/posts/1"
        };

        client.Get<Post>(request).Then(post =>
        {
            Debug.Log($"Title: {post.title}");
        }).Catch(err =>
        {
            Debug.LogError(err);
        });
    }
}

public class Post
{
    public int userId;
    public int id;
    public string title;
    public string body;
}

Rx version (Observable)

using Proyecto26;
using R3;
using UnityEngine;

public class ExampleRx : MonoBehaviour
{
    private RestClientRxWrapper client;

    void Start()
    {
        client = new RestClientRxWrapper();

        var request = new RequestHelper
        {
            Uri = "https://jsonplaceholder.typicode.com/posts/1"
        };

        client.Get<Post>(request)
            .Subscribe(
                post => Debug.Log($"Rx Title: {post.title}"),
                error => Debug.LogError(error),
                () => Debug.Log("Completed"));
    }
}

🔒 Authorization

You can implement IAuthHandler to automatically refresh tokens when a 401 Unauthorized error occurs.

public interface IAuthHandler
{
    bool CanHandle(Exception error);
    IPromise<bool> TryRefreshToken();
    RequestHelper CloneRequest(RequestHelper original);
}

🧩 Interceptors

Interceptors allow you to add headers, log requests, and handle responses.

public class LoggingInterceptor : IRestInterceptor
{
    public void OnRequest(ref RequestHelper request)
    {
        Debug.Log($"➡️ Request: {request.Uri}");
    }

    public void OnResponse(RequestHelper request, ResponseHelper response)
    {
        Debug.Log($"⬅️ Response: {response.StatusCode}");
    }

    public void OnError(RequestHelper request, Exception exception)
    {
        Debug.LogError($"❌ Error: {exception.Message}");
    }
}

📌 TODO (roadmap)

  • DSL with annotations ([Get], [Post], [Body])
  • API interface generation like Retrofit
  • Response caching
  • Unity Addressables support for asset loading

About

UniRetrofit is a lightweight wrapper around Proyecto26/RestClient, inspired by Retrofit (Android). It simplifies working with HTTP requests in Unity, adds support for interceptors, authorization, and convenient integration with Promises (RSG) and Reactive Extensions (R3).

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages