...

Source file src/github.com/go-openapi/runtime/client_request.go

Documentation: github.com/go-openapi/runtime

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

View as plain text