Skip to content

Commit 27e504c

Browse files
feat(block): remove a block #14
1 parent 7435f98 commit 27e504c

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/CoreApi/BlockApi.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
23
using System;
34
using System.Collections.Generic;
45
using System.Linq;
6+
using System.Net.Http;
57
using System.Text;
68
using System.Threading;
79
using System.Threading.Tasks;
@@ -123,6 +125,36 @@ internal BlockApi(IpfsClient ipfs)
123125
{
124126
return ipfs.DoCommandAsync<BlockInfo>("block/stat", cancel, hash);
125127
}
128+
129+
/// <summary>
130+
/// Remove a raw <see cref="Block">IPFS block</see>.
131+
/// </summary>
132+
/// <param name="cancel">
133+
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
134+
/// </param>
135+
/// <param name="hash">
136+
/// The <see cref="string"/> representation of a base58 encoded <see cref="Ipfs.MultiHash"/>.
137+
/// </param>
138+
/// <param name="ignoreNonexistent">
139+
/// If <b>true</b> do not raise exception when <paramref name="hash"/> does not
140+
/// exist. Default value is <b>false</b>.
141+
/// </param>
142+
/// <returns>
143+
/// The awaited Task will return the deleted <paramref name="hash"/> or
144+
/// <see cref="string.Empty"/> if the hash does not exist and <paramref name="ignoreNonexistent"/>
145+
/// is <b>true</b>.
146+
/// </returns>
147+
public async Task<string> RemoveAsync(string hash, bool ignoreNonexistent = false, CancellationToken cancel = default(CancellationToken)) // TODO CID support
148+
{
149+
var json = await ipfs.DoCommandAsync("block/rm", cancel, hash, "force=" + ignoreNonexistent.ToString().ToLowerInvariant());
150+
if (json.Length == 0)
151+
return "";
152+
var result = JObject.Parse(json);
153+
var error = (string)result["Error"];
154+
if (error != null)
155+
throw new HttpRequestException(error);
156+
return (string)result["Hash"];
157+
}
126158
}
127159

128160
}

test/CoreApi/BlockApiTest.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.VisualStudio.TestTools.UnitTesting;
33
using System;
44
using System.Text;
5+
using System.Threading.Tasks;
56

67
namespace Ipfs.Api
78
{
@@ -46,5 +47,25 @@ public void Stat()
4647
Assert.AreEqual(5, info.Size);
4748
}
4849

50+
[TestMethod]
51+
public async Task Remove()
52+
{
53+
var removed = await ipfs.Block.RemoveAsync(hash);
54+
Assert.AreEqual(hash, removed);
55+
}
56+
57+
[TestMethod]
58+
public void Remove_Unknown()
59+
{
60+
ExceptionAssert.Throws<Exception>(() => { var _ = ipfs.Block.RemoveAsync("QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rFF").Result; });
61+
}
62+
63+
[TestMethod]
64+
public async Task Remove_Unknown_OK()
65+
{
66+
var removed = await ipfs.Block.RemoveAsync("QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rFF", true);
67+
Assert.AreEqual("", removed);
68+
}
69+
4970
}
5071
}

0 commit comments

Comments
 (0)