Skip to content

Commit

Permalink
Merge branch 'beta/1.4.0' into cmd-master
Browse files Browse the repository at this point in the history
  • Loading branch information
nshumoogum committed Mar 21, 2018
2 parents 2788d0b + 453f18c commit df30620
Show file tree
Hide file tree
Showing 13 changed files with 287 additions and 197 deletions.
4 changes: 2 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ func routes(cfg config.Configuration, router *mux.Router, dataStore store.DataSt

api.router.HandleFunc("/healthcheck", api.healthCheck).Methods("GET")

versionPublishChecker := PublishCheck{Datastore: dataStore.Backend}
api.router.HandleFunc("/datasets", api.getDatasets).Methods("GET")
api.router.HandleFunc("/datasets/{id}", api.getDataset).Methods("GET")
api.router.HandleFunc("/datasets/{id}/editions", api.getEditions).Methods("GET")
Expand All @@ -95,8 +94,9 @@ func routes(cfg config.Configuration, router *mux.Router, dataStore store.DataSt

if cfg.EnablePrivateEnpoints {

log.Debug("private endpoints have been enabled", log.Data{})
log.Debug("private endpoints have been enabled", nil)

versionPublishChecker := PublishCheck{Datastore: dataStore.Backend}
api.router.HandleFunc("/datasets/{id}", api.privateAuth.Check(api.addDataset)).Methods("POST")
api.router.HandleFunc("/datasets/{id}", api.privateAuth.Check(api.putDataset)).Methods("PUT")
api.router.HandleFunc("/datasets/{id}", api.privateAuth.Check(api.deleteDataset)).Methods("DELETE")
Expand Down
147 changes: 104 additions & 43 deletions api/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ func (api *DatasetAPI) getDatasets(w http.ResponseWriter, r *http.Request) {
return
}

var bytes []byte
var b []byte
logData := log.Data{}

if api.EnablePrePublishView && r.Header.Get(internalToken) == api.internalToken {
logData["authenticated"] = true
datasets := &models.DatasetUpdateResults{}
datasets.Items = results
bytes, err = json.Marshal(datasets)
b, err = json.Marshal(datasets)
if err != nil {
log.ErrorC("failed to marshal dataset resource into bytes", err, nil)
handleErrorType(datasetDocType, err, w)
Expand All @@ -52,15 +52,15 @@ func (api *DatasetAPI) getDatasets(w http.ResponseWriter, r *http.Request) {

datasets.Items = mapResults(results)

bytes, err = json.Marshal(datasets)
b, err = json.Marshal(datasets)
if err != nil {
log.ErrorC("failed to marshal dataset resource into bytes", err, nil)
handleErrorType(datasetDocType, err, w)
return
}
}
setJSONContentType(w)
_, err = w.Write(bytes)
_, err = w.Write(b)
if err != nil {
log.Error(err, nil)
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand All @@ -79,7 +79,7 @@ func (api *DatasetAPI) getDataset(w http.ResponseWriter, r *http.Request) {
return
}

var bytes []byte
var b []byte
if !api.EnablePrePublishView || r.Header.Get(internalToken) != api.internalToken {
logData["authenticated"] = false
if dataset.Current == nil {
Expand All @@ -89,7 +89,7 @@ func (api *DatasetAPI) getDataset(w http.ResponseWriter, r *http.Request) {
}

dataset.Current.ID = dataset.ID
bytes, err = json.Marshal(dataset.Current)
b, err = json.Marshal(dataset.Current)
if err != nil {
log.ErrorC("failed to marshal dataset current sub document resource into bytes", err, logData)
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand All @@ -101,7 +101,7 @@ func (api *DatasetAPI) getDataset(w http.ResponseWriter, r *http.Request) {
log.Debug("published or unpublished dataset not found", nil)
handleErrorType(datasetDocType, errs.ErrDatasetNotFound, w)
}
bytes, err = json.Marshal(dataset)
b, err = json.Marshal(dataset)
if err != nil {
log.ErrorC("failed to marshal dataset current sub document resource into bytes", err, logData)
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand All @@ -110,7 +110,7 @@ func (api *DatasetAPI) getDataset(w http.ResponseWriter, r *http.Request) {
}

setJSONContentType(w)
_, err = w.Write(bytes)
_, err = w.Write(b)
if err != nil {
log.Error(err, logData)
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand All @@ -124,14 +124,18 @@ func (api *DatasetAPI) getEditions(w http.ResponseWriter, r *http.Request) {
logData := log.Data{"dataset_id": id}

var state string
var auth bool
if !api.EnablePrePublishView {
state = models.PublishedState
} else if r.Header.Get(internalToken) != api.internalToken {
logData["authenticated"] = false
logData["authenticated"] = auth
state = models.PublishedState
} else {
logData["authenticated"] = true
auth = true
logData["authenticated"] = auth
}
logData["state"] = state
log.Info("about to check resources exist", logData)

if err := api.dataStore.Backend.CheckDatasetExists(id, state); err != nil {
log.ErrorC("unable to find dataset", err, logData)
Expand All @@ -146,35 +150,61 @@ func (api *DatasetAPI) getEditions(w http.ResponseWriter, r *http.Request) {
return
}

bytes, err := json.Marshal(results)
if err != nil {
log.ErrorC("failed to marshal a list of edition resources into bytes", err, logData)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
var logMessage string
var b []byte

// If auth, we only return the .current document of editions (if a current doc exists then it's state is always published).
if auth {
b, err = json.Marshal(results)
if err != nil {
log.ErrorC("failed to marshal a list of edition resources into bytes", err, logData)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
logMessage = "get all editions with auth"

} else {

// flatten .current doc
var publicResults []*models.Edition
for i := range results.Items {
publicResults = append(publicResults, results.Items[i].Current)
}

b, err = json.Marshal(&models.EditionResults{Items: publicResults})
if err != nil {
log.ErrorC("failed to marshal a list of public edition resources into bytes", err, logData)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
logMessage = "get all editions without auth"
}

setJSONContentType(w)
_, err = w.Write(bytes)
_, err = w.Write(b)
if err != nil {
log.Error(err, logData)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
log.Debug("get all editions", logData)
log.Debug(logMessage, log.Data{"dataset_id": id})
}

func (api *DatasetAPI) getEdition(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
editionID := vars["edition"]
logData := log.Data{"dataset_id": id, "edition": editionID}

var state string
var auth bool
if !api.EnablePrePublishView {
state = models.PublishedState
} else if r.Header.Get(internalToken) != api.internalToken {
logData["authenticated"] = false
logData["authenticated"] = auth
state = models.PublishedState
} else {
logData["authenticated"] = true
auth = true
logData["authenticated"] = auth
}

if err := api.dataStore.Backend.CheckDatasetExists(id, state); err != nil {
Expand All @@ -190,20 +220,40 @@ func (api *DatasetAPI) getEdition(w http.ResponseWriter, r *http.Request) {
return
}

bytes, err := json.Marshal(edition)
if err != nil {
log.ErrorC("failed to marshal edition resource into bytes", err, logData)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
var logMessage string
var b []byte

// If auth, we only return the .current document of editions (if a current doc exists then it's state = published).
if auth {

// Edition requester has auth and gets everything
b, err = json.Marshal(edition)
if err != nil {
log.ErrorC("failed to marshal edition resource into bytes", err, logData)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
logMessage = "get edition with auth"

} else {

// User doesn't have auth so gets public edition response, (.current doc only).
b, err = json.Marshal(edition.Current)
if err != nil {
log.ErrorC("failed to marshal public edition resource into bytes", err, logData)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
logMessage = "get public edition without auth"
}

setJSONContentType(w)
_, err = w.Write(bytes)
_, err = w.Write(b)
if err != nil {
log.Error(err, log.Data{"dataset_id": id, "edition": editionID})
log.Error(err, logData)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
log.Debug("get edition", log.Data{"dataset_id": id, "edition": editionID})
log.Debug(logMessage, logData)
}

func (api *DatasetAPI) getVersions(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -269,15 +319,15 @@ func (api *DatasetAPI) getVersions(w http.ResponseWriter, r *http.Request) {
return
}

bytes, err := json.Marshal(results)
b, err := json.Marshal(results)
if err != nil {
log.ErrorC("failed to marshal list of version resources into bytes", err, logData)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

setJSONContentType(w)
_, err = w.Write(bytes)
_, err = w.Write(b)
if err != nil {
log.Error(err, log.Data{"dataset_id": id, "edition": editionID})
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down Expand Up @@ -344,15 +394,15 @@ func (api *DatasetAPI) getVersion(w http.ResponseWriter, r *http.Request) {
}
}

bytes, err := json.Marshal(results)
b, err := json.Marshal(results)
if err != nil {
log.ErrorC("failed to marshal version resource into bytes", err, logData)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

setJSONContentType(w)
_, err = w.Write(bytes)
_, err = w.Write(b)
if err != nil {
log.Error(err, logData)
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down Expand Up @@ -414,7 +464,7 @@ func (api *DatasetAPI) addDataset(w http.ResponseWriter, r *http.Request) {
return
}

bytes, err := json.Marshal(datasetDoc)
b, err := json.Marshal(datasetDoc)
if err != nil {
log.ErrorC("failed to marshal dataset resource into bytes", err, log.Data{"new_dataset": datasetID})
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -423,7 +473,7 @@ func (api *DatasetAPI) addDataset(w http.ResponseWriter, r *http.Request) {

setJSONContentType(w)
w.WriteHeader(http.StatusCreated)
_, err = w.Write(bytes)
_, err = w.Write(b)
if err != nil {
log.Error(err, log.Data{"dataset_id": datasetID})
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down Expand Up @@ -520,8 +570,19 @@ func (api *DatasetAPI) putVersion(w http.ResponseWriter, r *http.Request) {
}

if versionDoc.State == models.PublishedState {
if err := api.dataStore.Backend.UpdateEdition(datasetID, edition, versionDoc); err != nil {
log.ErrorC("failed to update the state of edition document to published", err, log.Data{"dataset_id": datasetID, "edition": edition, "version": version})

editionDoc, err := api.dataStore.Backend.GetEdition(datasetID, edition, "")
if err != nil {
log.ErrorC("failed to find the edition we're trying to update", err, log.Data{"dataset_id": datasetID, "edition": edition, "version": version})
handleErrorType(versionDocType, err, w)
return
}

editionDoc.Next.State = models.PublishedState
editionDoc.Current = editionDoc.Next

if err := api.dataStore.Backend.UpsertEdition(datasetID, edition, editionDoc); err != nil {
log.ErrorC("failed to update edition during publishing", err, log.Data{"dataset_id": datasetID, "edition": edition, "version": version})
handleErrorType(versionDocType, err, w)
return
}
Expand Down Expand Up @@ -746,15 +807,15 @@ func (api *DatasetAPI) getDimensions(w http.ResponseWriter, r *http.Request) {

listOfDimensions := &models.DatasetDimensionResults{Items: results}

bytes, err := json.Marshal(listOfDimensions)
b, err := json.Marshal(listOfDimensions)
if err != nil {
log.ErrorC("failed to marshal list of dimension resources into bytes", err, logData)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

setJSONContentType(w)
_, err = w.Write(bytes)
_, err = w.Write(b)
if err != nil {
log.Error(err, logData)
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down Expand Up @@ -798,12 +859,12 @@ func (api *DatasetAPI) createListOfDimensions(versionDoc *models.Version, dimens

func convertBSONToDimensionOption(data interface{}) (*models.DimensionOption, error) {
var dim models.DimensionOption
bytes, err := bson.Marshal(data)
b, err := bson.Marshal(data)
if err != nil {
return nil, err
}

bson.Unmarshal(bytes, &dim)
bson.Unmarshal(b, &dim)

return &dim, nil
}
Expand Down Expand Up @@ -846,15 +907,15 @@ func (api *DatasetAPI) getDimensionOptions(w http.ResponseWriter, r *http.Reques
return
}

bytes, err := json.Marshal(results)
b, err := json.Marshal(results)
if err != nil {
log.ErrorC("failed to marshal list of dimension option resources into bytes", err, logData)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

setJSONContentType(w)
_, err = w.Write(bytes)
_, err = w.Write(b)
if err != nil {
log.Error(err, logData)
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down Expand Up @@ -923,15 +984,15 @@ func (api *DatasetAPI) getMetadata(w http.ResponseWriter, r *http.Request) {
metaDataDoc = models.CreateMetaDataDoc(datasetDoc.Current, versionDoc, api.urlBuilder)
}

bytes, err := json.Marshal(metaDataDoc)
b, err := json.Marshal(metaDataDoc)
if err != nil {
log.ErrorC("failed to marshal metadata resource into bytes", err, logData)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

setJSONContentType(w)
_, err = w.Write(bytes)
_, err = w.Write(b)
if err != nil {
log.Error(err, logData)
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down
Loading

0 comments on commit df30620

Please sign in to comment.