Skip to content

Commit

Permalink
feat(api /publish): add publish API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 committed Oct 29, 2018
1 parent 327f697 commit 3d1b4bf
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ func NewServerRoutes(s *Server) *http.ServeMux {
m.Handle("/diff", s.middleware(dsh.DiffHandler))
m.Handle("/body/", s.middleware(dsh.BodyHandler))
m.Handle("/unpack/", s.middleware(dsh.UnpackHandler))
m.Handle("/publish/", s.middleware(dsh.PublishHandler))

renderh := NewRenderHandlers(s.qriNode.Repo)
m.Handle("/render/", s.middleware(renderh.RenderHandler))
Expand Down
7 changes: 7 additions & 0 deletions api/dataset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ func TestDatasetHandlers(t *testing.T) {
}
runHandlerTestCases(t, "export", h.ZipDatasetHandler, exportCases)

publishCases := []handlerTestCase{
{"OPTIONS", "/", nil},
{"POST", "/publish/me/cities", nil},
{"DELETE", "/publish/me/cities", nil},
}
runHandlerTestCases(t, "publish", h.PublishHandler, publishCases)

// TODO: Perhaps add an option to runHandlerTestCases to set Content-Type, then combine
// `runHandlerZipPostTestCases` with `runHandlerTestCases`.
unpackCases := []handlerTestCase{
Expand Down
32 changes: 32 additions & 0 deletions api/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,22 @@ func (h *DatasetHandlers) UnpackHandler(w http.ResponseWriter, r *http.Request)
}
}

// PublishHandler works with dataset publicity
func (h *DatasetHandlers) PublishHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "OPTIONS":
util.EmptyOkHandler(w, r)
case "GET":
// TODO - this should list published datasets
case "POST":
h.publishHandler(w, r, true)
case "DELETE":
h.publishHandler(w, r, false)
default:
util.NotFoundHandler(w, r)
}
}

// ZipDatasetHandler is the endpoint for getting a zip archive of a dataset
func (h *DatasetHandlers) ZipDatasetHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
Expand Down Expand Up @@ -705,6 +721,22 @@ func (h DatasetHandlers) bodyHandler(w http.ResponseWriter, r *http.Request) {
}
}

func (h DatasetHandlers) publishHandler(w http.ResponseWriter, r *http.Request, publish bool) {
ref, err := DatasetRefFromPath(r.URL.Path[len("/publish"):])
if err != nil {
util.WriteErrResponse(w, http.StatusBadRequest, err)
return
}

ref.Published = publish
var ok bool
if err := h.DatasetRequests.SetPublishStatus(&ref, &ok); err != nil {
util.WriteErrResponse(w, http.StatusInternalServerError, err)
return
}
util.WriteResponse(w, ref)
}

func (h DatasetHandlers) unpackHandler(w http.ResponseWriter, r *http.Request, postData []byte) {
zr, err := zip.NewReader(bytes.NewReader(postData), int64(len(postData)))
if err != nil {
Expand Down
Binary file modified api/testdata/api.snapshot
Binary file not shown.
1 change: 1 addition & 0 deletions repo/fs/refstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (n Refstore) PutRef(p repo.DatasetRef) (err error) {
}
}

// TODO - move this up into base package
if n.index != nil {
batch := n.index.NewBatch()
err = batch.Index(p.Path, ds)
Expand Down

0 comments on commit 3d1b4bf

Please sign in to comment.