Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit a1c120d

Browse files
committed
Add dag put
1 parent e9fa57e commit a1c120d

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

dag.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package shell
2+
3+
import (
4+
"encoding/json"
5+
"io/ioutil"
6+
"strings"
7+
8+
files "github.com/whyrusleeping/go-multipart-files"
9+
)
10+
11+
func (s *Shell) DagPut(data, kind string) (string, error) {
12+
req := s.newRequest("dag/put")
13+
req.Opts = map[string]string{
14+
"input-enc": "hex",
15+
"format": kind,
16+
}
17+
18+
r := strings.NewReader(data)
19+
rc := ioutil.NopCloser(r)
20+
fr := files.NewReaderFile("", "", rc, nil)
21+
slf := files.NewSliceFile("", "", []files.File{fr})
22+
fileReader := files.NewMultiFileReader(slf, true)
23+
req.Body = fileReader
24+
25+
resp, err := req.Send(s.httpcli)
26+
if err != nil {
27+
return "", err
28+
}
29+
defer resp.Close()
30+
31+
if resp.Error != nil {
32+
return "", resp.Error
33+
}
34+
35+
var out struct {
36+
Cid struct {
37+
Ref string `json:"/"`
38+
}
39+
}
40+
err = json.NewDecoder(resp.Output).Decode(&out)
41+
if err != nil {
42+
return "", err
43+
}
44+
45+
return out.Cid.Ref, nil
46+
}

0 commit comments

Comments
 (0)