Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/whitesharx/httx
⚡️ X-force HTTP/REST library for Unity ⚡️
https://github.com/whitesharx/httx
csharp dotnet http rest unity unity3d
Last synced: about 15 hours ago
JSON representation
⚡️ X-force HTTP/REST library for Unity ⚡️
- Host: GitHub
- URL: https://github.com/whitesharx/httx
- Owner: whitesharx
- License: mit
- Created: 2020-01-08T09:46:48.000Z (almost 5 years ago)
- Default Branch: develop
- Last Pushed: 2022-07-19T09:20:04.000Z (over 2 years ago)
- Last Synced: 2024-09-20T14:18:50.055Z (about 2 months ago)
- Topics: csharp, dotnet, http, rest, unity, unity3d
- Language: C#
- Homepage:
- Size: 307 KB
- Stars: 25
- Watchers: 7
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
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](https://docs.unity3d.com/Manual/PackagesList.html)
You can install this package using Unity Package Manager, just add the
following to your `Packages/manifest.json`:Add official [NPM](https://www.npmjs.com/) registry with WhiteSharx scope, or simply
add `com.whitesharx` scope if you already have NPM registry added:```json
{
"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):
```json
{
"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):
```json
{
"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`:
```xml
```
### 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:```csharp
Context.InitializeDefault(1, () => { /* Ready to go callback */ });
```To give you more detailed example, suppose you want to do it from MonoBehaviour:
```csharp
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:```csharp
var text = await new As(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:```csharp
// Make simple GET request for text data.
var text = await new As(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(new Get(new Json(url)));// Make POST request with JSON payload and parse JSON model as response.
var credentials = await new As(new Post(new Json(url, new User("John Doe"))));
```Take a look at some even more complicated stuff:
```csharp
// Make POST request that requires basic authorization.
var token = await new As(new Post(new Basic(new Json(url, user), name, password)));// Make POST request that requires bearer authorization.
var credentials = await new As(new Put(new Bearer(new Json(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:```csharp
// Make POST request that requires basic authorization.
var token = await new Json(url, user)
.Post()
.Basic(name, password)
.As();// Make POST request that requires bearer authorization.
var credentials = await new Json(url, user)
.Post()
.Bearer(token)
.As();
```Let's examine some caching capabilities. Httx supports **in-memory** and **disk** caching out of
the box. Also it supports native Unity **AssetBundle** caching:```csharp
// 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();// 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();
```Httx also supports Unity AssetBundles out of the box:
```csharp
// Download asset bundle. Local or remote urls supported.
var bundle = await new As(new Get(new Bundle(url)));// Download asset bundle and cache it on disk. Next request will hit offline file.
var bundle = await new As(new Cache(new Get(new Bundle(url)), Storage.Native));// Download asset bundle and display progress while downloading
var onProgress = new Progress(value => { /* Implementation */ });
var bundle = await new As(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.* [Documentation](https://github.com/whitesharx/httx/wiki/Home)
* [Examples](https://github.com/whitesharx/httx/wiki/Examples)## License
Httx is available under the [MIT](https://en.wikipedia.org/wiki/MIT_License) license.
Made with 🖤 at WhiteSharx