-
Notifications
You must be signed in to change notification settings - Fork 0
Examples
Sergey Ivonchik edited this page Nov 1, 2021
·
10 revisions
var contentLength = new Length(url);
// When you add Code() decorator to any request, it will
// return only HTTP response code as result
var request = new As<int>(new Get(new Code(new Text(url))));
var callbacks = new Callback<UnityWebRequest> {
OnBeforeRequestSent = requestBeforeSent => {
// Here your have access to ready to fire raw request
},
OnResponseReceived = requestAfterReceived => {
// Here your have access to raw response that arrived
}
};
var request = new As<string>(new Get(new Hook<UnityWebRequest>(new Text(url), callbacks)));
// Httx internally supports assets:// prefix. Just compose URL starting
// with this prefix and path to file you want to load. For example, let's load
// a file that places at StreamingAssets/tests/content.json.
var url = "assets://tests/content.json"
var model = await new As<JsonResponseModel>(new Get(new Json(url)));
public class InNoneMatchExampleBehaviour : MonoBehaviour {
private const int Version = 1;
// You need come kind of storage to store ETag
// values for content.
private string tagStorage = string.Empty;
[UsedImplicitly]
private void Awake() {
Context.InitializeDefault(Version, OnContextReady);
}
private async void OnContextReady() {
const string url = "https://some-etagged-content/for-example-any-s3-file.txt";
// First request always fires to network and checks if there's new
// content based on ETag value.
var freshResult = await GetCachedContentAsync(url);
// If tag is not modified since first request by server, it's simply
// returns cached data.
var cachedResult = await GetCachedContentAsync(url);
}
private async Task<string> GetCachedContentAsync(string url) {
try {
// If you set Condition() object for request, it will always skips initial
// cache "read hit" and fires network request to server. Based on ETag value
// server decides to return to you data in response or return NotModified.
return await new Text(url)
.Get()
.Match(new Condition(If.NoneMatch, tagStorage, eTag => { tagStorage = eTag; }))
.Cache(Storage.Disk)
.As<string>();
} catch (NotModifiedException) {
// If server returns NotModified, simply get your content from cache.
return await new Text(url)
.Get()
.Cache(Storage.Disk)
.As<string>();
}
}
}