Skip to content

Commit 1a19259

Browse files
Merge pull request #46 from richardschneider/basic-articles
docs: improve articles
2 parents 364b5f1 + 0e36ab2 commit 1a19259

File tree

10 files changed

+305
-61
lines changed

10 files changed

+305
-61
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![build status](https://ci.appveyor.com/api/projects/status/github/richardschneider/net-ipfs-api?branch=master&svg=true)](https://ci.appveyor.com/project/richardschneider/net-ipfs-api)
44
[![Coverage Status](https://coveralls.io/repos/github/richardschneider/net-ipfs-api/badge.svg?branch=master)](https://coveralls.io/github/richardschneider/net-ipfs-api?branch=master)
55
[![Version](https://img.shields.io/nuget/v/Ipfs.Api.svg)](https://www.nuget.org/packages/Ipfs.Api)
6-
[![docs](https://cdn.rawgit.com/richardschneider/net-ipfs-api/master/doc/images/docs-latest-green.svg)](https://richardschneider.github.io/net-ipfs-api)
6+
[![docs](https://cdn.rawgit.com/richardschneider/net-ipfs-api/master/doc/images/docs-latest-green.svg)](https://richardschneider.github.io/net-ipfs-api/articles/client.html)
77

88

99
A .Net client library for the IPFS HTTP API, implemented in C#.
@@ -17,7 +17,7 @@ More information, including the Class Reference, is on the [Project](https://ric
1717
- [Asynchronous I/O](https://richardschneider.github.io/net-ipfs-api/articles/async.html) to an IPFS server
1818
- Supports [cancellation](https://richardschneider.github.io/net-ipfs-api/articles/cancellation.html) of all requests to the IPFS Server
1919
- Requests that all responses are compressed
20-
- Comprehensive [documentation](https://richardschneider.github.io/net-ipfs-api)
20+
- Comprehensive [documentation](https://richardschneider.github.io/net-ipfs-api/articles/client.html)
2121
- C# style access to the [ipfs core interface](https://richardschneider.github.io/net-ipfs-core/api/Ipfs.CoreApi.html)
2222
- [Bitswap API](https://richardschneider.github.io/net-ipfs-core/api/Ipfs.CoreApi.IBitswapApi.html)
2323
- [Block API](https://richardschneider.github.io/net-ipfs-core/api/Ipfs.CoreApi.IBlockApi.html)
@@ -45,7 +45,7 @@ For the latest build or older non-released builds see [Continuous Integration](h
4545

4646
Every IPFS Api is a property of the [IpfsClient](https://richardschneider.github.io/net-ipfs-api/api/Ipfs.Api.IpfsClient.html). The following example reads a text file
4747

48-
```
48+
```csharp
4949
var ipfs = new IpfsClient();
5050

5151
const string filename = "QmXarR6rgkQ2fDSHjSY5nM2kuCXKYGViky5nohtwgF65Ec/about";

doc/Documentation.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@
8888
<Content Include="template\fonts\Lato-Regular.ttf" />
8989
<Content Include="template\fonts\Lato-Regular.woff" />
9090
<Content Include="template\fonts\Lato-Regular.woff2" />
91-
<Content Include="articles\cancellation.md" />
9291
<Content Include="articles\async.md" />
9392
<Content Include="articles\envvars.md" />
93+
<Content Include="articles\client.md" />
94+
<Content Include="articles\daemon.md" />
95+
<Content Include="articles\filesystem.md" />
9496
<None Include="Web.Debug.config">
9597
<DependentUpon>Web.config</DependentUpon>
9698
</None>

doc/api/.manifest

Lines changed: 96 additions & 1 deletion
Large diffs are not rendered by default.

doc/articles/async.md

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,45 @@
1-
# Asynchronous I/O
2-
3-
All requests to the IPFS server are [asynchronous](https://docs.microsoft.com/en-us/dotnet/csharp/async),
4-
which does not block current thread.
5-
6-
This means that callers should **normally** use the `async/await` paradigm
7-
8-
```cs
9-
async Task<string> AddText()
10-
{
11-
var result = await ipfs.FileSystem.AddTextAsync("I am pinned");
12-
return result.Hash;
13-
}
14-
```
15-
16-
If a synchronous operation is required, then this can work
17-
18-
```cs
19-
string AddText()
20-
{
21-
var result = ipfs.FileSystem.AddTextAsync("I am pinned").Result;
22-
return result.Hash;
23-
}
24-
```
1+
# Asynchronous I/O
2+
3+
All requests to the IPFS server are [asynchronous](https://docs.microsoft.com/en-us/dotnet/csharp/async),
4+
which does not block current thread.
5+
6+
This means that callers should **normally** use the `async/await` paradigm
7+
8+
```cs
9+
var result = await ipfs.FileSystem.AddTextAsync("I am pinned");
10+
```
11+
12+
## Synchronous
13+
14+
If a synchronous operation is required, then this can work
15+
16+
```cs
17+
var result = ipfs.FileSystem.AddTextAsync("I am pinned").Result;
18+
```
19+
20+
## Cancelling a request
21+
22+
All requests to the IPFS server can be cancelled by supplying
23+
an optional [CancellationToken](xref:System.Threading.CancellationToken). When
24+
the token is cancelled,
25+
a [TaskCanceledException](xref:System.Threading.Tasks.TaskCanceledException)
26+
will be `thrown`.
27+
28+
Here's a contrived example ([unit test](https://github.com/richardschneider/net-ipfs-api/blob/cancellation/test/CoreApi/CancellationTest.cs))
29+
that forces the getting of info on the local IPFS server to be cancelled
30+
31+
```csharp
32+
var cts = new CancellationTokenSource(500);
33+
try
34+
{
35+
await Task.Delay(1000);
36+
var peer = await ipfs.IdAsync(cts.Token);
37+
Assert.Fail("Did not throw TaskCanceledException");
38+
}
39+
catch (TaskCanceledException)
40+
{
41+
return;
42+
}
43+
```
44+
45+
See also [Task Cancellation](https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/task-cancellation)

doc/articles/cancellation.md

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

doc/articles/client.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Accessing IPFS
2+
3+
IPFS is a distributed peer to peer system. There is no central server! Typically, each machine (peer) runs
4+
a [daemon](daemon.md) that communicates with other peers.
5+
6+
The [IpfsClient](xref:Ipfs.Api.IpfsClient) provides a simple way for your program to access the daemon
7+
via the [IPFS HTTP API](https://docs.ipfs.io/reference/api/http/) protocol. The client
8+
should be used as a shared object in your program, much like [HttpClient](xref:System.Net.Http.HttpClient). It is
9+
thread safe (re-entrant) and conserves sockets and TCP connections when only one instance is used.
10+
11+
```csharp
12+
public class Program
13+
{
14+
static readonly IpfsClient ipfs = new IpfsClient();
15+
public async Task Main(string[] args)
16+
{
17+
// Get the Peer info of the daemon
18+
var peer = await ipfs.IdAsync();
19+
}
20+
}
21+
```
22+
23+
## Core API
24+
25+
The [Core API](xref:Ipfs.CoreApi.ICoreApi) is a set of interfaces to IPFS features and is implemented by the client. The
26+
[FileSystem](filesystem.md) and [PubSub]() features are most often used.
27+
28+
```csharp
29+
const string filename = "QmXarR6rgkQ2fDSHjSY5nM2kuCXKYGViky5nohtwgF65Ec/about";
30+
string text = await ipfs.FileSystem.ReadAllTextAsync(filename);
31+
```
32+
33+
### Features
34+
35+
| Feature | Purpose |
36+
| ------- | ------- |
37+
| [Bitswap](xref:Ipfs.CoreApi.IBitswapApi) | Data trading module for IPFS; requests blocks from and sends blocks to other peers |
38+
| [Block](xref:Ipfs.CoreApi.IBlockApi) | Manages the blocks |
39+
| [Bootstrap](xref:Ipfs.CoreApi.IBootstrapApi) | Trusted peers |
40+
| [Config](xref:Ipfs.CoreApi.IConfigApi) | Manages the configuration of the local peer |
41+
| [Dag](xref:Ipfs.CoreApi.IDagApi) | Manages the IPLD (linked data) Directed Acrylic Graph |
42+
| [Dht](xref:Ipfs.CoreApi.IDhtApi) | Manages the Distributed Hash Table |
43+
| [Dns](xref:Ipfs.CoreApi.IDhtApi) | DNS mapping to IPFS |
44+
| [Misc](xref:Ipfs.CoreApi.IGenericApi) | Some miscellaneous methods |
45+
| [FileSystem](xref:Ipfs.CoreApi.IFileSystemApi) | Manages the files/directories in IPFS |
46+
| [Key](xref:Ipfs.CoreApi.IKeyApi) | Manages the cryptographic keys |
47+
| [Name](xref:Ipfs.CoreApi.INameApi) | Manages the Interplanetary Name Space (IPNS) |
48+
| [Object](xref:Ipfs.CoreApi.IObjectApi) | Manages the IPFS Directed Acrylic Graph |
49+
| [Pin](xref:Ipfs.CoreApi.IPinApi) | Manage objects that are locally stored and permanent |
50+
| [PubSub](xref:Ipfs.CoreApi.IPubSubApi) | Publish and subscribe to topic messages |
51+
| [Swarm](xref:Ipfs.CoreApi.ISwarmApi) | Manages the swarm of peers |
52+

doc/articles/daemon.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# IPFS Daemon
2+
3+
The IPFS daemon is a service that runs on a machine and allows access to other peers on the network. This
4+
is what the [IPFS client](client.md) manages.
5+
6+
## Installing
7+
8+
The authoritive documentmenation is at [https://docs.ipfs.io/introduction/install/](https://docs.ipfs.io/introduction/install/) which
9+
describes the `go-ipfs` implementation.
10+
There is also [js-ipfs](https://docs.ipfs.io/reference/js/overview/) for NodeJS fans.
11+
12+
### Windows
13+
14+
For Windows using [chocolatey](https://chocolatey.org/)
15+
16+
```
17+
> choco install go-ipfs
18+
> ipfs init
19+
> ipfs daemon
20+
```
21+
22+
## Locating the daemon
23+
24+
By default the client looks for a deamon at `http://localhost:5001`. This can be overriden by either
25+
setting the environment variable [IpfsHttpUrl](envvars.md) or initialising the client with an URL.
26+
27+
```csharp
28+
// js-ipfs likes this address
29+
static readonly IpfsClient ipfs = new IpfsClient("http://127.0.0.1:5002");
30+
```
31+
32+

doc/articles/envvars.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Environment variables
22

33
The following [environment variables](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682653.aspx)
4-
are used to control the behaviour of the library
4+
are used to control the behaviour of the library. They override the default value.
55

66
| Name | Description |
77
| --- | --- |
8-
| IpfsHttpUrl | The default URL to the IPFS HTTP API server. See [DefaultApiUri](xref:Ipfs.Api.IpfsClient.DefaultApiUri) for more details. |
8+
| IpfsHttpUrl | The [default URL](xref:Ipfs.Api.IpfsClient.DefaultApiUri) to the IPFS HTTP API [daemon](daemon.md).

doc/articles/filesystem.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# IPFS file system
2+
3+
The official name is [UnixFS](https://docs.ipfs.io/guides/concepts/unixfs/). It allows files and directories of any size
4+
to be added and retrieved from IPFS via the [FileSystem](xref:Ipfs.CoreApi.IFileSystemApi)
5+
and [Object](xref:Ipfs.CoreApi.IObjectApi) API.
6+
7+
## Files
8+
9+
A file has a unique [content id (CID)](xref:Ipfs.Cid) which is the cryptographic hash of the content; see
10+
[CID concept](https://docs.ipfs.io/guides/concepts/cid/) for background information. The file's content is not just the file's
11+
data but is encapsulated with a [protocol buffer](https://en.wikipedia.org/wiki/Protocol_Buffers) encoding of the
12+
[PBNode](https://github.com/ipfs/go-ipfs/blob/0cb22ccf359e05fb5b55a9bf2f9c515bf7d4dba7/merkledag/pb/merkledag.proto#L31-L39)
13+
and [UnixFS Data](https://github.com/ipfs/go-ipfs/blob/0cb22ccf359e05fb5b55a9bf2f9c515bf7d4dba7/unixfs/pb/unixfs.proto#L3-L20).
14+
15+
Where
16+
- `PBNode.Data` contains unixfs message Data
17+
- unixfs `Data.Data` contans file's data
18+
19+
When the file's data exceeds the [chunking size](xref:Ipfs.CoreApi.AddFileOptions.ChunkSize), multiple [blocks](xref:Ipfs.CoreApi.IBlockApi)
20+
are generated. The returned CID points to a block that has `PBNode.Links` and no `PBNode.Data`.
21+
22+
### Adding a file
23+
24+
[AddAsync](xref:Ipfs.CoreApi.IFileSystemApi.AddAsync*) is used to add a stream of data to IPFS. It returns a
25+
[FileSystemNode](xref:Ipfs.IFileSystemNode) which
26+
describes the added the data. Of particular import is its [CID](xref:Ipfs.IDataBlock.Id). The helper methods
27+
[AddTextAsync](xref:Ipfs.CoreApi.IFileSystemApi.AddTextAsync*) and [AddFileAsync](xref:Ipfs.CoreApi.IFileSystemApi.AddFileAsync*) are also available.
28+
29+
All the Add methods accept [options](xref:Ipfs.CoreApi.AddFileOptions) to control how the data is added to IPFS.
30+
31+
```csharp
32+
var fsn = await ipfs.FileSystem.AddTextAsync("hello world");
33+
Console.WriteLine((string)fsn.Id)
34+
35+
// Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfD
36+
```
37+
38+
### Reading a file
39+
40+
[ReaFileAsync](xref:Ipfs.CoreApi.IFileSystemApi.ReadFileAsync*) is used to read a stream of data from IPFS. It returns a
41+
[Stream](xref:System.IO.Stream) containing just the file's data NOT the protocol buffer encoded data.
42+
43+
```csharp
44+
string path = "Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfD";
45+
using (var stream = await ipfs.FileSystem.ReadFileAsyc(path))
46+
{
47+
// Do something with the data
48+
}
49+
```
50+
51+
### Getting a CID
52+
53+
Normally, you get the CID by [adding](xref:Ipfs.CoreApi.IFileSystemApi.AddAsync*) the file to IPFS. You can avoid adding it
54+
to IPFS by using the [OnlyHash option](xref:Ipfs.CoreApi.AddFileOptions.OnlyHash).
55+
56+
```csharp
57+
var options = new AddFileOptions { OnlyHash = true };
58+
var fsn = await ipfs.FileSystem.AddTextAsync("hello world", options);
59+
Console.WriteLine((string)fsn.Id)
60+
61+
// Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfD
62+
```
63+

doc/articles/toc.yml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
- name: Introduction
22
href: intro.md
3+
- name: Accessing IPFS
4+
href: client.md
5+
- name: File system
6+
href: filesystem.md
37
- name: Asynchronous I/O
48
href: async.md
5-
items:
6-
- name: Cancellation
7-
href: cancellation.md
89
- name: Environment variables
9-
href: envvars.md
10+
href: envvars.md
11+
- name: IPFS daemon
12+
href: daemon.md
13+
- name: Class Reference
14+
href: ../api/Ipfs.Api.yml

0 commit comments

Comments
 (0)