blob: 4aef7edb9f9c56fce67b6e017b61f8fa076180d1 [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 "io/ioutil"
20 "net/url"
21 "time"
22
23 "github.com/go-openapi/strfmt"
24)
25
26// ClientRequestWriterFunc converts a function to a request writer interface
27type ClientRequestWriterFunc func(ClientRequest, strfmt.Registry) error
28
29// WriteToRequest adds data to the request
30func (fn ClientRequestWriterFunc) WriteToRequest(req ClientRequest, reg strfmt.Registry) error {
31 return fn(req, reg)
32}
33
34// ClientRequestWriter is an interface for things that know how to write to a request
35type ClientRequestWriter interface {
36 WriteToRequest(ClientRequest, strfmt.Registry) error
37}
38
39// ClientRequest is an interface for things that know how to
40// add information to a swagger client request
41type ClientRequest interface {
42 SetHeaderParam(string, ...string) error
43
44 SetQueryParam(string, ...string) error
45
46 SetFormParam(string, ...string) error
47
48 SetPathParam(string, string) error
49
50 GetQueryParams() url.Values
51
52 SetFileParam(string, ...NamedReadCloser) error
53
54 SetBodyParam(interface{}) error
55
56 SetTimeout(time.Duration) error
57
58 GetMethod() string
59
60 GetPath() string
61
62 GetBody() []byte
63
64 GetBodyParam() interface{}
65
66 GetFileParam() map[string][]NamedReadCloser
67}
68
69// NamedReadCloser represents a named ReadCloser interface
70type NamedReadCloser interface {
71 io.ReadCloser
72 Name() string
73}
74
75// NamedReader creates a NamedReadCloser for use as file upload
76func NamedReader(name string, rdr io.Reader) NamedReadCloser {
77 rc, ok := rdr.(io.ReadCloser)
78 if !ok {
79 rc = ioutil.NopCloser(rdr)
80 }
81 return &namedReadCloser{
82 name: name,
83 cr: rc,
84 }
85}
86
87type namedReadCloser struct {
88 name string
89 cr io.ReadCloser
90}
91
92func (n *namedReadCloser) Close() error {
93 return n.cr.Close()
94}
95func (n *namedReadCloser) Read(p []byte) (int, error) {
96 return n.cr.Read(p)
97}
98func (n *namedReadCloser) Name() string {
99 return n.name
100}