cs-codex-dist-tests/ProjectPlugins/CodexPlugin/MarketplaceAccess.cs

83 lines
2.6 KiB
C#
Raw Normal View History

2024-03-20 10:32:16 +00:00
using Logging;
2023-09-20 06:45:55 +00:00
using Utils;
namespace CodexPlugin
{
public interface IMarketplaceAccess
{
2024-03-20 10:11:41 +00:00
string MakeStorageAvailable(StorageAvailability availability);
2024-05-30 09:33:16 +00:00
IStoragePurchaseContract RequestStorage(StoragePurchaseRequest purchase);
2023-09-20 06:45:55 +00:00
}
public class MarketplaceAccess : IMarketplaceAccess
{
private readonly ILog log;
private readonly CodexAccess codexAccess;
public MarketplaceAccess(ILog log, CodexAccess codexAccess)
{
this.log = log;
this.codexAccess = codexAccess;
}
2024-05-30 09:33:16 +00:00
public IStoragePurchaseContract RequestStorage(StoragePurchaseRequest purchase)
2023-09-20 06:45:55 +00:00
{
2024-03-20 10:11:41 +00:00
purchase.Log(log);
2023-11-23 13:10:59 +00:00
2024-03-26 11:23:38 +00:00
var response = codexAccess.RequestStorage(purchase);
2023-09-20 06:45:55 +00:00
2024-03-26 11:23:38 +00:00
if (string.IsNullOrEmpty(response) ||
response == "Purchasing not available" ||
2023-11-23 13:10:59 +00:00
response == "Expiry required" ||
response == "Expiry needs to be in future" ||
response == "Expiry has to be before the request's end (now + duration)")
2023-09-20 06:45:55 +00:00
{
throw new InvalidOperationException(response);
}
Log($"Storage requested successfully. PurchaseId: '{response}'.");
var contract = new StoragePurchaseContract(log, codexAccess, response, purchase);
contract.WaitForStorageContractSubmitted();
return contract;
2023-09-20 06:45:55 +00:00
}
2024-03-20 10:11:41 +00:00
public string MakeStorageAvailable(StorageAvailability availability)
2023-09-20 06:45:55 +00:00
{
2024-03-20 10:11:41 +00:00
availability.Log(log);
2023-09-20 06:45:55 +00:00
2024-03-26 11:23:38 +00:00
var response = codexAccess.SalesAvailability(availability);
2023-09-20 06:45:55 +00:00
2024-03-26 11:23:38 +00:00
Log($"Storage successfully made available. Id: {response.Id}");
2023-09-20 06:45:55 +00:00
2024-03-26 11:23:38 +00:00
return response.Id;
2023-09-20 06:45:55 +00:00
}
private void Log(string msg)
{
log.Log($"{codexAccess.Container.Containers.Single().Name} {msg}");
2023-09-20 06:45:55 +00:00
}
}
public class MarketplaceUnavailable : IMarketplaceAccess
{
2024-03-20 10:11:41 +00:00
public string MakeStorageAvailable(StorageAvailability availability)
2023-11-23 13:10:59 +00:00
{
Unavailable();
throw new NotImplementedException();
}
2024-05-30 09:33:16 +00:00
public IStoragePurchaseContract RequestStorage(StoragePurchaseRequest purchase)
2023-09-20 06:45:55 +00:00
{
Unavailable();
2024-03-20 10:11:41 +00:00
throw new NotImplementedException();
2023-09-20 06:45:55 +00:00
}
private void Unavailable()
{
2023-09-20 08:51:47 +00:00
FrameworkAssert.Fail("Incorrect test setup: Marketplace was not enabled for this group of Codex nodes. Add 'EnableMarketplace(...)' after 'SetupCodexNodes()' to enable it.");
2023-09-20 06:45:55 +00:00
throw new InvalidOperationException();
}
}
}