Skip to content

Commit

Permalink
feat(api, history): get your own dataset's history, or a peer's datas…
Browse files Browse the repository at this point in the history
…et history using peername/datasetname
  • Loading branch information
ramfox committed Feb 7, 2018
1 parent e3bb5ab commit bb321de
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
26 changes: 20 additions & 6 deletions api/handlers/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package handlers
import (
"net/http"

"fmt"
util "github.com/datatogether/api/apiutil"
"github.com/ipfs/go-datastore"
"github.com/qri-io/qri/core"
"github.com/qri-io/qri/logging"
"github.com/qri-io/qri/repo"
Expand All @@ -13,13 +13,14 @@ import (
// HistoryHandlers wraps a HistoryRequests with http.HandlerFuncs
type HistoryHandlers struct {
core.HistoryRequests
log logging.Logger
log logging.Logger
repo repo.Repo
}

// NewHistoryHandlers allocates a HistoryHandlers pointer
func NewHistoryHandlers(log logging.Logger, r repo.Repo) *HistoryHandlers {
req := core.NewHistoryRequests(r, nil)
h := HistoryHandlers{*req, log}
h := HistoryHandlers{*req, log, r}
return &h
}

Expand All @@ -36,14 +37,27 @@ func (h *HistoryHandlers) LogHandler(w http.ResponseWriter, r *http.Request) {
}

func (h *HistoryHandlers) logHandler(w http.ResponseWriter, r *http.Request) {
args, err := DatasetRefFromPath(r.URL.Path[len("/history/"):])
if err != nil {
util.WriteErrResponse(w, http.StatusBadRequest, err)
return
}

if args.Name == "" {
util.WriteErrResponse(w, http.StatusBadRequest, fmt.Errorf("name of dataset needed"))
return
}

lp := core.ListParamsFromRequest(r)
lp.Peername = args.Peername

params := &core.LogParams{
ListParams: core.ListParamsFromRequest(r),
Path: datastore.NewKey(r.URL.Path[len("/history/"):]),
ListParams: lp,
Name: args.Name,
}

res := []*repo.DatasetRef{}
if err := h.Log(params, &res); err != nil {
h.log.Infof("")
util.WriteErrResponse(w, http.StatusInternalServerError, err)
return
}
Expand Down
2 changes: 2 additions & 0 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ func NewServerRoutes(s *Server) *http.ServeMux {
m.Handle("/export/", s.middleware(dsh.ZipDatasetHandler))

hh := handlers.NewHistoryHandlers(s.log, s.qriNode.Repo)
// TODO - stupid hack for now.
hh.HistoryRequests.Node = s.qriNode
m.Handle("/history/", s.middleware(hh.LogHandler))

rh := handlers.NewRootHandler(dsh, ph)
Expand Down
4 changes: 4 additions & 0 deletions api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ func TestServerRoutes(t *testing.T) {
{"GET", "/list/madeupname", nil, 500},
{"GET", "/list/madeupname/datasetname", nil, 400},
// {"GET", "/list/[peername]", {}, {proper response}, 200}
{"OPTIONS", "/history/", nil, 200},
{"GET", "/history/", nil, 400},
// {"GET", "/history/me/[datasetname]", {}, {proper response}, 200},
// {"GET", "/history/me/[bad datasetname]", nil, 200},
}

client := &http.Client{}
Expand Down

0 comments on commit bb321de

Please sign in to comment.