Skip to content

⚡️ X-force HTTP/REST library for Unity ⚡️

License

Notifications You must be signed in to change notification settings

whitesharx/httx

Repository files navigation

Httx

⚡️ X-Force HTTP/REST library for Unity ⚡️


  • Zero dependency, built for Unity
  • Simple, DSL-like API to compose your requests
  • Includes reliable memory, disk and native AssetBundle cache support
  • Easily extensible for your custom needs

Quick Start

You need Unity 2019.x or newer

Install Httx with Package Manger (NPM Package)

Httx distributed as standard Unity Package You can install this package using Unity Package Manager, just add the following to your Packages/manifest.json:

Add official NPM registry with WhiteSharx scope, or simply add com.whitesharx scope if you already have NPM registry added:

{
  "scopedRegistries": [
    {
      "name": "Official NPM Registry",
      "url": "https://registry.npmjs.org/",
      "scopes": [ "com.whitesharx" ]
    }
  ]
}

Add com.whitesharx.httx package to your dependencies (see repo tags to pick the version):

{
  "dependencies": {
    "com.whitesharx.httx": "x.x.x"
  }
}

Install Httx with Package Manger (Git url)

Or, your can simply install it from git url (see repo tags to pick the version):

{
  "dependencies": {
    "com.whitesharx.httx": "https://github.com/whitesharx/httx.git#x.x.x"
  }
}

Add preserve rule for linker

Preserve Httx assembly contents in your Assets/link.xml:

<linker>
  <assembly fullname="Httx" preserve="all"/>
</linker>

Initialize context

First, you need to initaize Context to make HTTP requests. If you do not need any fine-tune customization it's just one line of code:

Context.InitializeDefault(1, () => { /* Ready to go callback */ });

To give you more detailed example, suppose you want to do it from MonoBehaviour:

public class YourMonoBehaviour : MonoBehaviour {
  // XXX: Versioning exists to manage caching.
  private const int Version = 1;

  private void Awake() {
    Context.InitializeDefault(Version, OnContextReady);
  }

  private async void OnContextReady() {
    // Here you can safely make HTTP requests using Httx.
  }
}

Make your first HTTP requests

Now you are ready to make HTTP requests. Suppose you realized that you desperetly need to download whole Google...as text. Here you go:

var text = await new As<string>(new Get(new Text("https://google.com")));

Ok, that's was fun. But now, let's take a look at more serious stuff you can do with Httx starting with simple requests:

// Make simple GET request for text data.
var text = await new As<string>(new Get(new Text(url)));

// Make GET request for JSON data and parse it to your model with default JSONUtility.
var user = await new As<User>(new Get(new Json(url)));

// Make POST request with JSON payload and parse JSON model as response.
var credentials = await new As<Credentials>(new Post(new Json<User>(url, new User("John Doe"))));

Take a look at some even more complicated stuff:

// Make POST request that requires basic authorization.
var token = await new As<Token>(new Post(new Basic(new Json<User>(url, user), name, password)));

// Make POST request that requires bearer authorization.
var credentials = await new As<Credentials>(new Put(new Bearer(new Json<User>(url, user), token)));

Highly decorated requests become a bit messy. You can use Fluent API to build more complex requests. You can rewrite two requests above with Fluent API like this:

// Make POST request that requires basic authorization.
var token = await new Json<User>(url, user)
    .Post()
    .Basic(name, password)
    .As<Token>();

// Make POST request that requires bearer authorization.
var credentials = await new Json<User>(url, user)
    .Post()
    .Bearer(token)
    .As<Credentials>();

Let's examine some caching capabilities. Httx supports in-memory and disk caching out of the box. Also it supports native Unity AssetBundle caching:

// Download a picture and also cache it on disk.
// Next request to this url will be offline.
var texture = await new new Texture(url)
    .Get()
    .Cache(Storage.Disk)
    .As<Texture2D>();

// Download some "hot" data and cache it for a few seconds in memory.
// Requests to this urls made in next 4 seconds will hit only memory in-cache.
var bytes = new Bytes(url)
    .Get()
    .Cache(Storage.Memory, TimeSpan.FromSeconds(4))
    .As<byte[]>();

Httx also supports Unity AssetBundles out of the box:

// Download asset bundle. Local or remote urls supported.
var bundle = await new As<AssetBundle>(new Get(new Bundle(url)));

// Download asset bundle and cache it on disk. Next request will hit offline file.
var bundle = await new As<AssetBundle>(new Cache(new Get(new Bundle(url)), Storage.Native));

// Download asset bundle and display progress while downloading
var onProgress = new Progress<float>(value => { /* Implementation */ });
var bundle = await new As<AssetBundle>(new Get(new Bundle(url), onProgress));

Documentation

There's more stuff under the hood. Please check detailed documentation and examples to understand how this librarty could fit your needs.

License

Httx is available under the MIT license.

Made with 🖤 at WhiteSharx