Skip to content

Commit

Permalink
fix(api*): update history, add, remove, info to work with hashes
Browse files Browse the repository at this point in the history
as outlined in #222
  • Loading branch information
ramfox committed Mar 8, 2018
1 parent bf12ac4 commit 60f79fd
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
14 changes: 8 additions & 6 deletions api/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (h *DatasetHandlers) ZipDatasetHandler(w http.ResponseWriter, r *http.Reque
}

func (h *DatasetHandlers) zipDatasetHandler(w http.ResponseWriter, r *http.Request) {
args, err := DatasetRefFromPath(r.URL.Path[len("/export/"):])
args, err := DatasetRefFromPath(r.URL.Path[len("/export"):])
if err != nil {
util.WriteErrResponse(w, http.StatusBadRequest, err)
return
Expand Down Expand Up @@ -208,10 +208,12 @@ func (h *DatasetHandlers) getHandler(w http.ResponseWriter, r *http.Request) {
util.WriteErrResponse(w, http.StatusBadRequest, err)
return
}
if args.Name == "" {
util.WriteErrResponse(w, http.StatusBadRequest, errors.New("no dataset name or hash given"))

if err = repo.CanonicalizeDatasetRef(h.repo, &args); err != nil {
util.WriteErrResponse(w, http.StatusInternalServerError, err)
return
}

err = h.Get(&args, res)
if err != nil {
util.WriteErrResponse(w, http.StatusInternalServerError, err)
Expand Down Expand Up @@ -382,7 +384,7 @@ func (h *DatasetHandlers) initHandler(w http.ResponseWriter, r *http.Request) {
}

func (h *DatasetHandlers) addHandler(w http.ResponseWriter, r *http.Request) {
ref, err := DatasetRefFromPath(r.URL.Path[len("/add/"):])
ref, err := DatasetRefFromPath(r.URL.Path[len("/add"):])
if err != nil {
util.WriteErrResponse(w, http.StatusBadRequest, err)
return
Expand Down Expand Up @@ -507,7 +509,7 @@ func (h *DatasetHandlers) saveHandler(w http.ResponseWriter, r *http.Request) {
}

func (h *DatasetHandlers) removeHandler(w http.ResponseWriter, r *http.Request) {
p, err := DatasetRefFromPath(r.URL.Path[len("/remove/"):])
p, err := DatasetRefFromPath(r.URL.Path[len("/remove"):])
if err != nil {
util.WriteErrResponse(w, http.StatusBadRequest, err)
return
Expand All @@ -525,7 +527,7 @@ func (h *DatasetHandlers) removeHandler(w http.ResponseWriter, r *http.Request)
return
}

util.WriteResponse(w, ref.Dataset)
util.WriteResponse(w, ref)
}

// RenameReqParams is an encoding struct
Expand Down
6 changes: 3 additions & 3 deletions api/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ 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/"):])
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"))
if args.Name == "" && args.Path == "" {
util.WriteErrResponse(w, http.StatusBadRequest, fmt.Errorf("name of dataset or path needed"))
return
}

Expand Down
6 changes: 2 additions & 4 deletions api/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,13 @@ func (mh *RootHandler) Handler(w http.ResponseWriter, r *http.Request) {
}

res := repo.DatasetRef{}

err := mh.dsh.Get(&ref, &res)
if err != nil {
util.WriteErrResponse(w, http.StatusInternalServerError, err)
return
}
if res.Name == "" {
util.WriteErrResponse(w, http.StatusNotFound, errors.New("cannot find peer dataset"))
return
}

if res.IsEmpty() {
util.WriteErrResponse(w, http.StatusNotFound, errors.New("cannot find peer dataset"))
return
Expand Down
23 changes: 17 additions & 6 deletions core/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,11 +590,15 @@ func (r *DatasetRequests) Remove(p *repo.DatasetRef, ok *bool) (err error) {
return fmt.Errorf("either peername/name or path is required")
}

*p, err = r.repo.GetRef(*p)
ref, err := r.repo.GetRef(*p)
if err != nil {
return
}

if ref.Path != p.Path {
return fmt.Errorf("given path does not equal most recent dataset path: cannot delete a specific save, can only delete entire dataset history. use `me/dataset_name` to delete entire dataset")
}

if pinner, ok := r.repo.Store().(cafs.Pinner); ok {
// path := datastore.NewKey(strings.TrimSuffix(p.Path, "/"+dsfs.PackageFileDataset.String()))
if err = pinner.Unpin(datastore.NewKey(p.Path), true); err != nil {
Expand Down Expand Up @@ -735,6 +739,15 @@ func (r *DatasetRequests) Add(ref *repo.DatasetRef, res *repo.DatasetRef) (err e
}

path := datastore.NewKey(key.String() + "/" + dsfs.PackageFileDataset.String())

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

ref.Peername = profile.Peername
ref.PeerID = profile.ID

err = r.repo.PutRef(*ref)
if err != nil {
return fmt.Errorf("error putting dataset name in repo: %s", err.Error())
Expand All @@ -745,11 +758,9 @@ func (r *DatasetRequests) Add(ref *repo.DatasetRef, res *repo.DatasetRef) (err e
return fmt.Errorf("error loading newly saved dataset path: %s", path.String())
}

*res = repo.DatasetRef{
Name: ref.Name,
Path: path.String(),
Dataset: ds,
}
ref.Dataset = ds

*res = *ref
return
}

Expand Down
3 changes: 1 addition & 2 deletions core/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,11 @@ func (d *HistoryRequests) Log(params *LogParams, res *[]repo.DatasetRef) (err er
return err
}

got, err := d.repo.GetRef(ref)
_, err = d.repo.GetRef(ref)
if err != nil {
err = fmt.Errorf("error getting reference '%s': %s", ref, err.Error())
return getRemote(err)
}
ref = got

log := []repo.DatasetRef{}
limit := params.Limit
Expand Down

0 comments on commit 60f79fd

Please sign in to comment.