github.com/emreu/go-swagger@v0.22.1/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  /*
    44  {{ pascalize .Name }} {{ if .Summary }}{{ pluralizeFirstWord (humanize .Summary) }}{{ if .Description }}
    45  
    46  {{ blockcomment .Description }}{{ end }}{{ else if .Description}}{{ blockcomment .Description }}{{ else }}{{ humanize .Name }} API{{ end }}
    47  */
    48  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 }} {
    49    // TODO: Validate the params before sending
    50    if params == nil {
    51      params = New{{ pascalize .Name }}Params()
    52    }
    53    {{ $length := len .SuccessResponses }}
    54    {{ if .SuccessResponse }}result{{else}}_{{ end }}, err := a.transport.Submit(&runtime.ClientOperation{
    55      ID: {{ printf "%q" .Name }},
    56      Method: {{ printf "%q" .Method }},
    57      PathPattern: {{ printf "%q" .Path }},
    58      ProducesMediaTypes: {{ printf "%#v" .ProducesMediaTypes }},
    59      ConsumesMediaTypes: {{ printf "%#v" .ConsumesMediaTypes }},
    60      Schemes: {{ printf "%#v" .Schemes }},
    61      Params: params,
    62      Reader: &{{ pascalize .Name }}Reader{formats: a.formats{{ if .HasStreamingResponse }}, writer: writer{{ end }}},{{ if .Authorized }}
    63      AuthInfo: authInfo,{{ end}}
    64      Context: params.Context,
    65      Client: params.HTTPClient,
    66    })
    67    if err != nil {
    68      return {{ if .SuccessResponse }}{{ padSurround "nil" "nil" 0 $length }}, {{ end }}err
    69    }
    70    {{- if .SuccessResponse }}
    71      {{- if eq $length 1 }}
    72    success, ok := result.(*{{ pascalize .SuccessResponse.Name }})
    73    if ok {
    74      return success,nil
    75    }
    76    // unexpected success response
    77        {{- if .DefaultResponse }}{{/* if a default response is provided, fill this and return an error */}}
    78    unexpectedSuccess := result.(*{{ pascalize .DefaultResponse.Name }})
    79    return nil, runtime.NewAPIError("unexpected success response: content available as default response in error", unexpectedSuccess, unexpectedSuccess.Code())
    80        {{- else }}
    81    // safeguard: normally, absent a default response, unknown success responses return an error above: so this is a codegen issue
    82    msg := fmt.Sprintf("unexpected success response for {{ .Name }}: API contract not enforced by server. Client expected to get an error, but got: %T", result)
    83    panic(msg)
    84        {{- end }}
    85      {{- else }}{{/* several possible success responses */}}
    86    switch value := result.(type) {
    87        {{- range $i, $v := .SuccessResponses }}
    88    case *{{ pascalize $v.Name }}:
    89      return {{ padSurround "value" "nil" $i $length }}, nil
    90        {{- end }}
    91    }
    92        {{- if .DefaultResponse }}{{/* if a default response is provided, fill this and return an error */}}
    93    // unexpected success response
    94    unexpectedSuccess := result.(*{{ pascalize .DefaultResponse.Name }})
    95    return {{ padSurround "nil" "nil" 0 $length }}, runtime.NewAPIError("unexpected success response: content available as default response in error", unexpectedSuccess, unexpectedSuccess.Code())
    96        {{- else }}
    97    // safeguard: normally, absent a default response, unknown success responses return an error above: so this is a codegen issue
    98    msg := fmt.Sprintf("unexpected success response for {{ $.Name }}: API contract not enforced by server. Client expected to get an error, but got: %T", result)
    99    panic(msg)
   100        {{- end }}
   101      {{- end }}
   102    {{- else }}
   103    return nil
   104    {{- end }}
   105  }
   106  {{- end }}
   107  
   108  // SetTransport changes the transport on the client
   109  func (a *Client) SetTransport(transport runtime.ClientTransport) {
   110    a.transport = transport
   111  }