client_request.go 2.4 KB

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