Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
compelling fusillade + refit example
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using EndlessCatsApi.ServiceModel;
using EndlessCatsApp.Core.Helpers;
using Fusillade;
using Refit;
namespace EndlessCatsApp.Core.Services
{
[Headers("User-Agent: " + AppSettings.ApiClientUserAgent)]
public interface IEndlessCatsApi
{
/// <remarks>
/// - You can redefine the user-agent header on a per request basis via: [Headers("User-Agent: ELinks/0.9.3 (textmode; Linux 2.6.11 i686; 79x24)"]
/// - You can remove the user-agent header on a per request basis via: [Headers("User-Agent")]
/// </remarks>
[Get("/cats")]
Task<List<Cat>> GetMoreCats([Header("X-Auth-Token")] string apiKey);
}
public interface IApiService
{
IEndlessCatsApi Speculative { get; }
IEndlessCatsApi UserInitiated { get; }
IEndlessCatsApi Background { get; }
}
public class ApiService : IApiService
{
public const string ApiBaseAddress = "https://endlesscats.azurewebsites.net/api";
public ApiService(string apiBaseAddress = null)
{
Func<HttpMessageHandler, IEndlessCatsApi> createClient = messageHandler =>
{
Ensure.ArgumentIsOfType(messageHandler, typeof(RateLimitedHttpMessageHandler), "Fusillade RateLimitedHttpMessageHandler");
var client = new HttpClient(messageHandler)
{
BaseAddress = new Uri(apiBaseAddress ?? ApiBaseAddress)
};
return RestService.For<IEndlessCatsApi>(client);
};
_background = new Lazy<IEndlessCatsApi>(() => RestService.For<IEndlessCatsApi>(ApiBaseAddress));
_userInitiated = new Lazy<IEndlessCatsApi>(() => RestService.For<IEndlessCatsApi>(ApiBaseAddress));
_speculative = new Lazy<IEndlessCatsApi>(() => RestService.For<IEndlessCatsApi>(ApiBaseAddress));
}
private readonly Lazy<IEndlessCatsApi> _background;
private readonly Lazy<IEndlessCatsApi> _userInitiated;
private readonly Lazy<IEndlessCatsApi> _speculative;
public IEndlessCatsApi Background
{
get { return _background.Value; }
}
public IEndlessCatsApi UserInitiated
{
get { return _userInitiated.Value; }
}
public IEndlessCatsApi Speculative
{
get { return _speculative.Value; }
}
}
}
Owner

ghuntley commented Dec 4, 2014

var apiService = Locator.Current.GetType<IAPIService>();

// use this to download items such as avatars or thumbnail photos in background.
// If request for UserInitiated comes through then a processing slot of this
// queue is stolen for UserInitiated until it completes. 
apiService.Background.GetMoreCats();        

// use this download items in response to user action, say view fullsize photo.
apiService.UserInitiated.GetMoreCats();     

// use this to speculatively prefetch items a user may do a user action on.
// for example speculatively download fullsize photos that a user may click on
// if the device is connected by Wifi. ala Chrome HTTP Prefetch. Don't forget
// to configure the NetCache.Speculative.ResetLimit(1048576 * 5/*MB*/) as appropriate.
apiService.Speculative.GetMoreCats();

For completeness sake... I had to change lines 48,49,50 to...

_background = new Lazy<ITekConfApi>(() => createClient(
    new RateLimitedHttpMessageHandler(new NativeMessageHandler(), Priority.Background)));

_userInitiated = new Lazy<ITekConfApi>(() => createClient(
    new RateLimitedHttpMessageHandler(new NativeMessageHandler(), Priority.UserInitiated)));

_speculative = new Lazy<ITekConfApi>(() => createClient(
    new RateLimitedHttpMessageHandler(new NativeMessageHandler(), Priority.Speculative)));
Owner

ghuntley commented Jan 22, 2015

♨️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment