Skip to content

Commit

Permalink
Add support for dumping request and response in Go generated clients (#…
Browse files Browse the repository at this point in the history
…4566)

* Add support for dumping request and response in Go generated clients

The following change adds a new configuration setting, which
controls whether clients want to dump the HTTP request and response.

Useful when debugging API calls and clients.

* samples: Update Go samples with XML

* Use log.Logger when dumping HTTP request and response in Go client
  • Loading branch information
dnaeon authored and wing328 committed Nov 28, 2019
1 parent df7b9b5 commit df682ab
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 3 deletions.
22 changes: 21 additions & 1 deletion modules/openapi-generator/src/main/resources/go/client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"errors"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"net/http/httputil"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -161,7 +163,25 @@ func parameterToJson(obj interface{}) (string, error) {

// callAPI do the request.
func (c *APIClient) callAPI(request *http.Request) (*http.Response, error) {
return c.cfg.HTTPClient.Do(request)
if c.cfg.Debug {
dump, err := httputil.DumpRequestOut(request, true)
if err != nil {
return nil, err
}
log.Printf("\n%s\n", string(dump))
}

resp, err := c.cfg.HTTPClient.Do(request)

if c.cfg.Debug {
dump, err := httputil.DumpResponse(resp, true)
if err != nil {
return resp, err
}
log.Printf("\n%s\n", string(dump))
}

return resp, err
}

// ChangeBasePath changes base path to allow switching to mocks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Configuration struct {
Scheme string `json:"scheme,omitempty"`
DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
UserAgent string `json:"userAgent,omitempty"`
Debug bool `json:"debug,omitempty"`
HTTPClient *http.Client
}

Expand All @@ -57,6 +58,7 @@ func NewConfiguration() *Configuration {
BasePath: "{{{basePath}}}",
DefaultHeader: make(map[string]string),
UserAgent: "{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}OpenAPI-Generator/{{{packageVersion}}}/go{{/httpUserAgent}}",
Debug: false,
}
return cfg
}
Expand Down
22 changes: 21 additions & 1 deletion samples/client/petstore/go/go-petstore-withXml/client.go

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

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

22 changes: 21 additions & 1 deletion samples/client/petstore/go/go-petstore/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ import (
"errors"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"net/http/httputil"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -172,7 +174,25 @@ func parameterToJson(obj interface{}) (string, error) {

// callAPI do the request.
func (c *APIClient) callAPI(request *http.Request) (*http.Response, error) {
return c.cfg.HTTPClient.Do(request)
if c.cfg.Debug {
dump, err := httputil.DumpRequestOut(request, true)
if err != nil {
return nil, err
}
log.Printf("\n%s\n", string(dump))
}

resp, err := c.cfg.HTTPClient.Do(request)

if c.cfg.Debug {
dump, err := httputil.DumpResponse(resp, true)
if err != nil {
return resp, err
}
log.Printf("\n%s\n", string(dump))
}

return resp, err
}

// ChangeBasePath changes base path to allow switching to mocks
Expand Down
2 changes: 2 additions & 0 deletions samples/client/petstore/go/go-petstore/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Configuration struct {
Scheme string `json:"scheme,omitempty"`
DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
UserAgent string `json:"userAgent,omitempty"`
Debug bool `json:"debug,omitempty"`
HTTPClient *http.Client
}

Expand All @@ -65,6 +66,7 @@ func NewConfiguration() *Configuration {
BasePath: "http://petstore.swagger.io:80/v2",
DefaultHeader: make(map[string]string),
UserAgent: "OpenAPI-Generator/1.0.0/go",
Debug: false,
}
return cfg
}
Expand Down

0 comments on commit df682ab

Please sign in to comment.