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

Commit 46768a3

Browse files
committed
switch to using Request internally
1 parent 046ddfb commit 46768a3

File tree

6 files changed

+122
-523
lines changed

6 files changed

+122
-523
lines changed

bootstrap.go

+13-57
Original file line numberDiff line numberDiff line change
@@ -2,70 +2,26 @@ package shell
22

33
import (
44
"context"
5-
"encoding/json"
65
)
76

8-
func (s *Shell) BootstrapAdd(peers []string) ([]string, error) {
9-
resp, err := s.newRequest(context.Background(), "bootstrap/add", peers...).Send(s.httpcli)
10-
if err != nil {
11-
return nil, err
12-
}
13-
defer resp.Close()
14-
15-
if resp.Error != nil {
16-
return nil, resp.Error
17-
}
18-
19-
addOutput := &struct {
20-
Peers []string
21-
}{}
22-
23-
err = json.NewDecoder(resp.Output).Decode(addOutput)
24-
if err != nil {
25-
return nil, err
26-
}
7+
type PeersList struct {
8+
Peers []string
9+
}
2710

28-
return addOutput.Peers, nil
11+
func (s *Shell) BootstrapAdd(peers []string) ([]string, error) {
12+
var addOutput PeersList
13+
err := s.Request("bootstrap/add", peers...).Exec(context.Background(), &addOutput)
14+
return addOutput.Peers, err
2915
}
3016

3117
func (s *Shell) BootstrapAddDefault() ([]string, error) {
32-
resp, err := s.newRequest(context.Background(), "bootstrap/add/default").Send(s.httpcli)
33-
if err != nil {
34-
return nil, err
35-
}
36-
defer resp.Close()
37-
38-
if resp.Error != nil {
39-
return nil, resp.Error
40-
}
41-
42-
addOutput := &struct {
43-
Peers []string
44-
}{}
45-
46-
err = json.NewDecoder(resp.Output).Decode(addOutput)
47-
if err != nil {
48-
return nil, err
49-
}
50-
51-
return addOutput.Peers, nil
18+
var addOutput PeersList
19+
err := s.Request("bootstrap/add/default").Exec(context.Background(), &addOutput)
20+
return addOutput.Peers, err
5221
}
5322

5423
func (s *Shell) BootstrapRmAll() ([]string, error) {
55-
resp, err := s.newRequest(context.Background(), "bootstrap/rm/all").Send(s.httpcli)
56-
if err != nil {
57-
return nil, err
58-
}
59-
defer resp.Close()
60-
61-
rmAllOutput := &struct {
62-
Peers []string
63-
}{}
64-
65-
err = json.NewDecoder(resp.Output).Decode(rmAllOutput)
66-
if err != nil {
67-
return nil, err
68-
}
69-
70-
return rmAllOutput.Peers, nil
24+
var rmAllOutput PeersList
25+
err := s.Request("bootstrap/rm/all").Exec(context.Background(), &rmAllOutput)
26+
return rmAllOutput.Peers, err
7127
}

dag.go

+8-37
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package shell
33
import (
44
"bytes"
55
"context"
6-
"encoding/json"
76
"fmt"
87
"io"
98
"io/ioutil"
@@ -13,29 +12,10 @@ import (
1312
)
1413

1514
func (s *Shell) DagGet(ref string, out interface{}) error {
16-
req := s.newRequest(context.Background(), "dag/get")
17-
req.Args = []string{ref}
18-
19-
resp, err := req.Send(s.httpcli)
20-
if err != nil {
21-
return err
22-
}
23-
defer resp.Close()
24-
25-
if resp.Error != nil {
26-
return resp.Error
27-
}
28-
29-
return json.NewDecoder(resp.Output).Decode(out)
15+
return s.Request("dag/get", ref).Exec(context.Background(), out)
3016
}
3117

3218
func (s *Shell) DagPut(data interface{}, ienc, kind string) (string, error) {
33-
req := s.newRequest(context.Background(), "dag/put")
34-
req.Opts = map[string]string{
35-
"input-enc": ienc,
36-
"format": kind,
37-
}
38-
3919
var r io.Reader
4020
switch data := data.(type) {
4121
case string:
@@ -47,31 +27,22 @@ func (s *Shell) DagPut(data interface{}, ienc, kind string) (string, error) {
4727
default:
4828
return "", fmt.Errorf("cannot current handle putting values of type %T", data)
4929
}
30+
5031
rc := ioutil.NopCloser(r)
5132
fr := files.NewReaderFile("", "", rc, nil)
5233
slf := files.NewSliceFile("", "", []files.File{fr})
5334
fileReader := files.NewMultiFileReader(slf, true)
54-
req.Body = fileReader
55-
56-
resp, err := req.Send(s.httpcli)
57-
if err != nil {
58-
return "", err
59-
}
60-
defer resp.Close()
61-
62-
if resp.Error != nil {
63-
return "", resp.Error
64-
}
6535

6636
var out struct {
6737
Cid struct {
6838
Target string `json:"/"`
6939
}
7040
}
71-
err = json.NewDecoder(resp.Output).Decode(&out)
72-
if err != nil {
73-
return "", err
74-
}
7541

76-
return out.Cid.Target, nil
42+
return out.Cid.Target, s.
43+
Request("dag/put").
44+
Option("input-enc", ienc).
45+
Option("format", kind).
46+
Body(fileReader).
47+
Exec(context.Background(), &out)
7748
}

ipns.go

+17-55
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package shell
22

33
import (
4-
"bytes"
54
"context"
6-
"encoding/json"
7-
"strconv"
85
"time"
96
)
107

@@ -15,79 +12,44 @@ type PublishResponse struct {
1512

1613
// Publish updates a mutable name to point to a given value
1714
func (s *Shell) Publish(node string, value string) error {
18-
args := []string{value}
15+
var pubResp PublishResponse
16+
req := s.Request("name/publish")
1917
if node != "" {
20-
args = []string{node, value}
18+
req.Arguments(node)
2119
}
20+
req.Arguments(value)
2221

23-
resp, err := s.newRequest(context.Background(), "name/publish", args...).Send(s.httpcli)
24-
if err != nil {
25-
return err
26-
}
27-
defer resp.Close()
28-
29-
if resp.Error != nil {
30-
return resp.Error
31-
}
32-
33-
return nil
22+
return req.Exec(context.Background(), &pubResp)
3423
}
3524

3625
// PublishWithDetails is used for fine grained control over record publishing
3726
func (s *Shell) PublishWithDetails(contentHash, key string, lifetime, ttl time.Duration, resolve bool) (*PublishResponse, error) {
38-
39-
args := []string{contentHash}
40-
req := s.newRequest(context.Background(), "name/publish", args...)
41-
if key == "" {
42-
key = "self"
27+
var pubResp PublishResponse
28+
req := s.Request("name/publish", contentHash).Option("resolve", resolve)
29+
if key != "" {
30+
req.Option("key", key)
4331
}
44-
req.Opts["key"] = key
45-
if lifetime.Seconds() > 0 {
46-
req.Opts["lifetime"] = lifetime.String()
32+
if lifetime != 0 {
33+
req.Option("lifetime", lifetime)
4734
}
4835
if ttl.Seconds() > 0 {
49-
req.Opts["ttl"] = ttl.String()
36+
req.Option("ttl", ttl)
5037
}
51-
req.Opts["resolve"] = strconv.FormatBool(resolve)
52-
resp, err := req.Send(s.httpcli)
38+
err := req.Exec(context.Background(), &pubResp)
5339
if err != nil {
5440
return nil, err
5541
}
56-
defer resp.Close()
57-
if resp.Error != nil {
58-
return nil, resp.Error
59-
}
60-
buf := new(bytes.Buffer)
61-
buf.ReadFrom(resp.Output)
62-
var pubResp PublishResponse
63-
json.Unmarshal(buf.Bytes(), &pubResp)
6442
return &pubResp, nil
6543
}
6644

6745
// Resolve gets resolves the string provided to an /ipns/[name]. If asked to
6846
// resolve an empty string, resolve instead resolves the node's own /ipns value.
6947
func (s *Shell) Resolve(id string) (string, error) {
70-
var resp *Response
71-
var err error
48+
req := s.Request("name/resolve")
7249
if id != "" {
73-
resp, err = s.newRequest(context.Background(), "name/resolve", id).Send(s.httpcli)
74-
} else {
75-
resp, err = s.newRequest(context.Background(), "name/resolve").Send(s.httpcli)
76-
}
77-
if err != nil {
78-
return "", err
79-
}
80-
defer resp.Close()
81-
82-
if resp.Error != nil {
83-
return "", resp.Error
50+
req.Arguments(id)
8451
}
85-
8652
var out struct{ Path string }
87-
err = json.NewDecoder(resp.Output).Decode(&out)
88-
if err != nil {
89-
return "", err
90-
}
91-
92-
return out.Path, nil
53+
err := req.Exec(context.Background(), &out)
54+
return out.Path, err
9355
}

requestbuilder.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (r *RequestBuilder) Header(name, value string) *RequestBuilder {
7474

7575
// Send sends the request and return the response.
7676
func (r *RequestBuilder) Send(ctx context.Context) (*Response, error) {
77-
req := r.shell.newRequest(ctx, r.command, r.args...)
77+
req := NewRequest(ctx, r.shell.url, r.command, r.args...)
7878
req.Opts = r.opts
7979
req.Headers = r.headers
8080
req.Body = r.body

0 commit comments

Comments
 (0)