github.com/AngusLu/go-swagger@v0.28.0/generator/templates/client/response.gotmpl (about)

     1  {{- define "clientresponse" }}
     2  // New{{ pascalize .Name }} creates a {{ pascalize .Name }} with default headers values
     3  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 }} {
     4    {{- if .Headers.HasSomeDefaults }}
     5    var (
     6    // initialize headers with default values
     7      {{- range .Headers }}
     8        {{- if .HasDefault }}
     9          {{ template "simpleschemaDefaultsvar" . }}
    10         {{- end }}
    11      {{- end }}
    12    )
    13      {{- range .Headers }}
    14        {{- if .HasDefault }}
    15          {{ template "simpleschemaDefaultsinit" . }}
    16        {{- end }}
    17      {{- end }}
    18    {{- end }}
    19    return &{{ pascalize .Name }}{
    20      {{- if eq .Code -1 }}
    21      _statusCode: code,
    22      {{- end }}
    23      {{ range .Headers }}
    24        {{- if .HasDefault }}
    25      {{ pascalize .Name}}: {{ if and (not .IsArray) (not .HasDiscriminator) (not .IsInterface) (not .IsStream) .IsNullable }}&{{ end }}{{ varname .ID }}Default,
    26        {{- end }}
    27      {{- end }}
    28      {{- if .Schema }}
    29        {{- if .Schema.IsStream }}
    30      Payload: writer,
    31        {{- end }}
    32      {{- end }}
    33      }
    34  }
    35  
    36  /* {{ pascalize .Name}} describes a response with status code {{ .Code }}, with default header values.
    37  
    38   {{ if .Description }}{{ blockcomment .Description }}{{else}}{{ pascalize .Name }} {{ humanize .Name }}{{end}}
    39   */
    40  type {{ pascalize .Name }} struct {
    41    {{- if eq .Code -1 }}
    42    _statusCode int
    43    {{- end }}
    44    {{- range .Headers }}
    45      {{- if .Description }}
    46  
    47    /* {{ blockcomment .Description }}
    48       {{- if or .SwaggerFormat .Default }}
    49         {{ print "" }}
    50         {{- if .SwaggerFormat }}
    51       Format: {{ .SwaggerFormat }}
    52         {{- end }}
    53         {{- if .Default }}
    54       Default: {{ json .Default }}
    55         {{- end }}
    56       {{- end }}
    57    */
    58      {{- end }}
    59    {{ pascalize .Name }} {{ .GoType }}
    60    {{- end }}
    61    {{- if .Schema }}
    62  
    63    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}}
    64    {{- end }}
    65  }
    66    {{- if eq .Code -1 }}
    67  
    68  // Code gets the status code for the {{ humanize .Name }} response
    69  func ({{ .ReceiverName }} *{{ pascalize .Name }}) Code() int {
    70    return {{ .ReceiverName }}._statusCode
    71  }
    72    {{- end }}
    73  
    74  
    75  func ({{ .ReceiverName }} *{{ pascalize .Name }}) Error() string {
    76  	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 }})
    77  }
    78  
    79    {{- if .Schema }}
    80  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}} {
    81  	return o.Payload
    82  }
    83    {{- end }}
    84  
    85  func ({{ .ReceiverName }} *{{ pascalize .Name }}) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error {
    86    {{- range .Headers }}
    87  
    88    // hydrates response header {{.Name}}
    89    hdr{{ pascalize .Name }} := response.GetHeader("{{ .Name }}")
    90  
    91    if hdr{{ pascalize .Name }} != "" {
    92      {{- if .Converter }}
    93    val{{ camelize .Name }}, err := {{ .Converter }}(hdr{{ pascalize .Name }})
    94    if err != nil {
    95      return errors.InvalidType({{ .Path }}, "header", "{{ .GoType }}", hdr{{ pascalize .Name }})
    96    }
    97    {{ .ReceiverName }}.{{ pascalize .Name }} = val{{ camelize .Name }}
    98      {{- else if .Child }}
    99  
   100    // binding header items for {{ .Name }}
   101    val{{ pascalize .Name }}, err := {{ .ReceiverName }}.bindHeader{{ pascalize .Name }}(hdr{{ pascalize .Name }}, formats)
   102    if err != nil {
   103      return err
   104    }
   105  
   106    {{ .ReceiverName }}.{{ pascalize .Name }} = val{{ pascalize .Name }}
   107      {{- else if .IsCustomFormatter }}
   108    val{{ camelize .Name }}, err := formats.Parse({{ printf "%q" .SwaggerFormat }}, hdr{{ pascalize .Name }})
   109    if err != nil {
   110      return errors.InvalidType({{ .Path }}, "header", "{{ .GoType }}", hdr{{ pascalize .Name }})
   111    }
   112        {{- if .IsNullable }}
   113    v := (val{{ camelize .Name }}.({{ .GoType }}))
   114    {{ .ReceiverName }}.{{ pascalize .Name }} = &v
   115        {{- else }}
   116    {{ .ReceiverName }}.{{ pascalize .Name }} = *(val{{ camelize .Name }}.(*{{ .GoType }}))
   117        {{- end }}
   118      {{- else }}
   119        {{- if eq .GoType "string" }}
   120    {{ .ReceiverName }}.{{ pascalize .Name }} = hdr{{ pascalize .Name }}
   121        {{- else }}
   122    {{ .ReceiverName }}.{{ pascalize .Name }} = {{ .GoType }}(hdr{{ pascalize .Name }})
   123        {{- end }}
   124      {{- end }}
   125    }
   126    {{-  end }}
   127  
   128    {{- if .Schema }}
   129      {{- if .Schema.IsBaseType }}
   130  
   131    // response payload as interface type
   132    payload, err := {{ toPackageName .ModelsPackage }}.Unmarshal{{ dropPackage .Schema.GoType }}{{ if .Schema.IsArray}}Slice{{ end }}(response.Body(), consumer)
   133    if err != nil {
   134      return err
   135    }
   136    {{ .ReceiverName }}.Payload = payload
   137      {{- else if .Schema.IsComplexObject }}
   138  
   139    {{ .ReceiverName }}.Payload = new({{ .Schema.GoType }})
   140      {{- end }}
   141      {{- if not .Schema.IsBaseType }}
   142  
   143    // response payload
   144    if err := consumer.Consume(response.Body(), {{ if not (or .Schema.IsComplexObject .Schema.IsStream) }}&{{ end}}{{ .ReceiverName }}.Payload); err != nil && err != io.EOF {
   145      return err
   146    }
   147      {{- end }}
   148    {{- end }}
   149  
   150    return nil
   151  }
   152    {{- range .Headers }}
   153      {{- if .Child }}
   154  
   155  // bindHeader{{ pascalize $.Name }} binds the response header {{ .Name }}
   156  func ({{ .ReceiverName }} *{{ pascalize $.Name }}) bindHeader{{ pascalize .Name }}(hdr string, formats strfmt.Registry) ({{ .GoType }}, error) {
   157    {{ varname .Child.ValueExpression }}V := hdr
   158  
   159    {{ template "sliceclientheaderbinder" . }}
   160  
   161    return {{ varname .Child.ValueExpression }}C, nil
   162  }
   163      {{- end }}
   164    {{- end }}
   165  {{- end }}
   166  // Code generated by go-swagger; DO NOT EDIT.
   167  
   168  
   169  {{ if .Copyright -}}// {{ comment .Copyright -}}{{ end }}
   170  
   171  
   172  package {{ .Package }}
   173  
   174  // This file was generated by the swagger tool.
   175  // Editing this file might prove futile when you re-run the swagger generate command
   176  
   177  
   178  import (
   179    "io"
   180    "net/http"
   181  
   182    "github.com/go-openapi/errors"
   183    "github.com/go-openapi/runtime"
   184    "github.com/go-openapi/strfmt"
   185    "github.com/go-openapi/swag"
   186    "github.com/go-openapi/validate"
   187  
   188    {{ imports .DefaultImports }}
   189    {{ imports .Imports }}
   190  )
   191  
   192  // {{ pascalize .Name }}Reader is a Reader for the {{ pascalize .Name }} structure.
   193  type {{ pascalize .Name }}Reader struct {
   194    formats strfmt.Registry
   195  {{- if .HasStreamingResponse }}
   196    writer  io.Writer
   197  {{- end }}
   198  }
   199  
   200  // ReadResponse reads a server response into the received {{ .ReceiverName }}.
   201  func ({{ .ReceiverName }} *{{ pascalize .Name }}Reader) ReadResponse(response runtime.ClientResponse, consumer runtime.Consumer) (interface{}, error) {
   202    {{- if .Responses}}
   203    switch response.Code() {
   204    {{- end }}
   205    {{- range .Responses }}
   206      case {{ .Code }}:
   207        result := New{{ pascalize .Name }}({{ if .Schema }}{{ if .Schema.IsStream }}{{ $.ReceiverName }}.writer{{ end }}{{ end }})
   208        if err := result.readResponse(response, consumer, {{ $.ReceiverName }}.formats); err != nil {
   209          return nil, err
   210        }
   211        return {{ if .IsSuccess }}result, nil{{else}}nil, result{{ end }}
   212    {{- end }}
   213    {{- if .DefaultResponse }}
   214      {{- with .DefaultResponse }}
   215        {{- if $.Responses}}
   216      default:
   217        {{- end }}
   218        result := New{{ pascalize .Name }}(response.Code(){{ if .Schema }}{{ if .Schema.IsStream }}, {{ $.ReceiverName }}.writer{{ end }}{{ end }})
   219        if err := result.readResponse(response, consumer, {{ $.ReceiverName }}.formats); err != nil {
   220          return nil, err
   221        }
   222        if response.Code() / 100 == 2 {
   223          return result, nil
   224        }
   225        return nil, result
   226      {{- end }}
   227    {{- else }}
   228      {{- if $.Responses}}
   229      default:
   230      {{- end }}
   231        return nil, runtime.NewAPIError("response status code does not match any response statuses defined for this endpoint in the swagger spec", response, response.Code())
   232    {{- end }}
   233    {{- if .Responses}}
   234    }
   235    {{- end }}
   236  }
   237  
   238  {{ range .Responses }}
   239    {{ template "clientresponse" . }}
   240  {{ end }}
   241  {{ if .DefaultResponse }}
   242    {{ template "clientresponse" .DefaultResponse }}
   243  {{ end }}
   244  
   245  {{ range .ExtraSchemas }}
   246  /*{{ pascalize .Name }} {{ template "docstring" . }}
   247  swagger:model {{ .Name }}
   248  */
   249    {{- template "schema" . }}
   250  {{- end }}
   251  
   252  {{- define "sliceclientheaderbinder" }}
   253   {{- if .IsArray }}
   254   var (
   255     {{ varname .Child.ValueExpression }}C {{ .GoType }}
   256   )
   257   // {{ .Child.ItemsDepth }}CollectionFormat: {{ printf "%q" .CollectionFormat }}
   258   {{ varname .Child.ValueExpression }}R := swag.SplitByFormat({{ varname .Child.ValueExpression }}V, {{ printf "%q" .CollectionFormat }})
   259  
   260   for {{ if or .Child.IsCustomFormatter .Child.Converter }}{{ .IndexVar }}{{ else }}_{{ end }}, {{ varname .Child.ValueExpression }}IV := range {{ varname .Child.ValueExpression }}R {
   261     {{ template "sliceclientheaderbinder" .Child }}
   262     {{ varname .Child.ValueExpression }}C = append({{ varname .Child.ValueExpression }}C, {{ varname .Child.ValueExpression }}IC) // roll-up {{ .Child.GoType }} into {{ .GoType }}
   263   }
   264  
   265   {{- else }}
   266     // convert split string to {{ .GoType }}
   267     {{- if .IsCustomFormatter }}
   268   val, err := formats.Parse({{ printf "%q" .SwaggerFormat }}, {{ varname .ValueExpression }}IV)
   269   if err != nil {
   270     return nil, errors.InvalidType({{ .Path }}, "header{{ .ItemsDepth }}", "{{ .GoType }}", {{ varname .ValueExpression }}IV)
   271   }
   272       {{- if .IsNullable }}
   273   {{ varname .ValueExpression }}IC := (&val).(*{{ .GoType }})
   274       {{- else }}
   275   {{ varname .ValueExpression }}IC := val.({{ .GoType }})
   276       {{- end }}
   277     {{- else if .Converter }}
   278   val, err := {{- print " "}}{{ .Converter }}({{ varname .ValueExpression }}IV)
   279   if err != nil {
   280     return nil, errors.InvalidType({{ .Path }}, "header{{ .ItemsDepth }}", "{{ .GoType }}", {{ varname .ValueExpression }}IV)
   281   }
   282       {{- if .IsNullable }}
   283   {{ varname .ValueExpression }}IC := &val
   284       {{- else }}
   285   {{ varname .ValueExpression }}IC := val
   286       {{- end }}
   287     {{- else }}
   288     {{ varname .ValueExpression }}IC :=
   289       {{- if eq .GoType "string" }}
   290         {{- print " " }}{{ varname .ValueExpression }}IV
   291       {{- else }}
   292         {{- print " " }}fmt.Sprintf("%v", {{ varname .ValueExpression }}IV)
   293       {{- end }} // string as {{ .GoType }}
   294     {{- end }}
   295   {{- end }}
   296  {{- end }}