1
+ package httpapi
2
+
3
+ import (
4
+ "context"
5
+ "fmt"
6
+ "github.com/pkg/errors"
7
+ "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
8
+ "io/ioutil"
9
+ "strconv"
10
+
11
+ "github.com/ipfs/go-ipfs/core/coreapi/interface"
12
+
13
+ ipfspath "gx/ipfs/QmRG3XuGwT7GYuAqgWDJBKTzdaHMwAnc1x7J2KHEXNHxzG/go-path"
14
+ ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format"
15
+ )
16
+
17
+ type ipldNode struct {
18
+ ctx context.Context //TODO: should we re-consider adding ctx to ipld interfaces?
19
+ path iface.ResolvedPath
20
+ api * HttpApi
21
+ }
22
+
23
+ func (n * ipldNode ) RawData () []byte {
24
+ r , err := n .api .Block ().Get (n .ctx , n .path )
25
+ if err != nil {
26
+ panic (err ) // TODO: eww, should we add errors too / better ideas?
27
+ }
28
+
29
+ b , err := ioutil .ReadAll (r )
30
+ if err != nil {
31
+ panic (err )
32
+ }
33
+
34
+ return b
35
+ }
36
+
37
+ func (n * ipldNode ) Cid () cid.Cid {
38
+ return n .path .Cid ()
39
+ }
40
+
41
+ func (n * ipldNode ) String () string {
42
+ return fmt .Sprintf ("[Block %s]" , n .Cid ())
43
+ }
44
+
45
+ func (n * ipldNode ) Loggable () map [string ]interface {} {
46
+ return nil //TODO: we can't really do better here, can we?
47
+ }
48
+
49
+ // TODO: should we use 'full'/real ipld codecs for this? js-ipfs-api does that.
50
+ // We can also give people a choice
51
+ func (n * ipldNode ) Resolve (path []string ) (interface {}, []string , error ) {
52
+ p := ipfspath .Join ([]string {n .path .String (), ipfspath .Join (path )})
53
+
54
+ var out interface {}
55
+ n .api .request ("dag/get" , p ).Exec (n .ctx , & out )
56
+
57
+ // TODO: this is more than likely wrong, fix if we decide to stick with this 'http-ipld-node' hack
58
+ for len (path ) > 0 {
59
+ switch o := out .(type ) {
60
+ case map [string ]interface {}:
61
+ v , ok := o [path [0 ]]
62
+ if ! ok {
63
+ // TODO: ipld links
64
+ return nil , nil , errors .New ("no element under this path" )
65
+ }
66
+ out = v
67
+ case []interface {}:
68
+ n , err := strconv .ParseUint (path [0 ], 10 , 32 )
69
+ if err != nil {
70
+ return nil , nil , err
71
+ }
72
+ if len (o ) < int (n ) {
73
+ return nil , nil , errors .New ("no element under this path" )
74
+ }
75
+ out = o [n ]
76
+ }
77
+ path = path [1 :]
78
+ }
79
+
80
+ return out , path , nil
81
+ }
82
+
83
+
84
+ func (n * ipldNode ) Tree (path string , depth int ) []string {
85
+ panic ("implement me" )
86
+ }
87
+
88
+ func (n * ipldNode ) ResolveLink (path []string ) (* ipld.Link , []string , error ) {
89
+ panic ("implement me" )
90
+ }
91
+
92
+ func (n * ipldNode ) Copy () ipld.Node {
93
+ panic ("implement me" )
94
+ }
95
+
96
+ func (n * ipldNode ) Links () []* ipld.Link {
97
+ panic ("implement me" )
98
+ }
99
+
100
+ func (n * ipldNode ) Stat () (* ipld.NodeStat , error ) {
101
+ panic ("implement me" )
102
+ }
103
+
104
+ func (n * ipldNode ) Size () (uint64 , error ) {
105
+ panic ("implement me" )
106
+ }
107
+
108
+ var _ ipld.Node = & ipldNode {}
0 commit comments