Skip to content

Commit

Permalink
Change RequestHandler signature
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Jan 30, 2018
1 parent fe05385 commit 5ed61be
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion api/handler/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/rancher/norman/types"
)

func CreateHandler(apiContext *types.APIContext) error {
func CreateHandler(apiContext *types.APIContext, next types.RequestHandler) error {
var err error

data, err := ParseAndValidateBody(apiContext, true)
Expand Down
2 changes: 1 addition & 1 deletion api/handler/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/rancher/norman/types"
)

func DeleteHandler(request *types.APIContext) error {
func DeleteHandler(request *types.APIContext, next types.RequestHandler) error {
store := request.Schema.Store
if store == nil {
request.WriteResponse(http.StatusNoContent, nil)
Expand Down
4 changes: 2 additions & 2 deletions api/handler/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/rancher/norman/types"
)

func ListHandler(request *types.APIContext) error {
func ListHandler(request *types.APIContext, next types.RequestHandler) error {
var (
err error
data interface{}
Expand All @@ -24,7 +24,7 @@ func ListHandler(request *types.APIContext) error {
} else if request.Link == "" {
data, err = store.ByID(request, request.Schema, request.ID)
} else {
return request.Schema.LinkHandler(request)
return request.Schema.LinkHandler(request, nil)
}

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion api/handler/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/rancher/norman/types"
)

func UpdateHandler(apiContext *types.APIContext) error {
func UpdateHandler(apiContext *types.APIContext, next types.RequestHandler) error {
data, err := ParseAndValidateBody(apiContext, false)
if err != nil {
return err
Expand Down
10 changes: 8 additions & 2 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func NewAPIServer() *Server {
DeleteHandler: handler.DeleteHandler,
UpdateHandler: handler.UpdateHandler,
ListHandler: handler.ListHandler,
LinkHandler: func(*types.APIContext) error {
LinkHandler: func(*types.APIContext, types.RequestHandler) error {
return httperror.NewAPIError(httperror.NotFound, "Link not found")
},
ErrorHandler: httperror.ErrorHandler,
Expand Down Expand Up @@ -182,38 +182,44 @@ func (s *Server) handle(rw http.ResponseWriter, req *http.Request) (*types.APICo

if action == nil && apiRequest.Type != "" {
var handler types.RequestHandler
var nextHandler types.RequestHandler
if apiRequest.Link == "" {
switch apiRequest.Method {
case http.MethodGet:
if !apiRequest.AccessControl.CanList(apiRequest, apiRequest.Schema) {
return apiRequest, httperror.NewAPIError(httperror.PermissionDenied, "Can not list "+apiRequest.Schema.Type)
}
handler = apiRequest.Schema.ListHandler
nextHandler = s.Defaults.ListHandler
case http.MethodPost:
if !apiRequest.AccessControl.CanCreate(apiRequest, apiRequest.Schema) {
return apiRequest, httperror.NewAPIError(httperror.PermissionDenied, "Can not create "+apiRequest.Schema.Type)
}
handler = apiRequest.Schema.CreateHandler
nextHandler = s.Defaults.CreateHandler
case http.MethodPut:
if !apiRequest.AccessControl.CanUpdate(apiRequest, nil, apiRequest.Schema) {
return apiRequest, httperror.NewAPIError(httperror.PermissionDenied, "Can not update "+apiRequest.Schema.Type)
}
handler = apiRequest.Schema.UpdateHandler
nextHandler = s.Defaults.UpdateHandler
case http.MethodDelete:
if !apiRequest.AccessControl.CanDelete(apiRequest, nil, apiRequest.Schema) {
return apiRequest, httperror.NewAPIError(httperror.PermissionDenied, "Can not delete "+apiRequest.Schema.Type)
}
handler = apiRequest.Schema.DeleteHandler
nextHandler = s.Defaults.DeleteHandler
}
} else {
handler = apiRequest.Schema.ListHandler
nextHandler = s.Defaults.ListHandler
}

if handler == nil {
return apiRequest, httperror.NewAPIError(httperror.NotFound, "")
}

return apiRequest, handler(apiRequest)
return apiRequest, handler(apiRequest, nextHandler)
} else if action != nil {
return apiRequest, handleAction(action, apiRequest)
}
Expand Down
7 changes: 6 additions & 1 deletion api/writer/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ func addSchemasHeader(apiContext *types.APIContext) error {
return nil
}

apiContext.Response.Header().Set("X-Api-Schemas", apiContext.URLBuilder.Collection(schema, apiContext.Version))
version := apiContext.SchemasVersion
if version == nil {
version = apiContext.Version
}

apiContext.Response.Header().Set("X-Api-Schemas", apiContext.URLBuilder.Collection(schema, version))
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/subscribe/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Subscribe struct {
ProjectID string `norman:"type=reference[project]"`
}

func Handler(apiContext *types.APIContext) error {
func Handler(apiContext *types.APIContext, _ types.RequestHandler) error {
err := handler(apiContext)
if err != nil {
logrus.Errorf("Error during subscribe %v", err)
Expand Down
3 changes: 2 additions & 1 deletion types/server_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (r *RawResource) MarshalJSON() ([]byte, error) {

type ActionHandler func(actionName string, action *Action, request *APIContext) error

type RequestHandler func(request *APIContext) error
type RequestHandler func(request *APIContext, next RequestHandler) error

type QueryFilter func(opts *QueryOptions, data []map[string]interface{}) []map[string]interface{}

Expand Down Expand Up @@ -87,6 +87,7 @@ type APIContext struct {
Schema *Schema
Schemas *Schemas
Version *APIVersion
SchemasVersion *APIVersion
Query url.Values
ResponseFormat string
ReferenceValidator ReferenceValidator
Expand Down

0 comments on commit 5ed61be

Please sign in to comment.