Skip to content

Commit

Permalink
add interceptors and improve quality of generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
Karitham committed Aug 25, 2023
1 parent cf03382 commit 0ac89ab
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 28 deletions.
71 changes: 56 additions & 15 deletions examples/petstore-expanded/api/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 29 additions & 13 deletions templates/client.tmpl
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
type Client struct {
BaseURL string
Client *http.Client
ResponseInterceptor func(*http.Response) error
RequestOptions func(*requests.Builder) *requests.Builder
}

{{define "rt" -}}
{{if or (not (hasPrefix .Schema.GoType "*")) .Schema.SkipOptionalPointer -}}
p.{{.ParamName | ucFirst}}
{{- else -}}
{{if .IndirectOptional -}}
*p.{{.ParamName | ucFirst}}
{{- else -}}
p.{{.ParamName | ucFirst}}
{{- end -}}
{{- end -}}

Expand Down Expand Up @@ -83,45 +85,44 @@ func (c *Client) {{.OperationID}}(ctx context.Context
*{{ .Schema.TypeDecl }},
{{- end -}}
error) {

req := &requests.Builder{}
req = req.Client(c.Client)
req = req.Method("{{.Method}}")
req = req.Method(http.Method{{.Method | lower | title}})
req = req.BaseURL(c.BaseURL)
req = req.Path(
{{- if not (eq (.PathParams | len) 0) -}}
strings.NewReplacer({{range .PathParams}} "{ {{- .ParamName -}} }", fmt.Sprint({{.ParamName}}),{{end}}).Replace("{{.Path}}")
strings.NewReplacer({{range .PathParams}} "{ {{- .ParamName -}} }", {{if eq .Schema.GoType "string"}}{{.ParamName}}, {{else}}fmt.Sprint({{.ParamName}}),{{- end -}}{{end}}).Replace("{{.Path}}")
{{- else -}}
"{{.Path}}"
{{- end -}}
)

{{range .QueryParams -}}
{{if .Required -}}
req = req.Param("{{.ParamName}}", {{- template "typeToString" . -}} )
req = req.Param("{{.ParamName}}", {{- template "typeToString" . -}})
{{else -}}
if p.{{.ParamName | ucFirst}} != nil {
req = req.Param("{{.ParamName}}", {{- template "typeToString" . -}} )
req = req.Param("{{.ParamName}}", {{- template "typeToString" . -}})
}
{{end -}}
{{end -}}

{{range .HeaderParams -}}
{{if .Required -}}
req = req.Header("{{.ParamName}}", {{- template "typeToString" . -}} )
req = req.Header("{{.ParamName}}", {{- template "typeToString" . -}})
{{else -}}
if p.{{.ParamName | ucFirst}} != nil {
req = req.Header("{{.ParamName}}", {{- template "typeToString" . -}} )
req = req.Header("{{.ParamName}}", {{- template "typeToString" . -}})
}
{{end -}}
{{end -}}

{{range .CookieParams -}}
{{if .Required -}}
req = req.Cookie("{{.ParamName}}", fmt.Sprint(p.{{.ParamName | ucFirst}}))
req = req.Cookie("{{.ParamName}}", {{- template "typeToString" . -}})
{{else -}}
if p.{{.ParamName | ucFirst}} != nil {
req = req.Cookie("{{.ParamName}}", fmt.Sprint(p.{{.ParamName | ucFirst}}))
req = req.Cookie("{{.ParamName}}", {{- template "typeToString" . -}}))
}
{{end -}}
{{end -}}
Expand All @@ -132,7 +133,9 @@ func (c *Client) {{.OperationID}}(ctx context.Context
{{end}}

// define out handlers
{{if not (eq (len .GetResponseTypeDefinitions) 0) -}}
read := false // flag such that empty responses are kept nil
{{end -}}
{{range .GetResponseTypeDefinitions -}}
is{{.ResponseName}} := func(resp *http.Response) bool {
return resp.StatusCode {{.ResponseName | statusCodeRange}}
Expand Down Expand Up @@ -162,7 +165,20 @@ func (c *Client) {{.OperationID}}(ctx context.Context

{{end -}}

req = req.Handle(requests.ChainHandlers( {{- range .GetResponseTypeDefinitions -}} handle{{.ResponseName}}, {{- end -}}))
handlers := []func(*http.Response) error{}
if c.ResponseInterceptor != nil {
handlers = append(handlers, c.ResponseInterceptor)
}

{{if not (eq (len .GetResponseTypeDefinitions) 0) -}}
{{range .GetResponseTypeDefinitions -}} handlers = append(handlers, handle{{.ResponseName}})
{{ end -}}
{{end -}}
req = req.Handle(requests.ChainHandlers(handlers...))

if c.RequestOptions != nil {
req = c.RequestOptions(req)
}
err := req.Fetch(ctx)
if err != nil {
return {{range .GetResponseTypeDefinitions }}nil, {{ end }} err
Expand Down

0 comments on commit 0ac89ab

Please sign in to comment.