API Manager - swzwij/Unity-Tool-Armory GitHub Wiki

Overview

APIManager is a Unity C# class that serves as a manager for handling API requests and responses. It provides a straightforward way to send GET requests to specified API endpoints, with callbacks for successful responses and error handling. The class follows the Singleton pattern for ease of use and ensures persistence across scenes.

Installation

To use APIManager in your Unity project, follow these steps:

  1. Copy the APIManager.cs file into your Unity project's scripts directory.
  2. Ensure that the APIManager class is placed in the Unity scene.

Usage

Sending GET Requests

Use the GetCall method to send a GET request to the specified API endpoint.

APIRequest apiRequest = new YourAPIRequestClass(); // Create an instance of your API request configuration class
apiManagerInstance.GetCall<YourResponseType>(apiRequest, OnComplete, OnFailure);

Callbacks

Callback Description
OnComplete Callback invoked upon a successful response.
OnFailure Callback invoked when the request fails or encounters an error.

Example Callbacks

OnComplete

void OnComplete(YourResponseType responseData)
{
    // Handle successful response
}

OnFailure

void OnFailure(APIStatus status)
{
    // Handle request failure or error
    Debug.LogError(status.Status);
}

APIRequest Class

Overview

APIRequest is an abstract base class for defining API request configurations.

Usage

Create a class that inherits from APIRequest and implement the URL property.

public class YourAPIRequestClass : APIRequest
{
    public override string URL => "Your API Endpoint URL";
}

APIStatus Class

Overview

APIStatus represents the status of an API request, including success, failure, or specific error conditions.

Usage

Create an instance of APIStatus with the provided UnityWebRequest result.

public class YourAPIStatusHandler
{
    void HandleAPIStatus(UnityWebRequest result)
    {
        APIStatus status = new APIStatus(result);
        Debug.Log(status.Status);
    }
}