Skip to content

Commit 0975ce9

Browse files
Merge pull request #26 from richardschneider/core-api
Core API
2 parents 6fe0bbe + b81bac7 commit 0975ce9

33 files changed

+403
-1278
lines changed

appveyor.yml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
# gitversion will change the version number
22
version: x-{build}
33

4-
# branches to build
54
for:
65
-
7-
branches:
6+
branches:
87
only:
98
- master
109

11-
environment:
10+
environment:
1211
git_token:
13-
secure: NeX5NCOUXsCLc1UjTJjqB9F02FZ8Wq0VsxqTXC8kBdyK6zjxjebrf/9Da2sY1Kql
12+
secure: NeX5NCOUXsCLc1UjTJjqB9F02FZ8Wq0VsxqTXC8kBdyK6zjxjebrf/9Da2sY1Kql
1413
snk_secret:
15-
secure: 5QzEIgiDqTIrZruPaIQIvTlNMl5BZ7TGEps7ALyBfHE=
16-
-
17-
branches:
18-
except:
19-
- gh-pages
14+
secure: 5QzEIgiDqTIrZruPaIQIvTlNMl5BZ7TGEps7ALyBfHE=
2015

2116
configuration: Release
2217
os: Visual Studio 2017

src/Block.cs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,13 @@ namespace Ipfs.Api
1010
/// <inheritdoc />
1111
public class Block : IDataBlock
1212
{
13-
byte[] dataBytes;
13+
long? size;
1414

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

1818
/// <inheritdoc />
19-
public byte[] DataBytes
20-
{
21-
get
22-
{
23-
return dataBytes;
24-
}
25-
set
26-
{
27-
dataBytes = value;
28-
}
29-
}
19+
public byte[] DataBytes { get; set; }
3020

3121
/// <inheritdoc />
3222
public Stream DataStream
@@ -35,7 +25,25 @@ public Stream DataStream
3525
{
3626
return new MemoryStream(DataBytes, false);
3727
}
38-
}
28+
}
29+
30+
/// <inheritdoc />
31+
public long Size
32+
{
33+
get
34+
{
35+
if (size.HasValue)
36+
{
37+
return size.Value;
38+
}
39+
return DataBytes.Length;
40+
}
41+
set
42+
{
43+
size = value;
44+
}
45+
}
46+
3947
}
4048

4149
}

src/ConnectedPeer.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/CoreApi/BitswapApi.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 BitswapApi : IBitswapApi
17+
{
18+
IpfsClient ipfs;
19+
20+
internal BitswapApi(IpfsClient ipfs)
21+
{
22+
this.ipfs = ipfs;
23+
}
24+
}
25+
26+
}

src/CoreApi/BlockApi.cs

Lines changed: 39 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,13 @@
77
using System.Text;
88
using System.Threading;
99
using System.Threading.Tasks;
10-
10+
using Ipfs.CoreApi;
11+
using System.IO;
12+
1113
namespace Ipfs.Api
1214
{
1315

14-
/// <summary>
15-
/// Information about a raw IPFS Block.
16-
/// </summary>
17-
/// <seealso cref="BlockApi.StatAsync"/>
18-
public class BlockInfo
19-
{
20-
/// <summary>
21-
/// The <see cref="Cid"/> of the block.
22-
/// </summary>
23-
/// <value>
24-
/// The unique ID of the block.
25-
/// </value>
26-
public Cid Id { get; set; }
27-
28-
/// <summary>
29-
/// The serialised size (in bytes) of the block.
30-
/// </summary>
31-
public long Size { get; set; }
32-
}
33-
34-
/// <summary>
35-
/// Manages the raw <see cref="Block">IPFS blocks</see>.
36-
/// </summary>
37-
/// <remarks>
38-
/// An IPFS Block is a byte sequence that represents an IPFS Object
39-
/// (i.e. serialized byte buffers). It is useful to talk about them as "blocks" in Bitswap
40-
/// and other things that do not care about what is being stored.
41-
/// <para>
42-
/// It is also possible to store arbitrary stuff using ipfs block put/get as the API
43-
/// does not check for proper IPFS Object formatting.
44-
/// </para>
45-
/// <note>
46-
/// This may be very good or bad, we haven't decided yet 😄
47-
/// </note>
48-
/// <para>
49-
/// This API is accessed via the <see cref="IpfsClient.Block"/> property.
50-
/// </para>
51-
/// </remarks>
52-
/// <seealso href="https://github.com/ipfs/interface-ipfs-core/tree/master/API/block">Block API</seealso>
53-
public class BlockApi
16+
class BlockApi : IBlockApi
5417
{
5518
IpfsClient ipfs;
5619

@@ -59,16 +22,7 @@ internal BlockApi(IpfsClient ipfs)
5922
this.ipfs = ipfs;
6023
}
6124

62-
/// <summary>
63-
/// Gets a raw <see cref="Block">IPFS block</see>.
64-
/// </summary>
65-
/// <param name="cancel">
66-
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
67-
/// </param>
68-
/// <param name="id">
69-
/// The <see cref="Cid"/> of the block.
70-
/// </param>
71-
public async Task<Block> GetAsync(Cid id, CancellationToken cancel = default(CancellationToken)) // TODO CID support
25+
public async Task<IDataBlock> GetAsync(Cid id, CancellationToken cancel = default(CancellationToken)) // TODO CID support
7226
{
7327
var data = await ipfs.DownloadBytesAsync("block/get", cancel, id);
7428
return new Block
@@ -78,89 +32,63 @@ internal BlockApi(IpfsClient ipfs)
7832
};
7933
}
8034

81-
/// <summary>
82-
/// Stores a byte array as a raw <see cref="Block">IPFS block</see>.
83-
/// </summary>
84-
/// <param name="data">
85-
/// The byte array to send to the IPFS network.
86-
/// </param>
87-
/// <param name="cancel">
88-
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
89-
/// </param>
90-
public async Task<Block> PutAsync(byte[] data, CancellationToken cancel = default(CancellationToken))
35+
public async Task<Cid> PutAsync(
36+
byte[] data,
37+
string contentType = Cid.DefaultContentType,
38+
string multiHash = MultiHash.DefaultAlgorithmName,
39+
CancellationToken cancel = default(CancellationToken))
9140
{
92-
var json = await ipfs.UploadAsync("block/put", cancel, data);
41+
var options = new List<string>();
42+
if (multiHash != MultiHash.DefaultAlgorithmName || contentType != Cid.DefaultContentType)
43+
{
44+
options.Add($"mhtype={multiHash}");
45+
options.Add($"format={contentType}");
46+
}
47+
var json = await ipfs.UploadAsync("block/put", cancel, data, options.ToArray());
9348
var info = JObject.Parse(json);
94-
return new Block
95-
{
96-
DataBytes = data,
97-
Id = (string)info["Key"]
98-
};
49+
return (string)info["Key"];
9950
}
10051

101-
/// <summary>
102-
/// Stores a raw <see cref="Block">IPFS block</see>.
103-
/// </summary>
104-
/// <param name="block">
105-
/// The <seealso cref="Block"/> to send to the IPFS network.
106-
/// </param>
107-
/// <param name="cancel">
108-
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
109-
/// </param>
110-
public Task<Block> PutAsync(Block block, CancellationToken cancel = default(CancellationToken))
52+
public async Task<Cid> PutAsync(
53+
Stream data,
54+
string contentType = Cid.DefaultContentType,
55+
string multiHash = MultiHash.DefaultAlgorithmName,
56+
CancellationToken cancel = default(CancellationToken))
11157
{
112-
return PutAsync(block.DataBytes, cancel);
58+
var options = new List<string>();
59+
if (multiHash != MultiHash.DefaultAlgorithmName || contentType != Cid.DefaultContentType)
60+
{
61+
options.Add($"mhtype={multiHash}");
62+
options.Add($"format={contentType}");
63+
}
64+
var json = await ipfs.UploadAsync("block/put", cancel, data, options.ToArray());
65+
var info = JObject.Parse(json);
66+
return (string)info["Key"];
11367
}
11468

115-
/// <summary>
116-
/// Information on a raw <see cref="Block">IPFS block</see>.
117-
/// </summary>
118-
/// <param name="id">
119-
/// The <see cref="Cid"/> of the block.
120-
/// </param>
121-
/// <param name="cancel">
122-
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
123-
/// </param>
124-
public async Task<BlockInfo> StatAsync(Cid id, CancellationToken cancel = default(CancellationToken))
69+
public async Task<IDataBlock> StatAsync(Cid id, CancellationToken cancel = default(CancellationToken))
12570
{
12671
var json = await ipfs.DoCommandAsync("block/stat", cancel, id);
12772
var info = JObject.Parse(json);
128-
return new BlockInfo
73+
return new Block
12974
{
13075
Size = (long)info["Size"],
13176
Id = (string)info["Key"]
13277
};
13378
}
13479

135-
/// <summary>
136-
/// Remove a raw <see cref="Block">IPFS block</see>.
137-
/// </summary>
138-
/// <param name="cancel">
139-
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
140-
/// </param>
141-
/// <param name="id">
142-
/// The <see cref="Cid"/> of the block.
143-
/// </param>
144-
/// <param name="ignoreNonexistent">
145-
/// If <b>true</b> do not raise exception when <paramref name="id"/> does not
146-
/// exist. Default value is <b>false</b>.
147-
/// </param>
148-
/// <returns>
149-
/// The awaited Task will return the deleted <paramref name="id"/> or
150-
/// <see cref="string.Empty"/> if the hash does not exist and <paramref name="ignoreNonexistent"/>
151-
/// is <b>true</b>.
152-
/// </returns>
153-
public async Task<string> RemoveAsync(Cid id, bool ignoreNonexistent = false, CancellationToken cancel = default(CancellationToken)) // TODO CID support
80+
public async Task<Cid> RemoveAsync(Cid id, bool ignoreNonexistent = false, CancellationToken cancel = default(CancellationToken)) // TODO CID support
15481
{
15582
var json = await ipfs.DoCommandAsync("block/rm", cancel, id, "force=" + ignoreNonexistent.ToString().ToLowerInvariant());
15683
if (json.Length == 0)
157-
return "";
84+
return null;
15885
var result = JObject.Parse(json);
15986
var error = (string)result["Error"];
16087
if (error != null)
16188
throw new HttpRequestException(error);
162-
return (string)result["Hash"];
163-
}
89+
return (Cid)(string)result["Hash"];
90+
}
91+
16492
}
16593

16694
}

0 commit comments

Comments
 (0)