Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Enables HTTP compression via IHttpClientFactory (#1030)
Browse files Browse the repository at this point in the history
* Setup IHttpClientFactory with compression enabled

* IHttpClientFactory setup for remaining platforms
  • Loading branch information
skolima committed Aug 31, 2020
1 parent 0926036 commit 09bed2f
Show file tree
Hide file tree
Showing 22 changed files with 135 additions and 67 deletions.
1 change: 1 addition & 0 deletions NuKeeper.Abstractions/NuKeeper.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.7" />
<PackageReference Include="NuGet.Protocol" Version="5.7.0" />
</ItemGroup>

Expand Down
7 changes: 5 additions & 2 deletions NuKeeper.AzureDevOps/AzureDevOpsRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ public class AzureDevOpsRestClient
private readonly HttpClient _client;
private readonly INuKeeperLogger _logger;

public AzureDevOpsRestClient(HttpClient client, INuKeeperLogger logger, string personalAccessToken)
public AzureDevOpsRestClient(IHttpClientFactory clientFactory, INuKeeperLogger logger,
string personalAccessToken, Uri apiBaseAddress)
{
_client = client ?? throw new ArgumentNullException(nameof(client));
_logger = logger;

_client = clientFactory.CreateClient();
_client.BaseAddress = apiBaseAddress;
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
_client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{string.Empty}:{personalAccessToken}")));
Expand Down
10 changes: 4 additions & 6 deletions NuKeeper.AzureDevOps/AzureDevopsPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ namespace NuKeeper.AzureDevOps
public class AzureDevOpsPlatform : ICollaborationPlatform
{
private readonly INuKeeperLogger _logger;
private readonly IHttpClientFactory _clientFactory;
private AzureDevOpsRestClient _client;

public AzureDevOpsPlatform(INuKeeperLogger logger)
public AzureDevOpsPlatform(INuKeeperLogger logger, IHttpClientFactory clientFactory)
{
_logger = logger;
_clientFactory = clientFactory;
}

public void Initialise(AuthSettings settings)
Expand All @@ -27,11 +29,7 @@ public void Initialise(AuthSettings settings)
throw new ArgumentNullException(nameof(settings));
}

var httpClient = new HttpClient
{
BaseAddress = settings.ApiBase
};
_client = new AzureDevOpsRestClient(httpClient, _logger, settings.Token);
_client = new AzureDevOpsRestClient(_clientFactory, _logger, settings.Token, settings.ApiBase);
}

public Task<User> GetCurrentUser()
Expand Down
10 changes: 4 additions & 6 deletions NuKeeper.BitBucket/BitbucketPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@ namespace NuKeeper.BitBucket
public class BitbucketPlatform : ICollaborationPlatform
{
private readonly INuKeeperLogger _logger;
private readonly IHttpClientFactory _clientFactory;
private BitbucketRestClient _client;
private AuthSettings _settings;

public BitbucketPlatform(INuKeeperLogger logger)
public BitbucketPlatform(INuKeeperLogger logger, IHttpClientFactory clientFactory)
{
_logger = logger;
_clientFactory = clientFactory;
}

public void Initialise(AuthSettings settings)
{
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
var httpClient = new HttpClient
{
BaseAddress = settings.ApiBase
};
_client = new BitbucketRestClient(httpClient, _logger, settings.Username, settings.Token);
_client = new BitbucketRestClient(_clientFactory, _logger, settings.Username, settings.Token, settings.ApiBase);
}

public Task<User> GetCurrentUser()
Expand Down
6 changes: 4 additions & 2 deletions NuKeeper.BitBucket/BitbucketRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ public class BitbucketRestClient
private readonly HttpClient _client;
private readonly INuKeeperLogger _logger;

public BitbucketRestClient(HttpClient client, INuKeeperLogger logger, string username, string appPassword)
public BitbucketRestClient(IHttpClientFactory clientFactory, INuKeeperLogger logger, string username,
string appPassword, Uri apiBaseAddress)
{
_client = client ?? throw new ArgumentNullException(nameof(client));
_logger = logger;

_client = clientFactory.CreateClient();
_client.BaseAddress = apiBaseAddress;
var byteArray = Encoding.ASCII.GetBytes($"{username}:{appPassword}");
_client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
Expand Down
5 changes: 4 additions & 1 deletion NuKeeper.Gitea.Tests/GiteaSettingsReaderTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using NSubstitute;
using NuKeeper.Abstractions.CollaborationPlatform;
Expand All @@ -20,7 +21,9 @@ public void Setup()
{
_environmentVariablesProvider = Substitute.For<IEnvironmentVariablesProvider>();
_gitDiscovery = Substitute.For<IGitDiscoveryDriver>();
_giteaSettingsReader = new GiteaSettingsReader(_gitDiscovery, _environmentVariablesProvider);
var httpClientFactory = Substitute.For<IHttpClientFactory>();
httpClientFactory.CreateClient().Returns(new HttpClient());
_giteaSettingsReader = new GiteaSettingsReader(_gitDiscovery, _environmentVariablesProvider, httpClientFactory);
}

[Test]
Expand Down
11 changes: 4 additions & 7 deletions NuKeeper.Gitea/GiteaPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ namespace NuKeeper.Gitea
public class GiteaPlatform : ICollaborationPlatform
{
private readonly INuKeeperLogger _logger;
private readonly IHttpClientFactory _clientFactory;
private GiteaRestClient _client;

public GiteaPlatform(INuKeeperLogger logger)
public GiteaPlatform(INuKeeperLogger logger, IHttpClientFactory clientFactory)
{
_logger = logger;
_clientFactory = clientFactory;
}

public void Initialise(AuthSettings settings)
Expand All @@ -28,12 +30,7 @@ public void Initialise(AuthSettings settings)
throw new ArgumentNullException(nameof(settings));
}

var httpClient = new HttpClient
{
BaseAddress = settings.ApiBase
};

_client = new GiteaRestClient(httpClient, settings.Token, _logger);
_client = new GiteaRestClient(_clientFactory, settings.Token, _logger, settings.ApiBase);
}

public async Task<User> GetCurrentUser()
Expand Down
6 changes: 4 additions & 2 deletions NuKeeper.Gitea/GiteaRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ public class GiteaRestClient
private readonly HttpClient _client;
private readonly INuKeeperLogger _logger;

public GiteaRestClient(HttpClient client, string token, INuKeeperLogger logger)
public GiteaRestClient(IHttpClientFactory clientFactory, string token, INuKeeperLogger logger,
Uri apiBaseAddress)
{
_client = client ?? throw new ArgumentNullException(nameof(client));
_logger = logger;

_client = clientFactory.CreateClient();
_client.BaseAddress = apiBaseAddress;
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("token", token);
}
Expand Down
12 changes: 6 additions & 6 deletions NuKeeper.Gitea/GiteaSettingsReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ public class GiteaSettingsReader : ISettingsReader
private const string UrlPattern = "https://yourgiteaserver/{owner}/{repo}.git";
private const string ApiBaseAdress = "api/v1/";
private readonly IGitDiscoveryDriver _gitDriver;
private readonly IHttpClientFactory _clientFactory;

public GiteaSettingsReader(IGitDiscoveryDriver gitDiscoveryDriver, IEnvironmentVariablesProvider environmentVariablesProvider)
public GiteaSettingsReader(IGitDiscoveryDriver gitDiscoveryDriver, IEnvironmentVariablesProvider environmentVariablesProvider,
IHttpClientFactory clientFactory)
{
_environmentVariablesProvider = environmentVariablesProvider;
_clientFactory = clientFactory;
_gitDriver = gitDiscoveryDriver;
}

Expand All @@ -34,11 +37,8 @@ public async Task<bool> CanRead(Uri repositoryUri)
try
{
// There is no real identifier for gitea repos so try to get the gitea swagger json
var client = new HttpClient()
{
BaseAddress = GetBaseAddress(repositoryUri)
};

var client = _clientFactory.CreateClient();
client.BaseAddress = GetBaseAddress(repositoryUri);
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));

Expand Down
10 changes: 4 additions & 6 deletions NuKeeper.Gitlab/GitlabPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ namespace NuKeeper.Gitlab
public class GitlabPlatform : ICollaborationPlatform
{
private readonly INuKeeperLogger _logger;
private readonly IHttpClientFactory _clientFactory;
private GitlabRestClient _client;

public GitlabPlatform(INuKeeperLogger logger)
public GitlabPlatform(INuKeeperLogger logger, IHttpClientFactory clientFactory)
{
_logger = logger;
_clientFactory = clientFactory;
}

public void Initialise(AuthSettings settings)
Expand All @@ -30,11 +32,7 @@ public void Initialise(AuthSettings settings)
throw new ArgumentNullException(nameof(settings));
}

var httpClient = new HttpClient
{
BaseAddress = settings.ApiBase
};
_client = new GitlabRestClient(httpClient, settings.Token, _logger);
_client = new GitlabRestClient(_clientFactory, settings.Token, _logger, settings.ApiBase);
}

public async Task<User> GetCurrentUser()
Expand Down
5 changes: 3 additions & 2 deletions NuKeeper.Gitlab/GitlabRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ public class GitlabRestClient
private readonly HttpClient _client;
private readonly INuKeeperLogger _logger;

public GitlabRestClient(HttpClient client, string token, INuKeeperLogger logger)
public GitlabRestClient(IHttpClientFactory clientFactory, string token, INuKeeperLogger logger, Uri apiBaseAddress)
{
_client = client ?? throw new ArgumentNullException(nameof(client));
_logger = logger;

_client = clientFactory.CreateClient();
_client.BaseAddress = apiBaseAddress;
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
_client.DefaultRequestHeaders.Add("Private-Token", token);
}
Expand Down
7 changes: 6 additions & 1 deletion NuKeeper.Tests/Commands/GlobalCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using NuKeeper.Abstractions.Git;
using NuKeeper.AzureDevOps;
Expand All @@ -23,9 +24,13 @@ private static CollaborationFactory GetCollaborationFactory(Func<IGitDiscoveryDr
{
var environmentVariablesProvider = Substitute.For<IEnvironmentVariablesProvider>();

var httpClientFactory = Substitute.For<IHttpClientFactory>();
httpClientFactory.CreateClient().Returns(new HttpClient());

return new CollaborationFactory(
new ISettingsReader[] { createSettingsReader(new MockedGitDiscoveryDriver(), environmentVariablesProvider) },
Substitute.For<INuKeeperLogger>()
Substitute.For<INuKeeperLogger>(),
httpClientFactory
);
}

Expand Down
7 changes: 6 additions & 1 deletion NuKeeper.Tests/Commands/OrganisationCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using NuKeeper.Abstractions.Git;
using NuKeeper.AzureDevOps;
Expand All @@ -23,9 +24,13 @@ private static CollaborationFactory GetCollaborationFactory(Func<IGitDiscoveryDr
{
var environmentVariablesProvider = Substitute.For<IEnvironmentVariablesProvider>();

var httpClientFactory = Substitute.For<IHttpClientFactory>();
httpClientFactory.CreateClient().Returns(new HttpClient());

return new CollaborationFactory(
new ISettingsReader[] { createSettingsReader(new MockedGitDiscoveryDriver(), environmentVariablesProvider) },
Substitute.For<INuKeeperLogger>()
Substitute.For<INuKeeperLogger>(),
httpClientFactory
);
}

Expand Down
3 changes: 2 additions & 1 deletion NuKeeper.Tests/Commands/RepositoryCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,8 @@ public async Task WillReadMaxPackageUpdatesFromFile()
{
return new CollaborationFactory(
settingReaders ?? new ISettingsReader[] { new GitHubSettingsReader(new MockedGitDiscoveryDriver(), environmentVariablesProvider) },
Substitute.For<INuKeeperLogger>()
Substitute.For<INuKeeperLogger>(),
null
);
}
}
Expand Down
6 changes: 5 additions & 1 deletion NuKeeper.Tests/Engine/CollaborationFactoryTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using NSubstitute;
using NuKeeper.Abstractions.CollaborationPlatform;
Expand Down Expand Up @@ -31,7 +32,10 @@ private static CollaborationFactory GetCollaborationFactory()

var readers = new List<ISettingsReader> { settingReader1, settingReader2 };
var logger = Substitute.For<INuKeeperLogger>();
return new CollaborationFactory(readers, logger);
var httpClientFactory = Substitute.For<IHttpClientFactory>();
httpClientFactory.CreateClient().Returns(new HttpClient());

return new CollaborationFactory(readers, logger, httpClientFactory);
}

[Test]
Expand Down
15 changes: 9 additions & 6 deletions NuKeeper/Collaboration/CollaborationFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using NuKeeper.Abstractions;
using NuKeeper.Abstractions.CollaborationPlatform;
Expand All @@ -21,6 +22,7 @@ public class CollaborationFactory : ICollaborationFactory
{
private readonly IEnumerable<ISettingsReader> _settingReaders;
private readonly INuKeeperLogger _nuKeeperLogger;
private readonly IHttpClientFactory _httpClientFactory;
private Platform? _platform;

public IForkFinder ForkFinder { get; private set; }
Expand All @@ -34,10 +36,11 @@ public class CollaborationFactory : ICollaborationFactory
public CollaborationPlatformSettings Settings { get; }

public CollaborationFactory(IEnumerable<ISettingsReader> settingReaders,
INuKeeperLogger nuKeeperLogger)
INuKeeperLogger nuKeeperLogger, IHttpClientFactory httpClientFactory)
{
_settingReaders = settingReaders;
_nuKeeperLogger = nuKeeperLogger;
_httpClientFactory = httpClientFactory;
Settings = new CollaborationPlatformSettings();
}

Expand Down Expand Up @@ -133,7 +136,7 @@ private void CreateForPlatform()
switch (_platform.Value)
{
case Platform.AzureDevOps:
CollaborationPlatform = new AzureDevOpsPlatform(_nuKeeperLogger);
CollaborationPlatform = new AzureDevOpsPlatform(_nuKeeperLogger, _httpClientFactory);
RepositoryDiscovery = new AzureDevOpsRepositoryDiscovery(_nuKeeperLogger, CollaborationPlatform, Settings.Token);
ForkFinder = new AzureDevOpsForkFinder(CollaborationPlatform, _nuKeeperLogger, forkMode);

Expand All @@ -150,28 +153,28 @@ private void CreateForPlatform()
break;

case Platform.Bitbucket:
CollaborationPlatform = new BitbucketPlatform(_nuKeeperLogger);
CollaborationPlatform = new BitbucketPlatform(_nuKeeperLogger, _httpClientFactory);
RepositoryDiscovery = new BitbucketRepositoryDiscovery(_nuKeeperLogger);
ForkFinder = new BitbucketForkFinder(CollaborationPlatform, _nuKeeperLogger, forkMode);
CommitWorder = new BitbucketCommitWorder();
break;

case Platform.BitbucketLocal:
CollaborationPlatform = new BitBucketLocalPlatform(_nuKeeperLogger);
CollaborationPlatform = new BitBucketLocalPlatform(_nuKeeperLogger, _httpClientFactory);
RepositoryDiscovery = new BitbucketLocalRepositoryDiscovery(_nuKeeperLogger, CollaborationPlatform, Settings);
ForkFinder = new BitbucketForkFinder(CollaborationPlatform, _nuKeeperLogger, forkMode);
CommitWorder = new DefaultCommitWorder();
break;

case Platform.GitLab:
CollaborationPlatform = new GitlabPlatform(_nuKeeperLogger);
CollaborationPlatform = new GitlabPlatform(_nuKeeperLogger, _httpClientFactory);
RepositoryDiscovery = new GitlabRepositoryDiscovery(_nuKeeperLogger, CollaborationPlatform);
ForkFinder = new GitlabForkFinder(CollaborationPlatform, _nuKeeperLogger, forkMode);
CommitWorder = new DefaultCommitWorder();
break;

case Platform.Gitea:
CollaborationPlatform = new GiteaPlatform(_nuKeeperLogger);
CollaborationPlatform = new GiteaPlatform(_nuKeeperLogger, _httpClientFactory);
RepositoryDiscovery = new GiteaRepositoryDiscovery(_nuKeeperLogger, CollaborationPlatform);
ForkFinder = new GiteaForkFinder(CollaborationPlatform, _nuKeeperLogger, forkMode);
CommitWorder = new DefaultCommitWorder();
Expand Down
Loading

0 comments on commit 09bed2f

Please sign in to comment.