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
This commit is contained in:
Marin Atanasov Nikolov 2019-11-28 14:52:16 +02:00 committed by William Cheng
parent df7b9b53f1
commit df682aba99
6 changed files with 69 additions and 3 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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

View File

@ -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
}

View File

@ -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

View File

@ -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
}