mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-06-08 20:45:51 +00:00
extracting all the cross-platform setup of HttpMessageHandler into
a less awful class
This commit is contained in:
@@ -92,7 +92,7 @@ namespace Octokit
|
||||
/// <param name="credentialStore">Provides credentials to the client when making requests</param>
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
|
||||
public Connection(ProductHeaderValue productInformation, Uri baseAddress, ICredentialStore credentialStore)
|
||||
: this(productInformation, baseAddress, credentialStore, new HttpClientAdapter(), new SimpleJsonSerializer())
|
||||
: this(productInformation, baseAddress, credentialStore, new HttpClientAdapter(HttpMessageHandlerFactory.GetHandler), new SimpleJsonSerializer())
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -18,40 +18,18 @@ namespace Octokit.Internal
|
||||
/// </remarks>
|
||||
public class HttpClientAdapter : IHttpClient
|
||||
{
|
||||
#if !PORTABLE
|
||||
readonly IWebProxy _webProxy;
|
||||
#endif
|
||||
readonly HttpClient _http;
|
||||
|
||||
[SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
|
||||
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
|
||||
public HttpClientAdapter()
|
||||
public HttpClientAdapter(Func<HttpMessageHandler> getHandler)
|
||||
{
|
||||
var handler = GetHandler();
|
||||
Ensure.ArgumentNotNull(getHandler, "getHandler");
|
||||
|
||||
var handler = getHandler();
|
||||
_http = new HttpClient(new RedirectHandler { InnerHandler = handler });
|
||||
}
|
||||
|
||||
#if !PORTABLE
|
||||
[SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
|
||||
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
|
||||
public HttpClientAdapter(IWebProxy webProxy)
|
||||
{
|
||||
_webProxy = webProxy;
|
||||
var handler = GetHandler();
|
||||
_http = new HttpClient(new RedirectHandler { InnerHandler = handler });
|
||||
}
|
||||
|
||||
[SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
|
||||
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
|
||||
public HttpClientAdapter(IWebProxy webProxy, HttpMessageHandler handler)
|
||||
{
|
||||
Ensure.ArgumentNotNull(handler, "handler");
|
||||
|
||||
_webProxy = webProxy;
|
||||
_http = new HttpClient(new RedirectHandler { InnerHandler = handler});
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Sends the specified request and returns a response.
|
||||
/// </summary>
|
||||
@@ -89,28 +67,6 @@ namespace Octokit.Internal
|
||||
return cancellationTokenForRequest;
|
||||
}
|
||||
|
||||
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
|
||||
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
|
||||
protected virtual HttpClientHandler GetHandler()
|
||||
{
|
||||
var httpOptions = new HttpClientHandler
|
||||
{
|
||||
AllowAutoRedirect = false
|
||||
};
|
||||
#if !PORTABLE
|
||||
if (httpOptions.SupportsAutomaticDecompression)
|
||||
{
|
||||
httpOptions.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
|
||||
}
|
||||
if (httpOptions.SupportsProxy && _webProxy != null)
|
||||
{
|
||||
httpOptions.UseProxy = true;
|
||||
httpOptions.Proxy = _webProxy;
|
||||
}
|
||||
#endif
|
||||
return httpOptions;
|
||||
}
|
||||
|
||||
protected async virtual Task<IResponse> BuildResponse(HttpResponseMessage responseMessage)
|
||||
{
|
||||
Ensure.ArgumentNotNull(responseMessage, "responseMessage");
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace Octokit.Internal
|
||||
{
|
||||
public static class HttpMessageHandlerFactory
|
||||
{
|
||||
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
|
||||
public static HttpClientHandler GetHandler()
|
||||
{
|
||||
return GetHandler(null);
|
||||
}
|
||||
|
||||
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
|
||||
public static HttpClientHandler GetHandler(IWebProxy proxy)
|
||||
{
|
||||
var handler = new HttpClientHandler
|
||||
{
|
||||
AllowAutoRedirect = false
|
||||
};
|
||||
#if !PORTABLE
|
||||
if (handler.SupportsAutomaticDecompression)
|
||||
{
|
||||
handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
|
||||
}
|
||||
if (handler.SupportsProxy && proxy != null)
|
||||
{
|
||||
handler.UseProxy = true;
|
||||
handler.Proxy = proxy;
|
||||
}
|
||||
#endif
|
||||
return handler;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user