Skip to content

Commit

Permalink
Add Deprecation headers for legacy rest endpoints (#7686)
Browse files Browse the repository at this point in the history
* add deprecation headers for legacy rest endpoints

* add deprecation headers for missing tx routes

* rm handler-level deprecation headers

* switch to middleware Route.Use method for setting deprecation Headers

* set deprecation headers using subrouter

* cleanup gofmt

* goimports

* Update client/rest/rest.go

* update deprecation headers to be set on each module individually

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
clevinson and mergify[bot] authored Oct 29, 2020
1 parent 82f15f3 commit 536eb68
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 14 deletions.
2 changes: 1 addition & 1 deletion client/docs/swagger-ui/swagger-ui-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -41773,4 +41773,4 @@
}, o.resolve = i, e.exports = o, o.id = 1058
}])
});
//# sourceMappingURL=swagger-ui-bundle.js.map
//# sourceMappingURL=swagger-ui-bundle.js.map
2 changes: 1 addition & 1 deletion client/docs/swagger_legacy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2595,4 +2595,4 @@ definitions:
total:
type: array
items:
$ref: "#/definitions/Coin"
$ref: "#/definitions/Coin"
27 changes: 27 additions & 0 deletions client/rest/rest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package rest

import (
"net/http"

"github.com/gorilla/mux"
)

// addHTTPDeprecationHeaders is a mux middleware function for adding HTTP
// Deprecation headers to a http handler
func addHTTPDeprecationHeaders(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Deprecation", "true")
w.Header().Set("Link", "<https://docs.cosmos.network/v0.40/interfaces/rest.html>; rel=\"deprecation\"")
w.Header().Set("Warning", "199 - \"this endpoint is deprecated and may not work as before, see deprecation link for more info\"")
h.ServeHTTP(w, r)
})
}

// WithHTTPDeprecationHeaders returns a new *mux.Router, identical to its input
// but with the addition of HTTP Deprecation headers. This is used to mark legacy
// amino REST endpoints as deprecated in the REST API.
func WithHTTPDeprecationHeaders(r *mux.Router) *mux.Router {
subRouter := r.NewRoute().Subrouter()
subRouter.Use(addHTTPDeprecationHeaders)
return subRouter
}
3 changes: 1 addition & 2 deletions types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ package module
import (
"encoding/json"

"github.com/grpc-ecosystem/grpc-gateway/runtime"

"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
abci "github.com/tendermint/tendermint/abci/types"

Expand Down
7 changes: 5 additions & 2 deletions x/auth/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/rest"
)

// REST query and parameter values
Expand All @@ -12,7 +13,8 @@ const (
)

// RegisterRoutes registers the auth module REST routes.
func RegisterRoutes(clientCtx client.Context, r *mux.Router, storeName string) {
func RegisterRoutes(clientCtx client.Context, rtr *mux.Router, storeName string) {
r := rest.WithHTTPDeprecationHeaders(rtr)
r.HandleFunc(
"/auth/accounts/{address}", QueryAccountRequestHandlerFn(storeName, clientCtx),
).Methods(MethodGet)
Expand All @@ -24,7 +26,8 @@ func RegisterRoutes(clientCtx client.Context, r *mux.Router, storeName string) {
}

// RegisterTxRoutes registers all transaction routes on the provided router.
func RegisterTxRoutes(clientCtx client.Context, r *mux.Router) {
func RegisterTxRoutes(clientCtx client.Context, rtr *mux.Router) {
r := rest.WithHTTPDeprecationHeaders(rtr)
r.HandleFunc("/txs/{hash}", QueryTxRequestHandlerFn(clientCtx)).Methods("GET")
r.HandleFunc("/txs", QueryTxsRequestHandlerFn(clientCtx)).Methods("GET")
r.HandleFunc("/txs", BroadcastTxRequest(clientCtx)).Methods("POST")
Expand Down
4 changes: 3 additions & 1 deletion x/bank/client/rest/rest.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package rest

import (
"github.com/cosmos/cosmos-sdk/client/rest"
"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/client"
)

// RegisterHandlers registers all x/bank transaction and query HTTP REST handlers
// on the provided mux router.
func RegisterHandlers(clientCtx client.Context, r *mux.Router) {
func RegisterHandlers(clientCtx client.Context, rtr *mux.Router) {
r := rest.WithHTTPDeprecationHeaders(rtr)
r.HandleFunc("/bank/accounts/{address}/transfers", NewSendRequestHandlerFn(clientCtx)).Methods("POST")
r.HandleFunc("/bank/balances/{address}", QueryBalancesRequestHandlerFn(clientCtx)).Methods("GET")
r.HandleFunc("/bank/total", totalSupplyHandlerFn(clientCtx)).Methods("GET")
Expand Down
5 changes: 4 additions & 1 deletion x/distribution/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import (
"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/client"
clientrest "github.com/cosmos/cosmos-sdk/client/rest"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
govrest "github.com/cosmos/cosmos-sdk/x/gov/client/rest"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)

func RegisterHandlers(clientCtx client.Context, r *mux.Router) {
func RegisterHandlers(clientCtx client.Context, rtr *mux.Router) {
r := clientrest.WithHTTPDeprecationHeaders(rtr)

registerQueryRoutes(clientCtx, r)
registerTxHandlers(clientCtx, r)
}
Expand Down
5 changes: 4 additions & 1 deletion x/evidence/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/rest"

"github.com/gorilla/mux"
)
Expand All @@ -24,7 +25,9 @@ type EvidenceRESTHandler struct {

// RegisterRoutes registers all Evidence submission handlers for the evidence module's
// REST service handler.
func RegisterRoutes(clientCtx client.Context, r *mux.Router, handlers []EvidenceRESTHandler) {
func RegisterRoutes(clientCtx client.Context, rtr *mux.Router, handlers []EvidenceRESTHandler) {
r := rest.WithHTTPDeprecationHeaders(rtr)

registerQueryRoutes(clientCtx, r)
registerTxRoutes(clientCtx, r, handlers)
}
4 changes: 3 additions & 1 deletion x/gov/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/client"
clientrest "github.com/cosmos/cosmos-sdk/client/rest"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
)
Expand All @@ -28,7 +29,8 @@ type ProposalRESTHandler struct {
Handler func(http.ResponseWriter, *http.Request)
}

func RegisterHandlers(clientCtx client.Context, r *mux.Router, phs []ProposalRESTHandler) {
func RegisterHandlers(clientCtx client.Context, rtr *mux.Router, phs []ProposalRESTHandler) {
r := clientrest.WithHTTPDeprecationHeaders(rtr)
registerQueryRoutes(clientCtx, r)
registerTxHandlers(clientCtx, r, phs)
}
Expand Down
4 changes: 3 additions & 1 deletion x/mint/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/rest"
)

// RegisterRoutes registers minting module REST handlers on the provided router.
func RegisterRoutes(clientCtx client.Context, r *mux.Router) {
func RegisterRoutes(clientCtx client.Context, rtr *mux.Router) {
r := rest.WithHTTPDeprecationHeaders(rtr)
registerQueryRoutes(clientCtx, r)
}
5 changes: 4 additions & 1 deletion x/slashing/client/rest/rest.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package rest

import (
"github.com/cosmos/cosmos-sdk/client/rest"
"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/client"
)

func RegisterHandlers(clientCtx client.Context, r *mux.Router) {
func RegisterHandlers(clientCtx client.Context, rtr *mux.Router) {
r := rest.WithHTTPDeprecationHeaders(rtr)

registerQueryRoutes(clientCtx, r)
registerTxHandlers(clientCtx, r)
}
4 changes: 3 additions & 1 deletion x/staking/client/rest/rest.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package rest

import (
"github.com/cosmos/cosmos-sdk/client/rest"
"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/client"
)

func RegisterHandlers(clientCtx client.Context, r *mux.Router) {
func RegisterHandlers(clientCtx client.Context, rtr *mux.Router) {
r := rest.WithHTTPDeprecationHeaders(rtr)
registerQueryRoutes(clientCtx, r)
registerTxHandlers(clientCtx, r)
}
4 changes: 3 additions & 1 deletion x/upgrade/client/rest/rest.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package rest

import (
"github.com/cosmos/cosmos-sdk/client/rest"
"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/client"
)

// RegisterRoutes registers REST routes for the upgrade module under the path specified by routeName.
func RegisterRoutes(clientCtx client.Context, r *mux.Router) {
func RegisterRoutes(clientCtx client.Context, rtr *mux.Router) {
r := rest.WithHTTPDeprecationHeaders(rtr)
registerQueryRoutes(clientCtx, r)
registerTxHandlers(clientCtx, r)
}

0 comments on commit 536eb68

Please sign in to comment.