Skip to content

Commit

Permalink
feat(api): add ETDatasetDownload when a user hits certain "get" end…
Browse files Browse the repository at this point in the history
…points

The frontend considers using the "http://localhost:2503/ds/get/username/dataset_name/body", "http://localhost:2503/ds/get/username/dataset_name/body.csv" and "http://localhost:2503/ds/get/username/dataset_name?format=zip" endpoints as performing a "dataset download". These events need to be tracked in the collection, so we emit the `ETDatasetDownload` event for each circumstance.
  • Loading branch information
ramfox committed Sep 2, 2021
1 parent 75c0919 commit 43eb8b8
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions api/handlers.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package api

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

"github.com/qri-io/qri/api/util"
"github.com/qri-io/qri/base/archive"
"github.com/qri-io/qri/event"
"github.com/qri-io/qri/lib"
)

Expand Down Expand Up @@ -38,6 +40,7 @@ func GetBodyCSVHandler(inst *lib.Instance) http.HandlerFunc {
util.RespondWithError(w, err)
return
}
publishDownloadEvent(r.Context(), inst, p.Ref)
writeFileResponse(w, outBytes, "body.csv", "csv")
}
}
Expand Down Expand Up @@ -71,6 +74,8 @@ func GetHandler(inst *lib.Instance, routePrefix string) http.HandlerFunc {
util.RespondWithError(w, err)
return
}

publishDownloadEvent(r.Context(), inst, p.Ref)
writeFileResponse(w, outBytes, "body.csv", "csv")
return

Expand All @@ -87,6 +92,7 @@ func GetHandler(inst *lib.Instance, routePrefix string) http.HandlerFunc {
util.RespondWithError(w, err)
return
}
publishDownloadEvent(r.Context(), inst, p.Ref)
writeFileResponse(w, zipResults.Bytes, zipResults.GeneratedName, "zip")
return

Expand Down Expand Up @@ -192,3 +198,12 @@ func arrayContains(subject []string, target string) bool {
}
return false
}

func publishDownloadEvent(ctx context.Context, inst *lib.Instance, refStr string) {
ref, _, err := inst.ParseAndResolveRef(ctx, refStr, "local")
if err != nil {
log.Debugw("api.GetBodyCSVHandler - unable to resolve ref %q", err)
return
}
inst.Bus().Publish(ctx, event.ETDatasetDownload, ref.InitID)
}

0 comments on commit 43eb8b8

Please sign in to comment.