Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
sandypadmanabhan committed May 27, 2021
1 parent 88a9013 commit e7eaca9
Show file tree
Hide file tree
Showing 4 changed files with 326 additions and 91 deletions.
18 changes: 4 additions & 14 deletions api/dimensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"net/http"
"sort"
"strconv"

"github.com/ONSdigital/dp-dataset-api/apierrors"
errs "github.com/ONSdigital/dp-dataset-api/apierrors"
Expand Down Expand Up @@ -33,14 +32,10 @@ func (api *DatasetAPI) getDimensions(w http.ResponseWriter, r *http.Request, lim
logData := log.Data{"dataset_id": datasetID, "edition": edition, "version": version, "func": "getDimensions"}
var err error

versionId, err := strconv.Atoi(version)
versionId, err := checkVersion(ctx, version)
if err != nil {
log.Event(ctx, "invalid version request", log.ERROR, log.Error(err), logData)
return nil, 0, errs.ErrInvalidVersion
}
if !(versionId > 0) {
log.Event(ctx, "version is not a positive integer", log.ERROR, log.Error(err), logData)
return nil, 0, errs.ErrInvalidVersion
return nil, 0, err
}

list, totalCount, err := func() ([]models.Dimension, int, error) {
Expand Down Expand Up @@ -152,15 +147,10 @@ func (api *DatasetAPI) getDimensionOptions(w http.ResponseWriter, r *http.Reques
logData := log.Data{"dataset_id": datasetID, "edition": edition, "version": versionID, "dimension": dimension, "func": "getDimensionOptions"}
authorised := api.authenticate(r, logData)

versionId, err := strconv.Atoi(versionID)
versionId, err := checkVersion(ctx, versionID)
if err != nil {
log.Event(ctx, "invalid version requested", log.ERROR, log.Error(err), logData)
handleDimensionsErr(ctx, w, "invalid version", errs.ErrInvalidVersion, logData)
return nil, 0, err
}
if !(versionId > 0) {
log.Event(ctx, "version is not a positive integer", log.ERROR, log.Error(err), logData)
handleDimensionsErr(ctx, w, "failed due to version not a positive integer", errs.ErrInvalidVersion, logData)
handleDimensionsErr(ctx, w, "invalid version", err, logData)
return nil, 0, err
}

Expand Down
11 changes: 3 additions & 8 deletions api/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package api
import (
"encoding/json"
"net/http"
"strconv"

errs "github.com/ONSdigital/dp-dataset-api/apierrors"
"github.com/ONSdigital/dp-dataset-api/models"
Expand All @@ -21,15 +20,11 @@ func (api *DatasetAPI) getMetadata(w http.ResponseWriter, r *http.Request) {
logData := log.Data{"dataset_id": datasetID, "edition": edition, "version": version}

b, err := func() ([]byte, error) {
versionId, err := strconv.Atoi(version)

versionId, err := checkVersion(ctx, version)
if err != nil {
log.Event(ctx, "failed due to invalid version request", log.ERROR, log.Error(err), logData)
return nil, errs.ErrInvalidVersion
}
if !(versionId > 0) {
log.Event(ctx, "version is not a positive integer", log.ERROR, log.Error(err), logData)
return nil, errs.ErrInvalidVersion

return nil, err
}

versionDoc, err := api.dataStore.Backend.GetVersion(datasetID, edition, versionId, "")
Expand Down
42 changes: 22 additions & 20 deletions api/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,10 @@ func (api *DatasetAPI) getVersion(w http.ResponseWriter, r *http.Request) {
b, getVersionErr := func() ([]byte, error) {
authorised := api.authenticate(r, logData)

versionId, err := strconv.Atoi(version)
versionId, err := checkVersion(ctx, version)
if err != nil {
log.Event(ctx, "invalid version provided", log.ERROR, log.Error(err), logData)
return nil, errs.ErrInvalidVersion
}
if !(versionId > 0) {
log.Event(ctx, "version is not a positive integer", log.ERROR, log.Error(err), logData)
return nil, errs.ErrInvalidVersion
log.Event(ctx, "getVersion endpoint: invalid version", log.ERROR, log.Error(err), logData)
return nil, err
}

var state string
Expand Down Expand Up @@ -289,15 +285,12 @@ func (api *DatasetAPI) detachVersion(w http.ResponseWriter, r *http.Request) {
return errs.ErrNotFound
}

versionId, err := strconv.Atoi(version)
versionId, err := checkVersion(ctx, version)
if err != nil {
log.Event(ctx, "detachVersion endpoint: invalid version request", log.ERROR, log.Error(err), logData)
return errs.ErrInvalidVersion
}
if !(versionId > 0) {
log.Event(ctx, "detachVersion endpoint: version is not a positive integer", log.ERROR, log.Error(err), logData)
return errs.ErrInvalidVersion
return err
}

editionDoc, err := api.dataStore.Backend.GetEdition(datasetID, edition, "")
if err != nil {
log.Event(ctx, "detachVersion endpoint: Cannot find the specified edition", log.ERROR, log.Error(errs.ErrEditionNotFound), logData)
Expand Down Expand Up @@ -382,14 +375,10 @@ func (api *DatasetAPI) updateVersion(ctx context.Context, body io.ReadCloser, ve
return nil, nil, nil, err
}

version, err := strconv.Atoi(versionDetails.version)
version, err := checkVersion(ctx, versionDetails.version)
if err != nil {
log.Event(ctx, "putVersion endpoint: invalid version request", log.ERROR, log.Error(err), data)
return nil, nil, nil, errs.ErrInvalidVersion
}
if !(version > 0) {
log.Event(ctx, "putVersion endpoint: version is not a positive integer", log.ERROR, log.Error(err), data)
return nil, nil, nil, errs.ErrInvalidVersion
return nil, nil, nil, err
}

if err = api.dataStore.Backend.CheckEditionExists(versionDetails.datasetID, versionDetails.edition, ""); err != nil {
Expand Down Expand Up @@ -653,7 +642,7 @@ func handleVersionAPIErr(ctx context.Context, err error, w http.ResponseWriter,
status = http.StatusBadRequest
case strings.HasPrefix(err.Error(), "invalid fields:"):
status = http.StatusBadRequest
case strings.HasPrefix(err.Error(), "strconv.Atoi"):
case strings.HasPrefix(err.Error(), "invalid version requested"):
status = http.StatusBadRequest
default:
err = errs.ErrInternalServer
Expand All @@ -667,3 +656,16 @@ func handleVersionAPIErr(ctx context.Context, err error, w http.ResponseWriter,
log.Event(ctx, "request unsuccessful", log.ERROR, log.Error(err), data)
http.Error(w, err.Error(), status)
}

func checkVersion(ctx context.Context, version string) (int, error) {
versionId, err := strconv.Atoi(version)
if !(versionId > 0) {
log.Event(ctx, "version is not a positive integer", log.ERROR, log.Error(errs.ErrInvalidVersion), log.Data{"version": version})
return versionId, errs.ErrInvalidVersion
}
if err != nil {
log.Event(ctx, "invalid version provided", log.ERROR, log.Error(err), log.Data{"version": version})
return versionId, errs.ErrInvalidVersion
}
return versionId, nil
}
Loading

0 comments on commit e7eaca9

Please sign in to comment.