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