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; } | |
} | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
RobGibbens
commented
Dec 23, 2014
For completeness sake... I had to change lines 48,49,50 to...
|
This comment has been minimized.
This comment has been minimized.
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
ghuntley commentedDec 4, 2014