Created
July 14, 2019 08:35
-
-
Save hamid-shaikh/7a766529e79cea8e108d53c657ca1594 to your computer and use it in GitHub Desktop.
Refit + Prism Forms + Fusillade + ModernHttpClient
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.Net; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using Fusillade; | |
using ModernHttpClient; | |
using Newtonsoft.Json; | |
using Refit; | |
/// <summary> | |
/// Private and Public Variables | |
/// </summary> | |
/// <typeparam name="TInterfaceService"></typeparam> | |
public partial class ApiService<TInterfaceService> | |
{ | |
readonly JsonSerializerSettings jsonSerializerSettings; | |
readonly Lazy<TInterfaceService> _background; | |
readonly Lazy<TInterfaceService> _userInitiated; | |
readonly Lazy<TInterfaceService> _speculative; | |
} | |
/// <summary> | |
/// Constructor Creation | |
/// </summary> | |
/// <typeparam name="TInterfaceService"></typeparam> | |
public partial class ApiService<TInterfaceService> | |
{ | |
public ApiService() | |
{ | |
jsonSerializerSettings = new JsonSerializerSettings | |
{ | |
ReferenceLoopHandling = ReferenceLoopHandling.Ignore, | |
NullValueHandling = NullValueHandling.Ignore, | |
DateTimeZoneHandling = DateTimeZoneHandling.Utc | |
}; | |
_background = new Lazy<TInterfaceService>(() => CreateHttpClient( | |
new RateLimitedHttpMessageHandler(new NativeMessageHandler(), Priority.Background))); | |
_userInitiated = new Lazy<TInterfaceService>(() => CreateHttpClient( | |
new RateLimitedHttpMessageHandler(new NativeMessageHandler(), Priority.UserInitiated))); | |
_speculative = new Lazy<TInterfaceService>(() => CreateHttpClient( | |
new RateLimitedHttpMessageHandler(new NativeMessageHandler(), Priority.Speculative))); | |
} | |
} | |
/// <summary> | |
/// Private Methods | |
/// </summary> | |
/// <typeparam name="TInterfaceService"></typeparam> | |
public partial class ApiService<TInterfaceService> | |
{ | |
TInterfaceService CreateHttpClient(HttpMessageHandler messageHandler) | |
{ | |
var client = new HttpClient(messageHandler) | |
{ | |
BaseAddress = new Uri(AppConstants.BaseAddress), | |
Timeout = TimeSpan.FromSeconds(AppConstants.Timeout) | |
}; | |
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); | |
//client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8"); | |
return RestService.For<TInterfaceService>(client, new RefitSettings | |
{ | |
ContentSerializer = new JsonContentSerializer(jsonSerializerSettings) | |
}); | |
} | |
} | |
/// <summary> | |
/// IApiService<TInterfaceService> Implementation | |
/// </summary> | |
/// <typeparam name="TInterfaceService"></typeparam> | |
public partial class ApiService<TInterfaceService> : IApiService<TInterfaceService> | |
{ | |
public TInterfaceService Speculative { get => _speculative.Value; } | |
public TInterfaceService UserInitiated { get => _userInitiated.Value; } | |
public TInterfaceService Background { get => _background.Value; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Step 1 : - https://gist.github.com/hamid-shaikh/24d93419f9b5313685c4dfc40f4b59dc
Step 2 : - https://gist.github.com/hamid-shaikh/7a766529e79cea8e108d53c657ca1594
Step 3 : - https://gist.github.com/hamid-shaikh/803e88151281e24bc07fba2a4903cc49
Step 4 : - https://gist.github.com/hamid-shaikh/082fea0c0bb3accc7c1db180d81a9884
Step 5 : - https://gist.github.com/hamid-shaikh/0774fe04cf7121c0d596925c93fc1809