1 // Copyright 2015 go-swagger maintainers 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package runtime 16 17 import ( 18 "context" 19 "io" 20 "net/http" 21 22 "github.com/go-openapi/strfmt" 23 ) 24 25 // OperationHandlerFunc an adapter for a function to the OperationHandler interface 26 type OperationHandlerFunc func(interface{}) (interface{}, error) 27 28 // Handle implements the operation handler interface 29 func (s OperationHandlerFunc) Handle(data interface{}) (interface{}, error) { 30 return s(data) 31 } 32 33 // OperationHandler a handler for a swagger operation 34 type OperationHandler interface { 35 Handle(interface{}) (interface{}, error) 36 } 37 38 // ConsumerFunc represents a function that can be used as a consumer 39 type ConsumerFunc func(io.Reader, interface{}) error 40 41 // Consume consumes the reader into the data parameter 42 func (fn ConsumerFunc) Consume(reader io.Reader, data interface{}) error { 43 return fn(reader, data) 44 } 45 46 // Consumer implementations know how to bind the values on the provided interface to 47 // data provided by the request body 48 type Consumer interface { 49 // Consume performs the binding of request values 50 Consume(io.Reader, interface{}) error 51 } 52 53 // ProducerFunc represents a function that can be used as a producer 54 type ProducerFunc func(io.Writer, interface{}) error 55 56 // Produce produces the response for the provided data 57 func (f ProducerFunc) Produce(writer io.Writer, data interface{}) error { 58 return f(writer, data) 59 } 60 61 // Producer implementations know how to turn the provided interface into a valid 62 // HTTP response 63 type Producer interface { 64 // Produce writes to the http response 65 Produce(io.Writer, interface{}) error 66 } 67 68 // AuthenticatorFunc turns a function into an authenticator 69 type AuthenticatorFunc func(interface{}) (bool, interface{}, error) 70 71 // Authenticate authenticates the request with the provided data 72 func (f AuthenticatorFunc) Authenticate(params interface{}) (bool, interface{}, error) { 73 return f(params) 74 } 75 76 // Authenticator represents an authentication strategy 77 // implementations of Authenticator know how to authenticate the 78 // request data and translate that into a valid principal object or an error 79 type Authenticator interface { 80 Authenticate(interface{}) (bool, interface{}, error) 81 } 82 83 // AuthorizerFunc turns a function into an authorizer 84 type AuthorizerFunc func(*http.Request, interface{}) error 85 86 // Authorize authorizes the processing of the request for the principal 87 func (f AuthorizerFunc) Authorize(r *http.Request, principal interface{}) error { 88 return f(r, principal) 89 } 90 91 // Authorizer represents an authorization strategy 92 // implementations of Authorizer know how to authorize the principal object 93 // using the request data and returns error if unauthorized 94 type Authorizer interface { 95 Authorize(*http.Request, interface{}) error 96 } 97 98 // Validatable types implementing this interface allow customizing their validation 99 // this will be used instead of the reflective validation based on the spec document. 100 // the implementations are assumed to have been generated by the swagger tool so they should 101 // contain all the validations obtained from the spec 102 type Validatable interface { 103 Validate(strfmt.Registry) error 104 } 105 106 // ContextValidatable types implementing this interface allow customizing their validation 107 // this will be used instead of the reflective validation based on the spec document. 108 // the implementations are assumed to have been generated by the swagger tool so they should 109 // contain all the context validations obtained from the spec 110 type ContextValidatable interface { 111 ContextValidate(context.Context, strfmt.Registry) error 112 } 113