124 lines
4.8 KiB
Cheetah
124 lines
4.8 KiB
Cheetah
// ClientWithResponses builds on ClientInterface to offer response payloads
|
|
type ClientWithResponses struct {
|
|
ClientInterface
|
|
}
|
|
|
|
// NewClientWithResponses creates a new ClientWithResponses, which wraps
|
|
// Client with return type handling
|
|
func NewClientWithResponses(server string, opts ...ClientOption) (*ClientWithResponses, error) {
|
|
client, err := NewClient(server, opts...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &ClientWithResponses{client}, nil
|
|
}
|
|
|
|
{{$clientTypeName := opts.OutputOptions.ClientTypeName -}}
|
|
|
|
// WithBaseURL overrides the baseURL.
|
|
func WithBaseURL(baseURL string) ClientOption {
|
|
return func(c *{{ $clientTypeName }}) error {
|
|
newBaseURL, err := url.Parse(baseURL)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
c.Server = newBaseURL.String()
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// ClientWithResponsesInterface is the interface specification for the client with responses above.
|
|
type ClientWithResponsesInterface interface {
|
|
{{range . -}}
|
|
{{$hasParams := .RequiresParamObject -}}
|
|
{{$pathParams := .PathParams -}}
|
|
{{$opid := .OperationId -}}
|
|
// {{$opid}}{{if .HasBody}}WithBody{{end}}WithResponse request{{if .HasBody}} with any body{{end}}
|
|
{{$opid}}{{if .HasBody}}WithBody{{end}}WithResponse(ctx context.Context{{genParamArgs .PathParams}}{{if .RequiresParamObject}}, params *{{$opid}}Params{{end}}{{if .HasBody}}, contentType string, body io.Reader{{end}}, reqEditors... RequestEditorFn) (*{{genResponseTypeName $opid}}Container, error)
|
|
{{range .Bodies}}
|
|
{{if .IsSupportedByClient -}}
|
|
{{$opid}}{{.Suffix}}WithResponse(ctx context.Context{{genParamArgs $pathParams}}{{if $hasParams}}, params *{{$opid}}Params{{end}}, body {{$opid}}{{.NameTag}}RequestBody, reqEditors... RequestEditorFn) (*{{genResponseTypeName $opid}}Container, error)
|
|
{{end -}}
|
|
{{end}}{{/* range .Bodies */}}
|
|
{{end}}{{/* range . $opid := .OperationId */}}
|
|
}
|
|
|
|
{{range .}}{{$opid := .OperationId}}{{$op := .}}
|
|
type {{genResponseTypeName $opid | ucFirst}}Container struct {
|
|
Body []byte
|
|
HTTPResponse *http.Response
|
|
{{- range getResponseTypeDefinitions .}}
|
|
{{.TypeName}} *{{.Schema.TypeDecl}}
|
|
{{- end}}
|
|
}
|
|
|
|
// Status returns HTTPResponse.Status
|
|
func (r {{genResponseTypeName $opid | ucFirst}}Container) Status() string {
|
|
if r.HTTPResponse != nil {
|
|
return r.HTTPResponse.Status
|
|
}
|
|
return http.StatusText(0)
|
|
}
|
|
|
|
// StatusCode returns HTTPResponse.StatusCode
|
|
func (r {{genResponseTypeName $opid | ucFirst}}Container) StatusCode() int {
|
|
if r.HTTPResponse != nil {
|
|
return r.HTTPResponse.StatusCode
|
|
}
|
|
return 0
|
|
}
|
|
{{end}}
|
|
|
|
|
|
{{range .}}
|
|
{{$opid := .OperationId -}}
|
|
{{/* Generate client methods (with responses)*/}}
|
|
|
|
// {{$opid}}{{if .HasBody}}WithBody{{end}}WithResponse request{{if .HasBody}} with arbitrary body{{end}} returning *{{genResponseTypeName $opid}}Container
|
|
func (c *ClientWithResponses) {{$opid}}{{if .HasBody}}WithBody{{end}}WithResponse(ctx context.Context{{genParamArgs .PathParams}}{{if .RequiresParamObject}}, params *{{$opid}}Params{{end}}{{if .HasBody}}, contentType string, body io.Reader{{end}}, reqEditors... RequestEditorFn) (*{{genResponseTypeName $opid}}Container, error){
|
|
rsp, err := c.{{$opid}}{{if .HasBody}}WithBody{{end}}(ctx{{genParamNames .PathParams}}{{if .RequiresParamObject}}, params{{end}}{{if .HasBody}}, contentType, body{{end}}, reqEditors...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return Parse{{genResponseTypeName $opid | ucFirst}}Container(rsp)
|
|
}
|
|
|
|
{{$hasParams := .RequiresParamObject -}}
|
|
{{$pathParams := .PathParams -}}
|
|
{{$bodyRequired := .BodyRequired -}}
|
|
{{range .Bodies}}
|
|
{{if .IsSupportedByClient -}}
|
|
func (c *ClientWithResponses) {{$opid}}{{.Suffix}}WithResponse(ctx context.Context{{genParamArgs $pathParams}}{{if $hasParams}}, params *{{$opid}}Params{{end}}, body {{$opid}}{{.NameTag}}RequestBody, reqEditors... RequestEditorFn) (*{{genResponseTypeName $opid}}Container, error) {
|
|
rsp, err := c.{{$opid}}{{.Suffix}}(ctx{{genParamNames $pathParams}}{{if $hasParams}}, params{{end}}, body, reqEditors...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return Parse{{genResponseTypeName $opid | ucFirst}}Container(rsp)
|
|
}
|
|
{{end}}
|
|
{{end}}
|
|
|
|
{{end}}{{/* operations */}}
|
|
|
|
{{/* Generate parse functions for responses*/}}
|
|
{{range .}}{{$opid := .OperationId}}
|
|
|
|
// Parse{{genResponseTypeName $opid | ucFirst}} parses an HTTP response from a {{$opid}}WithResponse call
|
|
func Parse{{genResponseTypeName $opid | ucFirst}}Container(rsp *http.Response) (*{{genResponseTypeName $opid}}Container, error) {
|
|
bodyBytes, err := io.ReadAll(rsp.Body)
|
|
defer func() { _ = rsp.Body.Close() }()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
response := &{{genResponseTypeName $opid | ucFirst}}Container{
|
|
Body: bodyBytes,
|
|
HTTPResponse: rsp,
|
|
}
|
|
|
|
{{genResponseUnmarshal .}}
|
|
|
|
return response, nil
|
|
}
|
|
{{end}}{{/* range . $opid := .OperationId */}}
|