296 lines
8.5 KiB
C#
Raw Normal View History

2025-01-16 15:16:00 +01:00
using CodexOpenApi;
using Logging;
2024-04-02 14:53:45 +02:00
using Newtonsoft.Json;
using Utils;
2025-01-16 11:31:50 +01:00
using WebUtils;
2023-04-12 13:53:55 +02:00
2025-01-16 11:31:50 +01:00
namespace CodexClient
2023-04-12 13:53:55 +02:00
{
public class CodexAccess
2023-04-12 13:53:55 +02:00
{
private readonly ILog log;
2025-01-16 11:31:50 +01:00
private readonly IHttpFactory httpFactory;
2025-01-15 15:43:50 +01:00
private readonly IProcessControl processControl;
private ICodexInstance instance;
2024-03-26 08:58:16 +01:00
private readonly Mapper mapper = new Mapper();
2023-04-30 10:08:32 +02:00
2025-01-16 13:24:57 +01:00
public CodexAccess(ILog log, IHttpFactory httpFactory, IProcessControl processControl, ICodexInstance instance)
2023-04-12 13:53:55 +02:00
{
2025-01-16 11:31:50 +01:00
this.log = log;
this.httpFactory = httpFactory;
2025-01-15 15:43:50 +01:00
this.processControl = processControl;
this.instance = instance;
}
2025-01-15 15:43:50 +01:00
public void Stop(bool waitTillStopped)
{
2025-01-16 13:24:57 +01:00
processControl.Stop(waitTillStopped);
2025-01-15 15:43:50 +01:00
// Prevents accidental use after stop:
instance = null!;
}
2025-01-16 10:15:02 +01:00
public IDownloadedLog DownloadLog(string additionalName = "")
{
2025-01-29 14:44:19 +01:00
var file = log.CreateSubfile(GetName() + additionalName);
2025-01-31 14:24:55 +01:00
Log($"Downloading logs to '{file.Filename}'");
2025-01-29 14:44:19 +01:00
return processControl.DownloadLog(file);
2025-01-16 10:15:02 +01:00
}
public string GetImageName()
{
return instance.ImageName;
}
public DateTime GetStartUtc()
{
return instance.StartUtc;
}
2023-05-11 12:44:53 +02:00
2024-03-26 08:58:16 +01:00
public DebugInfo GetDebugInfo()
{
2024-03-26 15:12:28 +01:00
return mapper.Map(OnCodex(api => api.GetDebugInfoAsync()));
2023-06-29 16:07:49 +02:00
}
2025-03-03 16:44:04 +01:00
public void SetLogLevel(string logLevel)
{
try
{
OnCodex(async api =>
{
await api.SetDebugLogLevelAsync(logLevel);
return string.Empty;
});
}
catch (Exception exc)
{
log.Error("Failed to set log level: " + exc);
}
}
2024-11-21 14:17:57 +01:00
public string GetSpr()
{
return CrashCheck(() =>
{
var endpoint = GetEndpoint();
var json = endpoint.HttpGetString("spr");
var response = JsonConvert.DeserializeObject<SprResponse>(json);
return response!.Spr;
});
}
private class SprResponse
{
public string Spr { get; set; } = string.Empty;
}
2024-03-26 10:31:49 +01:00
public DebugPeer GetDebugPeer(string peerId)
2023-06-29 16:07:49 +02:00
{
2024-03-26 10:31:49 +01:00
// Cannot use openAPI: debug/peer endpoint is not specified there.
2024-06-14 09:05:56 +02:00
return CrashCheck(() =>
{
2024-06-14 09:05:56 +02:00
var endpoint = GetEndpoint();
var str = endpoint.HttpGetString($"debug/peer/{peerId}");
if (str.ToLowerInvariant() == "unable to find peer!")
2023-06-29 16:07:49 +02:00
{
2024-06-14 09:05:56 +02:00
return new DebugPeer
{
IsPeerFound = false
};
}
2023-06-29 16:07:49 +02:00
2024-06-14 09:05:56 +02:00
var result = endpoint.Deserialize<DebugPeer>(str);
result.IsPeerFound = true;
return result;
});
2023-06-29 16:07:49 +02:00
}
2024-03-26 12:14:02 +01:00
public void ConnectToPeer(string peerId, string[] peerMultiAddresses)
{
OnCodex(api =>
{
Time.Wait(api.ConnectPeerAsync(peerId, peerMultiAddresses));
return Task.FromResult(string.Empty);
});
}
2025-01-16 11:31:50 +01:00
public string UploadFile(UploadInput uploadInput)
2023-06-29 16:07:49 +02:00
{
2025-01-16 11:31:50 +01:00
return OnCodex(api => api.UploadAsync(uploadInput.ContentType, uploadInput.ContentDisposition, uploadInput.FileStream));
2023-06-29 16:07:49 +02:00
}
2025-01-16 11:31:50 +01:00
public Stream DownloadFile(string contentId)
2023-06-29 16:07:49 +02:00
{
2025-01-16 11:31:50 +01:00
var fileResponse = OnCodex(api => api.DownloadNetworkStreamAsync(contentId));
2024-03-26 11:39:59 +01:00
if (fileResponse.StatusCode != 200) throw new Exception("Download failed with StatusCode: " + fileResponse.StatusCode);
return fileResponse.Stream;
2023-11-10 08:20:08 +01:00
}
public LocalDataset DownloadStreamless(ContentId cid)
{
var response = OnCodex(api => api.DownloadNetworkAsync(cid.Id));
return mapper.Map(response);
}
public LocalDataset DownloadManifestOnly(ContentId cid)
{
var response = OnCodex(api => api.DownloadNetworkManifestAsync(cid.Id));
return mapper.Map(response);
}
2024-03-26 14:07:06 +01:00
public LocalDatasetList LocalFiles()
2023-11-10 08:20:08 +01:00
{
return mapper.Map(OnCodex(api => api.ListDataAsync()));
2023-06-29 16:07:49 +02:00
}
2024-03-26 11:39:59 +01:00
public StorageAvailability SalesAvailability(StorageAvailability request)
2023-06-29 16:07:49 +02:00
{
2024-03-26 15:12:28 +01:00
var body = mapper.Map(request);
2024-10-16 14:05:02 +02:00
var read = OnCodex(api => api.OfferStorageAsync(body));
2024-03-26 15:12:28 +01:00
return mapper.Map(read);
2023-06-29 16:07:49 +02:00
}
2024-09-23 10:52:12 +02:00
public StorageAvailability[] GetAvailabilities()
{
2024-10-16 14:05:02 +02:00
var collection = OnCodex(api => api.GetAvailabilitiesAsync());
2024-09-23 10:52:12 +02:00
return mapper.Map(collection);
}
2024-03-26 11:39:59 +01:00
public string RequestStorage(StoragePurchaseRequest request)
2023-06-29 16:07:49 +02:00
{
2024-03-26 15:12:28 +01:00
var body = mapper.Map(request);
2024-10-16 14:05:02 +02:00
return OnCodex(api => api.CreateStorageRequestAsync(request.ContentId.Id, body));
2023-06-29 16:07:49 +02:00
}
public CodexSpace Space()
{
2024-10-16 14:05:02 +02:00
var space = OnCodex(api => api.SpaceAsync());
return mapper.Map(space);
}
2025-03-03 16:27:10 +01:00
public StoragePurchase? GetPurchaseStatus(string purchaseId)
2023-06-29 16:07:49 +02:00
{
2025-04-08 13:07:55 +02:00
var purchase = OnCodex(api => api.GetPurchaseAsync(purchaseId));
return mapper.Map(purchase);
2023-06-29 16:07:49 +02:00
}
public string GetName()
{
return instance.Name;
}
public Address GetDiscoveryEndpoint()
{
return instance.DiscoveryEndpoint;
}
public Address GetApiEndpoint()
{
return instance.ApiEndpoint;
}
public Address GetListenEndpoint()
{
return instance.ListenEndpoint;
}
2025-01-16 13:24:57 +01:00
public bool HasCrashed()
{
return processControl.HasCrashed();
}
2025-01-15 15:43:50 +01:00
public Address? GetMetricsEndpoint()
{
2025-01-16 10:15:02 +01:00
return instance.MetricsEndpoint;
2025-01-15 15:43:50 +01:00
}
public EthAccount? GetEthAccount()
{
2025-01-16 10:15:02 +01:00
return instance.EthAccount;
2025-01-15 15:43:50 +01:00
}
public void DeleteDataDirFolder()
2024-06-13 08:51:52 +02:00
{
2025-01-16 13:24:57 +01:00
processControl.DeleteDataDirFolder();
2024-06-13 08:51:52 +02:00
}
2025-01-16 15:16:00 +01:00
private T OnCodex<T>(Func<CodexApiClient, Task<T>> action)
2023-06-29 16:07:49 +02:00
{
2025-01-16 13:24:57 +01:00
var result = httpFactory.CreateHttp(GetHttpId(), h => CheckContainerCrashed()).OnClient(client => CallCodex(client, action));
return result;
}
2025-01-16 15:16:00 +01:00
private T OnCodex<T>(Func<CodexApiClient, Task<T>> action, Retry retry)
{
2025-01-16 13:24:57 +01:00
var result = httpFactory.CreateHttp(GetHttpId(), h => CheckContainerCrashed()).OnClient(client => CallCodex(client, action), retry);
2024-03-25 15:46:45 +01:00
return result;
}
2025-01-16 15:16:00 +01:00
private T CallCodex<T>(HttpClient client, Func<CodexApiClient, Task<T>> action)
{
var address = GetAddress();
2025-01-16 15:16:00 +01:00
var api = new CodexApiClient(client);
api.BaseUrl = $"{address.Host}:{address.Port}/api/codex/v1";
2024-06-14 09:05:56 +02:00
return CrashCheck(() => Time.Wait(action(api)));
}
private T CrashCheck<T>(Func<T> action)
{
try
{
return action();
}
finally
{
2025-01-16 13:24:57 +01:00
CheckContainerCrashed();
2024-06-14 09:05:56 +02:00
}
}
2024-03-26 10:31:49 +01:00
private IEndpoint GetEndpoint()
{
2025-01-16 11:31:50 +01:00
return httpFactory
2025-01-16 13:24:57 +01:00
.CreateHttp(GetHttpId(), h => CheckContainerCrashed())
.CreateEndpoint(GetAddress(), "/api/codex/v1/", GetName());
2024-03-26 10:31:49 +01:00
}
private Address GetAddress()
{
return instance.ApiEndpoint;
}
2024-07-25 10:10:11 +02:00
private string GetHttpId()
{
return GetAddress().ToString();
}
2025-01-16 13:24:57 +01:00
private void CheckContainerCrashed()
2023-08-15 11:01:18 +02:00
{
2025-01-16 13:24:57 +01:00
if (processControl.HasCrashed()) throw new Exception($"Container {GetName()} has crashed.");
2023-08-15 11:01:18 +02:00
}
private void Throw(Failure failure)
{
throw failure.Exception;
}
private void Log(string msg)
{
2025-01-31 14:24:55 +01:00
log.Log($"({GetName()}) {msg}");
}
}
2024-10-30 08:34:41 +01:00
public class UploadInput
{
public UploadInput(string contentType, string contentDisposition, FileStream fileStream)
{
ContentType = contentType;
ContentDisposition = contentDisposition;
FileStream = fileStream;
}
public string ContentType { get; }
public string ContentDisposition { get; }
public FileStream FileStream { get; }
}
2023-04-12 13:53:55 +02:00
}