Skip to content

Commit 2b377db

Browse files
committed
update examples for v0.12.0
1 parent 264b8e9 commit 2b377db

37 files changed

+1531
-89
lines changed

api-code-examples/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# iroh API code examples
2+
3+
This directory includes all the code examples that show how to achieve `iroh` cli commands programmatically using rust, golang, and python.
4+
5+
## running the examples
6+
7+
There is a `run_files.sh` bash script that will run all of the python and golang examples. The following sections will describe how to set up the golang and python environments to run the script correctly.
8+
9+
You can also run the individual examples without using the `run_files.sh` script.
10+
11+
### running python
12+
13+
Download python3 and download some way to manage virtual environments. pip will likely come with python, but if not, download pip3 as well.
14+
15+
Navigate into the `python` directory.
16+
17+
Create and activate a virtual environment for this project.
18+
19+
Install iroh:
20+
`python3 -m pip install iroh`
21+
22+
To run an example, eg for file `author-list.py`:
23+
`python3 run author-list.py`
24+
25+
### running golang
26+
27+
Download & set up go.
28+
29+
Navigate to the go directory.
30+
31+
Run the following commands:
32+
```
33+
$ go get github.com/n0-computer/iroh-ffi/iroh-go/iroh
34+
$ git submodule add https://github.com/n0-computer/iroh-ffi.git extern/iroh-ffi
35+
$ make -f extern/iroh-ffi/InstallGo
36+
$ go mod edit -replace=github.com/n0-computer/iroh-ffi/iroh-go=./extern/iroh-ffi/iroh-go
37+
```
38+
39+
To run an example, eg for the file `author-list.py`:
40+
`go run author-list.py`
41+
42+
### running the script
43+
Once the above are set up, run the `run_files.sh` script to iterate through each go and python file to ensure that the examples work!

api-code-examples/api.mjs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const doc = [
33
{
44
name: 'doc delete',
55
description: 'Delete all document entries below a key prefix.',
6-
slug: 'doc-del',
6+
slug: 'doc-delete',
77
arguments: [
88
{ name: "prefix", necessity: 'required', description: "Prefix to delete. All entries whose key starts with or is equal to the prefix will be deleted" }
99
],
@@ -373,17 +373,6 @@ Collection: bafkr4ie3xsx3vdsbflainnk6p4xs4h2hq3hdmuasuoflkgybvnsbljb3ke`,
373373
console: `> `,
374374
}
375375
},
376-
{
377-
name: 'blob validate',
378-
description: 'Validate hashes on the running node.',
379-
slug: 'blob-validate',
380-
arguments: [
381-
{ name: 'repair', necessity: '', description: 'Repair the store by removing invalid data.' }
382-
],
383-
examples: {
384-
console: `> `,
385-
}
386-
}
387376
]
388377

389378
const api = {

api-code-examples/go/author-list.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@ func main() {
1212
panic(err)
1313
}
1414

15-
if _, err := node.AuthorCreate(); err != nil {
15+
author, err := node.AuthorCreate()
16+
if err != nil {
1617
panic(err)
1718
}
19+
fmt.Println("Created author:", author.ToString())
1820

1921
authors, err := node.AuthorList()
2022
if err != nil {
2123
panic(err)
2224
}
2325

26+
fmt.Println("Authors:")
2427
for _, author := range authors {
25-
fmt.Println(author.ToString())
28+
fmt.Println("\t", author.ToString())
2629
}
2730
}

api-code-examples/go/blob-add.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/n0-computer/iroh-ffi/iroh-go/iroh"
9+
)
10+
11+
type addCallback struct {
12+
}
13+
14+
func (a addCallback) Progress(event *iroh.AddProgress) *iroh.IrohError {
15+
switch t := event.Type(); t {
16+
case iroh.AddProgressTypeFound:
17+
fmt.Println("AddProgress - Found:")
18+
found := event.AsFound()
19+
fmt.Printf("\tid: %d, name: %s, size: %d\n", found.Id, found.Name, found.Size)
20+
case iroh.AddProgressTypeProgress:
21+
fmt.Println("AddProgress - Progress:")
22+
progress := event.AsProgress()
23+
fmt.Printf("\tid: %d, offset: %d\n", progress.Id, progress.Offset)
24+
case iroh.AddProgressTypeDone:
25+
fmt.Println("AddProgress - Done:")
26+
done := event.AsDone()
27+
fmt.Printf("\tid: %d, hash: %s\n", done.Id, done.Hash.ToString())
28+
case iroh.AddProgressTypeAllDone:
29+
fmt.Println("AddProgress - AllDone:")
30+
allDone := event.AsAllDone()
31+
fmt.Printf("\thash: %s, format: %v, tag: %s\n", allDone.Hash.ToString(), allDone.Format, allDone.Tag.ToString())
32+
case iroh.AddProgressTypeAbort:
33+
fmt.Println("AddProgress - Abort:")
34+
abort := event.AsAbort()
35+
fmt.Printf("\terror: %s", abort.Error)
36+
default:
37+
fmt.Println("Unknown AddProgress event: %v", event)
38+
return &iroh.IrohError{}
39+
}
40+
return nil
41+
}
42+
43+
func main() {
44+
// create folder
45+
if err := os.Mkdir("tmp", os.ModePerm); err != nil {
46+
panic(err)
47+
}
48+
defer func() {
49+
os.RemoveAll("tmp")
50+
fmt.Println("Removed dir 'tmp'")
51+
}()
52+
53+
path, err := filepath.Abs(filepath.Join("tmp"))
54+
if err != nil {
55+
panic(err)
56+
}
57+
58+
fmt.Println("Created dir \"tmp\"")
59+
60+
fileNames := []string{"foo", "bar", "bat"}
61+
// create three files in the folder
62+
for _, fileName := range fileNames {
63+
path := filepath.Join("tmp", fileName)
64+
f, err := os.Create(path)
65+
if err != nil {
66+
panic(err)
67+
}
68+
_, err = f.WriteString(fmt.Sprintf("%s", fileName))
69+
if err != nil {
70+
panic(err)
71+
}
72+
fmt.Printf("Created file %q\n", path)
73+
}
74+
75+
node, err := iroh.NewIrohNode("iroh_data_dir")
76+
if err != nil {
77+
panic(err)
78+
}
79+
80+
// when `inPlace` is true, iroh will NOT copy over the files into its
81+
// internal database. Only use this option when you know the file will never
82+
// move or change
83+
inPlace := false
84+
// iroh blobs can be "tagged" with human readable names, this creates a tag
85+
// automatically
86+
tag := iroh.SetTagOptionAuto()
87+
// when adding a single file, if you use `iroh.WrapOptionWrap`, you will turn
88+
// the single file into a collection with one entry
89+
wrap := iroh.WrapOptionNoWrap()
90+
// you can use this callback to react to progress updates
91+
callback := addCallback{}
92+
93+
// import the directory, creating one blob for each file, and one metadata
94+
// blob that stores the file names for each blob
95+
// also creates a 'collection' from the directory, grouping together the
96+
// blobs
97+
98+
err = node.BlobsAddFromPath(path, inPlace, tag, wrap, callback)
99+
if err != nil {
100+
panic(err)
101+
}
102+
103+
// Output:
104+
// Created dir "tmp"
105+
// Created file "tmp/foo"
106+
// Created file "tmp/bar"
107+
// Created file "tmp/bat"
108+
// AddProgress - Found:
109+
// id: 2, name: $HOME/tmp/bar, size: 3
110+
// AddProgress - Found:
111+
// id: 0, name: $HOME/tmp/foo, size: 3
112+
// AddProgress - Found:
113+
// id: 1, name: $HOME/tmp/bat, size: 3
114+
// AddProgress - Progress:
115+
// id: 1, offset: 3
116+
// AddProgress - Done:
117+
// id: 1, hash: bafkr4iabccdb2eyeu764xoewbcqv62sjaggxibtmxx5tnmwer3wp3rquq4
118+
// AddProgress - Progress:
119+
// id: 2, offset: 3
120+
// AddProgress - Progress:
121+
// id: 0, offset: 3
122+
// AddProgress - Done:
123+
// id: 2, hash: bafkr4ihs5cl65v6sa3gykxkecwmpuuq2xr22vfuvh2l4amgjmewdbqjjhu
124+
// AddProgress - Done:
125+
// id: 0, hash: bafkr4iae4c5tt4yldi76xcpvg3etxykqkvec352im5fqbutolj2xo5yc5e
126+
// AddProgress - AllDone:
127+
// hash: bafkr4iaotzhxuiak7eusnngngnwsqdu4crf4lmdxzkbhuebunevecjzkim, format: 2, tag: "auto-2023-12-09T17:37:42.432Z"
128+
// Removed dir 'tmp'
129+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/n0-computer/iroh-ffi/iroh-go/iroh"
7+
)
8+
9+
func main() {
10+
node, err := iroh.NewIrohNode("iroh_data_dir")
11+
if err != nil {
12+
panic(err)
13+
}
14+
15+
b := []byte("hello world!")
16+
tag := iroh.SetTagOptionAuto()
17+
18+
// add blob
19+
outcome, err := node.BlobsAddBytes(b, tag)
20+
if err != nil {
21+
panic(err)
22+
}
23+
24+
fmt.Printf("Added blob %s (%d bytes)\n", outcome.Hash.ToString(), outcome.Size)
25+
26+
fmt.Println("blobs list:")
27+
28+
blobs, err := node.BlobsList()
29+
if err != nil {
30+
panic(err)
31+
}
32+
33+
for _, hash := range blobs {
34+
fmt.Println("\t", hash.ToString())
35+
}
36+
37+
// Output:
38+
// Added blob bafkr4ib2uyoebh6xof6j3hddsibk6l5oi4ga55tjxz52fsxkk544wu2otu (12 bytes)
39+
// blobs list:
40+
// bafkr4ib2uyoebh6xof6j3hddsibk6l5oi4ga55tjxz52fsxkk544wu2otu
41+
//
42+
// (may contain additional hashes if you have previously added content to your node)
43+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/n0-computer/iroh-ffi/iroh-go/iroh"
9+
)
10+
11+
type addCallback struct {
12+
hashCh chan iroh.Hash
13+
}
14+
15+
func (a addCallback) Progress(event *iroh.AddProgress) *iroh.IrohError {
16+
if event.Type() == iroh.AddProgressTypeAllDone {
17+
allDone := event.AsAllDone()
18+
a.hashCh <- *allDone.Hash
19+
}
20+
return nil
21+
}
22+
23+
func main() {
24+
// create folder
25+
if err := os.Mkdir("tmp", os.ModePerm); err != nil {
26+
panic(err)
27+
}
28+
defer func() {
29+
os.RemoveAll("tmp")
30+
fmt.Println("Removed dir 'tmp'")
31+
}()
32+
33+
path, err := filepath.Abs(filepath.Join("tmp"))
34+
if err != nil {
35+
panic(err)
36+
}
37+
38+
fmt.Println("Created dir \"tmp\"")
39+
40+
fileNames := []string{"foo", "bar", "bat"}
41+
// create three files in the folder
42+
for _, fileName := range fileNames {
43+
path := filepath.Join("tmp", fileName)
44+
f, err := os.Create(path)
45+
if err != nil {
46+
panic(err)
47+
}
48+
_, err = f.WriteString(fmt.Sprintf("%s", fileName))
49+
if err != nil {
50+
panic(err)
51+
}
52+
fmt.Printf("Created file %q\n", path)
53+
}
54+
55+
node, err := iroh.NewIrohNode("iroh_data_dir")
56+
if err != nil {
57+
panic(err)
58+
}
59+
60+
// when `inPlace` is true, iroh will NOT copy over the files into its
61+
// internal database. Only use this option when you know the file will never
62+
// move or change
63+
inPlace := false
64+
// iroh blobs can be "tagged" with human readable names, this creates a tag
65+
// automatically
66+
tag := iroh.SetTagOptionNamed(iroh.TagFromString("my_collection"))
67+
// when adding a single file, if you use `iroh.WrapOptionWrap`, you will turn
68+
// the single file into a collection with one entry
69+
wrap := iroh.WrapOptionNoWrap()
70+
// you can use this callback to react to progress updates
71+
hashCh := make(chan iroh.Hash, 1)
72+
callback := addCallback{hashCh}
73+
74+
// import the directory, creating one blob for each file, and one metadata
75+
// blob that stores the file names for each blob
76+
// also creates a 'collection' from the directory, grouping together the
77+
// blobs
78+
79+
err = node.BlobsAddFromPath(path, inPlace, tag, wrap, callback)
80+
if err != nil {
81+
panic(err)
82+
}
83+
84+
hash := <-hashCh
85+
86+
fmt.Println("Added collection", hash.ToString())
87+
88+
fmt.Println("collections list:")
89+
90+
collRes, err := node.BlobsListCollections()
91+
if err != nil {
92+
panic(err)
93+
}
94+
95+
for _, res := range collRes {
96+
fmt.Printf("\thash: %s tag: %s\n", res.Hash.ToString(), res.Tag.ToString())
97+
}
98+
99+
// Output:
100+
// Created dir "tmp"
101+
// Created file "tmp/foo"
102+
// Created file "tmp/bar"
103+
// Created file "tmp/bat"
104+
// AllDone
105+
// Added collection bafkr4iaotzhxuiak7eusnngngnwsqdu4crf4lmdxzkbhuebunevecjzkim
106+
// collections list:
107+
// hash:bafkr4iaotzhxuiak7eusnngngnwsqdu4crf4lmdxzkbhuebunevecjzkim tag:"my_collection"
108+
// Removed dir 'tmp'
109+
//
110+
// (may contain additional collections if you have previously added content to your node)
111+
}

0 commit comments

Comments
 (0)