github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/generator/templates/contrib/stratoscale/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 "net/http" 15 "github.com/go-openapi/errors" 16 "github.com/go-openapi/swag" 17 "github.com/go-openapi/runtime" 18 "github.com/go-openapi/validate" 19 20 strfmt "github.com/go-openapi/strfmt" 21 22 {{ imports .DefaultImports }} 23 {{ imports .Imports }} 24 ) 25 26 //go:generate mockery --name API --keeptree --with-expecter --case underscore 27 28 // API is the interface of the {{ humanize .Name }} client 29 type API interface { 30 {{ range .Operations -}} 31 /* 32 {{ pascalize .Name }} {{ if .Summary }}{{ pluralizeFirstWord (humanize .Summary) }}{{ if .Description }} 33 34 {{ blockcomment .Description }}{{ end }}{{ else if .Description}}{{ blockcomment .Description }}{{ else }}{{ humanize .Name }} API{{ end -}} 35 */ 36 {{ pascalize .Name }}(ctx context.Context, params *{{ pascalize .Name }}Params{{ if .HasStreamingResponse }}, writer io.Writer{{ end }}) {{ if .SuccessResponse }}({{ range .SuccessResponses }}*{{ pascalize .Name }}, {{ end }}{{ end }}error{{ if .SuccessResponse }}){{ end }} 37 {{ end -}} 38 } 39 40 // New creates a new {{ humanize .Name }} API client. 41 func New(transport runtime.ClientTransport, formats strfmt.Registry, authInfo runtime.ClientAuthInfoWriter) *Client { 42 return &Client{ 43 transport: transport, 44 formats: formats, 45 authInfo: authInfo, 46 } 47 } 48 49 /* 50 Client {{ if .Summary }}{{ .Summary }}{{ if .Description }} 51 52 {{ blockcomment .Description }}{{ end }}{{ else if .Description}}{{ blockcomment .Description }}{{ else }}for {{ humanize .Name }} API{{ end }} 53 */ 54 type Client struct { 55 transport runtime.ClientTransport 56 formats strfmt.Registry 57 authInfo runtime.ClientAuthInfoWriter 58 } 59 60 {{ range .Operations -}} 61 /* 62 {{ pascalize .Name }} {{ if .Summary }}{{ pluralizeFirstWord (humanize .Summary) }}{{ if .Description }} 63 64 {{ blockcomment .Description }}{{ end }}{{ else if .Description}}{{ blockcomment .Description }}{{ else }}{{ humanize .Name }} API{{ end }} 65 */ 66 func (a *Client) {{ pascalize .Name }}(ctx context.Context, params *{{ pascalize .Name }}Params{{ if .HasStreamingResponse }}, writer io.Writer{{ end }}) {{ if .SuccessResponse }}({{ range .SuccessResponses }}*{{ pascalize .Name }}, {{ end }}{{ end }}error{{ if .SuccessResponse }}){{ end }} { 67 {{ $length := len .SuccessResponses }} 68 {{ $success := .SuccessResponses }} 69 {{ if .Responses }}result{{else}}_{{end}}, err := a.transport.Submit(&runtime.ClientOperation{ 70 ID: {{ printf "%q" .Name }}, 71 Method: {{ printf "%q" .Method }}, 72 PathPattern: {{ printf "%q" .Path }}, 73 ProducesMediaTypes: {{ printf "%#v" .ProducesMediaTypes }}, 74 ConsumesMediaTypes: {{ printf "%#v" .ConsumesMediaTypes }}, 75 Schemes: {{ printf "%#v" .Schemes }}, 76 Params: params, 77 Reader: &{{ pascalize .Name }}Reader{formats: a.formats{{ if .HasStreamingResponse }}, writer: writer{{ end }}}, 78 {{ if .Authorized -}} 79 AuthInfo: a.authInfo, 80 {{ end -}} 81 Context: ctx, 82 Client: params.HTTPClient, 83 }) 84 if err != nil { 85 return {{ if $success }}{{ padSurround "nil" "nil" 0 $length }}, {{ end }}err 86 } 87 {{- if .Responses }} 88 switch value := result.(type) { 89 {{- range $i, $v := .Responses }} 90 case *{{ pascalize $v.Name }}: 91 {{- if $v.IsSuccess }} 92 return {{ if $success }}{{ padSurround "value" "nil" $i $length }},{{ end }}nil 93 {{- else }} 94 return {{ if $success }}{{ padSurround "nil" "nil" 0 $length }},{{ end }}runtime.NewAPIError("unsuccessful response", value, value.Code()) 95 {{- end }} 96 {{- end }} 97 } 98 {{- if .DefaultResponse }} 99 // unexpected success response 100 unexpectedSuccess := result.(*{{ pascalize .DefaultResponse.Name }}) 101 return {{ if $success }}{{ padSurround "nil" "nil" 0 $length }}, {{ end }}runtime.NewAPIError("unexpected success response: content available as default response in error", unexpectedSuccess, unexpectedSuccess.Code()) 102 {{- else }} 103 // safeguard: normally, absent a default response, unknown success responses return an error above: so this is a codegen issue 104 msg := fmt.Sprintf("unexpected success response for {{ .Name }}: API contract not enforced by server. Client expected to get an error, but got: %T", result) 105 panic(msg) 106 {{- end }} 107 {{- else }} 108 return nil 109 {{- end }} 110 } 111 {{ end }}