From f0007657088447e23e5415505da4c32bf1199fbd Mon Sep 17 00:00:00 2001 From: Ross Lovas Date: Tue, 13 Aug 2024 13:51:26 +0930 Subject: [PATCH] Replace OctopusRepositoryExtensions with methods that reuse RootResource --- ...AreaShouldNotRegress..NETCore.approved.txt | 12 +++-- ...houldNotRegress..NETFramework.approved.txt | 12 +++-- .../IOctopusAsyncRepository.cs | 5 +- .../IOctopusRepository.cs | 4 ++ .../OctopusAsyncRepository.cs | 46 ++++++++++++------ .../OctopusRepository.cs | 48 ++++++++++++------- 6 files changed, 87 insertions(+), 40 deletions(-) diff --git a/source/Octopus.Client.Tests/PublicSurfaceAreaFixture.ThePublicSurfaceAreaShouldNotRegress..NETCore.approved.txt b/source/Octopus.Client.Tests/PublicSurfaceAreaFixture.ThePublicSurfaceAreaShouldNotRegress..NETCore.approved.txt index c0299011f..d4e3928de 100644 --- a/source/Octopus.Client.Tests/PublicSurfaceAreaFixture.ThePublicSurfaceAreaShouldNotRegress..NETCore.approved.txt +++ b/source/Octopus.Client.Tests/PublicSurfaceAreaFixture.ThePublicSurfaceAreaShouldNotRegress..NETCore.approved.txt @@ -89,6 +89,8 @@ Octopus.Client Octopus.Client.IOctopusCommonAsyncRepository Octopus.Client.IOctopusSystemAsyncRepository { + Octopus.Client.IOctopusSpaceAsyncRepository ForSpace(Octopus.Client.Model.SpaceResource) + Octopus.Client.IOctopusSystemAsyncRepository ForSystem() } interface IOctopusClient IDisposable @@ -168,6 +170,8 @@ Octopus.Client Octopus.Client.IOctopusCommonRepository Octopus.Client.IOctopusSystemRepository { + Octopus.Client.IOctopusSpaceRepository ForSpace(Octopus.Client.Model.SpaceResource) + Octopus.Client.IOctopusSystemRepository ForSystem() } interface IOctopusSpaceAsyncRepository Octopus.Client.IOctopusCommonAsyncRepository @@ -440,6 +444,8 @@ Octopus.Client Octopus.Client.Repositories.Async.IVariableSetRepository VariableSets { get; } Octopus.Client.Repositories.Async.IWorkerPoolRepository WorkerPools { get; } Octopus.Client.Repositories.Async.IWorkerRepository Workers { get; } + Octopus.Client.IOctopusSpaceAsyncRepository ForSpace(Octopus.Client.Model.SpaceResource) + Octopus.Client.IOctopusSystemAsyncRepository ForSystem() Task HasLink(String) Task HasLinkParameter(String, String) Task Link(String) @@ -582,6 +588,8 @@ Octopus.Client Octopus.Client.Repositories.IVariableSetRepository VariableSets { get; } Octopus.Client.Repositories.IWorkerPoolRepository WorkerPools { get; } Octopus.Client.Repositories.IWorkerRepository Workers { get; } + Octopus.Client.IOctopusSpaceRepository ForSpace(Octopus.Client.Model.SpaceResource) + Octopus.Client.IOctopusSystemRepository ForSystem() Boolean HasLink(String) Boolean HasLinkParameter(String, String) String Link(String) @@ -591,10 +599,6 @@ Octopus.Client abstract class OctopusRepositoryExtensions { static Octopus.Client.IOctopusAsyncRepository CreateRepository(Octopus.Client.IOctopusAsyncClient, Octopus.Client.RepositoryScope) - static Octopus.Client.IOctopusSpaceAsyncRepository ForSpace(Octopus.Client.IOctopusAsyncRepository, Octopus.Client.Model.SpaceResource) - static Octopus.Client.IOctopusSpaceRepository ForSpace(Octopus.Client.IOctopusRepository, Octopus.Client.Model.SpaceResource) - static Octopus.Client.IOctopusSystemAsyncRepository ForSystem(Octopus.Client.IOctopusAsyncRepository) - static Octopus.Client.IOctopusSystemRepository ForSystem(Octopus.Client.IOctopusRepository) } class OctopusRequest { diff --git a/source/Octopus.Client.Tests/PublicSurfaceAreaFixture.ThePublicSurfaceAreaShouldNotRegress..NETFramework.approved.txt b/source/Octopus.Client.Tests/PublicSurfaceAreaFixture.ThePublicSurfaceAreaShouldNotRegress..NETFramework.approved.txt index 104f9d15c..f267ad83b 100644 --- a/source/Octopus.Client.Tests/PublicSurfaceAreaFixture.ThePublicSurfaceAreaShouldNotRegress..NETFramework.approved.txt +++ b/source/Octopus.Client.Tests/PublicSurfaceAreaFixture.ThePublicSurfaceAreaShouldNotRegress..NETFramework.approved.txt @@ -89,6 +89,8 @@ Octopus.Client Octopus.Client.IOctopusCommonAsyncRepository Octopus.Client.IOctopusSystemAsyncRepository { + Octopus.Client.IOctopusSpaceAsyncRepository ForSpace(Octopus.Client.Model.SpaceResource) + Octopus.Client.IOctopusSystemAsyncRepository ForSystem() } interface IOctopusClient IDisposable @@ -168,6 +170,8 @@ Octopus.Client Octopus.Client.IOctopusCommonRepository Octopus.Client.IOctopusSystemRepository { + Octopus.Client.IOctopusSpaceRepository ForSpace(Octopus.Client.Model.SpaceResource) + Octopus.Client.IOctopusSystemRepository ForSystem() } interface IOctopusSpaceAsyncRepository Octopus.Client.IOctopusCommonAsyncRepository @@ -440,6 +444,8 @@ Octopus.Client Octopus.Client.Repositories.Async.IVariableSetRepository VariableSets { get; } Octopus.Client.Repositories.Async.IWorkerPoolRepository WorkerPools { get; } Octopus.Client.Repositories.Async.IWorkerRepository Workers { get; } + Octopus.Client.IOctopusSpaceAsyncRepository ForSpace(Octopus.Client.Model.SpaceResource) + Octopus.Client.IOctopusSystemAsyncRepository ForSystem() Task HasLink(String) Task HasLinkParameter(String, String) Task Link(String) @@ -580,6 +586,8 @@ Octopus.Client Octopus.Client.Repositories.IVariableSetRepository VariableSets { get; } Octopus.Client.Repositories.IWorkerPoolRepository WorkerPools { get; } Octopus.Client.Repositories.IWorkerRepository Workers { get; } + Octopus.Client.IOctopusSpaceRepository ForSpace(Octopus.Client.Model.SpaceResource) + Octopus.Client.IOctopusSystemRepository ForSystem() Boolean HasLink(String) Boolean HasLinkParameter(String, String) String Link(String) @@ -589,10 +597,6 @@ Octopus.Client abstract class OctopusRepositoryExtensions { static Octopus.Client.IOctopusAsyncRepository CreateRepository(Octopus.Client.IOctopusAsyncClient, Octopus.Client.RepositoryScope) - static Octopus.Client.IOctopusSpaceAsyncRepository ForSpace(Octopus.Client.IOctopusAsyncRepository, Octopus.Client.Model.SpaceResource) - static Octopus.Client.IOctopusSpaceRepository ForSpace(Octopus.Client.IOctopusRepository, Octopus.Client.Model.SpaceResource) - static Octopus.Client.IOctopusSystemAsyncRepository ForSystem(Octopus.Client.IOctopusAsyncRepository) - static Octopus.Client.IOctopusSystemRepository ForSystem(Octopus.Client.IOctopusRepository) } class OctopusRequest { diff --git a/source/Octopus.Server.Client/IOctopusAsyncRepository.cs b/source/Octopus.Server.Client/IOctopusAsyncRepository.cs index 2b50cfbf3..a5a49f717 100644 --- a/source/Octopus.Server.Client/IOctopusAsyncRepository.cs +++ b/source/Octopus.Server.Client/IOctopusAsyncRepository.cs @@ -1,5 +1,4 @@ -using System.Threading.Tasks; -using Octopus.Client.Repositories.Async; +using Octopus.Client.Model; namespace Octopus.Client { @@ -10,5 +9,7 @@ namespace Octopus.Client /// public interface IOctopusAsyncRepository: IOctopusSpaceAsyncRepository, IOctopusSystemAsyncRepository { + IOctopusSpaceAsyncRepository ForSpace(SpaceResource space); + IOctopusSystemAsyncRepository ForSystem(); } } \ No newline at end of file diff --git a/source/Octopus.Server.Client/IOctopusRepository.cs b/source/Octopus.Server.Client/IOctopusRepository.cs index 3300b0069..bc0b9c33d 100644 --- a/source/Octopus.Server.Client/IOctopusRepository.cs +++ b/source/Octopus.Server.Client/IOctopusRepository.cs @@ -1,3 +1,5 @@ +using Octopus.Client.Model; + namespace Octopus.Client { /// @@ -7,5 +9,7 @@ namespace Octopus.Client /// public interface IOctopusRepository: IOctopusSpaceRepository, IOctopusSystemRepository { + IOctopusSpaceRepository ForSpace(SpaceResource space); + IOctopusSystemRepository ForSystem(); } } \ No newline at end of file diff --git a/source/Octopus.Server.Client/OctopusAsyncRepository.cs b/source/Octopus.Server.Client/OctopusAsyncRepository.cs index efc77dafe..c30bc1eb9 100644 --- a/source/Octopus.Server.Client/OctopusAsyncRepository.cs +++ b/source/Octopus.Server.Client/OctopusAsyncRepository.cs @@ -11,22 +11,12 @@ namespace Octopus.Client { - public static partial class OctopusRepositoryExtensions + public static class OctopusRepositoryExtensions { public static IOctopusAsyncRepository CreateRepository(this IOctopusAsyncClient client, RepositoryScope scope = null) { return new OctopusAsyncRepository(client, scope); } - - public static IOctopusSpaceAsyncRepository ForSpace(this IOctopusAsyncRepository repo, SpaceResource space) - { - return repo.Client.ForSpace(space); - } - - public static IOctopusSystemAsyncRepository ForSystem(this IOctopusAsyncRepository repo) - { - return repo.Client.ForSystem(); - } } /// @@ -50,7 +40,7 @@ public class OctopusAsyncRepository : IOctopusAsyncRepository private readonly Lazy> loadSpaceRootResource; private static readonly string rootDocumentUri = "~/api"; - public OctopusAsyncRepository(IOctopusAsyncClient client, RepositoryScope repositoryScope = null) + private OctopusAsyncRepository(IOctopusAsyncClient client, RepositoryScope repositoryScope, RootResource rootResource) { #if FULL_FRAMEWORK LocationChecker.CheckAssemblyLocation(); @@ -120,11 +110,27 @@ public OctopusAsyncRepository(IOctopusAsyncClient client, RepositoryScope reposi UserPermissions = new UserPermissionsRepository(this); UserTeams = new UserTeamsRepository(this); UpgradeConfiguration = new UpgradeConfigurationRepository(this); - loadRootResource = new AsyncLazy(LoadRootDocumentInner); - loadSpaceRootResource = new Lazy>(LoadSpaceRootDocumentInner, true); DeploymentFreezes = new DeploymentFreezeRepository(client); + + loadRootResource = rootResource is not null + ? new AsyncLazy(_ => Task.FromResult(rootResource)) + : new AsyncLazy(LoadRootDocumentInner); + + loadSpaceRootResource = new Lazy>(LoadSpaceRootDocumentInner, true); + } + + protected OctopusAsyncRepository(OctopusAsyncRepository source, RepositoryScope repositoryScope) + : this( + source.Client, + repositoryScope, + source.loadRootResource.HasValue ? source.loadRootResource.Value(CancellationToken.None).Result : null) + { } + public OctopusAsyncRepository(IOctopusAsyncClient client, RepositoryScope repositoryScope = null) + : this(client, repositoryScope, null) + { + } public IOctopusAsyncClient Client { get; } public RepositoryScope Scope { get; private set; } @@ -233,6 +239,18 @@ public async Task Link(string name) public Task LoadRootDocument(CancellationToken cancellationToken) => loadRootResource.Value(cancellationToken); public Task LoadSpaceRootDocument() => loadSpaceRootResource.Value; + public virtual IOctopusSpaceAsyncRepository ForSpace(SpaceResource space) + { + if (space is null) + { + throw new ArgumentNullException(nameof(space)); + } + + return new OctopusAsyncRepository(this, RepositoryScope.ForSpace(space)); + } + + public virtual IOctopusSystemAsyncRepository ForSystem() => new OctopusAsyncRepository(this, RepositoryScope.ForSystem()); + private async Task LoadRootDocumentInner(CancellationToken cancellationToken) { var watch = Stopwatch.StartNew(); diff --git a/source/Octopus.Server.Client/OctopusRepository.cs b/source/Octopus.Server.Client/OctopusRepository.cs index 6d011de15..57a4dc8e5 100644 --- a/source/Octopus.Server.Client/OctopusRepository.cs +++ b/source/Octopus.Server.Client/OctopusRepository.cs @@ -9,19 +9,6 @@ namespace Octopus.Client { - public static partial class OctopusRepositoryExtensions - { - public static IOctopusSpaceRepository ForSpace(this IOctopusRepository repo, SpaceResource space) - { - return repo.Client.ForSpace(space); - } - - public static IOctopusSystemRepository ForSystem(this IOctopusRepository repo) - { - return repo.Client.ForSystem(); - } - } - /// /// A simplified interface to commonly-used parts of the API. /// Functionality not exposed by this interface can be accessed @@ -46,7 +33,7 @@ public OctopusRepository(OctopusServerEndpoint endpoint, RepositoryScope reposit { } - public OctopusRepository(IOctopusClient client, RepositoryScope repositoryScope = null) + private OctopusRepository(IOctopusClient client, RepositoryScope repositoryScope, RootResource rootResource) { #if FULL_FRAMEWORK LocationChecker.CheckAssemblyLocation(); @@ -115,9 +102,26 @@ public OctopusRepository(IOctopusClient client, RepositoryScope repositoryScope UserTeams = new UserTeamsRepository(this); UserInvites = new UserInvitesRepository(this); UpgradeConfiguration = new UpgradeConfigurationRepository(this); - loadRootResource = new Lazy(LoadRootDocumentInner, true); - loadSpaceRootResource = new Lazy(LoadSpaceRootDocumentInner, true); DeploymentFreezes = new DeploymentFreezeRepository(client); + + loadRootResource = rootResource is not null + ? new Lazy(() => rootResource) + : new Lazy(LoadRootDocumentInner); + + loadSpaceRootResource = new Lazy(LoadSpaceRootDocumentInner, true); + } + + protected OctopusRepository(OctopusRepository source, RepositoryScope repositoryScope) + : this( + source.Client, + repositoryScope, + source.loadRootResource.IsValueCreated ? source.loadRootResource.Value : null) + { + } + + public OctopusRepository(IOctopusClient client, RepositoryScope repositoryScope = null) + : this(client, repositoryScope, null) + { } public IOctopusClient Client { get; } @@ -215,6 +219,18 @@ public string Link(string name) public RootResource LoadRootDocument() => loadRootResource.Value; public SpaceRootResource LoadSpaceRootDocument() => loadSpaceRootResource.Value; + public virtual IOctopusSpaceRepository ForSpace(SpaceResource space) + { + if (space is null) + { + throw new ArgumentNullException(nameof(space)); + } + + return new OctopusRepository(this, RepositoryScope.ForSpace(space)); + } + + public virtual IOctopusSystemRepository ForSystem() => new OctopusRepository(this, RepositoryScope.ForSystem()); + RootResource LoadRootDocumentInner() { var watch = Stopwatch.StartNew();