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