Skip to content

Commit

Permalink
feat(api dry run): add dry run & moar file options to /new
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 committed Sep 21, 2018
1 parent ef2d5ca commit 52cfa19
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
9 changes: 6 additions & 3 deletions actions/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,12 @@ func CreateDataset(node *p2p.QriNode, name string, ds *dataset.Dataset, data caf
r.LogEvent(repo.ETDsPinned, ref)
}

err = ReadDataset(r, &ref)
if err != nil {
body, err = r.Store().Get(datastore.NewKey(ref.Dataset.BodyPath))
if err = ReadDataset(r, &ref); err != nil {
return
}

if body, err = r.Store().Get(datastore.NewKey(ref.Dataset.BodyPath)); err != nil {
fmt.Println("error getting from store:", err.Error())
}

return
Expand Down
66 changes: 58 additions & 8 deletions api/datasets.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"bytes"
"encoding/json"
"errors"
"fmt"
Expand All @@ -11,6 +12,8 @@ import (
"path/filepath"
"strings"

"github.com/qri-io/ioes"

util "github.com/datatogether/api/apiutil"
"github.com/qri-io/dataset"
"github.com/qri-io/dataset/dsutil"
Expand All @@ -24,14 +27,15 @@ import (
// DatasetHandlers wraps a requests struct to interface with http.HandlerFunc
type DatasetHandlers struct {
lib.DatasetRequests
node *p2p.QriNode
repo repo.Repo
ReadOnly bool
}

// NewDatasetHandlers allocates a DatasetHandlers pointer
func NewDatasetHandlers(node *p2p.QriNode, readOnly bool) *DatasetHandlers {
req := lib.NewDatasetRequests(node, nil)
h := DatasetHandlers{*req, node.Repo, readOnly}
h := DatasetHandlers{*req, node, node.Repo, readOnly}
return &h
}

Expand Down Expand Up @@ -392,11 +396,31 @@ func (h *DatasetHandlers) initHandler(w http.ResponseWriter, r *http.Request) {
defer os.Remove(f.Name())
io.Copy(f, tfFile)
if dsp.Transform == nil {
dsp.Transform = &dataset.TransformPod{
Syntax: "skylark",
ScriptPath: f.Name(),
}
dsp.Transform = &dataset.TransformPod{}
}
dsp.Transform.Syntax = "skylark"
dsp.Transform.ScriptPath = f.Name()
}

vizFile, _, err := r.FormFile("viz")
if err != nil && err != http.ErrMissingFile {
util.WriteErrResponse(w, http.StatusBadRequest, fmt.Errorf("error opening viz file: %s", err))
return
}
if vizFile != nil {
// TODO - this assumes an html viz file
f, err := ioutil.TempFile("", "viz")
if err != nil {
util.WriteErrResponse(w, http.StatusBadRequest, err)
return
}
defer os.Remove(f.Name())
io.Copy(f, vizFile)
if dsp.Viz == nil {
dsp.Viz = &dataset.Viz{}
}
dsp.Viz.Format = "html"
dsp.Viz.ScriptPath = f.Name()
}

dsp.Peername = r.FormValue("peername")
Expand All @@ -423,6 +447,18 @@ func (h *DatasetHandlers) initHandler(w http.ResponseWriter, r *http.Request) {

}

// TODO - fix this awful mess, ioes needs some method for piping it's output
prev := h.node.LocalStreams
defer func() {
h.node.LocalStreams = prev
}()

in := &bytes.Buffer{}
out := &bytes.Buffer{}
errOut := &bytes.Buffer{}
s := ioes.NewIOStreams(in, out, errOut)
h.node.LocalStreams = s

res := &repo.DatasetRef{}
p := &lib.SaveParams{
Dataset: dsp,
Expand All @@ -435,16 +471,30 @@ func (h *DatasetHandlers) initHandler(w http.ResponseWriter, r *http.Request) {
util.WriteErrResponse(w, http.StatusInternalServerError, err)
return
}

if p.ReturnBody {
// TODO - this'll only work for JSON responses
data := []byte{}
if err := json.NewDecoder(res.Dataset.Body.(io.Reader)).Decode(&data); err != nil {
data, err := ioutil.ReadAll(res.Dataset.Body.(io.Reader))
if err != nil {
log.Info(err.Error())
util.WriteErrResponse(w, http.StatusInternalServerError, err)
return
}

res.Dataset.Body = json.RawMessage(data)
}
util.WriteResponse(w, res.Dataset)

// util.WriteResponse(w, res)
env := map[string]interface{}{
"meta": map[string]interface{}{
"code": http.StatusOK,
"message": string(out.Bytes()),
},
"data": res.Dataset,
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(env)
}

func (h *DatasetHandlers) addHandler(w http.ResponseWriter, r *http.Request) {
Expand Down
Binary file modified api/testdata/api.snapshot
Binary file not shown.

0 comments on commit 52cfa19

Please sign in to comment.