Skip to content

Commit f6281d0

Browse files
feat: implement bootstrap API #14
1 parent 581ffaa commit f6281d0

File tree

4 files changed

+148
-1
lines changed

4 files changed

+148
-1
lines changed

src/CoreApi/BootstrapApi.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Net.Http;
7+
using System.Text;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
using Ipfs.CoreApi;
11+
using System.IO;
12+
13+
namespace Ipfs.Api
14+
{
15+
16+
class BootstrapApi : IBootstrapApi
17+
{
18+
IpfsClient ipfs;
19+
20+
internal BootstrapApi(IpfsClient ipfs)
21+
{
22+
this.ipfs = ipfs;
23+
}
24+
25+
public async Task<MultiAddress> AddAsync(MultiAddress address, CancellationToken cancel = default(CancellationToken))
26+
{
27+
var json = await ipfs.DoCommandAsync("bootstrap/add", cancel, address.ToString());
28+
var addrs = (JArray)(JObject.Parse(json)["Peers"]);
29+
var a = addrs.FirstOrDefault();
30+
if (a == null)
31+
return null;
32+
return new MultiAddress((string)a);
33+
}
34+
35+
public async Task<IEnumerable<MultiAddress>> AddDefaultsAsync(CancellationToken cancel = default(CancellationToken))
36+
{
37+
var json = await ipfs.DoCommandAsync("bootstrap/add/default", cancel);
38+
var addrs = (JArray)(JObject.Parse(json)["Peers"]);
39+
return addrs
40+
.Select(a => new MultiAddress((string)a));
41+
}
42+
43+
public async Task<IEnumerable<MultiAddress>> ListAsync(CancellationToken cancel = default(CancellationToken))
44+
{
45+
var json = await ipfs.DoCommandAsync("bootstrap/list", cancel);
46+
var addrs = (JArray)(JObject.Parse(json)["Peers"]);
47+
return addrs
48+
.Select(a => new MultiAddress((string)a));
49+
}
50+
51+
public Task RemoveAllAsync(CancellationToken cancel = default(CancellationToken))
52+
{
53+
return ipfs.DoCommandAsync("bootstrap/rm/all", cancel);
54+
}
55+
56+
public async Task<MultiAddress> RemoveAsync(MultiAddress address, CancellationToken cancel = default(CancellationToken))
57+
{
58+
var json = await ipfs.DoCommandAsync("bootstrap/rm", cancel, address.ToString());
59+
var addrs = (JArray)(JObject.Parse(json)["Peers"]);
60+
var a = addrs.FirstOrDefault();
61+
if (a == null)
62+
return null;
63+
return new MultiAddress((string)a);
64+
}
65+
}
66+
67+
}

src/IpfsApi.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</PropertyGroup>
2828

2929
<ItemGroup>
30-
<PackageReference Include="Ipfs.Core" Version="0.18.3" />
30+
<PackageReference Include="Ipfs.Core" Version="0.18.4" />
3131
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
3232
<PackageReference Include="System.Net.Http" Version="4.3.3" Condition="'$(TargetFramework)' == 'netstandard14'" />
3333
<PackageReference Include="System.Net.Http" Version="4.3.3" Condition="'$(TargetFramework)' == 'net45'" />

src/IpfsClient.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public IpfsClient()
6161
UserAgent = string.Format("net-ipfs/{0}.{1}", version.Major, version.Minor);
6262
TrustedPeers = new TrustedPeerCollection(this);
6363

64+
Bootstrap = new BootstrapApi(this);
6465
Bitswap = new BitswapApi(this);
6566
Block = new BlockApi(this);
6667
Config = new ConfigApi(this);
@@ -115,6 +116,9 @@ public IpfsClient(string host)
115116
/// <inheritdoc />
116117
public IBitswapApi Bitswap { get; private set; }
117118

119+
/// <inheritdoc />
120+
public IBootstrapApi Bootstrap { get; private set; }
121+
118122
/// <inheritdoc />
119123
public IGenericApi Generic { get; private set; }
120124

test/CoreApi/BootstrapTest.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using Ipfs.Api;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using System;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Ipfs.Api
10+
{
11+
12+
[TestClass]
13+
public class BootstapApiTest
14+
{
15+
IpfsClient ipfs = TestFixture.Ipfs;
16+
MultiAddress somewhere = "/ip4/127.0.0.1/tcp/4009/ipfs/QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ";
17+
18+
[TestMethod]
19+
public async Task Add_Remove()
20+
{
21+
var addr = await ipfs.Bootstrap.AddAsync(somewhere);
22+
Assert.IsNotNull(addr);
23+
Assert.AreEqual(somewhere, addr);
24+
var addrs = await ipfs.Bootstrap.ListAsync();
25+
Assert.IsTrue(addrs.Any(a => a == somewhere));
26+
27+
addr = await ipfs.Bootstrap.RemoveAsync(somewhere);
28+
Assert.IsNotNull(addr);
29+
Assert.AreEqual(somewhere, addr);
30+
addrs = await ipfs.Bootstrap.ListAsync();
31+
Assert.IsFalse(addrs.Any(a => a == somewhere));
32+
}
33+
34+
[TestMethod]
35+
public async Task List()
36+
{
37+
var addrs = await ipfs.Bootstrap.ListAsync();
38+
Assert.IsNotNull(addrs);
39+
Assert.AreNotEqual(0, addrs.Count());
40+
}
41+
42+
[TestMethod]
43+
public async Task Remove_All()
44+
{
45+
var original = await ipfs.Bootstrap.ListAsync();
46+
await ipfs.Bootstrap.RemoveAllAsync();
47+
var addrs = await ipfs.Bootstrap.ListAsync();
48+
Assert.AreEqual(0, addrs.Count());
49+
foreach (var addr in original)
50+
{
51+
await ipfs.Bootstrap.AddAsync(addr);
52+
}
53+
}
54+
55+
[TestMethod]
56+
public async Task Add_Defaults()
57+
{
58+
var original = await ipfs.Bootstrap.ListAsync();
59+
await ipfs.Bootstrap.RemoveAllAsync();
60+
try
61+
{
62+
await ipfs.Bootstrap.AddDefaultsAsync();
63+
var addrs = await ipfs.Bootstrap.ListAsync();
64+
Assert.AreNotEqual(0, addrs.Count());
65+
}
66+
finally
67+
{
68+
await ipfs.Bootstrap.RemoveAllAsync();
69+
foreach (var addr in original)
70+
{
71+
await ipfs.Bootstrap.AddAsync(addr);
72+
}
73+
}
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)