From df682aba992c495811ec2f3e5b2b6cb0173cb09f Mon Sep 17 00:00:00 2001 From: Marin Atanasov Nikolov Date: Thu, 28 Nov 2019 14:52:16 +0200 Subject: [PATCH] Add support for dumping request and response in Go generated clients (#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 --- .../src/main/resources/go/client.mustache | 22 ++++++++++++++++++- .../main/resources/go/configuration.mustache | 2 ++ .../petstore/go/go-petstore-withXml/client.go | 22 ++++++++++++++++++- .../go/go-petstore-withXml/configuration.go | 2 ++ .../client/petstore/go/go-petstore/client.go | 22 ++++++++++++++++++- .../petstore/go/go-petstore/configuration.go | 2 ++ 6 files changed, 69 insertions(+), 3 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/go/client.mustache b/modules/openapi-generator/src/main/resources/go/client.mustache index cefd6c7c409..eaa56c9cb78 100644 --- a/modules/openapi-generator/src/main/resources/go/client.mustache +++ b/modules/openapi-generator/src/main/resources/go/client.mustache @@ -9,8 +9,10 @@ import ( "errors" "fmt" "io" + "log" "mime/multipart" "net/http" + "net/http/httputil" "net/url" "os" "path/filepath" @@ -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 diff --git a/modules/openapi-generator/src/main/resources/go/configuration.mustache b/modules/openapi-generator/src/main/resources/go/configuration.mustache index 98f9d8fee26..17bc0aab306 100644 --- a/modules/openapi-generator/src/main/resources/go/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/go/configuration.mustache @@ -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 } @@ -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 } diff --git a/samples/client/petstore/go/go-petstore-withXml/client.go b/samples/client/petstore/go/go-petstore-withXml/client.go index 490d804736f..6fcca6e13e1 100644 --- a/samples/client/petstore/go/go-petstore-withXml/client.go +++ b/samples/client/petstore/go/go-petstore-withXml/client.go @@ -18,8 +18,10 @@ import ( "errors" "fmt" "io" + "log" "mime/multipart" "net/http" + "net/http/httputil" "net/url" "os" "path/filepath" @@ -173,7 +175,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 diff --git a/samples/client/petstore/go/go-petstore-withXml/configuration.go b/samples/client/petstore/go/go-petstore-withXml/configuration.go index d471b566189..9618bd142cb 100644 --- a/samples/client/petstore/go/go-petstore-withXml/configuration.go +++ b/samples/client/petstore/go/go-petstore-withXml/configuration.go @@ -57,6 +57,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 } @@ -66,6 +67,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 } diff --git a/samples/client/petstore/go/go-petstore/client.go b/samples/client/petstore/go/go-petstore/client.go index 343078e15cd..4bd7fa2780d 100644 --- a/samples/client/petstore/go/go-petstore/client.go +++ b/samples/client/petstore/go/go-petstore/client.go @@ -17,8 +17,10 @@ import ( "errors" "fmt" "io" + "log" "mime/multipart" "net/http" + "net/http/httputil" "net/url" "os" "path/filepath" @@ -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 diff --git a/samples/client/petstore/go/go-petstore/configuration.go b/samples/client/petstore/go/go-petstore/configuration.go index b3b81ad0824..40bb03ddcb5 100644 --- a/samples/client/petstore/go/go-petstore/configuration.go +++ b/samples/client/petstore/go/go-petstore/configuration.go @@ -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 } @@ -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 }