Serge Bazanski | cc25bdf | 2018-10-25 14:02:58 +0200 | [diff] [blame] | 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 | "io" |
| 19 | "net/http" |
| 20 | |
| 21 | "github.com/go-openapi/strfmt" |
| 22 | ) |
| 23 | |
| 24 | // OperationHandlerFunc an adapter for a function to the OperationHandler interface |
| 25 | type OperationHandlerFunc func(interface{}) (interface{}, error) |
| 26 | |
| 27 | // Handle implements the operation handler interface |
| 28 | func (s OperationHandlerFunc) Handle(data interface{}) (interface{}, error) { |
| 29 | return s(data) |
| 30 | } |
| 31 | |
| 32 | // OperationHandler a handler for a swagger operation |
| 33 | type OperationHandler interface { |
| 34 | Handle(interface{}) (interface{}, error) |
| 35 | } |
| 36 | |
| 37 | // ConsumerFunc represents a function that can be used as a consumer |
| 38 | type ConsumerFunc func(io.Reader, interface{}) error |
| 39 | |
| 40 | // Consume consumes the reader into the data parameter |
| 41 | func (fn ConsumerFunc) Consume(reader io.Reader, data interface{}) error { |
| 42 | return fn(reader, data) |
| 43 | } |
| 44 | |
| 45 | // Consumer implementations know how to bind the values on the provided interface to |
| 46 | // data provided by the request body |
| 47 | type Consumer interface { |
| 48 | // Consume performs the binding of request values |
| 49 | Consume(io.Reader, interface{}) error |
| 50 | } |
| 51 | |
| 52 | // ProducerFunc represents a function that can be used as a producer |
| 53 | type ProducerFunc func(io.Writer, interface{}) error |
| 54 | |
| 55 | // Produce produces the response for the provided data |
| 56 | func (f ProducerFunc) Produce(writer io.Writer, data interface{}) error { |
| 57 | return f(writer, data) |
| 58 | } |
| 59 | |
| 60 | // Producer implementations know how to turn the provided interface into a valid |
| 61 | // HTTP response |
| 62 | type Producer interface { |
| 63 | // Produce writes to the http response |
| 64 | Produce(io.Writer, interface{}) error |
| 65 | } |
| 66 | |
| 67 | // AuthenticatorFunc turns a function into an authenticator |
| 68 | type AuthenticatorFunc func(interface{}) (bool, interface{}, error) |
| 69 | |
| 70 | // Authenticate authenticates the request with the provided data |
| 71 | func (f AuthenticatorFunc) Authenticate(params interface{}) (bool, interface{}, error) { |
| 72 | return f(params) |
| 73 | } |
| 74 | |
| 75 | // Authenticator represents an authentication strategy |
| 76 | // implementations of Authenticator know how to authenticate the |
| 77 | // request data and translate that into a valid principal object or an error |
| 78 | type Authenticator interface { |
| 79 | Authenticate(interface{}) (bool, interface{}, error) |
| 80 | } |
| 81 | |
| 82 | // AuthorizerFunc turns a function into an authorizer |
| 83 | type AuthorizerFunc func(*http.Request, interface{}) error |
| 84 | |
| 85 | // Authorize authorizes the processing of the request for the principal |
| 86 | func (f AuthorizerFunc) Authorize(r *http.Request, principal interface{}) error { |
| 87 | return f(r, principal) |
| 88 | } |
| 89 | |
| 90 | // Authorizer represents an authorization strategy |
| 91 | // implementations of Authorizer know how to authorize the principal object |
| 92 | // using the request data and returns error if unauthorized |
| 93 | type Authorizer interface { |
| 94 | Authorize(*http.Request, interface{}) error |
| 95 | } |
| 96 | |
| 97 | // Validatable types implementing this interface allow customizing their validation |
| 98 | // this will be used instead of the reflective validation based on the spec document. |
| 99 | // the implementations are assumed to have been generated by the swagger tool so they should |
| 100 | // contain all the validations obtained from the spec |
| 101 | type Validatable interface { |
| 102 | Validate(strfmt.Registry) error |
| 103 | } |