Skip to content

Commit

Permalink
Replace OctopusRepositoryExtensions with methods that reuse RootResource
Browse files Browse the repository at this point in the history
  • Loading branch information
rosslovas committed Aug 15, 2024
1 parent d2af450 commit f000765
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<Boolean> HasLink(String)
Task<Boolean> HasLinkParameter(String, String)
Task<String> Link(String)
Expand Down Expand Up @@ -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)
Expand All @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<Boolean> HasLink(String)
Task<Boolean> HasLinkParameter(String, String)
Task<String> Link(String)
Expand Down Expand Up @@ -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)
Expand All @@ -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
{
Expand Down
5 changes: 3 additions & 2 deletions source/Octopus.Server.Client/IOctopusAsyncRepository.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Threading.Tasks;
using Octopus.Client.Repositories.Async;
using Octopus.Client.Model;

namespace Octopus.Client
{
Expand All @@ -10,5 +9,7 @@ namespace Octopus.Client
/// </summary>
public interface IOctopusAsyncRepository: IOctopusSpaceAsyncRepository, IOctopusSystemAsyncRepository
{
IOctopusSpaceAsyncRepository ForSpace(SpaceResource space);
IOctopusSystemAsyncRepository ForSystem();
}
}
4 changes: 4 additions & 0 deletions source/Octopus.Server.Client/IOctopusRepository.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Octopus.Client.Model;

namespace Octopus.Client
{
/// <summary>
Expand All @@ -7,5 +9,7 @@ namespace Octopus.Client
/// </summary>
public interface IOctopusRepository: IOctopusSpaceRepository, IOctopusSystemRepository
{
IOctopusSpaceRepository ForSpace(SpaceResource space);
IOctopusSystemRepository ForSystem();
}
}
46 changes: 32 additions & 14 deletions source/Octopus.Server.Client/OctopusAsyncRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

/// <summary>
Expand All @@ -50,7 +40,7 @@ public class OctopusAsyncRepository : IOctopusAsyncRepository
private readonly Lazy<Task<SpaceRootResource>> 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();
Expand Down Expand Up @@ -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<RootResource>(LoadRootDocumentInner);
loadSpaceRootResource = new Lazy<Task<SpaceRootResource>>(LoadSpaceRootDocumentInner, true);
DeploymentFreezes = new DeploymentFreezeRepository(client);

loadRootResource = rootResource is not null
? new AsyncLazy<RootResource>(_ => Task.FromResult(rootResource))
: new AsyncLazy<RootResource>(LoadRootDocumentInner);

loadSpaceRootResource = new Lazy<Task<SpaceRootResource>>(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; }
Expand Down Expand Up @@ -233,6 +239,18 @@ public async Task<string> Link(string name)
public Task<RootResource> LoadRootDocument(CancellationToken cancellationToken) => loadRootResource.Value(cancellationToken);
public Task<SpaceRootResource> 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<RootResource> LoadRootDocumentInner(CancellationToken cancellationToken)
{
var watch = Stopwatch.StartNew();
Expand Down
48 changes: 32 additions & 16 deletions source/Octopus.Server.Client/OctopusRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

/// <summary>
/// A simplified interface to commonly-used parts of the API.
/// Functionality not exposed by this interface can be accessed
Expand All @@ -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();
Expand Down Expand Up @@ -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<RootResource>(LoadRootDocumentInner, true);
loadSpaceRootResource = new Lazy<SpaceRootResource>(LoadSpaceRootDocumentInner, true);
DeploymentFreezes = new DeploymentFreezeRepository(client);

loadRootResource = rootResource is not null
? new Lazy<RootResource>(() => rootResource)
: new Lazy<RootResource>(LoadRootDocumentInner);

loadSpaceRootResource = new Lazy<SpaceRootResource>(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; }
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit f000765

Please sign in to comment.