ReliableRestClientWrapper by jsolana,mediatechsolutions

<PackageReference Include="ReliableRestClientWrapper" Version="0.1.1" />

 ReliableRestClientWrapper

using Polly; using ReliableRestClient.Exceptions; using RestSharp; using RestSharp.Authenticators; using RestSharp.Deserializers; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Cache; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading; using System.Threading.Tasks; namespace ReliableRestClient { public class ReliableRestClientWrapper { private IRestClient _innerClient; private readonly ISyncPolicy _retryPolicy; private int[] HttpStatusCodesWorthRetrying = new int[3] { 500, 502, 503 }; private int[] HttpStatusCodesTimeout = new int[3] { 0, 408, 504 }; public IAuthenticator Authenticator { get { return _innerClient.get_Authenticator(); } set { _innerClient.set_Authenticator(value); } } public Uri BaseUrl { get { return _innerClient.get_BaseUrl(); } set { _innerClient.set_BaseUrl(value); } } public RequestCachePolicy CachePolicy { get { return _innerClient.get_CachePolicy(); } set { _innerClient.set_CachePolicy(value); } } public X509CertificateCollection ClientCertificates { get { return _innerClient.get_ClientCertificates(); } set { _innerClient.set_ClientCertificates(value); } } public CookieContainer CookieContainer { get { return _innerClient.get_CookieContainer(); } set { _innerClient.set_CookieContainer(value); } } public IList<Parameter> DefaultParameters => _innerClient.get_DefaultParameters(); public Encoding Encoding { get { return _innerClient.get_Encoding(); } set { _innerClient.set_Encoding(value); } } public bool FollowRedirects { get { return _innerClient.get_FollowRedirects(); } set { _innerClient.set_FollowRedirects(value); } } public int? MaxRedirects { get { return _innerClient.get_MaxRedirects(); } set { _innerClient.set_MaxRedirects(value); } } public bool PreAuthenticate { get { return _innerClient.get_PreAuthenticate(); } set { _innerClient.set_PreAuthenticate(value); } } public IWebProxy Proxy { get { return _innerClient.get_Proxy(); } set { _innerClient.set_Proxy(value); } } public int ReadWriteTimeout { get { return _innerClient.get_ReadWriteTimeout(); } set { _innerClient.set_ReadWriteTimeout(value); } } public int Timeout { get { return _innerClient.get_Timeout(); } set { _innerClient.set_Timeout(value); } } public string UserAgent { get { return _innerClient.get_UserAgent(); } set { _innerClient.set_UserAgent(value); } } public bool UseSynchronizationContext { get { return _innerClient.get_UseSynchronizationContext(); } set { _innerClient.set_UseSynchronizationContext(value); } } public bool AutomaticDecompression { get { return _innerClient.get_AutomaticDecompression(); } set { _innerClient.set_AutomaticDecompression(value); } } public string ConnectionGroupName { get { return _innerClient.get_ConnectionGroupName(); } set { _innerClient.set_ConnectionGroupName(value); } } public bool UnsafeAuthenticatedConnectionSharing { get { return _innerClient.get_UnsafeAuthenticatedConnectionSharing(); } set { _innerClient.set_UnsafeAuthenticatedConnectionSharing(value); } } public string BaseHost { get { return _innerClient.get_BaseHost(); } set { _innerClient.set_BaseHost(value); } } public bool AllowMultipleDefaultParametersWithSameName { get { return _innerClient.get_AllowMultipleDefaultParametersWithSameName(); } set { _innerClient.set_AllowMultipleDefaultParametersWithSameName(value); } } public bool Pipelined { get { return _innerClient.get_Pipelined(); } set { _innerClient.set_Pipelined(value); } } public RemoteCertificateValidationCallback RemoteCertificateValidationCallback { get { return _innerClient.get_RemoteCertificateValidationCallback(); } set { _innerClient.set_RemoteCertificateValidationCallback(value); } } public ReliableRestClientWrapper(IRestClient innerClient, ISyncPolicy retryPolicy) { _innerClient = innerClient; _retryPolicy = retryPolicy; } public void AddHandler(string contentType, IDeserializer deserializer) { _innerClient.AddHandler(contentType, deserializer); } public Uri BuildUri(IRestRequest request) { return _innerClient.BuildUri(request); } public void ClearHandlers() { _innerClient.ClearHandlers(); } public byte[] DownloadData(IRestRequest request) { return _innerClient.DownloadData(request); } public virtual IRestResponse Execute(IRestRequest request) { IRestResponse response = null; _retryPolicy.Execute((Action)delegate { response = _innerClient.Execute(request); ProcessResponse(response); }); return response; } public IRestResponse<T> Execute<T>(IRestRequest request) where T : new { IRestResponse<T> response = null; _retryPolicy.Execute((Action)delegate { response = _innerClient.Execute<T>(request); this.ProcessResponse<T>(response); }); return response; } public IRestResponse ExecuteAsGet(IRestRequest request, string httpMethod) { IRestResponse response = null; _retryPolicy.Execute((Action)delegate { response = _innerClient.ExecuteAsGet(request, httpMethod); ProcessResponse(response); }); return response; } public IRestResponse<T> ExecuteAsGet<T>(IRestRequest request, string httpMethod) where T : new { IRestResponse<T> response = null; _retryPolicy.Execute((Action)delegate { response = _innerClient.ExecuteAsGet<T>(request, httpMethod); this.ProcessResponse<T>(response); }); return response; } public IRestResponse ExecuteAsPost(IRestRequest request, string httpMethod) { IRestResponse response = null; _retryPolicy.Execute((Action)delegate { response = _innerClient.ExecuteAsPost(request, httpMethod); ProcessResponse(response); }); return response; } public IRestResponse<T> ExecuteAsPost<T>(IRestRequest request, string httpMethod) where T : new { IRestResponse<T> response = null; _retryPolicy.Execute((Action)delegate { response = _innerClient.ExecuteAsPost<T>(request, httpMethod); this.ProcessResponse<T>(response); }); return response; } public IRestResponse<T> Deserialize<T>(IRestResponse response) { return _innerClient.Deserialize<T>(response); } public IRestResponse Execute(IRestRequest request, Method httpMethod) { IRestResponse response = null; _retryPolicy.Execute((Action)delegate { response = _innerClient.Execute(request, httpMethod); ProcessResponse(response); }); return response; } public IRestResponse<T> Execute<T>(IRestRequest request, Method httpMethod) where T : new { IRestResponse<T> response = null; _retryPolicy.Execute((Action)delegate { response = _innerClient.Execute<T>(request, httpMethod); this.ProcessResponse<T>(response); }); return response; } public byte[] DownloadData(IRestRequest request, bool throwOnError) { return _innerClient.DownloadData(request, throwOnError); } public void ConfigureWebRequest(Action<HttpWebRequest> configurator) { _innerClient.ConfigureWebRequest(configurator); } public Task<IRestResponse<T>> ExecuteTaskAsync<T>(IRestRequest request, Method httpMethod) { return _innerClient.ExecuteTaskAsync<T>(request, httpMethod); } public Task<IRestResponse> ExecuteTaskAsync(IRestRequest request, CancellationToken token, Method httpMethod) { return _innerClient.ExecuteTaskAsync(request, token, httpMethod); } public RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action<IRestResponse, RestRequestAsyncHandle> callback, Method httpMethod) { return _innerClient.ExecuteAsync(request, callback, httpMethod); } public RestRequestAsyncHandle ExecuteAsync<T>(IRestRequest request, Action<IRestResponse<T>, RestRequestAsyncHandle> callback, Method httpMethod) { return _innerClient.ExecuteAsync<T>(request, callback, httpMethod); } public RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action<IRestResponse, RestRequestAsyncHandle> callback) { return _innerClient.ExecuteAsync(request, callback); } public RestRequestAsyncHandle ExecuteAsync<T>(IRestRequest request, Action<IRestResponse<T>, RestRequestAsyncHandle> callback) { return _innerClient.ExecuteAsync<T>(request, callback); } public RestRequestAsyncHandle ExecuteAsyncGet(IRestRequest request, Action<IRestResponse, RestRequestAsyncHandle> callback, string httpMethod) { return _innerClient.ExecuteAsyncGet(request, callback, httpMethod); } public RestRequestAsyncHandle ExecuteAsyncGet<T>(IRestRequest request, Action<IRestResponse<T>, RestRequestAsyncHandle> callback, string httpMethod) { return _innerClient.ExecuteAsyncGet<T>(request, callback, httpMethod); } public RestRequestAsyncHandle ExecuteAsyncPost(IRestRequest request, Action<IRestResponse, RestRequestAsyncHandle> callback, string httpMethod) { return _innerClient.ExecuteAsyncPost(request, callback, httpMethod); } public RestRequestAsyncHandle ExecuteAsyncPost<T>(IRestRequest request, Action<IRestResponse<T>, RestRequestAsyncHandle> callback, string httpMethod) { return _innerClient.ExecuteAsyncPost<T>(request, callback, httpMethod); } public Task<IRestResponse> ExecuteGetTaskAsync(IRestRequest request) { return _innerClient.ExecuteGetTaskAsync(request); } public Task<IRestResponse> ExecuteGetTaskAsync(IRestRequest request, CancellationToken token) { return _innerClient.ExecuteGetTaskAsync(request, token); } public Task<IRestResponse<T>> ExecuteGetTaskAsync<T>(IRestRequest request) { return _innerClient.ExecuteGetTaskAsync<T>(request); } public Task<IRestResponse<T>> ExecuteGetTaskAsync<T>(IRestRequest request, CancellationToken token) { return _innerClient.ExecuteGetTaskAsync<T>(request, token); } public Task<IRestResponse> ExecutePostTaskAsync(IRestRequest request) { return _innerClient.ExecutePostTaskAsync(request); } public Task<IRestResponse> ExecutePostTaskAsync(IRestRequest request, CancellationToken token) { return _innerClient.ExecutePostTaskAsync(request, token); } public Task<IRestResponse<T>> ExecutePostTaskAsync<T>(IRestRequest request) { return _innerClient.ExecutePostTaskAsync<T>(request); } public Task<IRestResponse<T>> ExecutePostTaskAsync<T>(IRestRequest request, CancellationToken token) { return _innerClient.ExecutePostTaskAsync<T>(request, token); } public Task<IRestResponse> ExecuteTaskAsync(IRestRequest request) { return _innerClient.ExecuteTaskAsync(request); } public Task<IRestResponse> ExecuteTaskAsync(IRestRequest request, CancellationToken token) { return _innerClient.ExecuteTaskAsync(request, token); } public Task<IRestResponse<T>> ExecuteTaskAsync<T>(IRestRequest request) { return _innerClient.ExecuteTaskAsync<T>(request); } public Task<IRestResponse<T>> ExecuteTaskAsync<T>(IRestRequest request, CancellationToken token) { return _innerClient.ExecuteTaskAsync<T>(request, token); } public void RemoveHandler(string contentType) { _innerClient.RemoveHandler(contentType); } private void ProcessResponse<T>(IRestResponse<T> response) { if (HttpStatusCodesWorthRetrying.Contains((int)response.get_StatusCode())) throw new RestServerSideException((int)response.get_StatusCode(), response.get_ErrorMessage(), response.get_ErrorException()); if (HttpStatusCodesTimeout.Contains((int)response.get_StatusCode())) throw new RestTimeoutException((int)response.get_StatusCode(), response.get_ErrorMessage(), response.get_ErrorException()); } private void ProcessResponse(IRestResponse response) { if (HttpStatusCodesWorthRetrying.Contains((int)response.get_StatusCode())) throw new RestServerSideException((int)response.get_StatusCode(), response.get_ErrorMessage(), response.get_ErrorException()); if (HttpStatusCodesTimeout.Contains((int)response.get_StatusCode())) throw new RestTimeoutException((int)response.get_StatusCode(), response.get_ErrorMessage(), response.get_ErrorException()); } } }