Created
December 4, 2014 21:30
-
-
Save ghuntley/ed2eb754a47e2cd3bc90 to your computer and use it in GitHub Desktop.
compelling fusillade + refit example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} | |
} | |
} |
Author
ghuntley
commented
Dec 4, 2014
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)));
♨️
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment