Skip to content

Commit df682ab

Browse files
dnaeonwing328
authored andcommitted
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
1 parent df7b9b5 commit df682ab

File tree

6 files changed

+69
-3
lines changed

6 files changed

+69
-3
lines changed

modules/openapi-generator/src/main/resources/go/client.mustache

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import (
99
"errors"
1010
"fmt"
1111
"io"
12+
"log"
1213
"mime/multipart"
1314
"net/http"
15+
"net/http/httputil"
1416
"net/url"
1517
"os"
1618
"path/filepath"
@@ -161,7 +163,25 @@ func parameterToJson(obj interface{}) (string, error) {
161163

162164
// callAPI do the request.
163165
func (c *APIClient) callAPI(request *http.Request) (*http.Response, error) {
164-
return c.cfg.HTTPClient.Do(request)
166+
if c.cfg.Debug {
167+
dump, err := httputil.DumpRequestOut(request, true)
168+
if err != nil {
169+
return nil, err
170+
}
171+
log.Printf("\n%s\n", string(dump))
172+
}
173+
174+
resp, err := c.cfg.HTTPClient.Do(request)
175+
176+
if c.cfg.Debug {
177+
dump, err := httputil.DumpResponse(resp, true)
178+
if err != nil {
179+
return resp, err
180+
}
181+
log.Printf("\n%s\n", string(dump))
182+
}
183+
184+
return resp, err
165185
}
166186

167187
// ChangeBasePath changes base path to allow switching to mocks

modules/openapi-generator/src/main/resources/go/configuration.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type Configuration struct {
4848
Scheme string `json:"scheme,omitempty"`
4949
DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
5050
UserAgent string `json:"userAgent,omitempty"`
51+
Debug bool `json:"debug,omitempty"`
5152
HTTPClient *http.Client
5253
}
5354

@@ -57,6 +58,7 @@ func NewConfiguration() *Configuration {
5758
BasePath: "{{{basePath}}}",
5859
DefaultHeader: make(map[string]string),
5960
UserAgent: "{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}OpenAPI-Generator/{{{packageVersion}}}/go{{/httpUserAgent}}",
61+
Debug: false,
6062
}
6163
return cfg
6264
}

samples/client/petstore/go/go-petstore-withXml/client.go

Lines changed: 21 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/client/petstore/go/go-petstore-withXml/configuration.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/client/petstore/go/go-petstore/client.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ import (
1717
"errors"
1818
"fmt"
1919
"io"
20+
"log"
2021
"mime/multipart"
2122
"net/http"
23+
"net/http/httputil"
2224
"net/url"
2325
"os"
2426
"path/filepath"
@@ -172,7 +174,25 @@ func parameterToJson(obj interface{}) (string, error) {
172174

173175
// callAPI do the request.
174176
func (c *APIClient) callAPI(request *http.Request) (*http.Response, error) {
175-
return c.cfg.HTTPClient.Do(request)
177+
if c.cfg.Debug {
178+
dump, err := httputil.DumpRequestOut(request, true)
179+
if err != nil {
180+
return nil, err
181+
}
182+
log.Printf("\n%s\n", string(dump))
183+
}
184+
185+
resp, err := c.cfg.HTTPClient.Do(request)
186+
187+
if c.cfg.Debug {
188+
dump, err := httputil.DumpResponse(resp, true)
189+
if err != nil {
190+
return resp, err
191+
}
192+
log.Printf("\n%s\n", string(dump))
193+
}
194+
195+
return resp, err
176196
}
177197

178198
// ChangeBasePath changes base path to allow switching to mocks

samples/client/petstore/go/go-petstore/configuration.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type Configuration struct {
5656
Scheme string `json:"scheme,omitempty"`
5757
DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
5858
UserAgent string `json:"userAgent,omitempty"`
59+
Debug bool `json:"debug,omitempty"`
5960
HTTPClient *http.Client
6061
}
6162

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

0 commit comments

Comments
 (0)