mirror of
https://github.com/rancher/norman.git
synced 2025-09-02 07:44:51 +00:00
Change RequestHandler signature
This commit is contained in:
@@ -6,7 +6,7 @@ import (
|
|||||||
"github.com/rancher/norman/types"
|
"github.com/rancher/norman/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateHandler(apiContext *types.APIContext) error {
|
func CreateHandler(apiContext *types.APIContext, next types.RequestHandler) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
data, err := ParseAndValidateBody(apiContext, true)
|
data, err := ParseAndValidateBody(apiContext, true)
|
||||||
|
@@ -6,7 +6,7 @@ import (
|
|||||||
"github.com/rancher/norman/types"
|
"github.com/rancher/norman/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func DeleteHandler(request *types.APIContext) error {
|
func DeleteHandler(request *types.APIContext, next types.RequestHandler) error {
|
||||||
store := request.Schema.Store
|
store := request.Schema.Store
|
||||||
if store == nil {
|
if store == nil {
|
||||||
request.WriteResponse(http.StatusNoContent, nil)
|
request.WriteResponse(http.StatusNoContent, nil)
|
||||||
|
@@ -7,7 +7,7 @@ import (
|
|||||||
"github.com/rancher/norman/types"
|
"github.com/rancher/norman/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ListHandler(request *types.APIContext) error {
|
func ListHandler(request *types.APIContext, next types.RequestHandler) error {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
data interface{}
|
data interface{}
|
||||||
@@ -24,7 +24,7 @@ func ListHandler(request *types.APIContext) error {
|
|||||||
} else if request.Link == "" {
|
} else if request.Link == "" {
|
||||||
data, err = store.ByID(request, request.Schema, request.ID)
|
data, err = store.ByID(request, request.Schema, request.ID)
|
||||||
} else {
|
} else {
|
||||||
return request.Schema.LinkHandler(request)
|
return request.Schema.LinkHandler(request, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@@ -6,7 +6,7 @@ import (
|
|||||||
"github.com/rancher/norman/types"
|
"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)
|
data, err := ParseAndValidateBody(apiContext, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@@ -61,7 +61,7 @@ func NewAPIServer() *Server {
|
|||||||
DeleteHandler: handler.DeleteHandler,
|
DeleteHandler: handler.DeleteHandler,
|
||||||
UpdateHandler: handler.UpdateHandler,
|
UpdateHandler: handler.UpdateHandler,
|
||||||
ListHandler: handler.ListHandler,
|
ListHandler: handler.ListHandler,
|
||||||
LinkHandler: func(*types.APIContext) error {
|
LinkHandler: func(*types.APIContext, types.RequestHandler) error {
|
||||||
return httperror.NewAPIError(httperror.NotFound, "Link not found")
|
return httperror.NewAPIError(httperror.NotFound, "Link not found")
|
||||||
},
|
},
|
||||||
ErrorHandler: httperror.ErrorHandler,
|
ErrorHandler: httperror.ErrorHandler,
|
||||||
@@ -182,6 +182,7 @@ func (s *Server) handle(rw http.ResponseWriter, req *http.Request) (*types.APICo
|
|||||||
|
|
||||||
if action == nil && apiRequest.Type != "" {
|
if action == nil && apiRequest.Type != "" {
|
||||||
var handler types.RequestHandler
|
var handler types.RequestHandler
|
||||||
|
var nextHandler types.RequestHandler
|
||||||
if apiRequest.Link == "" {
|
if apiRequest.Link == "" {
|
||||||
switch apiRequest.Method {
|
switch apiRequest.Method {
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
@@ -189,31 +190,36 @@ func (s *Server) handle(rw http.ResponseWriter, req *http.Request) (*types.APICo
|
|||||||
return apiRequest, httperror.NewAPIError(httperror.PermissionDenied, "Can not list "+apiRequest.Schema.Type)
|
return apiRequest, httperror.NewAPIError(httperror.PermissionDenied, "Can not list "+apiRequest.Schema.Type)
|
||||||
}
|
}
|
||||||
handler = apiRequest.Schema.ListHandler
|
handler = apiRequest.Schema.ListHandler
|
||||||
|
nextHandler = s.Defaults.ListHandler
|
||||||
case http.MethodPost:
|
case http.MethodPost:
|
||||||
if !apiRequest.AccessControl.CanCreate(apiRequest, apiRequest.Schema) {
|
if !apiRequest.AccessControl.CanCreate(apiRequest, apiRequest.Schema) {
|
||||||
return apiRequest, httperror.NewAPIError(httperror.PermissionDenied, "Can not create "+apiRequest.Schema.Type)
|
return apiRequest, httperror.NewAPIError(httperror.PermissionDenied, "Can not create "+apiRequest.Schema.Type)
|
||||||
}
|
}
|
||||||
handler = apiRequest.Schema.CreateHandler
|
handler = apiRequest.Schema.CreateHandler
|
||||||
|
nextHandler = s.Defaults.CreateHandler
|
||||||
case http.MethodPut:
|
case http.MethodPut:
|
||||||
if !apiRequest.AccessControl.CanUpdate(apiRequest, nil, apiRequest.Schema) {
|
if !apiRequest.AccessControl.CanUpdate(apiRequest, nil, apiRequest.Schema) {
|
||||||
return apiRequest, httperror.NewAPIError(httperror.PermissionDenied, "Can not update "+apiRequest.Schema.Type)
|
return apiRequest, httperror.NewAPIError(httperror.PermissionDenied, "Can not update "+apiRequest.Schema.Type)
|
||||||
}
|
}
|
||||||
handler = apiRequest.Schema.UpdateHandler
|
handler = apiRequest.Schema.UpdateHandler
|
||||||
|
nextHandler = s.Defaults.UpdateHandler
|
||||||
case http.MethodDelete:
|
case http.MethodDelete:
|
||||||
if !apiRequest.AccessControl.CanDelete(apiRequest, nil, apiRequest.Schema) {
|
if !apiRequest.AccessControl.CanDelete(apiRequest, nil, apiRequest.Schema) {
|
||||||
return apiRequest, httperror.NewAPIError(httperror.PermissionDenied, "Can not delete "+apiRequest.Schema.Type)
|
return apiRequest, httperror.NewAPIError(httperror.PermissionDenied, "Can not delete "+apiRequest.Schema.Type)
|
||||||
}
|
}
|
||||||
handler = apiRequest.Schema.DeleteHandler
|
handler = apiRequest.Schema.DeleteHandler
|
||||||
|
nextHandler = s.Defaults.DeleteHandler
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
handler = apiRequest.Schema.ListHandler
|
handler = apiRequest.Schema.ListHandler
|
||||||
|
nextHandler = s.Defaults.ListHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
if handler == nil {
|
if handler == nil {
|
||||||
return apiRequest, httperror.NewAPIError(httperror.NotFound, "")
|
return apiRequest, httperror.NewAPIError(httperror.NotFound, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
return apiRequest, handler(apiRequest)
|
return apiRequest, handler(apiRequest, nextHandler)
|
||||||
} else if action != nil {
|
} else if action != nil {
|
||||||
return apiRequest, handleAction(action, apiRequest)
|
return apiRequest, handleAction(action, apiRequest)
|
||||||
}
|
}
|
||||||
|
@@ -16,7 +16,12 @@ func addSchemasHeader(apiContext *types.APIContext) error {
|
|||||||
return nil
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -25,7 +25,7 @@ type Subscribe struct {
|
|||||||
ProjectID string `norman:"type=reference[project]"`
|
ProjectID string `norman:"type=reference[project]"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func Handler(apiContext *types.APIContext) error {
|
func Handler(apiContext *types.APIContext, _ types.RequestHandler) error {
|
||||||
err := handler(apiContext)
|
err := handler(apiContext)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("Error during subscribe %v", err)
|
logrus.Errorf("Error during subscribe %v", err)
|
||||||
|
@@ -47,7 +47,7 @@ func (r *RawResource) MarshalJSON() ([]byte, error) {
|
|||||||
|
|
||||||
type ActionHandler func(actionName string, action *Action, request *APIContext) 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{}
|
type QueryFilter func(opts *QueryOptions, data []map[string]interface{}) []map[string]interface{}
|
||||||
|
|
||||||
@@ -87,6 +87,7 @@ type APIContext struct {
|
|||||||
Schema *Schema
|
Schema *Schema
|
||||||
Schemas *Schemas
|
Schemas *Schemas
|
||||||
Version *APIVersion
|
Version *APIVersion
|
||||||
|
SchemasVersion *APIVersion
|
||||||
Query url.Values
|
Query url.Values
|
||||||
ResponseFormat string
|
ResponseFormat string
|
||||||
ReferenceValidator ReferenceValidator
|
ReferenceValidator ReferenceValidator
|
||||||
|
Reference in New Issue
Block a user