github.com/6543-forks/go-swagger@v0.26.0/generator/templates/client/response.gotmpl (about)

     1  {{ define "clientresponse" }}// New{{ pascalize .Name }} creates a {{ pascalize .Name }} with default headers values
     2  func New{{ pascalize .Name }}({{ if eq .Code -1 }}code int{{ end }}{{ if .Schema }}{{ if and (eq .Code -1) .Schema.IsStream }}, {{end}}{{ if .Schema.IsStream }}writer io.Writer{{ end }}{{ end }}) *{{ pascalize .Name }} {
     3    return &{{ pascalize .Name }}{
     4      {{ if eq .Code -1 }}_statusCode: code,
     5      {{ end }}{{ range .Headers }}{{ if .HasDefault }}{{ pascalize .Name }}: {{ printf "%#v" .Default }},
     6      {{ end }}{{ end }}{{ if .Schema }}{{ if .Schema.IsStream }}Payload: writer,
     7      {{ end }}{{ end }}}
     8  }
     9  
    10  /*{{ pascalize .Name}} handles this case with default header values.
    11  
    12  {{ if .Description }}{{ blockcomment .Description }}{{else}}{{ pascalize .Name }} {{ humanize .Name }}{{end}}
    13  */
    14  type {{ pascalize .Name }} struct {
    15    {{ if eq .Code -1 }}
    16    _statusCode int
    17  
    18    {{ end }}{{ range .Headers }}{{ if .Description }}/*{{ blockcomment .Description }}
    19    */{{ end }}
    20    {{ pascalize .Name }} {{ .GoType }}
    21    {{ end }}
    22    {{ if .Schema }}
    23    Payload {{ if and (not .Schema.IsBaseType) (not .Schema.IsInterface) .Schema.IsComplexObject (not .Schema.IsStream) }}*{{ end }}{{ if (not .Schema.IsStream) }}{{ .Schema.GoType }}{{ else }}io.Writer{{end}}
    24    {{ end }}
    25  }{{ if eq .Code -1 }}
    26  
    27  // Code gets the status code for the {{ humanize .Name }} response
    28  func ({{ .ReceiverName }} *{{ pascalize .Name }}) Code() int {
    29    return {{ .ReceiverName }}._statusCode
    30  }
    31  {{ end }}
    32  
    33  
    34  func ({{ .ReceiverName }} *{{ pascalize .Name }}) Error() string {
    35  	return fmt.Sprintf("[{{ upper .Method }} {{ .Path }}][%d] {{ if .Name }}{{ .Name }} {{ else }}unknown error {{ end }}{{ if .Schema }} %+v{{ end }}", {{ if eq .Code -1 }}{{ .ReceiverName }}._statusCode{{ else }}{{ .Code }}{{ end }}{{ if .Schema }}, o.Payload{{ end }})
    36  }
    37  
    38  {{ if .Schema }}
    39  func ({{ .ReceiverName }} *{{ pascalize .Name }}) GetPayload() {{ if and (not .Schema.IsBaseType) (not .Schema.IsInterface) .Schema.IsComplexObject (not .Schema.IsStream) }}*{{ end }}{{ if (not .Schema.IsStream) }}{{ .Schema.GoType }}{{ else }}io.Writer{{end}} {
    40  	return o.Payload
    41  }
    42  {{ end }}
    43  
    44  func ({{ .ReceiverName }} *{{ pascalize .Name }}) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error {
    45    {{ range .Headers }}
    46    // response header {{.Name}}
    47    {{if .Converter }}{{ camelize .Name }}, err := {{ .Converter }}(response.GetHeader("{{ .Name }}"))
    48    if err != nil {
    49      return errors.InvalidType({{ .Path }}, "header", "{{ .GoType }}", response.GetHeader("{{ .Name }}"))
    50    }
    51    {{ .ReceiverName }}.{{ pascalize .Name }} = {{ camelize .Name }}
    52    {{ else if .IsCustomFormatter }}
    53    {{ camelize .Name }}, err := formats.Parse({{ printf "%q" .SwaggerFormat }}, response.GetHeader("{{ .Name }}"))
    54    if err != nil {
    55      return errors.InvalidType({{ .Path }}, "header", "{{ .GoType }}", response.GetHeader("{{ .Name }}"))
    56    }
    57    {{ .ReceiverName }}.{{ pascalize .Name }} = *({{ camelize .Name }}.(*{{ .GoType }}))
    58    {{ else}}{{ .ReceiverName }}.{{ pascalize .Name }} = response.GetHeader("{{ .Name }}")
    59    {{end}}
    60    {{ end }}
    61    {{ if .Schema }}
    62    {{ if .Schema.IsBaseType }}
    63    // response payload as interface type
    64    payload, err := {{ toPackageName .ModelsPackage }}.Unmarshal{{ dropPackage .Schema.GoType }}{{ if .Schema.IsArray}}Slice{{ end }}(response.Body(), consumer)
    65    if err != nil {
    66      return err
    67    }
    68    {{ .ReceiverName }}.Payload = payload
    69    {{ else if .Schema.IsComplexObject }}
    70    {{ .ReceiverName }}.Payload = new({{ .Schema.GoType }})
    71    {{ end }}{{ if not .Schema.IsBaseType }}
    72    // response payload
    73    if err := consumer.Consume(response.Body(), {{ if not (or .Schema.IsComplexObject .Schema.IsStream) }}&{{ end}}{{ .ReceiverName }}.Payload); err != nil && err != io.EOF {
    74      return err
    75    }
    76    {{ end }}{{ end }}
    77    return nil
    78  }
    79  {{ end }}// Code generated by go-swagger; DO NOT EDIT.
    80  
    81  
    82  {{ if .Copyright -}}// {{ comment .Copyright -}}{{ end }}
    83  
    84  
    85  package {{ .Package }}
    86  
    87  // This file was generated by the swagger tool.
    88  // Editing this file might prove futile when you re-run the swagger generate command
    89  
    90  
    91  import (
    92    "io"
    93    "net/http"
    94  
    95    "github.com/go-openapi/errors"
    96    "github.com/go-openapi/runtime"
    97    "github.com/go-openapi/strfmt"
    98    "github.com/go-openapi/swag"
    99    "github.com/go-openapi/validate"
   100  
   101    {{ imports .DefaultImports }}
   102    {{ imports .Imports }}
   103  )
   104  
   105  // {{ pascalize .Name }}Reader is a Reader for the {{ pascalize .Name }} structure.
   106  type {{ pascalize .Name }}Reader struct {
   107    formats strfmt.Registry{{ if .HasStreamingResponse }}
   108    writer  io.Writer{{ end }}
   109  }
   110  
   111  // ReadResponse reads a server response into the received {{ .ReceiverName }}.
   112  func ({{ .ReceiverName }} *{{ pascalize .Name }}Reader) ReadResponse(response runtime.ClientResponse, consumer runtime.Consumer) (interface{}, error) {
   113    {{- if .Responses}}
   114    switch response.Code() {
   115    {{- end }}
   116    {{- range .Responses }}
   117      case {{ .Code }}:
   118        result := New{{ pascalize .Name }}({{ if .Schema }}{{ if .Schema.IsStream }}{{ $.ReceiverName }}.writer{{ end }}{{ end }})
   119        if err := result.readResponse(response, consumer, {{ $.ReceiverName }}.formats); err != nil {
   120          return nil, err
   121        }
   122        return {{ if .IsSuccess }}result, nil{{else}}nil, result{{end}}
   123    {{- end }}
   124    {{- if .DefaultResponse }}
   125      {{- with .DefaultResponse }}
   126        {{- if $.Responses}}
   127      default:
   128        {{- end }}
   129        result := New{{ pascalize .Name }}(response.Code(){{ if .Schema }}{{ if .Schema.IsStream }}, {{ $.ReceiverName }}.writer{{ end }}{{ end }})
   130        if err := result.readResponse(response, consumer, {{ $.ReceiverName }}.formats); err != nil {
   131          return nil, err
   132        }
   133        if response.Code() / 100 == 2 {
   134          return result, nil
   135        }
   136        return nil, result
   137      {{- end }}
   138    {{- else }}
   139      {{ if $.Responses}}
   140      default:
   141      {{- end }}
   142        return nil, runtime.NewAPIError("response status code does not match any response statuses defined for this endpoint in the swagger spec", response, response.Code())
   143    {{- end }}
   144    {{- if .Responses}}
   145    }
   146    {{- end }}
   147  }
   148  
   149  {{ range .Responses }}
   150  {{ template "clientresponse" . }}
   151  {{ end }}
   152  {{ if .DefaultResponse }}
   153  {{ template "clientresponse" .DefaultResponse }}
   154  {{ end }}
   155  
   156  {{ range .ExtraSchemas }}
   157  /*{{ pascalize .Name }} {{ template "docstring" . }}
   158  swagger:model {{ .Name }}
   159  */
   160  {{- template "schema" . }}
   161  {{- end }}