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