github.com/kaisawind/go-swagger@v0.19.0/generator/templates/client/client.gotmpl (about)

     1  // Code generated by go-swagger; DO NOT EDIT.
     2  
     3  
     4  {{ if .Copyright -}}// {{ comment .Copyright -}}{{ end }}
     5  
     6  
     7  package {{ .Name }}
     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  import (
    13    "net/http"
    14    "github.com/go-openapi/errors"
    15    "github.com/go-openapi/swag"
    16    "github.com/go-openapi/runtime"
    17    "github.com/go-openapi/validate"
    18  
    19    strfmt "github.com/go-openapi/strfmt"
    20  
    21    {{ range .DefaultImports }}{{ printf "%q" .}}
    22    {{ end }}
    23    {{ range $key, $value := .Imports }}{{ $key }} {{ printf "%q" $value }}
    24    {{ end }}
    25  )
    26  
    27  // New creates a new {{ humanize .Name }} API client.
    28  func New(transport runtime.ClientTransport, formats strfmt.Registry) *Client {
    29    return &Client{transport: transport, formats: formats}
    30  }
    31  
    32  /*
    33  Client {{ if .Summary }}{{ .Summary }}{{ if .Description }}
    34  
    35  {{ blockcomment .Description }}{{ end }}{{ else if .Description}}{{ blockcomment .Description }}{{ else }}for {{ humanize .Name }} API{{ end }}
    36  */
    37  type Client struct {
    38    transport runtime.ClientTransport
    39    formats strfmt.Registry
    40  }
    41  
    42  {{ range .Operations }}/*
    43  {{ pascalize .Name }} {{ if .Summary }}{{ pluralizeFirstWord (humanize .Summary) }}{{ if .Description }}
    44  
    45  {{ blockcomment .Description }}{{ end }}{{ else if .Description}}{{ blockcomment .Description }}{{ else }}{{ humanize .Name }} API{{ end }}
    46  */
    47  func (a *Client) {{ pascalize .Name }}(params *{{ pascalize .Name }}Params{{ if .Authorized }}, authInfo runtime.ClientAuthInfoWriter{{end}}{{ if .HasStreamingResponse }}, writer io.Writer{{ end }}) {{ if .SuccessResponse }}({{ range .SuccessResponses }}*{{ pascalize .Name }}, {{ end }}{{ end }}error{{ if .SuccessResponse }}){{ end }} {
    48    // TODO: Validate the params before sending
    49    if params == nil {
    50      params = New{{ pascalize .Name }}Params()
    51    }
    52    {{ $length := len .SuccessResponses }}
    53    {{ if .SuccessResponse }}result{{else}}_{{ end }}, err := a.transport.Submit(&runtime.ClientOperation{
    54      ID: {{ printf "%q" .Name }},
    55      Method: {{ printf "%q" .Method }},
    56      PathPattern: {{ printf "%q" .Path }},
    57      ProducesMediaTypes: {{ printf "%#v" .ProducesMediaTypes }},
    58      ConsumesMediaTypes: {{ printf "%#v" .ConsumesMediaTypes }},
    59      Schemes: {{ printf "%#v" .Schemes }},
    60      Params: params,
    61      Reader: &{{ pascalize .Name }}Reader{formats: a.formats{{ if .HasStreamingResponse }}, writer: writer{{ end }}},{{ if .Authorized }}
    62      AuthInfo: authInfo,{{ end}}
    63      Context: params.Context,
    64      Client: params.HTTPClient,
    65    })
    66    if err != nil {
    67      return {{ if .SuccessResponse }}{{ padSurround "nil" "nil" 0 $length }}, {{ end }}err
    68    }
    69    {{ if .SuccessResponse }}{{ if eq $length 1 }}return result.(*{{ pascalize .SuccessResponse.Name }}), nil{{ else }}switch value := result.(type) { {{ range $i, $v := .SuccessResponses }}
    70      case *{{ pascalize $v.Name }}:
    71        return {{ padSurround "value" "nil" $i $length }}, nil{{ end }} }
    72    return {{ padSurround "nil" "nil" 0 $length }}, nil{{ end }}
    73    {{ else }}return nil{{ end }}
    74  
    75  }
    76  {{ end }}
    77  
    78  // SetTransport changes the transport on the client
    79  func (a *Client) SetTransport(transport runtime.ClientTransport) {
    80    a.transport = transport
    81  }