Skip to content

Commit 0996705

Browse files
Merge pull request #52 from richardschneider/core-api-50-0
Upgrade to core api v0.50.0
2 parents 60e4b2f + d5da246 commit 0996705

File tree

6 files changed

+119
-7
lines changed

6 files changed

+119
-7
lines changed

src/CoreApi/DhtApi.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,33 @@ internal DhtApi(IpfsClient ipfs)
2929
return ipfs.IdAsync(id, cancel);
3030
}
3131

32-
public async Task<IEnumerable<Peer>> FindProvidersAsync(Cid id, int limit = 20, CancellationToken cancel = default(CancellationToken))
33-
{
32+
public async Task<IEnumerable<Peer>> FindProvidersAsync(Cid id, int limit = 20, Action<Peer> providerFound = null, CancellationToken cancel = default(CancellationToken))
33+
{
34+
// TODO: providerFound action
3435
var stream = await ipfs.PostDownloadAsync("dht/findprovs", cancel, id, $"num-providers={limit}");
3536
return ProviderFromStream(stream, limit);
36-
}
37-
37+
}
38+
39+
public Task<byte[]> GetAsync(byte[] key, CancellationToken cancel = default(CancellationToken))
40+
{
41+
throw new NotImplementedException();
42+
}
43+
44+
public Task ProvideAsync(Cid cid, bool advertise = true, CancellationToken cancel = default(CancellationToken))
45+
{
46+
throw new NotImplementedException();
47+
}
48+
49+
public Task PutAsync(byte[] key, out byte[] value, CancellationToken cancel = default(CancellationToken))
50+
{
51+
throw new NotImplementedException();
52+
}
53+
54+
public Task<bool> TryGetAsync(byte[] key, out byte[] value, CancellationToken cancel = default(CancellationToken))
55+
{
56+
throw new NotImplementedException();
57+
}
58+
3859
IEnumerable<Peer> ProviderFromStream(Stream stream, int limit = int.MaxValue)
3960
{
4061
using (var sr = new StreamReader(stream))

src/CoreApi/PubSubApi.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,27 @@ internal PubSubApi(IpfsClient ipfs)
4242
return strings.Select(s => new Peer { Id = (string)s } );
4343
}
4444

45+
public Task PublishAsync(string topic, byte[] message, CancellationToken cancel = default(CancellationToken))
46+
{
47+
var url = new StringBuilder();
48+
url.Append("/api/v0/pubsub/pub");
49+
url.Append("?arg=");
50+
url.Append(System.Net.WebUtility.UrlEncode(topic));
51+
url.Append("&arg=");
52+
var data = Encoding.ASCII.GetString(System.Net.WebUtility.UrlEncodeToBytes(message, 0, message.Length));
53+
url.Append(data);
54+
return ipfs.DoCommandAsync(new Uri(ipfs.ApiUri, url.ToString()), cancel);
55+
}
56+
57+
public Task PublishAsync(string topic, Stream message, CancellationToken cancel = default(CancellationToken))
58+
{
59+
using (MemoryStream ms = new MemoryStream())
60+
{
61+
message.CopyTo(ms);
62+
return PublishAsync(topic, ms.ToArray(), cancel);
63+
}
64+
}
65+
4566
public async Task PublishAsync(string topic, string message, CancellationToken cancel = default(CancellationToken))
4667
{
4768
var _ = await ipfs.DoCommandAsync("pubsub/pub", cancel, topic, "arg=" + message);

src/IpfsClient.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,21 @@ public async Task<string> DoCommandAsync(string command, CancellationToken cance
272272
}
273273
}
274274

275+
internal async Task DoCommandAsync(Uri url, CancellationToken cancel)
276+
{
277+
if (log.IsDebugEnabled)
278+
log.Debug("POST " + url.ToString());
279+
using (var response = await Api().PostAsync(url, null, cancel))
280+
{
281+
await ThrowOnErrorAsync(response);
282+
var body = await response.Content.ReadAsStringAsync();
283+
if (log.IsDebugEnabled)
284+
log.Debug("RSP " + body);
285+
return;
286+
}
287+
}
288+
289+
275290
/// <summary>
276291
/// Perform an <see href="https://ipfs.io/docs/api/">IPFS API command</see> returning
277292
/// a specific <see cref="Type"/>.

src/IpfsHttpClient.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
</PropertyGroup>
2828

2929
<ItemGroup>
30-
<PackageReference Include="Ipfs.Core" Version="0.40.0" />
31-
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
30+
<PackageReference Include="Ipfs.Core" Version="0.50.0" />
31+
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
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'" />
3434
</ItemGroup>

test/CoreApi/DhtApiTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public async Task FindProviders()
2828
{
2929
var ipfs = TestFixture.Ipfs;
3030
var cts = new CancellationTokenSource(TimeSpan.FromMinutes(2));
31-
var providers = await ipfs.Dht.FindProvidersAsync(helloWorldID, 1, cts.Token);
31+
var providers = await ipfs.Dht.FindProvidersAsync(helloWorldID, 1, cancel: cts.Token);
3232
Assert.AreNotEqual(0, providers.Count());
3333
}
3434

test/CoreApi/PubSubApiTest.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using Microsoft.VisualStudio.TestTools.UnitTesting;
33
using Newtonsoft.Json.Linq;
44
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
57
using System.Linq;
68
using System.Text;
79
using System.Threading;
@@ -173,5 +175,58 @@ await ipfs.PubSub.SubscribeAsync(topic, msg =>
173175
await Task.Delay(1000);
174176
Assert.AreEqual(1, messageCount1);
175177
}
178+
179+
[TestMethod]
180+
public async Task Subscribe_BinaryMessage()
181+
{
182+
var messages = new List<IPublishedMessage>();
183+
var expected = new byte[] { 0, 1, 2, 4, (byte)'a', (byte)'b', 0xfe, 0xff };
184+
var ipfs = TestFixture.Ipfs;
185+
var topic = "net-ipfs-http-client-test-" + Guid.NewGuid().ToString();
186+
var cs = new CancellationTokenSource();
187+
try
188+
{
189+
await ipfs.PubSub.SubscribeAsync(topic, msg =>
190+
{
191+
messages.Add(msg);
192+
}, cs.Token);
193+
await ipfs.PubSub.PublishAsync(topic, expected);
194+
195+
await Task.Delay(1000);
196+
Assert.AreEqual(1, messages.Count);
197+
CollectionAssert.AreEqual(expected, messages[0].DataBytes);
198+
}
199+
finally
200+
{
201+
cs.Cancel();
202+
}
203+
}
204+
205+
[TestMethod]
206+
public async Task Subscribe_StreamMessage()
207+
{
208+
var messages = new List<IPublishedMessage>();
209+
var expected = new byte[] { 0, 1, 2, 4, (byte)'a', (byte)'b', 0xfe, 0xff };
210+
var ipfs = TestFixture.Ipfs;
211+
var topic = "net-ipfs-http-client-test-" + Guid.NewGuid().ToString();
212+
var cs = new CancellationTokenSource();
213+
try
214+
{
215+
await ipfs.PubSub.SubscribeAsync(topic, msg =>
216+
{
217+
messages.Add(msg);
218+
}, cs.Token);
219+
var ms = new MemoryStream(expected, false);
220+
await ipfs.PubSub.PublishAsync(topic, ms);
221+
222+
await Task.Delay(1000);
223+
Assert.AreEqual(1, messages.Count);
224+
CollectionAssert.AreEqual(expected, messages[0].DataBytes);
225+
}
226+
finally
227+
{
228+
cs.Cancel();
229+
}
230+
}
176231
}
177232
}

0 commit comments

Comments
 (0)