@@ -11,7 +11,8 @@ import (
1111 "github.com/brave-intl/bat-go/datastore"
1212 "github.com/brave-intl/bat-go/grant"
1313 "github.com/brave-intl/bat-go/middleware"
14- "github.com/brave-intl/bat-go/utils"
14+ "github.com/brave-intl/bat-go/utils/closers"
15+ "github.com/brave-intl/bat-go/utils/handlers"
1516 "github.com/garyburd/redigo/redis"
1617 raven "github.com/getsentry/raven-go"
1718 "github.com/go-chi/chi"
@@ -29,36 +30,36 @@ func GrantsRouter() chi.Router {
2930 if err != nil {
3031 panic ("THROTTLE_GRANT_REQUESTS was provided but not a valid number" )
3132 }
32- r .Method ("POST" , "/" , chiware .Throttle (int (throttle ))(middleware .InstrumentHandler ("RedeemGrants" , utils .AppHandler (RedeemGrants ))))
33+ r .Method ("POST" , "/" , chiware .Throttle (int (throttle ))(middleware .InstrumentHandler ("RedeemGrants" , handlers .AppHandler (RedeemGrants ))))
3334 } else {
34- r .Method ("POST" , "/" , middleware .InstrumentHandler ("RedeemGrants" , utils .AppHandler (RedeemGrants )))
35+ r .Method ("POST" , "/" , middleware .InstrumentHandler ("RedeemGrants" , handlers .AppHandler (RedeemGrants )))
3536 }
36- r .Method ("PUT" , "/{grantId}" , middleware .InstrumentHandler ("ClaimGrant" , utils .AppHandler (ClaimGrant )))
37+ r .Method ("PUT" , "/{grantId}" , middleware .InstrumentHandler ("ClaimGrant" , handlers .AppHandler (ClaimGrant )))
3738 return r
3839}
3940
4041// ClaimGrant is the handler for claiming grants
41- func ClaimGrant (w http.ResponseWriter , r * http.Request ) * utils .AppError {
42- defer utils . PanicCloser (r .Body )
42+ func ClaimGrant (w http.ResponseWriter , r * http.Request ) * handlers .AppError {
43+ defer closers . Panic (r .Body )
4344
4445 body , err := ioutil .ReadAll (r .Body )
4546 if err != nil {
46- return utils .WrapError ("Error reading body" , err )
47+ return handlers .WrapError ("Error reading body" , err )
4748 }
4849
4950 var req grant.ClaimGrantRequest
5051 err = json .Unmarshal (body , & req )
5152 if err != nil {
52- return utils .WrapError ("Error unmarshalling body" , err )
53+ return handlers .WrapError ("Error unmarshalling body" , err )
5354 }
5455 _ , err = govalidator .ValidateStruct (req )
5556 if err != nil {
56- return utils .WrapValidationError (err )
57+ return handlers .WrapValidationError (err )
5758 }
5859
5960 if grantID := chi .URLParam (r , "grantId" ); grantID != "" {
6061 if ! govalidator .IsUUIDv4 (grantID ) {
61- return & utils .AppError {
62+ return & handlers .AppError {
6263 Message : "Error validating request url parameter" ,
6364 Code : http .StatusBadRequest ,
6465 Data : map [string ]interface {}{
@@ -72,7 +73,7 @@ func ClaimGrant(w http.ResponseWriter, r *http.Request) *utils.AppError {
7273 err = req .Claim (r .Context (), grantID )
7374 if err != nil {
7475 // FIXME not all errors are 4xx
75- return utils .WrapError ("Error claiming grant" , err )
76+ return handlers .WrapError ("Error claiming grant" , err )
7677 }
7778 }
7879
@@ -90,31 +91,31 @@ func ClaimGrant(w http.ResponseWriter, r *http.Request) *utils.AppError {
9091}
9192
9293// RedeemGrants is the handler for redeeming one or more grants
93- func RedeemGrants (w http.ResponseWriter , r * http.Request ) * utils .AppError {
94- defer utils . PanicCloser (r .Body )
94+ func RedeemGrants (w http.ResponseWriter , r * http.Request ) * handlers .AppError {
95+ defer closers . Panic (r .Body )
9596
9697 body , err := ioutil .ReadAll (r .Body )
9798 if err != nil {
98- return utils .WrapError ("Error reading body" , err )
99+ return handlers .WrapError ("Error reading body" , err )
99100 }
100101
101102 var req grant.RedeemGrantsRequest
102103 err = json .Unmarshal (body , & req )
103104 if err != nil {
104- return utils .WrapError ("Error unmarshalling body" , err )
105+ return handlers .WrapError ("Error unmarshalling body" , err )
105106 }
106107 _ , err = govalidator .ValidateStruct (req )
107108 if err != nil {
108- return utils .WrapValidationError (err )
109+ return handlers .WrapValidationError (err )
109110 }
110111
111112 redeemedIDs , err := grant .GetRedeemedIDs (r .Context (), req .Grants )
112113 if err != nil {
113- return utils .WrapError ("Error checking grant redemption status" , err )
114+ return handlers .WrapError ("Error checking grant redemption status" , err )
114115 }
115116
116117 if len (redeemedIDs ) > 0 {
117- return & utils .AppError {
118+ return & handlers .AppError {
118119 Message : "One or more grants have already been redeemed" ,
119120 Code : http .StatusGone ,
120121 Data : map [string ]interface {}{"redeemedIDs" : redeemedIDs },
@@ -124,7 +125,7 @@ func RedeemGrants(w http.ResponseWriter, r *http.Request) *utils.AppError {
124125 txInfo , err := req .Redeem (r .Context ())
125126 if err != nil {
126127 // FIXME not all errors are 4xx
127- return utils .WrapError ("Error redeeming grant" , err )
128+ return handlers .WrapError ("Error redeeming grant" , err )
128129 }
129130
130131 w .WriteHeader (http .StatusOK )
0 commit comments