From af3d862c5bb0cc985ee5aceb002a809d69cfeb06 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Thu, 28 Nov 2019 21:37:50 +0800 Subject: [PATCH] update go samples --- .../client/petstore/go/go-petstore/client.go | 22 ++++++++++++++++++- .../petstore/go/go-petstore/configuration.go | 2 ++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/samples/openapi3/client/petstore/go/go-petstore/client.go b/samples/openapi3/client/petstore/go/go-petstore/client.go index 83da6040263..bdfa115bad9 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/client.go +++ b/samples/openapi3/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" @@ -175,7 +177,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/openapi3/client/petstore/go/go-petstore/configuration.go b/samples/openapi3/client/petstore/go/go-petstore/configuration.go index b3b81ad0824..40bb03ddcb5 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/configuration.go +++ b/samples/openapi3/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 }