Skip to content

Commit

Permalink
feat(DagApi): implement the interface #30
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed May 13, 2018
1 parent 09073eb commit 76ef63e
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 36 deletions.
79 changes: 71 additions & 8 deletions src/CoreApi/DagApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
using System.Threading;
using System.Threading.Tasks;
using Ipfs.CoreApi;

using System.Globalization;

namespace Ipfs.Api
{

Expand All @@ -23,14 +24,76 @@ internal DagApi(IpfsClient ipfs)
}


public Task<Cid> PutAsync(ILinkedNode data, string contentType, string multiHash = MultiHash.DefaultAlgorithmName, CancellationToken cancel = default(CancellationToken))
{
throw new NotImplementedException();
}

Task<ILinkedNode> IDagApi.GetAsync(string path, CancellationToken cancel)
public async Task<Cid> PutAsync(
JObject data,
string contentType = "cbor",
string multiHash = MultiHash.DefaultAlgorithmName,
bool pin = true,
CancellationToken cancel = default(CancellationToken))
{
throw new NotImplementedException();
using (var ms = new MemoryStream())
{
using (var sw = new StreamWriter(ms, new UTF8Encoding(false), 4096, true) { AutoFlush = true })
using (var jw = new JsonTextWriter(sw))
{
var serializer = new JsonSerializer
{
Culture = CultureInfo.InvariantCulture
};
serializer.Serialize(jw, data);
}
ms.Position = 0;
return await PutAsync(ms, contentType, multiHash, pin, cancel);
}
}

public async Task<Cid> PutAsync(object data, string contentType = "cbor", string multiHash = "sha2-256", bool pin = true, CancellationToken cancel = default(CancellationToken))
{
using (var ms = new MemoryStream(
Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data)),
false))
{
return await PutAsync(ms, contentType, multiHash, pin, cancel);
}
}

public async Task<Cid> PutAsync(
Stream data,
string contentType = "cbor",
string multiHash = "sha2-256",
bool pin = true,
CancellationToken cancel = default(CancellationToken))
{
var json = await ipfs.UploadAsync("dag/put", cancel,
data, null,
$"format={contentType}",
$"pin={pin.ToString().ToLowerInvariant()}",
$"hash={multiHash}");
var result = JObject.Parse(json);
return (Cid)(string)result["Cid"]["/"];
}

public async Task<JObject> GetAsync(
Cid id,
CancellationToken cancel = default(CancellationToken))
{
var json = await ipfs.DoCommandAsync("dag/get", cancel, id);
return JObject.Parse(json);
}


public async Task<JToken> GetAsync(
string path,
CancellationToken cancel = default(CancellationToken))
{
var json = await ipfs.DoCommandAsync("dag/get", cancel, path);
return JToken.Parse(json);
}

public async Task<T> GetAsync<T>(Cid id, CancellationToken cancel = default(CancellationToken))
{
var json = await ipfs.DoCommandAsync("dag/get", cancel, id);
return JsonConvert.DeserializeObject<T>(json);
}
}
}
2 changes: 1 addition & 1 deletion src/IpfsApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Ipfs.Core" Version="0.24.0" />
<PackageReference Include="Ipfs.Core" Version="0.25.1" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="System.Net.Http" Version="4.3.3" Condition="'$(TargetFramework)' == 'netstandard14'" />
<PackageReference Include="System.Net.Http" Version="4.3.3" Condition="'$(TargetFramework)' == 'net45'" />
Expand Down
80 changes: 53 additions & 27 deletions test/CoreApi/DagApiTest.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,53 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
using System;
using System.Text;

namespace Ipfs.Api
{
[TestClass]
public class DagApiTest
{
[TestMethod]
public void GetAsync()
{
var ipfs = TestFixture.Ipfs;
ExceptionAssert.Throws<NotImplementedException>(() => ipfs.Dag.GetAsync("cid"));
}

[TestMethod]
public void PutAsync()
{
var ipfs = TestFixture.Ipfs;
ExceptionAssert.Throws<NotImplementedException>(() => ipfs.Dag.PutAsync(null, null));
}

}
}

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
using System;
using System.Text;
using System.Threading.Tasks;

namespace Ipfs.Api
{
[TestClass]
public class DagApiTest
{
class Name
{
public string First { get; set; }
public string Last { get; set; }
}

[TestMethod]
public async Task PutAndGet_JSON()
{
var ipfs = TestFixture.Ipfs;
var expected = new JObject();
expected["a"] = "alpha";
var id = await ipfs.Dag.PutAsync(expected);
Assert.IsNotNull(id);

var actual = await ipfs.Dag.GetAsync(id);
Assert.IsNotNull(actual);
Assert.AreEqual(expected["a"], actual["a"]);

var value = (string) await ipfs.Dag.GetAsync(id.Encode() + "/a");
Assert.AreEqual(expected["a"], value);
}

[TestMethod]
public async Task PutAndGet_POCO()
{
var ipfs = TestFixture.Ipfs;
var expected = new Name { First = "John", Last = "Smith" };
var id = await ipfs.Dag.PutAsync(expected);
Assert.IsNotNull(id);

var actual = await ipfs.Dag.GetAsync<Name>(id);
Assert.IsNotNull(actual);
Assert.AreEqual(expected.First, actual.First);
Assert.AreEqual(expected.Last, actual.Last);

var value = (string)await ipfs.Dag.GetAsync(id.Encode() + "/Last");
Assert.AreEqual(expected.Last, value);
}
}
}

0 comments on commit 76ef63e

Please sign in to comment.