Skip to content

Commit 1b2e22f

Browse files
fix(KeyApi): implement RenameAsync
1 parent 01e0647 commit 1b2e22f

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

src/CoreApi/BlockApi.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal BlockApi(IpfsClient ipfs)
2222
this.ipfs = ipfs;
2323
}
2424

25-
public async Task<IDataBlock> GetAsync(Cid id, CancellationToken cancel = default(CancellationToken)) // TODO CID support
25+
public async Task<IDataBlock> GetAsync(Cid id, CancellationToken cancel = default(CancellationToken))
2626
{
2727
var data = await ipfs.DownloadBytesAsync("block/get", cancel, id);
2828
return new Block
@@ -101,7 +101,7 @@ public async Task<Cid> PutAsync(
101101
};
102102
}
103103

104-
public async Task<Cid> RemoveAsync(Cid id, bool ignoreNonexistent = false, CancellationToken cancel = default(CancellationToken)) // TODO CID support
104+
public async Task<Cid> RemoveAsync(Cid id, bool ignoreNonexistent = false, CancellationToken cancel = default(CancellationToken))
105105
{
106106
var json = await ipfs.DoCommandAsync("block/rm", cancel, id, "force=" + ignoreNonexistent.ToString().ToLowerInvariant());
107107
if (json.Length == 0)

src/CoreApi/KeyApi.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,15 @@ internal KeyApi(IpfsClient ipfs)
7272
.First();
7373
}
7474

75-
public Task<IKey> RenameAsync(string oldName, string newName, CancellationToken cancel = default(CancellationToken))
75+
public async Task<IKey> RenameAsync(string oldName, string newName, CancellationToken cancel = default(CancellationToken))
7676
{
77-
throw new NotImplementedException();
77+
var json = await ipfs.DoCommandAsync("key/rename", cancel, oldName, $"arg={newName}");
78+
var key = JObject.Parse(json);
79+
return new KeyInfo
80+
{
81+
Id = (string)key["Id"],
82+
Name = (string)key["Now"]
83+
};
7884
}
7985

8086
public Task<string> ExportAsync(string name, char[] password, CancellationToken cancel = default(CancellationToken))

test/CoreApi/KeyApiTest.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,39 @@ public async Task Remove_Key()
7474
Assert.IsFalse(keys.Any(k => k.Name == name));
7575
}
7676

77+
[TestMethod]
78+
public async Task Rename_Key()
79+
{
80+
var oname = "net-api-test-rename1";
81+
var rname = "net-api-test-rename2";
82+
IpfsClient ipfs = TestFixture.Ipfs;
83+
var okey = await ipfs.Key.CreateAsync(oname, "rsa", 1024);
84+
try
85+
{
86+
Assert.AreEqual(oname, okey.Name);
87+
88+
var rkey = await ipfs.Key.RenameAsync(oname, rname);
89+
Assert.AreEqual(okey.Id, rkey.Id);
90+
Assert.AreEqual(rname, rkey.Name);
91+
92+
var keys = await ipfs.Key.ListAsync();
93+
Assert.IsTrue(keys.Any(k => k.Name == rname));
94+
Assert.IsFalse(keys.Any(k => k.Name == oname));
95+
}
96+
finally
97+
{
98+
try
99+
{
100+
await ipfs.Key.RemoveAsync(oname);
101+
}
102+
catch (Exception) { }
103+
try
104+
{
105+
await ipfs.Key.RemoveAsync(rname);
106+
}
107+
catch (Exception) { }
108+
}
109+
}
110+
77111
}
78112
}

0 commit comments

Comments
 (0)