Selaa lähdekoodia

local refs api

kpmy 8 vuotta sitten
vanhempi
commit
2c4662566f
3 muutettua tiedostoa jossa 95 lisäystä ja 1 poistoa
  1. 10 1
      ipfs_api/api.go
  2. 41 0
      ipfs_api/local_refs.go
  3. 44 0
      ipfs_api/refs_test.go

+ 10 - 1
ipfs_api/api.go

@@ -4,6 +4,8 @@ import (
 	"log"
 
 	"github.com/ipfs/go-ipfs-api"
+	"github.com/jbenet/go-multiaddr"
+	"github.com/jbenet/go-multiaddr-net"
 	"github.com/streamrail/concurrent-map"
 	"net/http"
 )
@@ -21,8 +23,15 @@ type MyShell struct {
 
 func reset() {
 	if sh == nil || !sh.IsUp() {
+		u := Addr
+		if a, err := multiaddr.NewMultiaddr(u); err == nil {
+			_, host, err := manet.DialArgs(a)
+			if err == nil {
+				u = host
+			}
+		}
 		sh = &MyShell{
-			Url:    Addr,
+			Url:    u,
 			Client: http.DefaultClient,
 			cache:  cmap.New(),
 		}

+ 41 - 0
ipfs_api/local_refs.go

@@ -0,0 +1,41 @@
+package ipfs_api
+
+import (
+	"encoding/json"
+
+	"github.com/ipfs/go-ipfs-api"
+)
+
+func (s *MyShell) LocalRefs() (<-chan string, error) {
+	req := shell.NewRequest(s.Url, "refs/local")
+
+	resp, err := req.Send(s.Client)
+	if err != nil {
+		return nil, err
+	}
+
+	if resp.Error != nil {
+		return nil, resp.Error
+	}
+
+	out := make(chan string)
+	go func() {
+		var ref struct {
+			Ref string
+		}
+		defer resp.Close()
+		defer close(out)
+		dec := json.NewDecoder(resp.Output)
+		for {
+			err := dec.Decode(&ref)
+			if err != nil {
+				return
+			}
+			if len(ref.Ref) > 0 {
+				out <- ref.Ref
+			}
+		}
+	}()
+
+	return out, nil
+}

+ 44 - 0
ipfs_api/refs_test.go

@@ -0,0 +1,44 @@
+package ipfs_api
+
+import (
+	"bytes"
+	"io"
+	"net/http"
+	"testing"
+
+	"github.com/ipfs/go-ipfs-api"
+	ma "github.com/jbenet/go-multiaddr"
+	manet "github.com/jbenet/go-multiaddr-net"
+	"github.com/kpmy/ypk/fn"
+)
+
+func TestRefsLocal(t *testing.T) {
+	//sh := shell.NewShell("127.0.0.1:5001")
+	var url = "127.0.0.1:5001"
+	if a, err := ma.NewMultiaddr(url); err == nil {
+		_, host, err := manet.DialArgs(a)
+		if err == nil {
+			url = host
+		}
+	}
+	req := shell.NewRequest(url, "refs/local")
+	if resp, err := req.Send(http.DefaultClient); err == nil {
+		if !fn.IsNil(resp.Error) {
+			t.Error(resp.Error)
+		} else {
+			buf := &bytes.Buffer{}
+			io.Copy(buf, resp.Output)
+			t.Log(buf.String())
+		}
+	} else {
+		t.Error(err)
+	}
+	sh := Shell()
+	if ch, err := sh.LocalRefs(); err == nil {
+		for x := range ch {
+			t.Log(x)
+		}
+	} else {
+		t.Error(err)
+	}
+}