# REST Express

> Transform Postman collections into Unity-ready API clients. Test APIs in-editor, generate typed C# in minutes.

## Overview

REST Express is a Unity Editor tool that turns Postman collections into production-ready C# API clients. It ships with:

-   an **API Importer** window where you send real requests inside Unity,
-   a **Script Generator** that produces clean async / coroutine C# code,
-   a shared runtime for logging, retries, and multipart uploads.

If your game or app talks to a REST backend, REST Express removes the boilerplate between your Postman collection and your `MonoBehaviour`.

## Requirements

-   Unity **2020.3 LTS** or newer (through Unity 6000)
-   **.NET 4.x** scripting runtime
-   A **Postman Collection v2.1** JSON export
-   Any render pipeline — REST Express is editor + runtime code only

## Installation

### Unity Asset Store (recommended)

1.  Visit the [REST Express listing](https://assetstore.unity.com/packages/slug/319060) on the Unity Asset Store.
2.  Click **Add to My Assets**, then open **Window → Package Manager → My Assets** in Unity.
3.  Select REST Express and click **Import**.

### Git UPM

Add the following to `Packages/manifest.json`:

```json
{
  "dependencies": {
    "com.simpleyetefficient.rest-express": "https://github.com/simple-yet-efficient/rest-express.git"
  }
}
```

## Quick start

### 1\. Import your Postman collection

Open **Window → REST Express → API Importer**. Drag your Postman v2.1 JSON into the window, or click **Import Collection…** and browse to it. The collection tree populates immediately — folders, requests, variables, and all.

### 2\. Test endpoints in the Editor

Select any request in the tree, fill in the path / query / body, and press **Send**. You'll see the response, headers, and status code formatted inline. History is persisted per collection so you can replay a request from yesterday without re-filling the body.

### 3\. Generate a typed C# client

Open **Window → REST Express → Script Generator**. Pick a namespace, choose **Async (Task)** or **Coroutine (IEnumerator)**, and click **Generate**. You'll get:

-   one static class per folder,
-   one method per request,
-   strongly typed request / response models derived from your sample payloads.

Drop the generated code into your project and call it from anywhere.

### Minimal example

```csharp
using SyE.RestExpress.Generated;

async void SignIn()
{
    var response = await AuthApi.LoginAsync(new LoginRequest {
        email = "me@example.com",
        password = "hunter2",
    });

    if (response.IsSuccess) {
        PlayerPrefs.SetString("token", response.Data.token);
    } else {
        Debug.LogError($"Login failed: {response.StatusCode} — {response.Error}");
    }
}
```

## Feature reference

### Authentication headers

REST Express reads **Bearer**, **Basic**, and **API Key** auth directly from Postman collections. Tokens stored at the collection level are injected automatically into generated clients via a `RestExpressOptions` object.

### Multipart & file uploads

File uploads work both in the Importer (pick a local file path) and in generated clients (`Stream`, `byte[]`, or `FileInfo` arguments).

### Error handling

Every generated method returns a `RestResponse<T>` wrapper with `IsSuccess`, `StatusCode`, `Data`, and `Error`. No exceptions thrown on HTTP failures — you stay in control.

### Logging

Toggle logs from **Tools → REST Express → Enable / Disable Logging**. When enabled, every request prints method, URL, timing, and status to the Unity Console.

## Best practices

-   **Commit your Postman collection** to the repo next to the generated code — keep them in lockstep.
-   **Regenerate after every schema change.** The Script Generator is idempotent; re-running it will overwrite only the generated files inside `Assets/_Generated/RestExpress/`.
-   **Wrap generated clients** behind a small façade in your game code so you can swap transport later (mocks in tests, CDNs in prod).

## Troubleshooting

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| Nothing imports | Collection is v1.0 | Re-export from Postman as **Collection v2.1** |
| 403 on every request | Missing token header | Set collection-level auth in Postman or pass a `RestExpressOptions` |
| Coroutines don't compile | `.NET 3.5` runtime | Switch to **.NET 4.x** in Player Settings |
| Files don't upload on WebGL | Browser CORS | Add `Access-Control-Allow-Origin` to the server's response |

## Support

-   **Issues & feature requests:** [github.com/simple-yet-efficient/rest-express/issues](https://github.com/simple-yet-efficient/rest-express/issues)

REST Express is MIT-licensed. You can read the source, fork it, and ship modifications in your own projects.
