Skip to content

Commit 76ef63e

Browse files
feat(DagApi): implement the interface #30
1 parent 09073eb commit 76ef63e

File tree

3 files changed

+125
-36
lines changed

3 files changed

+125
-36
lines changed

src/CoreApi/DagApi.cs

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
using System.Threading;
1010
using System.Threading.Tasks;
1111
using Ipfs.CoreApi;
12-
12+
using System.Globalization;
13+
1314
namespace Ipfs.Api
1415
{
1516

@@ -23,14 +24,76 @@ internal DagApi(IpfsClient ipfs)
2324
}
2425

2526

26-
public Task<Cid> PutAsync(ILinkedNode data, string contentType, string multiHash = MultiHash.DefaultAlgorithmName, CancellationToken cancel = default(CancellationToken))
27-
{
28-
throw new NotImplementedException();
29-
}
30-
31-
Task<ILinkedNode> IDagApi.GetAsync(string path, CancellationToken cancel)
27+
public async Task<Cid> PutAsync(
28+
JObject data,
29+
string contentType = "cbor",
30+
string multiHash = MultiHash.DefaultAlgorithmName,
31+
bool pin = true,
32+
CancellationToken cancel = default(CancellationToken))
3233
{
33-
throw new NotImplementedException();
34+
using (var ms = new MemoryStream())
35+
{
36+
using (var sw = new StreamWriter(ms, new UTF8Encoding(false), 4096, true) { AutoFlush = true })
37+
using (var jw = new JsonTextWriter(sw))
38+
{
39+
var serializer = new JsonSerializer
40+
{
41+
Culture = CultureInfo.InvariantCulture
42+
};
43+
serializer.Serialize(jw, data);
44+
}
45+
ms.Position = 0;
46+
return await PutAsync(ms, contentType, multiHash, pin, cancel);
47+
}
48+
}
49+
50+
public async Task<Cid> PutAsync(object data, string contentType = "cbor", string multiHash = "sha2-256", bool pin = true, CancellationToken cancel = default(CancellationToken))
51+
{
52+
using (var ms = new MemoryStream(
53+
Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data)),
54+
false))
55+
{
56+
return await PutAsync(ms, contentType, multiHash, pin, cancel);
57+
}
3458
}
59+
60+
public async Task<Cid> PutAsync(
61+
Stream data,
62+
string contentType = "cbor",
63+
string multiHash = "sha2-256",
64+
bool pin = true,
65+
CancellationToken cancel = default(CancellationToken))
66+
{
67+
var json = await ipfs.UploadAsync("dag/put", cancel,
68+
data, null,
69+
$"format={contentType}",
70+
$"pin={pin.ToString().ToLowerInvariant()}",
71+
$"hash={multiHash}");
72+
var result = JObject.Parse(json);
73+
return (Cid)(string)result["Cid"]["/"];
74+
}
75+
76+
public async Task<JObject> GetAsync(
77+
Cid id,
78+
CancellationToken cancel = default(CancellationToken))
79+
{
80+
var json = await ipfs.DoCommandAsync("dag/get", cancel, id);
81+
return JObject.Parse(json);
82+
}
83+
84+
85+
public async Task<JToken> GetAsync(
86+
string path,
87+
CancellationToken cancel = default(CancellationToken))
88+
{
89+
var json = await ipfs.DoCommandAsync("dag/get", cancel, path);
90+
return JToken.Parse(json);
91+
}
92+
93+
public async Task<T> GetAsync<T>(Cid id, CancellationToken cancel = default(CancellationToken))
94+
{
95+
var json = await ipfs.DoCommandAsync("dag/get", cancel, id);
96+
return JsonConvert.DeserializeObject<T>(json);
97+
}
3598
}
3699
}

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.24.0" />
30+
<PackageReference Include="Ipfs.Core" Version="0.25.1" />
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'" />

test/CoreApi/DagApiTest.cs

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,53 @@
1-
using Microsoft.VisualStudio.TestTools.UnitTesting;
2-
using Newtonsoft.Json.Linq;
3-
using System;
4-
using System.Text;
5-
6-
namespace Ipfs.Api
7-
{
8-
[TestClass]
9-
public class DagApiTest
10-
{
11-
[TestMethod]
12-
public void GetAsync()
13-
{
14-
var ipfs = TestFixture.Ipfs;
15-
ExceptionAssert.Throws<NotImplementedException>(() => ipfs.Dag.GetAsync("cid"));
16-
}
17-
18-
[TestMethod]
19-
public void PutAsync()
20-
{
21-
var ipfs = TestFixture.Ipfs;
22-
ExceptionAssert.Throws<NotImplementedException>(() => ipfs.Dag.PutAsync(null, null));
23-
}
24-
25-
}
26-
}
27-
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using Newtonsoft.Json.Linq;
3+
using System;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Ipfs.Api
8+
{
9+
[TestClass]
10+
public class DagApiTest
11+
{
12+
class Name
13+
{
14+
public string First { get; set; }
15+
public string Last { get; set; }
16+
}
17+
18+
[TestMethod]
19+
public async Task PutAndGet_JSON()
20+
{
21+
var ipfs = TestFixture.Ipfs;
22+
var expected = new JObject();
23+
expected["a"] = "alpha";
24+
var id = await ipfs.Dag.PutAsync(expected);
25+
Assert.IsNotNull(id);
26+
27+
var actual = await ipfs.Dag.GetAsync(id);
28+
Assert.IsNotNull(actual);
29+
Assert.AreEqual(expected["a"], actual["a"]);
30+
31+
var value = (string) await ipfs.Dag.GetAsync(id.Encode() + "/a");
32+
Assert.AreEqual(expected["a"], value);
33+
}
34+
35+
[TestMethod]
36+
public async Task PutAndGet_POCO()
37+
{
38+
var ipfs = TestFixture.Ipfs;
39+
var expected = new Name { First = "John", Last = "Smith" };
40+
var id = await ipfs.Dag.PutAsync(expected);
41+
Assert.IsNotNull(id);
42+
43+
var actual = await ipfs.Dag.GetAsync<Name>(id);
44+
Assert.IsNotNull(actual);
45+
Assert.AreEqual(expected.First, actual.First);
46+
Assert.AreEqual(expected.Last, actual.Last);
47+
48+
var value = (string)await ipfs.Dag.GetAsync(id.Encode() + "/Last");
49+
Assert.AreEqual(expected.Last, value);
50+
}
51+
}
52+
}
53+

0 commit comments

Comments
 (0)