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
15package runtime
16
17import (
18	"io"
19	"io/ioutil"
20	"net/http"
21	"net/url"
22	"time"
23
24	"github.com/go-openapi/strfmt"
25)
26
27// ClientRequestWriterFunc converts a function to a request writer interface
28type ClientRequestWriterFunc func(ClientRequest, strfmt.Registry) error
29
30// WriteToRequest adds data to the request
31func (fn ClientRequestWriterFunc) WriteToRequest(req ClientRequest, reg strfmt.Registry) error {
32	return fn(req, reg)
33}
34
35// ClientRequestWriter is an interface for things that know how to write to a request
36type ClientRequestWriter interface {
37	WriteToRequest(ClientRequest, strfmt.Registry) error
38}
39
40// ClientRequest is an interface for things that know how to
41// add information to a swagger client request
42type ClientRequest interface {
43	SetHeaderParam(string, ...string) error
44
45	GetHeaderParams() http.Header
46
47	SetQueryParam(string, ...string) error
48
49	SetFormParam(string, ...string) error
50
51	SetPathParam(string, string) error
52
53	GetQueryParams() url.Values
54
55	SetFileParam(string, ...NamedReadCloser) error
56
57	SetBodyParam(interface{}) error
58
59	SetTimeout(time.Duration) error
60
61	GetMethod() string
62
63	GetPath() string
64
65	GetBody() []byte
66
67	GetBodyParam() interface{}
68
69	GetFileParam() map[string][]NamedReadCloser
70}
71
72// NamedReadCloser represents a named ReadCloser interface
73type NamedReadCloser interface {
74	io.ReadCloser
75	Name() string
76}
77
78// NamedReader creates a NamedReadCloser for use as file upload
79func NamedReader(name string, rdr io.Reader) NamedReadCloser {
80	rc, ok := rdr.(io.ReadCloser)
81	if !ok {
82		rc = ioutil.NopCloser(rdr)
83	}
84	return &namedReadCloser{
85		name: name,
86		cr:   rc,
87	}
88}
89
90type namedReadCloser struct {
91	name string
92	cr   io.ReadCloser
93}
94
95func (n *namedReadCloser) Close() error {
96	return n.cr.Close()
97}
98func (n *namedReadCloser) Read(p []byte) (int, error) {
99	return n.cr.Read(p)
100}
101func (n *namedReadCloser) Name() string {
102	return n.name
103}
104
105type TestClientRequest struct {
106	Headers http.Header
107	Body    interface{}
108}
109
110func (t *TestClientRequest) SetHeaderParam(name string, values ...string) error {
111	if t.Headers == nil {
112		t.Headers = make(http.Header)
113	}
114	t.Headers.Set(name, values[0])
115	return nil
116}
117
118func (t *TestClientRequest) SetQueryParam(_ string, _ ...string) error { return nil }
119
120func (t *TestClientRequest) SetFormParam(_ string, _ ...string) error { return nil }
121
122func (t *TestClientRequest) SetPathParam(_ string, _ string) error { return nil }
123
124func (t *TestClientRequest) SetFileParam(_ string, _ ...NamedReadCloser) error { return nil }
125
126func (t *TestClientRequest) SetBodyParam(body interface{}) error {
127	t.Body = body
128	return nil
129}
130
131func (t *TestClientRequest) SetTimeout(time.Duration) error {
132	return nil
133}
134
135func (t *TestClientRequest) GetQueryParams() url.Values { return nil }
136
137func (t *TestClientRequest) GetMethod() string { return "" }
138
139func (t *TestClientRequest) GetPath() string { return "" }
140
141func (t *TestClientRequest) GetBody() []byte { return nil }
142
143func (t *TestClientRequest) GetBodyParam() interface{} {
144	return t.Body
145}
146
147func (t *TestClientRequest) GetFileParam() map[string][]NamedReadCloser {
148	return nil
149}
150
151func (t *TestClientRequest) GetHeaderParams() http.Header {
152	return t.Headers
153}
154