Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
"hash": "QmQine7gvHncNevKtG9QXxf3nXcwSj6aDDmMm52mHofEEp",
"name": "tar-utils",
"version": "0.0.3"
},
{
"author": "whyrusleeping",
"hash": "QmcoBbyTiL9PFjo1GFixJwqQ8mZLJ36CribuqyKmS1okPu",
"name": "go-libp2p-metrics",
"version": "2.1.3"
}
],
"gxVersion": "0.7.0",
Expand Down
52 changes: 52 additions & 0 deletions shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr-net"
tar "github.com/whyrusleeping/tar-utils"

p2pmetrics "github.com/libp2p/go-libp2p-metrics"
)

const (
Expand Down Expand Up @@ -92,6 +94,25 @@ func (s *Shell) newRequest(ctx context.Context, command string, args ...string)
return NewRequest(ctx, s.url, command, args...)
}

func (s *Shell) Request(ctx context.Context, command string, args ...string) (*Request, *Response, error) {
req := s.newRequest(ctx, command, args...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe allow passing option map here too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point

res, err := req.Send(s.httpcli)
return req, res, err
}

func (s *Shell) RequestDecode(ctx context.Context, dec interface{}, command string, args ...string) error {
_, res, err := s.Request(ctx, command, args...)
if err != nil {
return err
}
defer res.Close()
if res.Error != nil {
return res.Error
}

return json.NewDecoder(res.Output).Decode(dec)
}

type IdOutput struct {
ID string
PublicKey string
Expand Down Expand Up @@ -831,3 +852,34 @@ func (s *Shell) ObjectStat(key string) (*ObjectStats, error) {

return stat, nil
}

// ObjectStat gets stats for the DAG object named by key. It returns
// the stats of the requested Object or an error.
func (s *Shell) StatsBW() (*p2pmetrics.Stats, error) {
v := &p2pmetrics.Stats{}
err := s.RequestDecode(context.TODO(), &v, "stats/bw")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other functions use..Background

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even better, let's just take a context (it's a new command).

return v, err
}

type SwarmStreamInfo struct {
Protocol string
}

type SwarmConnInfo struct {
Addr string
Peer string
Latency string
Muxer string
Streams []SwarmStreamInfo
}

type SwarmConnInfos struct {
Peers []SwarmConnInfo
}

// SwarmPeers gets all the swarm peers
func (s *Shell) SwarmPeers() (*SwarmConnInfos, error) {
v := &SwarmConnInfos{}
err := s.RequestDecode(context.TODO(), &v, "swarm/peers")
return v, err
}
14 changes: 14 additions & 0 deletions shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,17 @@ func TestDagPut(t *testing.T) {
is.Nil(err)
is.Equal(c, "zdpuAt47YjE9XTgSxUBkiYCbmnktKajQNheQBGASHj3FfYf8M")
}

func TestStatsBW(t *testing.T) {
is := is.New(t)
s := NewShell(shellUrl)
_, err := s.StatsBW()
is.Nil(err)
}

func TestSwarmPeers(t *testing.T) {
is := is.New(t)
s := NewShell(shellUrl)
_, err := s.SwarmPeers()
is.Nil(err)
}