blob: 65de0aa44b96105e46f49c7d742ea9fab8b32210 [file] [log] [blame]
Serge Bazanskicc25bdf2018-10-25 14:02:58 +02001// 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
15package runtime
16
17import (
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
25type OperationHandlerFunc func(interface{}) (interface{}, error)
26
27// Handle implements the operation handler interface
28func (s OperationHandlerFunc) Handle(data interface{}) (interface{}, error) {
29 return s(data)
30}
31
32// OperationHandler a handler for a swagger operation
33type OperationHandler interface {
34 Handle(interface{}) (interface{}, error)
35}
36
37// ConsumerFunc represents a function that can be used as a consumer
38type ConsumerFunc func(io.Reader, interface{}) error
39
40// Consume consumes the reader into the data parameter
41func (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
47type 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
53type ProducerFunc func(io.Writer, interface{}) error
54
55// Produce produces the response for the provided data
56func (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
62type 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
68type AuthenticatorFunc func(interface{}) (bool, interface{}, error)
69
70// Authenticate authenticates the request with the provided data
71func (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
78type Authenticator interface {
79 Authenticate(interface{}) (bool, interface{}, error)
80}
81
82// AuthorizerFunc turns a function into an authorizer
83type AuthorizerFunc func(*http.Request, interface{}) error
84
85// Authorize authorizes the processing of the request for the principal
86func (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
93type 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
101type Validatable interface {
102 Validate(strfmt.Registry) error
103}