2
0
mirror of synced 2025-01-18 20:41:25 +00:00

84 lines
2.6 KiB
C#
Raw Normal View History

2024-03-20 11:32:16 +01:00
using Logging;
2023-09-20 08:45:55 +02:00
using Utils;
namespace CodexPlugin
{
public interface IMarketplaceAccess
{
2024-03-20 11:11:41 +01:00
string MakeStorageAvailable(StorageAvailability availability);
2024-05-30 11:33:16 +02:00
IStoragePurchaseContract RequestStorage(StoragePurchaseRequest purchase);
2023-09-20 08:45:55 +02: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 11:33:16 +02:00
public IStoragePurchaseContract RequestStorage(StoragePurchaseRequest purchase)
2023-09-20 08:45:55 +02:00
{
2024-03-20 11:11:41 +01:00
purchase.Log(log);
2023-11-23 14:10:59 +01:00
2024-03-26 12:23:38 +01:00
var response = codexAccess.RequestStorage(purchase);
2023-09-20 08:45:55 +02:00
2024-03-26 12:23:38 +01:00
if (string.IsNullOrEmpty(response) ||
2024-07-02 10:08:13 +02:00
response == "Unable to encode manifest" ||
2024-03-26 12:23:38 +01:00
response == "Purchasing not available" ||
2023-11-23 14:10:59 +01: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 08:45:55 +02: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 08:45:55 +02:00
}
2024-03-20 11:11:41 +01:00
public string MakeStorageAvailable(StorageAvailability availability)
2023-09-20 08:45:55 +02:00
{
2024-03-20 11:11:41 +01:00
availability.Log(log);
2023-09-20 08:45:55 +02:00
2024-03-26 12:23:38 +01:00
var response = codexAccess.SalesAvailability(availability);
2023-09-20 08:45:55 +02:00
2024-03-26 12:23:38 +01:00
Log($"Storage successfully made available. Id: {response.Id}");
2023-09-20 08:45:55 +02:00
2024-03-26 12:23:38 +01:00
return response.Id;
2023-09-20 08:45:55 +02:00
}
private void Log(string msg)
{
log.Log($"{codexAccess.Container.Containers.Single().Name} {msg}");
2023-09-20 08:45:55 +02:00
}
}
public class MarketplaceUnavailable : IMarketplaceAccess
{
2024-03-20 11:11:41 +01:00
public string MakeStorageAvailable(StorageAvailability availability)
2023-11-23 14:10:59 +01:00
{
Unavailable();
throw new NotImplementedException();
}
2024-05-30 11:33:16 +02:00
public IStoragePurchaseContract RequestStorage(StoragePurchaseRequest purchase)
2023-09-20 08:45:55 +02:00
{
Unavailable();
2024-03-20 11:11:41 +01:00
throw new NotImplementedException();
2023-09-20 08:45:55 +02:00
}
private void Unavailable()
{
2023-09-20 10:51:47 +02: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 08:45:55 +02:00
throw new InvalidOperationException();
}
}
}