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

Commit

Permalink
Merge pull request #44 from hsanjuan/stat
Browse files Browse the repository at this point in the history
shell.go: add ObjectStat support
  • Loading branch information
whyrusleeping committed Mar 6, 2017
2 parents ae1414f + 35af39f commit 458b0e6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
32 changes: 32 additions & 0 deletions shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,38 @@ func (s *Shell) PubSubPublish(topic, data string) error {
return nil
}

type ObjectStats struct {
Hash string
BlockSize int
CumulativeSize int
DataSize int
LinksSize int
NumLinks int
}

// ObjectStat gets stats for the DAG object named by key. It returns
// the stats of the requested Object or an error.
func (s *Shell) ObjectStat(key string) (*ObjectStats, error) {
resp, err := s.newRequest("object/stat", key).Send(s.httpcli)
if err != nil {
return nil, err
}
defer resp.Close()

if resp.Error != nil {
return nil, resp.Error
}

stat := &ObjectStats{}

err = json.NewDecoder(resp.Output).Decode(stat)
if err != nil {
return nil, err
}

return stat, nil
}

func (s *Shell) DiagNet(format string) ([]byte, error) {
var result = new(bytes.Buffer)

Expand Down
11 changes: 11 additions & 0 deletions shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,14 @@ func TestPubSub(t *testing.T) {

is.Nil(sub.Cancel())
}

func TestObjectStat(t *testing.T) {
obj := "QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V"
is := is.New(t)
s := NewShell(shellUrl)
stat, err := s.ObjectStat("QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V")
is.Nil(err)
is.Equal(stat.Hash, obj)
is.Equal(stat.LinksSize, 3)
is.Equal(stat.CumulativeSize, 1688)
}

0 comments on commit 458b0e6

Please sign in to comment.