Skip to content

Instantly share code, notes, and snippets.

@ghuntley
Created December 4, 2014 21:30
Show Gist options
  • Save ghuntley/ed2eb754a47e2cd3bc90 to your computer and use it in GitHub Desktop.
Save ghuntley/ed2eb754a47e2cd3bc90 to your computer and use it in GitHub Desktop.
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; }
}
}
}
@ghuntley
Copy link
Author

♨️

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