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

Commit 458b0e6

Browse files
Merge pull request #44 from hsanjuan/stat
shell.go: add ObjectStat support
2 parents ae1414f + 35af39f commit 458b0e6

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

shell.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,38 @@ func (s *Shell) PubSubPublish(topic, data string) error {
745745
return nil
746746
}
747747

748+
type ObjectStats struct {
749+
Hash string
750+
BlockSize int
751+
CumulativeSize int
752+
DataSize int
753+
LinksSize int
754+
NumLinks int
755+
}
756+
757+
// ObjectStat gets stats for the DAG object named by key. It returns
758+
// the stats of the requested Object or an error.
759+
func (s *Shell) ObjectStat(key string) (*ObjectStats, error) {
760+
resp, err := s.newRequest("object/stat", key).Send(s.httpcli)
761+
if err != nil {
762+
return nil, err
763+
}
764+
defer resp.Close()
765+
766+
if resp.Error != nil {
767+
return nil, resp.Error
768+
}
769+
770+
stat := &ObjectStats{}
771+
772+
err = json.NewDecoder(resp.Output).Decode(stat)
773+
if err != nil {
774+
return nil, err
775+
}
776+
777+
return stat, nil
778+
}
779+
748780
func (s *Shell) DiagNet(format string) ([]byte, error) {
749781
var result = new(bytes.Buffer)
750782

shell_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,14 @@ func TestPubSub(t *testing.T) {
201201

202202
is.Nil(sub.Cancel())
203203
}
204+
205+
func TestObjectStat(t *testing.T) {
206+
obj := "QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V"
207+
is := is.New(t)
208+
s := NewShell(shellUrl)
209+
stat, err := s.ObjectStat("QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V")
210+
is.Nil(err)
211+
is.Equal(stat.Hash, obj)
212+
is.Equal(stat.LinksSize, 3)
213+
is.Equal(stat.CumulativeSize, 1688)
214+
}

0 commit comments

Comments
 (0)