Skip to content

First Launch

Latest

Choose a tag to compare

@noahjames404 noahjames404 released this 29 Mar 09:57
· 4 commits to main since this release
1bddb08

APIBuilder Documentation

Overview

The APIBuilder class is a fluent-style API request builder for Unity, using UnityWebRequest to handle HTTP requests. It allows developers to create, configure, and send requests in an organized and structured manner.

Installation

Ensure that your Unity project has access to UnityEngine.Networking before using APIBuilder.

using Maphatar.UnityAPI;

Usage

Creating a Request

To create a new request, use the static Make method and provide an endpoint URL:

var request = APIBuilder.Make("https://api.example.com/data");

Setting Headers

Headers can be added using the SetHeaderRequest method:

request.SetHeaderRequest("Authorization", "Bearer YOUR_TOKEN");

Setting Timeout

You can set a timeout duration (in seconds) for the request:

request.SetTimeout(10);

Choosing HTTP Methods

Specify the HTTP method using one of the following:

request.Get();   // For GET requests
request.Post();  // For POST requests
request.Put();   // For PUT requests

Alternatively, use SetMethod for custom methods:

request.SetMethod("DELETE");

Adding a Request Body

For requests that require a body (e.g., POST, PUT), use:

request.BodyData("{\"key\":\"value\"}");

Handling Responses

Define success and error handlers using OnSuccess and OnError:

request
    .OnSuccess(response => Debug.Log("Success: " + response.downloadHandler.text))
    .OnError(response => Debug.LogError("Error: " + response.error));

Preventing Duplicate Requests

To prevent duplicate requests, use the StackPrevention method with a unique key:

request.StackPrevention("UniqueKey");

If a request with the same key is still in progress, subsequent requests will not be sent.

Sending the Request

Once the request is configured, send it using:

request.SendRequest();

Example Usage

APIBuilder.Make("https://api.example.com/data")
    .SetHeaderRequest("Content-Type", "application/json")
    .Post()
    .BodyData("{\"name\":\"John\"}")
    .OnSuccess(response => Debug.Log("Response: " + response.downloadHandler.text))
    .OnError(response => Debug.LogError("Request failed: " + response.error))
    .StackPrevention("UserCreation")
    .SendRequest();