Skip to content

Commit

Permalink
fix(DatasetRef): fixes to datasetRef handling
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 committed Mar 7, 2018
1 parent e1e46f4 commit 134e0f9
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 42 deletions.
60 changes: 30 additions & 30 deletions api/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"github.com/qri-io/qri/p2p"
"github.com/qri-io/qri/repo"
"github.com/qri-io/qri/repo/profile"
// peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"

peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
)

// PeerHandlers wraps a requests struct to interface with http.HandlerFunc
Expand Down Expand Up @@ -50,18 +51,17 @@ func (h *PeerHandlers) PeerHandler(w http.ResponseWriter, r *http.Request) {
}
}

// TODO: add back connect endpoint
// ConnectToPeerHandler is the endpoint for explicitly connecting to a peer
// func (h *PeerHandlers) ConnectToPeerHandler(w http.ResponseWriter, r *http.Request) {
// switch r.Method {
// case "OPTIONS":
// util.EmptyOkHandler(w, r)
// case "GET":
// h.connectToPeerHandler(w, r)
// default:
// util.NotFoundHandler(w, r)
// }
// }
func (h *PeerHandlers) ConnectToPeerHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "OPTIONS":
util.EmptyOkHandler(w, r)
case "GET":
h.connectToPeerHandler(w, r)
default:
util.NotFoundHandler(w, r)
}
}

// ConnectionsHandler is the endpoint for listing qri & IPFS connections
func (h *PeerHandlers) ConnectionsHandler(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -120,21 +120,21 @@ func (h *PeerHandlers) namespaceHandler(w http.ResponseWriter, r *http.Request)

}

// TODO: add back connect endpoint
// func (h *PeerHandlers) connectToPeerHandler(w http.ResponseWriter, r *http.Request) {
// b58pid := r.URL.Path[len("/connect/"):]
// pid, err := peer.IDB58Decode(b58pid)
// if err != nil {
// util.WriteErrResponse(w, http.StatusBadRequest, err)
// return
// }

// res := &profile.Profile{}
// if err := h.ConnectToPeer(&pid, res); err != nil {
// h.log.Infof("error connecting to peer: %s", err.Error())
// util.WriteErrResponse(w, http.StatusInternalServerError, err)
// return
// }

// util.WriteResponse(w, res)
// }
func (h *PeerHandlers) connectToPeerHandler(w http.ResponseWriter, r *http.Request) {
b58pid := r.URL.Path[len("/connect/"):]

pid, err := peer.IDB58Decode(b58pid)
if err != nil {
util.WriteErrResponse(w, http.StatusBadRequest, err)
return
}

res := &profile.Profile{}
if err := h.ConnectToPeer(&pid, res); err != nil {
h.log.Infof("error connecting to peer: %s", err.Error())
util.WriteErrResponse(w, http.StatusInternalServerError, err)
return
}

util.WriteResponse(w, res)
}
4 changes: 2 additions & 2 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ func NewServerRoutes(s *Server) *http.ServeMux {
ph := NewPeerHandlers(s.log, s.qriNode.Repo, s.qriNode)
m.Handle("/peers", s.middleware(ph.PeersHandler))
m.Handle("/peers/", s.middleware(ph.PeerHandler))
// TODO - add back connect endpoint
// m.Handle("/connect/", s.middleware(ph.ConnectToPeerHandler))

m.Handle("/connect/", s.middleware(ph.ConnectToPeerHandler))
m.Handle("/connections", s.middleware(ph.ConnectionsHandler))

dsh := NewDatasetHandlers(s.log, s.qriNode.Repo)
Expand Down
1 change: 1 addition & 0 deletions api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func TestServerRoutes(t *testing.T) {
{"GET", "/export/me/cities", "", "", 200},
{"GET", "/diff", "diffRequest.json", "diffResponse.json", 200},
{"GET", "/diff", "diffRequestPlusMinusColor.json", "diffResponsePlusMinusColor.json", 200},
{"GET", "/connect/", "", "", 400},
// blatently checking all options for easy test coverage bump
{"OPTIONS", "/add", "", "", 200},
{"OPTIONS", "/add/", "", "", 200},
Expand Down
23 changes: 14 additions & 9 deletions core/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,27 @@ func (r *DatasetRequests) List(p *ListParams, res *[]repo.DatasetRef) error {
PeerID: p.PeerID,
}

if ds.Peername == "" && ds.PeerID == "" {
ds.Peername = "me"
}

pro, err := r.repo.Profile()
if err != nil {
return fmt.Errorf("error getting profile: %s", err.Error())
}

if err := repo.CanonicalizePeer(r.repo, ds); err != nil {
return fmt.Errorf("error canonicalizing peer: %s", err.Error())
}

if ds.Peername != "" && r.Node != nil {
if ds.Peername != pro.Peername {
if r.Node == nil {
return fmt.Errorf("cannot list remote datasets without p2p connection")
}

replies, err := r.Node.RequestDatasetsList(ds.Peername)
*res = replies
return err
} else if ds.Peername != "" {
pro, err := r.repo.Profile()
if err != nil {
return err
}
if pro.Peername != ds.Peername && r.Node == nil {
return fmt.Errorf("cannot list remote datasets without p2p connection")
}
}

store := r.repo.Store()
Expand Down
10 changes: 10 additions & 0 deletions core/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ func (d *PeerRequests) ConnectToPeer(pid *peer.ID, res *profile.Profile) error {
return d.cli.Call("PeerRequests.ConnectToPeer", pid, res)
}

if profile, err := d.qriNode.Repo.Peers().GetPeer(*pid); err == nil {
*pid, err = profile.IPFSPeerID()
if err != nil {
return fmt.Errorf("error getting IPFS peer ID: %s", err.Error())
}
}
// if err != nil {
// return fmt.Errorf("error getting peer profile: %s", err.Error())
// }

if err := d.qriNode.ConnectToPeer(*pid); err != nil {
return fmt.Errorf("error connecting to peer: %s", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion repo/fs/peer_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (r PeerStore) GetPeer(id peer.ID) (*profile.Profile, error) {

ids := id.Pretty()
for p, d := range ps {
if ids == p {
if ids == p || d.ID == ids {
return d, nil
}
}
Expand Down
8 changes: 8 additions & 0 deletions repo/fs/refstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ func (n *Refstore) names() ([]repo.DatasetRef, error) {
for i, rs := range refs {
ref, err := repo.ParseDatasetRef(rs)
if err != nil {
// hold over for
// TODO - remove by 0.3.0
if err.Error() == "invalid PeerID: 'ipfs'" {
ref.PeerID = ""
ref.Path = fmt.Sprintf("/ipfs/%s", ref.Path)
ns[i] = ref
continue
}
return nil, err
}
ns[i] = ref
Expand Down

0 comments on commit 134e0f9

Please sign in to comment.