Skip to content

Commit

Permalink
Merge pull request #26 from richardschneider/core-api
Browse files Browse the repository at this point in the history
Core API
  • Loading branch information
richardschneider authored Jan 12, 2018
2 parents 6fe0bbe + b81bac7 commit 0975ce9
Show file tree
Hide file tree
Showing 33 changed files with 403 additions and 1,278 deletions.
13 changes: 4 additions & 9 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
# gitversion will change the version number
version: x-{build}

# branches to build
for:
-
branches:
branches:
only:
- master

environment:
environment:
git_token:
secure: NeX5NCOUXsCLc1UjTJjqB9F02FZ8Wq0VsxqTXC8kBdyK6zjxjebrf/9Da2sY1Kql
secure: NeX5NCOUXsCLc1UjTJjqB9F02FZ8Wq0VsxqTXC8kBdyK6zjxjebrf/9Da2sY1Kql
snk_secret:
secure: 5QzEIgiDqTIrZruPaIQIvTlNMl5BZ7TGEps7ALyBfHE=
-
branches:
except:
- gh-pages
secure: 5QzEIgiDqTIrZruPaIQIvTlNMl5BZ7TGEps7ALyBfHE=

configuration: Release
os: Visual Studio 2017
Expand Down
34 changes: 21 additions & 13 deletions src/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,13 @@ namespace Ipfs.Api
/// <inheritdoc />
public class Block : IDataBlock
{
byte[] dataBytes;
long? size;

/// <inheritdoc />
public Cid Id { get; set; }

/// <inheritdoc />
public byte[] DataBytes
{
get
{
return dataBytes;
}
set
{
dataBytes = value;
}
}
public byte[] DataBytes { get; set; }

/// <inheritdoc />
public Stream DataStream
Expand All @@ -35,7 +25,25 @@ public Stream DataStream
{
return new MemoryStream(DataBytes, false);
}
}
}

/// <inheritdoc />
public long Size
{
get
{
if (size.HasValue)
{
return size.Value;
}
return DataBytes.Length;
}
set
{
size = value;
}
}

}

}
28 changes: 0 additions & 28 deletions src/ConnectedPeer.cs

This file was deleted.

26 changes: 26 additions & 0 deletions src/CoreApi/BitswapApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Ipfs.CoreApi;
using System.IO;

namespace Ipfs.Api
{

class BitswapApi : IBitswapApi
{
IpfsClient ipfs;

internal BitswapApi(IpfsClient ipfs)
{
this.ipfs = ipfs;
}
}

}
150 changes: 39 additions & 111 deletions src/CoreApi/BlockApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,13 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using Ipfs.CoreApi;
using System.IO;

namespace Ipfs.Api
{

/// <summary>
/// Information about a raw IPFS Block.
/// </summary>
/// <seealso cref="BlockApi.StatAsync"/>
public class BlockInfo
{
/// <summary>
/// The <see cref="Cid"/> of the block.
/// </summary>
/// <value>
/// The unique ID of the block.
/// </value>
public Cid Id { get; set; }

/// <summary>
/// The serialised size (in bytes) of the block.
/// </summary>
public long Size { get; set; }
}

/// <summary>
/// Manages the raw <see cref="Block">IPFS blocks</see>.
/// </summary>
/// <remarks>
/// An IPFS Block is a byte sequence that represents an IPFS Object
/// (i.e. serialized byte buffers). It is useful to talk about them as "blocks" in Bitswap
/// and other things that do not care about what is being stored.
/// <para>
/// It is also possible to store arbitrary stuff using ipfs block put/get as the API
/// does not check for proper IPFS Object formatting.
/// </para>
/// <note>
/// This may be very good or bad, we haven't decided yet 😄
/// </note>
/// <para>
/// This API is accessed via the <see cref="IpfsClient.Block"/> property.
/// </para>
/// </remarks>
/// <seealso href="https://github.com/ipfs/interface-ipfs-core/tree/master/API/block">Block API</seealso>
public class BlockApi
class BlockApi : IBlockApi
{
IpfsClient ipfs;

Expand All @@ -59,16 +22,7 @@ internal BlockApi(IpfsClient ipfs)
this.ipfs = ipfs;
}

/// <summary>
/// Gets a raw <see cref="Block">IPFS block</see>.
/// </summary>
/// <param name="cancel">
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
/// </param>
/// <param name="id">
/// The <see cref="Cid"/> of the block.
/// </param>
public async Task<Block> GetAsync(Cid id, CancellationToken cancel = default(CancellationToken)) // TODO CID support
public async Task<IDataBlock> GetAsync(Cid id, CancellationToken cancel = default(CancellationToken)) // TODO CID support
{
var data = await ipfs.DownloadBytesAsync("block/get", cancel, id);
return new Block
Expand All @@ -78,89 +32,63 @@ internal BlockApi(IpfsClient ipfs)
};
}

/// <summary>
/// Stores a byte array as a raw <see cref="Block">IPFS block</see>.
/// </summary>
/// <param name="data">
/// The byte array to send to the IPFS network.
/// </param>
/// <param name="cancel">
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
/// </param>
public async Task<Block> PutAsync(byte[] data, CancellationToken cancel = default(CancellationToken))
public async Task<Cid> PutAsync(
byte[] data,
string contentType = Cid.DefaultContentType,
string multiHash = MultiHash.DefaultAlgorithmName,
CancellationToken cancel = default(CancellationToken))
{
var json = await ipfs.UploadAsync("block/put", cancel, data);
var options = new List<string>();
if (multiHash != MultiHash.DefaultAlgorithmName || contentType != Cid.DefaultContentType)
{
options.Add($"mhtype={multiHash}");
options.Add($"format={contentType}");
}
var json = await ipfs.UploadAsync("block/put", cancel, data, options.ToArray());
var info = JObject.Parse(json);
return new Block
{
DataBytes = data,
Id = (string)info["Key"]
};
return (string)info["Key"];
}

/// <summary>
/// Stores a raw <see cref="Block">IPFS block</see>.
/// </summary>
/// <param name="block">
/// The <seealso cref="Block"/> to send to the IPFS network.
/// </param>
/// <param name="cancel">
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
/// </param>
public Task<Block> PutAsync(Block block, CancellationToken cancel = default(CancellationToken))
public async Task<Cid> PutAsync(
Stream data,
string contentType = Cid.DefaultContentType,
string multiHash = MultiHash.DefaultAlgorithmName,
CancellationToken cancel = default(CancellationToken))
{
return PutAsync(block.DataBytes, cancel);
var options = new List<string>();
if (multiHash != MultiHash.DefaultAlgorithmName || contentType != Cid.DefaultContentType)
{
options.Add($"mhtype={multiHash}");
options.Add($"format={contentType}");
}
var json = await ipfs.UploadAsync("block/put", cancel, data, options.ToArray());
var info = JObject.Parse(json);
return (string)info["Key"];
}

/// <summary>
/// Information on a raw <see cref="Block">IPFS block</see>.
/// </summary>
/// <param name="id">
/// The <see cref="Cid"/> of the block.
/// </param>
/// <param name="cancel">
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
/// </param>
public async Task<BlockInfo> StatAsync(Cid id, CancellationToken cancel = default(CancellationToken))
public async Task<IDataBlock> StatAsync(Cid id, CancellationToken cancel = default(CancellationToken))
{
var json = await ipfs.DoCommandAsync("block/stat", cancel, id);
var info = JObject.Parse(json);
return new BlockInfo
return new Block
{
Size = (long)info["Size"],
Id = (string)info["Key"]
};
}

/// <summary>
/// Remove a raw <see cref="Block">IPFS block</see>.
/// </summary>
/// <param name="cancel">
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
/// </param>
/// <param name="id">
/// The <see cref="Cid"/> of the block.
/// </param>
/// <param name="ignoreNonexistent">
/// If <b>true</b> do not raise exception when <paramref name="id"/> does not
/// exist. Default value is <b>false</b>.
/// </param>
/// <returns>
/// The awaited Task will return the deleted <paramref name="id"/> or
/// <see cref="string.Empty"/> if the hash does not exist and <paramref name="ignoreNonexistent"/>
/// is <b>true</b>.
/// </returns>
public async Task<string> RemoveAsync(Cid id, bool ignoreNonexistent = false, CancellationToken cancel = default(CancellationToken)) // TODO CID support
public async Task<Cid> RemoveAsync(Cid id, bool ignoreNonexistent = false, CancellationToken cancel = default(CancellationToken)) // TODO CID support
{
var json = await ipfs.DoCommandAsync("block/rm", cancel, id, "force=" + ignoreNonexistent.ToString().ToLowerInvariant());
if (json.Length == 0)
return "";
return null;
var result = JObject.Parse(json);
var error = (string)result["Error"];
if (error != null)
throw new HttpRequestException(error);
return (string)result["Hash"];
}
return (Cid)(string)result["Hash"];
}

}

}
Loading

0 comments on commit 0975ce9

Please sign in to comment.