Skip to content

Commit

Permalink
Fix #161 Define a constant instead of duplicating literals "Content-T…
Browse files Browse the repository at this point in the history
…ype" and "application/json"
  • Loading branch information
albinpa authored and georgepadayatti committed Sep 12, 2023
1 parent c38c290 commit 053bd66
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 124 deletions.
9 changes: 9 additions & 0 deletions src/config/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package config

// All http response content types
const (
ContentTypeHeader = "Content-Type"
ContentTypeJSON = "application/json"
ContentTypeImage = "image/jpeg"
ContentTypeFormURLEncoded = "application/x-www-form-urlencoded"
)
3 changes: 2 additions & 1 deletion src/handler/actionlog_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/bb-consent/api/src/actionlog"
"github.com/bb-consent/api/src/common"
"github.com/bb-consent/api/src/config"
"github.com/gorilla/mux"
)

Expand Down Expand Up @@ -48,6 +49,6 @@ func GetOrgLogs(w http.ResponseWriter, r *http.Request) {

ls.Links = common.CreatePaginationLinks(r, startID, lastID, limit)
response, _ := json.Marshal(ls)
w.Header().Set("Content-Type", "application/json")
w.Header().Set(config.ContentTypeHeader, config.ContentTypeJSON)
w.Write(response)
}
23 changes: 12 additions & 11 deletions src/handler/consent_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/asaskevich/govalidator"
"github.com/bb-consent/api/src/actionlog"
"github.com/bb-consent/api/src/common"
"github.com/bb-consent/api/src/config"
"github.com/bb-consent/api/src/consent"
"github.com/bb-consent/api/src/consenthistory"
"github.com/bb-consent/api/src/org"
Expand Down Expand Up @@ -191,7 +192,7 @@ func GetConsents(w http.ResponseWriter, r *http.Request) {
//fmt.Printf("c:%v", c)
response, _ := json.Marshal(RespData)

w.Header().Set("Content-Type", "application/json")
w.Header().Set(config.ContentTypeHeader, config.ContentTypeJSON)
w.Write(response)
}

Expand All @@ -217,7 +218,7 @@ func GetConsentByID(w http.ResponseWriter, r *http.Request) {

c := createConsentGetResponse(consent, o)
response, _ := json.Marshal(c)
w.Header().Set("Content-Type", "application/json")
w.Header().Set(config.ContentTypeHeader, config.ContentTypeJSON)
w.Write(response)
}

Expand Down Expand Up @@ -289,7 +290,7 @@ func GetConsentPurposeByID(w http.ResponseWriter, r *http.Request) {
latestConsentHistory, err := consenthistory.GetLatestByUserOrgPurposeID(userID, orgID, purposeID)
if err != nil {
response, _ := json.Marshal(cpResp)
w.Header().Set("Content-Type", "application/json")
w.Header().Set(config.ContentTypeHeader, config.ContentTypeJSON)
w.Write(response)
return
}
Expand All @@ -299,7 +300,7 @@ func GetConsentPurposeByID(w http.ResponseWriter, r *http.Request) {
}

response, _ := json.Marshal(cpResp)
w.Header().Set("Content-Type", "application/json")
w.Header().Set(config.ContentTypeHeader, config.ContentTypeJSON)
w.Write(response)
}

Expand Down Expand Up @@ -340,7 +341,7 @@ func GetAllUsersConsentedToAttribute(w http.ResponseWriter, r *http.Request) {

ou.Links = common.CreatePaginationLinks(r, startID, lastID, limit)
response, _ := json.Marshal(ou)
w.Header().Set("Content-Type", "application/json")
w.Header().Set(config.ContentTypeHeader, config.ContentTypeJSON)
w.Write(response)
return
}
Expand All @@ -364,7 +365,7 @@ func GetAllUsersConsentedToAttribute(w http.ResponseWriter, r *http.Request) {

resp.Links = common.CreatePaginationLinks(r, startID, nextID, limit)
response, _ := json.Marshal(resp)
w.Header().Set("Content-Type", "application/json")
w.Header().Set(config.ContentTypeHeader, config.ContentTypeJSON)
w.Write(response)
}

Expand Down Expand Up @@ -408,7 +409,7 @@ func GetPurposeAllConsentStatus(w http.ResponseWriter, r *http.Request) {
}

response, _ := json.Marshal(purposeStatus{consentStatus})
w.Header().Set("Content-Type", "application/json")
w.Header().Set(config.ContentTypeHeader, config.ContentTypeJSON)
w.Write(response)
}

Expand Down Expand Up @@ -449,7 +450,7 @@ func GetAllUsersConsentedToPurpose(w http.ResponseWriter, r *http.Request) {

ou.Links = common.CreatePaginationLinks(r, startID, lastID, limit)
response, _ := json.Marshal(ou)
w.Header().Set("Content-Type", "application/json")
w.Header().Set(config.ContentTypeHeader, config.ContentTypeJSON)
w.Write(response)
return
}
Expand All @@ -473,7 +474,7 @@ func GetAllUsersConsentedToPurpose(w http.ResponseWriter, r *http.Request) {

resp.Links = common.CreatePaginationLinks(r, startID, nextID, limit)
response, _ := json.Marshal(resp)
w.Header().Set("Content-Type", "application/json")
w.Header().Set(config.ContentTypeHeader, config.ContentTypeJSON)
w.Write(response)
}

Expand Down Expand Up @@ -659,7 +660,7 @@ func UpdatePurposeAllConsentsv2(w http.ResponseWriter, r *http.Request) {
go webhooks.TriggerConsentWebhookEvent(userID, purposeID, consentID, orgID, webhooks.EventTypes[webhookEventTypeID], strconv.FormatInt(time.Now().UTC().Unix(), 10), 0, consentedAttributes)

response, _ := json.Marshal(cRespWithDataRetention)
w.Header().Set("Content-Type", "application/json")
w.Header().Set(config.ContentTypeHeader, config.ContentTypeJSON)
w.Write(response)
}

Expand Down Expand Up @@ -856,6 +857,6 @@ func UpdatePurposeAttribute(w http.ResponseWriter, r *http.Request) {

updateResp := consentUpdateresp{"Consent updated successfully", http.StatusOK}
response, _ := json.Marshal(updateResp)
w.Header().Set("Content-Type", "application/json")
w.Header().Set(config.ContentTypeHeader, config.ContentTypeJSON)
w.Write(response)
}
3 changes: 2 additions & 1 deletion src/handler/consenthistory_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/bb-consent/api/src/common"
"github.com/bb-consent/api/src/config"
"github.com/bb-consent/api/src/consenthistory"
"github.com/bb-consent/api/src/token"
)
Expand Down Expand Up @@ -133,7 +134,7 @@ func GetUserConsentHistory(w http.ResponseWriter, r *http.Request) {

response, _ := json.Marshal(chsResp)

w.Header().Set("Content-Type", "application/json")
w.Header().Set(config.ContentTypeHeader, config.ContentTypeJSON)
w.Write(response)
}

Expand Down
13 changes: 7 additions & 6 deletions src/handler/datarequest_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"

"github.com/bb-consent/api/src/common"
"github.com/bb-consent/api/src/config"
dr "github.com/bb-consent/api/src/datarequests"
"github.com/bb-consent/api/src/token"
"github.com/bb-consent/api/src/webhooks"
Expand All @@ -26,7 +27,7 @@ func GetDeleteMyData(w http.ResponseWriter, r *http.Request) {
}

response, _ := json.Marshal(drs)
w.Header().Set("Content-Type", "application/json")
w.Header().Set(config.ContentTypeHeader, config.ContentTypeJSON)
w.Write(response)
}

Expand Down Expand Up @@ -95,7 +96,7 @@ func GetMyOrgDataRequestStatus(w http.ResponseWriter, r *http.Request) {

drs.Links = common.CreatePaginationLinks(r, startID, lastID, limit)
response, _ := json.Marshal(drs)
w.Header().Set("Content-Type", "application/json")
w.Header().Set(config.ContentTypeHeader, config.ContentTypeJSON)
w.Write(response)
}

Expand Down Expand Up @@ -192,7 +193,7 @@ func GetDeleteMyDataStatus(w http.ResponseWriter, r *http.Request) {
}

response, _ := json.Marshal(resp)
w.Header().Set("Content-Type", "application/json")
w.Header().Set(config.ContentTypeHeader, config.ContentTypeJSON)
w.Write(response)
}

Expand Down Expand Up @@ -239,7 +240,7 @@ func CancelMyDataRequest(w http.ResponseWriter, r *http.Request) {
}

response, _ := json.Marshal(transformDataReqToResp(dReq))
w.Header().Set("Content-Type", "application/json")
w.Header().Set(config.ContentTypeHeader, config.ContentTypeJSON)
w.WriteHeader(http.StatusOK)
w.Write(response)
}
Expand All @@ -257,7 +258,7 @@ func GetDownloadMyData(w http.ResponseWriter, r *http.Request) {
}

response, _ := json.Marshal(drs)
w.Header().Set("Content-Type", "application/json")
w.Header().Set(config.ContentTypeHeader, config.ContentTypeJSON)
w.Write(response)
}

Expand Down Expand Up @@ -307,6 +308,6 @@ func GetDownloadMyDataStatus(w http.ResponseWriter, r *http.Request) {
}

response, _ := json.Marshal(resp)
w.Header().Set("Content-Type", "application/json")
w.Header().Set(config.ContentTypeHeader, config.ContentTypeJSON)
w.Write(response)
}
Loading

0 comments on commit 053bd66

Please sign in to comment.