mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-06-03 03:01:31 +00:00
extracting all the cross-platform setup of HttpMessageHandler into
a less awful class
This commit is contained in:
@@ -14,7 +14,7 @@ public class HttpClientAdapterTests
|
||||
[IntegrationTest]
|
||||
public async Task CanDownloadImage()
|
||||
{
|
||||
var httpClient = new HttpClientAdapter();
|
||||
var httpClient = new HttpClientAdapter(HttpMessageHandlerFactory.GetHandler);
|
||||
var request = new Request
|
||||
{
|
||||
BaseAddress = new Uri("https://github.global.ssl.fastly.net/", UriKind.Absolute),
|
||||
@@ -36,7 +36,7 @@ public class HttpClientAdapterTests
|
||||
[IntegrationTest]
|
||||
public async Task CanCancelARequest()
|
||||
{
|
||||
var httpClient = new HttpClientAdapter();
|
||||
var httpClient = new HttpClientAdapter(HttpMessageHandlerFactory.GetHandler);
|
||||
var request = new Request
|
||||
{
|
||||
BaseAddress = new Uri("https://github.global.ssl.fastly.net/", UriKind.Absolute),
|
||||
|
||||
@@ -178,6 +178,11 @@ namespace Octokit.Tests.Http
|
||||
|
||||
sealed class HttpClientAdapterTester : HttpClientAdapter
|
||||
{
|
||||
public HttpClientAdapterTester()
|
||||
: base (HttpMessageHandlerFactory.GetHandler)
|
||||
{
|
||||
}
|
||||
|
||||
public HttpRequestMessage BuildRequestMessageTester(IRequest request)
|
||||
{
|
||||
return BuildRequestMessage(request);
|
||||
@@ -187,11 +192,6 @@ namespace Octokit.Tests.Http
|
||||
{
|
||||
return await BuildResponse(responseMessage);
|
||||
}
|
||||
|
||||
protected override HttpClientHandler GetHandler()
|
||||
{
|
||||
return new HttpClientHandler();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Binary file not shown.
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -393,6 +393,7 @@
|
||||
<Compile Include="Models\Response\PullRequestFile.cs" />
|
||||
<Compile Include="Models\Request\PublicRepositoryRequest.cs" />
|
||||
<Compile Include="Exceptions\TwoFactorAuthorizationException.cs" />
|
||||
<Compile Include="Http\HttpMessageHandlerFactory.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
@@ -409,6 +409,7 @@
|
||||
<Compile Include="Models\Request\RepositoryHooksPingRequest.cs" />
|
||||
<Compile Include="Exceptions\TwoFactorAuthorizationException.cs" />
|
||||
<Compile Include="Models\Request\RepositoryHookTestRequest.cs" />
|
||||
<Compile Include="Http\HttpMessageHandlerFactory.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
|
||||
</Project>
|
||||
@@ -402,6 +402,7 @@
|
||||
<Compile Include="Models\Request\RepositoryHooksPingRequest.cs" />
|
||||
<Compile Include="Exceptions\TwoFactorAuthorizationException.cs" />
|
||||
<Compile Include="Models\Request\RepositoryHookTestRequest.cs" />
|
||||
<Compile Include="Http\HttpMessageHandlerFactory.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.MonoTouch.CSharp.targets" />
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
|
||||
@@ -128,6 +128,7 @@
|
||||
<Compile Include="Exceptions\NotFoundException.cs" />
|
||||
<Compile Include="Exceptions\TwoFactorChallengeFailedException.cs" />
|
||||
<Compile Include="Exceptions\TwoFactorRequiredException.cs" />
|
||||
<Compile Include="GlobalSuppressions.cs" />
|
||||
<Compile Include="Helpers\ApiExtensions.cs" />
|
||||
<Compile Include="Helpers\ApiUrls.cs" />
|
||||
<Compile Include="Helpers\AuthorizationExtensions.cs" />
|
||||
@@ -391,6 +392,7 @@
|
||||
<Compile Include="Models\Response\PullRequestFile.cs" />
|
||||
<Compile Include="Models\Request\PublicRepositoryRequest.cs" />
|
||||
<Compile Include="Exceptions\TwoFactorAuthorizationException.cs" />
|
||||
<Compile Include="Http\HttpMessageHandlerFactory.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<CodeAnalysisDictionary Include="..\CustomDictionary.xml">
|
||||
|
||||
@@ -395,6 +395,7 @@
|
||||
<Compile Include="Models\Response\PullRequestFile.cs" />
|
||||
<Compile Include="Models\Request\PublicRepositoryRequest.cs" />
|
||||
<Compile Include="Exceptions\TwoFactorAuthorizationException.cs" />
|
||||
<Compile Include="Http\HttpMessageHandlerFactory.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<CodeAnalysisDictionary Include="..\CustomDictionary.xml">
|
||||
@@ -410,4 +411,4 @@
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
||||
</Project>
|
||||
@@ -81,6 +81,7 @@
|
||||
<Compile Include="Helpers\HttpClientExtensions.cs" />
|
||||
<Compile Include="Helpers\PropertyOrField.cs" />
|
||||
<Compile Include="Helpers\SerializeNullAttribute.cs" />
|
||||
<Compile Include="Http\HttpMessageHandlerFactory.cs" />
|
||||
<Compile Include="Http\ProductHeaderValue.cs" />
|
||||
<Compile Include="Models\Request\GistFileUpdate.cs" />
|
||||
<Compile Include="Models\Request\NewMerge.cs" />
|
||||
|
||||
Reference in New Issue
Block a user