github.com/6543-forks/go-swagger@v0.26.0/generator/templates/client/facade.gotmpl (about)

     1  // Code generated by go-swagger; DO NOT EDIT.
     2  
     3  
     4  {{ if .Copyright -}}// {{ comment .Copyright -}}{{ end }}
     5  
     6  
     7  package {{ .Package }}
     8  
     9  // This file was generated by the swagger tool.
    10  // Editing this file might prove futile when you re-run the swagger generate command
    11  
    12  
    13  import (
    14    "net/http"
    15  
    16    "github.com/go-openapi/errors"
    17    "github.com/go-openapi/runtime"
    18    httptransport "github.com/go-openapi/runtime/client"
    19    "github.com/go-openapi/spec"
    20    "github.com/go-openapi/strfmt"
    21    "github.com/go-openapi/swag"
    22  
    23    {{ imports .DefaultImports }}
    24    {{ imports .Imports }}
    25  )
    26  
    27  // Default {{ humanize .Name }} HTTP client.
    28  var Default = NewHTTPClient(nil)
    29  
    30  const (
    31      // DefaultHost is the default Host
    32      // found in Meta (info) section of spec file
    33      DefaultHost string = {{ printf "%#v" .Host }}
    34      // DefaultBasePath is the default BasePath
    35      // found in Meta (info) section of spec file
    36      DefaultBasePath string = {{ printf "%#v" .BasePath }}
    37  )
    38  
    39  // DefaultSchemes are the default schemes found in Meta (info) section of spec file
    40  var DefaultSchemes = {{ printf "%#v" .Schemes }}
    41  
    42  // NewHTTPClient creates a new {{ humanize .Name }} HTTP client.
    43  func NewHTTPClient(formats strfmt.Registry) *{{ pascalize .Name }} {
    44    return NewHTTPClientWithConfig(formats, nil)
    45  }
    46  
    47  // NewHTTPClientWithConfig creates a new {{ humanize .Name }} HTTP client,
    48  // using a customizable transport config.
    49  func NewHTTPClientWithConfig(formats strfmt.Registry, cfg *TransportConfig) *{{ pascalize .Name }} {
    50    // ensure nullable parameters have default
    51    if cfg == nil {
    52      cfg = DefaultTransportConfig()
    53    }
    54  
    55    // create transport and client
    56    transport := httptransport.New(cfg.Host, cfg.BasePath, cfg.Schemes)
    57    return New(transport, formats)
    58  }
    59  
    60  // New creates a new {{ humanize .Name }} client
    61  func New(transport runtime.ClientTransport, formats strfmt.Registry) *{{ pascalize .Name }} {
    62    // ensure nullable parameters have default
    63    if formats == nil {
    64      formats = strfmt.Default
    65    }
    66  
    67    cli := new({{ pascalize .Name }})
    68    cli.Transport = transport
    69    {{- range .OperationGroups }}
    70    cli.{{ pascalize .Name }} = {{ .PackageAlias }}.New(transport, formats)
    71    {{- end }}
    72    return cli
    73  }
    74  
    75  // DefaultTransportConfig creates a TransportConfig with the
    76  // default settings taken from the meta section of the spec file.
    77  func DefaultTransportConfig() *TransportConfig {
    78      return &TransportConfig {
    79          Host: DefaultHost,
    80          BasePath: DefaultBasePath,
    81          Schemes: DefaultSchemes,
    82      }
    83  }
    84  
    85  // TransportConfig contains the transport related info,
    86  // found in the meta section of the spec file.
    87  type TransportConfig struct {
    88      Host string
    89      BasePath string
    90      Schemes []string
    91  }
    92  
    93  // WithHost overrides the default host,
    94  // provided by the meta section of the spec file.
    95  func (cfg *TransportConfig) WithHost(host string) *TransportConfig {
    96      cfg.Host = host
    97      return cfg
    98  }
    99  
   100  // WithBasePath overrides the default basePath,
   101  // provided by the meta section of the spec file.
   102  func (cfg *TransportConfig) WithBasePath(basePath string) *TransportConfig {
   103      cfg.BasePath = basePath
   104      return cfg
   105  }
   106  
   107  // WithSchemes overrides the default schemes,
   108  // provided by the meta section of the spec file.
   109  func (cfg *TransportConfig) WithSchemes(schemes []string) *TransportConfig {
   110      cfg.Schemes = schemes
   111      return cfg
   112  }
   113  
   114  // {{ pascalize .Name }} is a client for {{ humanize .Name }}
   115  type {{ pascalize .Name }} struct {
   116    {{ range .OperationGroups }}
   117      {{ pascalize .Name }} {{ .PackageAlias }}.ClientService
   118    {{ end }}
   119    Transport runtime.ClientTransport
   120  }
   121  
   122  
   123  // SetTransport changes the transport on the client and all its subresources
   124  func (c *{{pascalize .Name}}) SetTransport(transport runtime.ClientTransport) {
   125    c.Transport = transport
   126    {{- range .OperationGroups }}
   127    c.{{ pascalize .Name }}.SetTransport(transport)
   128    {{- end }}
   129  }