client-with-responses.tmpl 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. {{/* Generate client methods */}}
  2. {{range .}}
  3. {{if (hasValidRequestAndResponse .) -}}{{/* skip non-JSON bodies*/}}
  4. {{$hasParams := .RequiresParamObject -}}
  5. {{$pathParams := .PathParams -}}
  6. {{$opid := .OperationId -}}
  7. {{$hasResponse := hasSingle2xxJSONResponse . -}}
  8. {{$lenServers := 0 -}}
  9. {{if .Spec.Servers}}
  10. {{$lenServers = len .Spec.Servers -}}
  11. {{end}}
  12. {{$response2xx := get2xxResponseTypeDefinition . -}}
  13. {{$hasBodyOrPathParams := or .HasBody (gt (len .PathParams) 0) -}}
  14. {{$pathStr := genParamFmtString .Path -}}
  15. // {{$opid}} calls the {{.Method}} on {{.Path}}
  16. // {{.Summary}}
  17. func (c *Client) {{$opid}}(ctx context.Context, {{if $hasBodyOrPathParams}}params *{{$opid}}AllParams{{else}}{{if $hasParams}} params *{{$opid}}Params{{end}}{{end}}) ({{if $hasResponse}}*{{$response2xx.Schema.TypeDecl}},{{end}} error) {
  18. var err error
  19. {{if .HasBody -}}
  20. var bodyReader io.Reader
  21. buf, err := json.Marshal(params.Body)
  22. if err != nil {
  23. return {{if $hasResponse}}nil, {{end}}err
  24. }
  25. bodyReader = bytes.NewReader(buf)
  26. {{end}}
  27. {{range $paramIdx, $param := .PathParams}}
  28. var pathParam{{$paramIdx}} string
  29. {{if .IsPassThrough}}
  30. pathParam{{$paramIdx}} = params.{{.GoVariableName|ucFirst}}
  31. {{end}}
  32. {{if .IsJson}}
  33. var pathParamBuf{{$paramIdx}} []byte
  34. pathParamBuf{{$paramIdx}}, err = json.Marshal(params.{{.GoVariableName|ucFirst}})
  35. if err != nil {
  36. return {{if $hasResponse}}nil, {{end}}err
  37. }
  38. pathParam{{$paramIdx}} = string(pathParamBuf{{$paramIdx}})
  39. {{end}}
  40. {{if .IsStyled}}
  41. pathParam{{$paramIdx}}, err = runtime.StyleParamWithLocation("{{.Style}}", {{.Explode}}, "{{.ParamName}}", runtime.ParamLocationPath, params.{{.GoVariableName|ucFirst}})
  42. if err != nil {
  43. return {{if $hasResponse}}nil, {{end}}err
  44. }
  45. {{end}}
  46. {{end}}
  47. serverURL, err := url.Parse(c.{{if eq $lenServers 0}}APIEndpoint{{else}}Server{{end}})
  48. if err != nil {
  49. return {{if $hasResponse}}nil, {{end}}err
  50. }
  51. operationPath := fmt.Sprintf("{{if eq (index $pathStr 0) '/'}}.{{end}}{{$pathStr}}"{{range $paramIdx, $param := .PathParams}}, pathParam{{$paramIdx}}{{end}})
  52. queryURL, err := serverURL.Parse(operationPath)
  53. if err != nil {
  54. return {{if $hasResponse}}nil, {{end}}err
  55. }
  56. {{if .QueryParams}}
  57. queryValues := queryURL.Query()
  58. {{range $paramIdx, $param := .QueryParams}}
  59. {{if not .Required}} if params.{{.GoName}} != nil { {{end}}
  60. {{if .IsPassThrough}}
  61. queryValues.Add("{{.ParamName}}", {{if not .Required}}*{{end}}params.{{.GoName}})
  62. {{end}}
  63. {{if .IsJson}}
  64. if queryParamBuf, err := json.Marshal({{if not .Required}}*{{end}}params.{{.GoName}}); err != nil {
  65. return {{if $hasResponse}}nil, {{end}}err
  66. } else {
  67. queryValues.Add("{{.ParamName}}", string(queryParamBuf))
  68. }
  69. {{end}}
  70. {{if .IsStyled}}
  71. if queryFrag, err := runtime.StyleParamWithLocation("{{.Style}}", {{.Explode}}, "{{.ParamName}}", runtime.ParamLocationQuery, {{if not .Required}}*{{end}}params.{{.GoName}}); err != nil {
  72. return {{if $hasResponse}}nil, {{end}}err
  73. } else if parsed, err := url.ParseQuery(queryFrag); err != nil {
  74. return {{if $hasResponse}}nil, {{end}}err
  75. } else {
  76. for k, v := range parsed {
  77. for _, v2 := range v {
  78. queryValues.Add(k, v2)
  79. }
  80. }
  81. }
  82. {{end}}
  83. {{if not .Required}}}{{end}}
  84. {{end}}
  85. queryURL.RawQuery = queryValues.Encode()
  86. {{end}}{{/* if .QueryParams */}}
  87. req, err := http.NewRequest("{{.Method}}", queryURL.String(), {{if .HasBody}}bodyReader{{else}}nil{{end}})
  88. if err != nil {
  89. return {{if $hasResponse}}nil, {{end}}err
  90. }
  91. {{if .HasBody}}req.Header.Add("Content-Type", "{{(index .Bodies 0).ContentType}}"){{end}}
  92. {{range $paramIdx, $param := .HeaderParams}}
  93. {{if not .Required}} if params.{{.GoName}} != nil { {{end}}
  94. var headerParam{{$paramIdx}} string
  95. {{if .IsPassThrough}}
  96. headerParam{{$paramIdx}} = {{if not .Required}}*{{end}}params.{{.GoName}}
  97. {{end}}
  98. {{if .IsJson}}
  99. var headerParamBuf{{$paramIdx}} []byte
  100. headerParamBuf{{$paramIdx}}, err = json.Marshal({{if not .Required}}*{{end}}params.{{.GoName}})
  101. if err != nil {
  102. return {{if $hasResponse}}nil, {{end}}err
  103. }
  104. headerParam{{$paramIdx}} = string(headerParamBuf{{$paramIdx}})
  105. {{end}}
  106. {{if .IsStyled}}
  107. headerParam{{$paramIdx}}, err = runtime.StyleParamWithLocation("{{.Style}}", {{.Explode}}, "{{.ParamName}}", runtime.ParamLocationHeader, {{if not .Required}}*{{end}}params.{{.GoName}})
  108. if err != nil {
  109. return {{if $hasResponse}}nil, {{end}}err
  110. }
  111. {{end}}
  112. req.Header.Set("{{.ParamName}}", headerParam{{$paramIdx}})
  113. {{if not .Required}}}{{end}}
  114. {{end}}
  115. {{range $paramIdx, $param := .CookieParams}}
  116. {{if not .Required}} if params.{{.GoName}} != nil { {{end}}
  117. var cookieParam{{$paramIdx}} string
  118. {{if .IsPassThrough}}
  119. cookieParam{{$paramIdx}} = {{if not .Required}}*{{end}}params.{{.GoName}}
  120. {{end}}
  121. {{if .IsJson}}
  122. var cookieParamBuf{{$paramIdx}} []byte
  123. cookieParamBuf{{$paramIdx}}, err = json.Marshal({{if not .Required}}*{{end}}params.{{.GoName}})
  124. if err != nil {
  125. return {{if $hasResponse}}nil, {{end}}err
  126. }
  127. cookieParam{{$paramIdx}} = url.QueryEscape(string(cookieParamBuf{{$paramIdx}}))
  128. {{end}}
  129. {{if .IsStyled}}
  130. cookieParam{{$paramIdx}}, err = runtime.StyleParamWithLocation("simple", {{.Explode}}, "{{.ParamName}}", runtime.ParamLocationCookie, {{if not .Required}}*{{end}}params.{{.GoName}})
  131. if err != nil {
  132. return {{if $hasResponse}}nil, {{end}}err
  133. }
  134. {{end}}
  135. cookie{{$paramIdx}} := &http.Cookie{
  136. Name:"{{.ParamName}}",
  137. Value:cookieParam{{$paramIdx}},
  138. }
  139. req.AddCookie(cookie{{$paramIdx}})
  140. {{if not .Required}}}{{end}}
  141. {{end}}
  142. req = req.WithContext(ctx)
  143. rsp, err := c.Client.Do(req)
  144. if err != nil {
  145. return {{if $hasResponse}}nil, {{end}}err
  146. }
  147. {{if $hasResponse -}}
  148. bodyBytes, err := ioutil.ReadAll(rsp.Body)
  149. {{end}}
  150. defer func() { _ = rsp.Body.Close() }()
  151. {{if $hasResponse -}}
  152. if err != nil {
  153. return nil, err
  154. }
  155. response := &{{$response2xx.Schema.TypeDecl}}{}
  156. switch(rsp.StatusCode) {
  157. case {{$response2xx.ResponseName}}:
  158. if err := unmarshalJSONResponse(bodyBytes, &response); err != nil {
  159. return nil, err
  160. }
  161. default:
  162. return nil, decodeError(bodyBytes, rsp)
  163. }
  164. return response, nil
  165. {{else}}
  166. if rsp.StatusCode > 299 {
  167. bodyBytes, err := ioutil.ReadAll(rsp.Body)
  168. if err != nil {
  169. return err
  170. }
  171. return decodeError(bodyBytes, rsp)
  172. }
  173. return nil
  174. {{end}}
  175. }
  176. {{end}}{{/* if */}}
  177. {{end}}{{/* Range */}}
  178. /*
  179. */