From bce9a2c124265b804dd9c3418c211d60adb913b3 Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 26 Mar 2024 14:07:06 +0100 Subject: [PATCH] Lines up new openAPI.yaml --- Framework/Utils/ByteSize.cs | 4 ++- ProjectPlugins/CodexPlugin/CodexAccess.cs | 6 ++--- ProjectPlugins/CodexPlugin/CodexNode.cs | 4 +-- ProjectPlugins/CodexPlugin/CodexTypes.cs | 17 +++++++++++- ProjectPlugins/CodexPlugin/Mapper.cs | 29 ++++++++++++++++++--- ProjectPlugins/CodexPlugin/openapi.yaml | 4 +++ Tests/CodexTests/BasicTests/ExampleTests.cs | 4 +-- 7 files changed, 55 insertions(+), 13 deletions(-) diff --git a/Framework/Utils/ByteSize.cs b/Framework/Utils/ByteSize.cs index a5204c7..1184607 100644 --- a/Framework/Utils/ByteSize.cs +++ b/Framework/Utils/ByteSize.cs @@ -2,6 +2,9 @@ { public class ByteSize { + public static readonly ByteSize Zero = new ByteSize(0); + public const double DefaultSecondsPerMB = 10.0; + public ByteSize(long sizeInBytes) { if (sizeInBytes < 0) throw new ArgumentException("Cannot create ByteSize object with size less than 0. Was: " + sizeInBytes); @@ -10,7 +13,6 @@ public long SizeInBytes { get; } - public const double DefaultSecondsPerMB = 10.0; public long ToMB() { diff --git a/ProjectPlugins/CodexPlugin/CodexAccess.cs b/ProjectPlugins/CodexPlugin/CodexAccess.cs index 4111af4..3759ae9 100644 --- a/ProjectPlugins/CodexPlugin/CodexAccess.cs +++ b/ProjectPlugins/CodexPlugin/CodexAccess.cs @@ -70,7 +70,7 @@ namespace CodexPlugin return fileResponse.Stream; } - public LocalDataset[] LocalFiles() + public LocalDatasetList LocalFiles() { return Map(OnCodex(api => api.ListDataAsync())); } @@ -85,9 +85,7 @@ namespace CodexPlugin public string RequestStorage(StoragePurchaseRequest request) { var body = Map(request); - throw new Exception("todo"); - var read = ""; /* fix incoming*/ OnCodex(api => api.CreateStorageRequestAsync(request.ContentId.Id, body)); - return read; + return OnCodex(api => api.CreateStorageRequestAsync(request.ContentId.Id, body)); } public StoragePurchase GetPurchaseStatus(string purchaseId) diff --git a/ProjectPlugins/CodexPlugin/CodexNode.cs b/ProjectPlugins/CodexPlugin/CodexNode.cs index ce27632..e1e475c 100644 --- a/ProjectPlugins/CodexPlugin/CodexNode.cs +++ b/ProjectPlugins/CodexPlugin/CodexNode.cs @@ -16,7 +16,7 @@ namespace CodexPlugin DebugPeer GetDebugPeer(string peerId); ContentId UploadFile(TrackedFile file); TrackedFile? DownloadContent(ContentId contentId, string fileLabel = ""); - LocalDataset[] LocalFiles(); + LocalDatasetList LocalFiles(); void ConnectToPeer(ICodexNode node); DebugInfoVersion Version { get; } IMarketplaceAccess Marketplace { get; } @@ -119,7 +119,7 @@ namespace CodexPlugin return file; } - public LocalDataset[] LocalFiles() + public LocalDatasetList LocalFiles() { return CodexAccess.LocalFiles(); } diff --git a/ProjectPlugins/CodexPlugin/CodexTypes.cs b/ProjectPlugins/CodexPlugin/CodexTypes.cs index 813c85c..945251e 100644 --- a/ProjectPlugins/CodexPlugin/CodexTypes.cs +++ b/ProjectPlugins/CodexPlugin/CodexTypes.cs @@ -1,4 +1,5 @@ using Newtonsoft.Json; +using Utils; namespace CodexPlugin { @@ -50,9 +51,23 @@ namespace CodexPlugin public string[] Addresses { get; set; } = Array.Empty(); } + public class LocalDatasetList + { + public LocalDataset[] Content { get; set; } = Array.Empty(); + } + public class LocalDataset { - public ContentId Cid { get; set; } = new ContentId(); + public ContentId Cid { get; set; } = new(); + public Manifest Manifest { get; set; } = new(); + } + + public class Manifest + { + public string RootHash { get; set; } = string.Empty; + public ByteSize OriginalBytes { get; set; } = ByteSize.Zero; + public ByteSize BlockSize { get; set; } = ByteSize.Zero; + public bool Protected { get; set; } } public class SalesRequestStorageRequest diff --git a/ProjectPlugins/CodexPlugin/Mapper.cs b/ProjectPlugins/CodexPlugin/Mapper.cs index 76b2401..014fbc7 100644 --- a/ProjectPlugins/CodexPlugin/Mapper.cs +++ b/ProjectPlugins/CodexPlugin/Mapper.cs @@ -1,6 +1,7 @@ using CodexContractsPlugin; using Newtonsoft.Json.Linq; using System.Numerics; +using Utils; namespace CodexPlugin { @@ -19,10 +20,21 @@ namespace CodexPlugin }; } - public LocalDataset[] Map(ICollection dataList) + public LocalDatasetList Map(CodexOpenApi.DataList dataList) { - throw new Exception("todo"); - return Array.Empty(); + return new LocalDatasetList + { + Content = dataList.Content.Select(Map).ToArray() + }; + } + + public LocalDataset Map(CodexOpenApi.DataItem dataItem) + { + return new LocalDataset + { + Cid = new ContentId(dataItem.Cid), + Manifest = MapManifest(dataItem.Manifest) + }; } public CodexOpenApi.SalesAvailabilityCREATE Map(StorageAvailability availability) @@ -105,6 +117,17 @@ namespace CodexPlugin }; } + private Manifest MapManifest(CodexOpenApi.ManifestItem manifest) + { + return new Manifest + { + BlockSize = new ByteSize(Convert.ToInt64(manifest.BlockSize)), + OriginalBytes = new ByteSize(Convert.ToInt64(manifest.OriginalBytes)), + RootHash = manifest.RootHash, + Protected = manifest.Protected + }; + } + private JArray JArray(IDictionary map, string name) { return (JArray)map[name]; diff --git a/ProjectPlugins/CodexPlugin/openapi.yaml b/ProjectPlugins/CodexPlugin/openapi.yaml index adfc887..cfbc800 100644 --- a/ProjectPlugins/CodexPlugin/openapi.yaml +++ b/ProjectPlugins/CodexPlugin/openapi.yaml @@ -632,6 +632,10 @@ paths: responses: "200": description: Returns the Request ID as decimal string + content: + text/plain: + schema: + type: string "400": description: Invalid or missing Request ID "404": diff --git a/Tests/CodexTests/BasicTests/ExampleTests.cs b/Tests/CodexTests/BasicTests/ExampleTests.cs index 3250668..203e1f0 100644 --- a/Tests/CodexTests/BasicTests/ExampleTests.cs +++ b/Tests/CodexTests/BasicTests/ExampleTests.cs @@ -20,8 +20,8 @@ namespace CodexTests.BasicTests var cid = primary.UploadFile(GenerateTestFile(5.MB())); - var content = primary.LocalFiles(); - CollectionAssert.Contains(content.Select(c => c.Cid), cid); + var localDatasets = primary.LocalFiles(); + CollectionAssert.Contains(localDatasets.Content.Select(c => c.Cid), cid); var log = Ci.DownloadLog(primary);