Skip to content

Commit

Permalink
Use function from library
Browse files Browse the repository at this point in the history
  • Loading branch information
rafahop committed Mar 23, 2023
1 parent 7e765a6 commit 79991e2
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 24 deletions.
3 changes: 2 additions & 1 deletion api/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ONSdigital/dp-api-clients-go/v2/headers"
errs "github.com/ONSdigital/dp-dataset-api/apierrors"
"github.com/ONSdigital/dp-dataset-api/models"
dpresponse "github.com/ONSdigital/dp-net/v2/handlers/response"
dphttp "github.com/ONSdigital/dp-net/v2/http"
dprequest "github.com/ONSdigital/dp-net/v2/request"
"github.com/ONSdigital/log.go/v2/log"
Expand Down Expand Up @@ -205,7 +206,7 @@ func (api *DatasetAPI) getVersion(w http.ResponseWriter, r *http.Request) {

setJSONContentType(w)
if v.ETag != "" {
w.Header().Add("Etag", v.ETag)
dpresponse.SetETag(w, v.ETag)
}

versionBytes, err := json.Marshal(v)
Expand Down
13 changes: 7 additions & 6 deletions dimension/dimension.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ONSdigital/dp-dataset-api/models"
"github.com/ONSdigital/dp-dataset-api/store"
"github.com/ONSdigital/dp-dataset-api/utils"
dpresponse "github.com/ONSdigital/dp-net/v2/handlers/response"
dphttp "github.com/ONSdigital/dp-net/v2/http"
dprequest "github.com/ONSdigital/dp-net/v2/request"
"github.com/ONSdigital/log.go/v2/log"
Expand Down Expand Up @@ -75,7 +76,7 @@ func (s *Store) GetDimensionsHandler(w http.ResponseWriter, r *http.Request, lim
}

log.Info(ctx, "successfully get dimensions for an instance resource", logData)
setETag(w, instance.ETag)
dpresponse.SetETag(w, instance.ETag)
return dimensions, totalCount, nil
}

Expand Down Expand Up @@ -129,7 +130,7 @@ func (s *Store) GetUniqueDimensionAndOptionsHandler(w http.ResponseWriter, r *ht
}

log.Info(ctx, "successfully get unique dimension options for an instance resource", logData)
setETag(w, instance.ETag)
dpresponse.SetETag(w, instance.ETag)
return slicedOptions, totalCount, nil
}

Expand Down Expand Up @@ -168,7 +169,7 @@ func (s *Store) AddHandler(w http.ResponseWriter, r *http.Request) {
}
log.Info(ctx, "added dimension to instance resource", logData)

setETag(w, newETag)
dpresponse.SetETag(w, newETag)
}

// PatchDimensionsHandler represents adding multiple dimensions to a specific instance
Expand Down Expand Up @@ -208,7 +209,7 @@ func (s *Store) PatchDimensionsHandler(w http.ResponseWriter, r *http.Request) {

// set content type and write response body
setJSONPatchContentType(w)
setETag(w, newETag)
dpresponse.SetETag(w, newETag)
writeBody(ctx, w, b, logData)
log.Info(ctx, "successfully patched dimensions of an instance resource", logData)
}
Expand Down Expand Up @@ -433,7 +434,7 @@ func (s *Store) PatchOptionHandler(w http.ResponseWriter, r *http.Request) {

// set content type and write response body
setJSONPatchContentType(w)
setETag(w, newETag)
dpresponse.SetETag(w, newETag)
writeBody(ctx, w, b, logData)
log.Info(ctx, "successfully patched dimension option of an instance resource", logData)
}
Expand Down Expand Up @@ -511,5 +512,5 @@ func (s *Store) AddNodeIDHandler(w http.ResponseWriter, r *http.Request) {

logData["action"] = AddDimensionAction
log.Info(ctx, "added node id to dimension of an instance resource", logData)
setETag(w, newETag)
dpresponse.SetETag(w, newETag)
}
4 changes: 0 additions & 4 deletions dimension/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,6 @@ func setJSONPatchContentType(w http.ResponseWriter) {
w.Header().Add("Content-Type", "application/json-patch+json")
}

func setETag(w http.ResponseWriter, eTag string) {
w.Header().Add("ETag", eTag)
}

func writeBody(ctx context.Context, w http.ResponseWriter, b []byte, data log.Data) {
if _, err := w.Write(b); err != nil {
log.Error(ctx, "failed to write response body", err, data)
Expand Down
3 changes: 2 additions & 1 deletion instance/dimensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

errs "github.com/ONSdigital/dp-dataset-api/apierrors"
"github.com/ONSdigital/dp-dataset-api/models"
dpresponse "github.com/ONSdigital/dp-net/v2/handlers/response"
dphttp "github.com/ONSdigital/dp-net/v2/http"
"github.com/ONSdigital/log.go/v2/log"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -107,5 +108,5 @@ func (s *Store) UpdateDimension(w http.ResponseWriter, r *http.Request) {

log.Info(ctx, "updated instance dimension: request successful", logData)

setETag(w, newETag)
dpresponse.SetETag(w, newETag)
}
3 changes: 2 additions & 1 deletion instance/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

errs "github.com/ONSdigital/dp-dataset-api/apierrors"
"github.com/ONSdigital/dp-dataset-api/models"
dpresponse "github.com/ONSdigital/dp-net/v2/handlers/response"
"github.com/ONSdigital/log.go/v2/log"
"github.com/gorilla/mux"
)
Expand Down Expand Up @@ -73,5 +74,5 @@ func (s *Store) AddEvent(w http.ResponseWriter, r *http.Request) {
}

log.Info(ctx, "add instance event: request successful", data)
setETag(w, newETag)
dpresponse.SetETag(w, newETag)
}
5 changes: 3 additions & 2 deletions instance/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

errs "github.com/ONSdigital/dp-dataset-api/apierrors"
"github.com/ONSdigital/dp-dataset-api/models"
dpresponse "github.com/ONSdigital/dp-net/v2/handlers/response"
dphttp "github.com/ONSdigital/dp-net/v2/http"
"github.com/ONSdigital/log.go/v2/log"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -56,7 +57,7 @@ func (s *Store) UpdateObservations(w http.ResponseWriter, r *http.Request) {
}

log.Info(ctx, "update imported observations: request successful", logData)
setETag(w, newETag)
dpresponse.SetETag(w, newETag)
}

// UpdateImportTask updates any task in the request body against an instance
Expand Down Expand Up @@ -190,7 +191,7 @@ func (s *Store) UpdateImportTask(w http.ResponseWriter, r *http.Request) {
}

log.Info(ctx, "updateImportTask endpoint: request successful", logData)
setETag(w, eTag)
dpresponse.SetETag(w, eTag)
}

func unmarshalImportTasks(reader io.Reader) (*models.InstanceImportTasks, error) {
Expand Down
15 changes: 6 additions & 9 deletions instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import (
"github.com/ONSdigital/dp-dataset-api/models"
"github.com/ONSdigital/dp-dataset-api/mongo"
"github.com/ONSdigital/dp-dataset-api/store"
dpresponse "github.com/ONSdigital/dp-net/v2/handlers/response"
dphttp "github.com/ONSdigital/dp-net/v2/http"
"github.com/ONSdigital/log.go/v2/log"
"github.com/gorilla/mux"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
)

//Store provides a backend for instances
// Store provides a backend for instances
type Store struct {
store.Storer
Host string
Expand All @@ -40,7 +41,7 @@ func (e taskError) Error() string {
return ""
}

//GetList returns a list of instances, the total count of instances that match the query parameters and an error
// GetList returns a list of instances, the total count of instances that match the query parameters and an error
func (s *Store) GetList(w http.ResponseWriter, r *http.Request, limit int, offset int) (interface{}, int, error) {
ctx := r.Context()
stateFilterQuery := r.URL.Query().Get("state")
Expand Down Expand Up @@ -125,7 +126,7 @@ func (s *Store) Get(w http.ResponseWriter, r *http.Request) {
}

setJSONContentType(w)
setETag(w, instance.ETag)
dpresponse.SetETag(w, instance.ETag)
writeBody(ctx, w, b, logData)
log.Info(ctx, "get instance: request successful", logData)
}
Expand Down Expand Up @@ -169,7 +170,7 @@ func (s *Store) Add(w http.ResponseWriter, r *http.Request) {
}

setJSONContentType(w)
setETag(w, instance.ETag)
dpresponse.SetETag(w, instance.ETag)
w.WriteHeader(http.StatusCreated)
writeBody(ctx, w, b, logData)

Expand Down Expand Up @@ -305,7 +306,7 @@ func (s *Store) Update(w http.ResponseWriter, r *http.Request) {
}

setJSONContentType(w)
setETag(w, newETag)
dpresponse.SetETag(w, newETag)
w.WriteHeader(http.StatusOK)
writeBody(ctx, w, b, logData)

Expand Down Expand Up @@ -454,10 +455,6 @@ func getIfMatch(r *http.Request) string {
return ifMatch
}

func setETag(w http.ResponseWriter, eTag string) {
w.Header().Set("ETag", eTag)
}

func writeBody(ctx context.Context, w http.ResponseWriter, b []byte, logData log.Data) {
if _, err := w.Write(b); err != nil {
log.Fatal(ctx, "failed to write http response body", err, logData)
Expand Down

0 comments on commit 79991e2

Please sign in to comment.