Skip to content

Commit

Permalink
feat(api.List): allow /list/[peer_id]
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 committed Mar 5, 2018
1 parent 52151e6 commit a2771f9
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 13 deletions.
32 changes: 21 additions & 11 deletions api/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"strings"

util "github.com/datatogether/api/apiutil"
// "github.com/ipfs/go-datastore"
"github.com/qri-io/cafs/memfs"
"github.com/qri-io/dataset/dsutil"
"github.com/qri-io/dsdiff"
Expand Down Expand Up @@ -97,6 +96,7 @@ func (h *DatasetHandlers) DiffHandler(w http.ResponseWriter, r *http.Request) {

// PeerListHandler is a dataset list endpoint
func (h *DatasetHandlers) PeerListHandler(w http.ResponseWriter, r *http.Request) {
h.log.Infof("%s %s", r.Method, r.URL.Path)
switch r.Method {
case "OPTIONS":
util.EmptyOkHandler(w, r)
Expand Down Expand Up @@ -189,6 +189,7 @@ func (h *DatasetHandlers) zipDatasetHandler(w http.ResponseWriter, r *http.Reque
func (h *DatasetHandlers) listHandler(w http.ResponseWriter, r *http.Request) {
args := core.ListParamsFromRequest(r)
args.OrderBy = "created"

res := []repo.DatasetRef{}
if err := h.List(&args, &res); err != nil {
h.log.Infof("error listing datasets: %s", err.Error())
Expand Down Expand Up @@ -286,18 +287,27 @@ func (h *DatasetHandlers) diffHandler(w http.ResponseWriter, r *http.Request) {
}

func (h *DatasetHandlers) peerListHandler(w http.ResponseWriter, r *http.Request) {
h.log.Info(r.URL.Path)
p := core.ListParamsFromRequest(r)
ref, err := DatasetRefFromPath(r.URL.Path[len("/list/"):])
if err != nil {
util.WriteErrResponse(w, http.StatusBadRequest, err)
return
}
if !ref.IsPeerRef() {
util.WriteErrResponse(w, http.StatusBadRequest, errors.New("request needs to be in the form '/list/[peername]'"))
return
}
p.Peername = ref.Peername
p.OrderBy = "created"

// TODO - cheap peerId detection
peerID := r.URL.Path[len("/list/"):]
if len(peerID) > 0 && peerID[:2] == "Qm" {
p.PeerID = peerID
} else {
ref, err := DatasetRefFromPath(r.URL.Path[len("/list/"):])
if err != nil {
util.WriteErrResponse(w, http.StatusBadRequest, err)
return
}
if !ref.IsPeerRef() {
util.WriteErrResponse(w, http.StatusBadRequest, errors.New("request needs to be in the form '/list/[peername]'"))
return
}
p.Peername = ref.Peername
}

res := []repo.DatasetRef{}
if err := h.List(&p, &res); err != nil {
h.log.Infof("error listing peer's datasets: %s", err.Error())
Expand Down
35 changes: 33 additions & 2 deletions api/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ import (
// PeerHandlers wraps a requests struct to interface with http.HandlerFunc
type PeerHandlers struct {
core.PeerRequests
log logging.Logger
repo repo.Repo
log logging.Logger
}

// NewPeerHandlers allocates a PeerHandlers pointer
func NewPeerHandlers(log logging.Logger, r repo.Repo, node *p2p.QriNode) *PeerHandlers {
req := core.NewPeerRequests(node, nil)
h := PeerHandlers{*req, log}
h := PeerHandlers{*req, r, log}
return &h
}

Expand All @@ -37,6 +38,18 @@ func (h *PeerHandlers) PeersHandler(w http.ResponseWriter, r *http.Request) {
}
}

// PeerHandler gets info on a single peer
func (h *PeerHandlers) PeerHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "OPTIONS":
util.EmptyOkHandler(w, r)
case "GET":
h.peerHandler(w, r)
default:
util.NotFoundHandler(w, r)
}
}

// 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) {
Expand Down Expand Up @@ -89,6 +102,24 @@ func (h *PeerHandlers) listConnectionsHandler(w http.ResponseWriter, r *http.Req
util.WriteResponse(w, peers)
}

func (h *PeerHandlers) peerHandler(w http.ResponseWriter, r *http.Request) {
p := &core.PeerInfoParams{
PeerID: r.URL.Path[len("/peers/"):],
}
res := &profile.Profile{}
if err := h.Info(p, res); err != nil {
h.log.Infof("error getting peer info: %s", err.Error())
util.WriteErrResponse(w, http.StatusInternalServerError, err)
return
}

util.WriteResponse(w, res)
}

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/"):]
Expand Down
2 changes: 2 additions & 0 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,13 @@ 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("/connections", s.middleware(ph.ConnectionsHandler))

dsh := NewDatasetHandlers(s.log, s.qriNode.Repo)

// TODO - stupid hack for now.
dsh.DatasetRequests.Node = s.qriNode
m.Handle("/list", s.middleware(dsh.ListHandler))
Expand Down
9 changes: 9 additions & 0 deletions core/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/qri-io/jsonschema"
"github.com/qri-io/qri/p2p"
"github.com/qri-io/qri/repo"
"github.com/qri-io/qri/repo/profile"
"github.com/qri-io/varName"
)

Expand Down Expand Up @@ -69,6 +70,14 @@ func (r *DatasetRequests) List(p *ListParams, res *[]repo.DatasetRef) error {
return fmt.Errorf("error canonicalizing peername: %s", err.Error())
}

if p.PeerID != "" {
if pid, err := profile.NewB58PeerID(p.PeerID); err == nil {
if peer, err := r.repo.Peers().GetPeer(pid); err == nil {
p.Peername = peer.Peername
}
}
}

if p.Peername != "" && r.Node != nil {
replies, err := r.Node.RequestDatasetsList(p.Peername)
*res = replies
Expand Down
1 change: 1 addition & 0 deletions core/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type GetParams struct {
// ListParams define limits & offsets, not pages & page sizes.
// TODO - rename this to PageParams.
type ListParams struct {
PeerID string
Peername string
OrderBy string
Limit int
Expand Down
5 changes: 5 additions & 0 deletions repo/profile/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,8 @@ func (p *Profile) IPFSPeerID() (peer.ID, error) {
}
return "", fmt.Errorf("no IPFS Peer ID found")
}

// NewB58PeerID creates a peer.ID from a base58-encoded string
func NewB58PeerID(pid string) (peer.ID, error) {
return peer.IDB58Decode(pid)
}

0 comments on commit a2771f9

Please sign in to comment.