diff --git a/examples/petstore-expanded/api/client.go b/examples/petstore-expanded/api/client.go index 310e7e7..2283a4c 100644 --- a/examples/petstore-expanded/api/client.go +++ b/examples/petstore-expanded/api/client.go @@ -15,16 +15,17 @@ import ( ) type Client struct { - BaseURL string - Client *http.Client + BaseURL string + Client *http.Client + ResponseInterceptor func(*http.Response) error + RequestOptions func(*requests.Builder) *requests.Builder } // FindPets Returns all pets func (c *Client) FindPets(ctx context.Context, p FindPetsParams) (*[]Pet, *Error, error) { - req := &requests.Builder{} req = req.Client(c.Client) - req = req.Method("GET") + req = req.Method(http.MethodGet) req = req.BaseURL(c.BaseURL) req = req.Path("/pets") @@ -32,7 +33,7 @@ func (c *Client) FindPets(ctx context.Context, p FindPetsParams) (*[]Pet, *Error req = req.Param("tags", strings.Join(p.Tags, ",")) } if p.Limit != nil { - req = req.Param("limit", fmt.Sprint(p.Limit)) + req = req.Param("limit", fmt.Sprint(*p.Limit)) } // define out handlers @@ -89,7 +90,18 @@ func (c *Client) FindPets(ctx context.Context, p FindPetsParams) (*[]Pet, *Error return err } - req = req.Handle(requests.ChainHandlers(handle200, handledefault)) + handlers := []func(*http.Response) error{} + if c.ResponseInterceptor != nil { + handlers = append(handlers, c.ResponseInterceptor) + } + + handlers = append(handlers, handle200) + handlers = append(handlers, handledefault) + req = req.Handle(requests.ChainHandlers(handlers...)) + + if c.RequestOptions != nil { + req = c.RequestOptions(req) + } err := req.Fetch(ctx) if err != nil { return nil, nil, err @@ -100,10 +112,9 @@ func (c *Client) FindPets(ctx context.Context, p FindPetsParams) (*[]Pet, *Error // AddPet Creates a new pet func (c *Client) AddPet(ctx context.Context, body NewPet) (*Pet, *Error, error) { - req := &requests.Builder{} req = req.Client(c.Client) - req = req.Method("POST") + req = req.Method(http.MethodPost) req = req.BaseURL(c.BaseURL) req = req.Path("/pets") @@ -164,7 +175,18 @@ func (c *Client) AddPet(ctx context.Context, body NewPet) (*Pet, *Error, error) return err } - req = req.Handle(requests.ChainHandlers(handle201, handledefault)) + handlers := []func(*http.Response) error{} + if c.ResponseInterceptor != nil { + handlers = append(handlers, c.ResponseInterceptor) + } + + handlers = append(handlers, handle201) + handlers = append(handlers, handledefault) + req = req.Handle(requests.ChainHandlers(handlers...)) + + if c.RequestOptions != nil { + req = c.RequestOptions(req) + } err := req.Fetch(ctx) if err != nil { return nil, nil, err @@ -175,10 +197,9 @@ func (c *Client) AddPet(ctx context.Context, body NewPet) (*Pet, *Error, error) // DeletePet Deletes a pet by ID func (c *Client) DeletePet(ctx context.Context, id int64) (*Error, error) { - req := &requests.Builder{} req = req.Client(c.Client) - req = req.Method("DELETE") + req = req.Method(http.MethodDelete) req = req.BaseURL(c.BaseURL) req = req.Path(strings.NewReplacer("{id}", fmt.Sprint(id)).Replace("/pets/{id}")) @@ -210,7 +231,17 @@ func (c *Client) DeletePet(ctx context.Context, id int64) (*Error, error) { return err } - req = req.Handle(requests.ChainHandlers(handledefault)) + handlers := []func(*http.Response) error{} + if c.ResponseInterceptor != nil { + handlers = append(handlers, c.ResponseInterceptor) + } + + handlers = append(handlers, handledefault) + req = req.Handle(requests.ChainHandlers(handlers...)) + + if c.RequestOptions != nil { + req = c.RequestOptions(req) + } err := req.Fetch(ctx) if err != nil { return nil, err @@ -221,10 +252,9 @@ func (c *Client) DeletePet(ctx context.Context, id int64) (*Error, error) { // FindPetByID Returns a pet by ID func (c *Client) FindPetByID(ctx context.Context, id int64) (*Pet, *Error, error) { - req := &requests.Builder{} req = req.Client(c.Client) - req = req.Method("GET") + req = req.Method(http.MethodGet) req = req.BaseURL(c.BaseURL) req = req.Path(strings.NewReplacer("{id}", fmt.Sprint(id)).Replace("/pets/{id}")) @@ -282,7 +312,18 @@ func (c *Client) FindPetByID(ctx context.Context, id int64) (*Pet, *Error, error return err } - req = req.Handle(requests.ChainHandlers(handle200, handledefault)) + handlers := []func(*http.Response) error{} + if c.ResponseInterceptor != nil { + handlers = append(handlers, c.ResponseInterceptor) + } + + handlers = append(handlers, handle200) + handlers = append(handlers, handledefault) + req = req.Handle(requests.ChainHandlers(handlers...)) + + if c.RequestOptions != nil { + req = c.RequestOptions(req) + } err := req.Fetch(ctx) if err != nil { return nil, nil, err diff --git a/templates/client.tmpl b/templates/client.tmpl index 95979ca..0ab5ef0 100644 --- a/templates/client.tmpl +++ b/templates/client.tmpl @@ -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 -}} @@ -83,14 +85,13 @@ 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 -}} @@ -98,30 +99,30 @@ func (c *Client) {{.OperationID}}(ctx context.Context {{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 -}} @@ -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}} @@ -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