diff --git a/soap.go b/soap.go index e669f70..2a5d2ca 100644 --- a/soap.go +++ b/soap.go @@ -7,6 +7,7 @@ import ( "fmt" "io/ioutil" "net/http" + "net/http/httputil" "net/url" "strings" "sync" @@ -23,8 +24,18 @@ type SoapParams interface {} type Params map[string]interface{} type ArrayParams [][2]interface{} +// Config config the Client +type Config struct { + Dump bool +} + // SoapClient return new *Client to handle the requests with the WSDL func SoapClient(wsdl string, httpClient *http.Client) (*Client, error) { + return SoapClientWithConfig(wsdl, httpClient, &Config{Dump: false}) +} + +// SoapClientWithConfig return new *Client to handle the requests with the WSDL +func SoapClientWithConfig(wsdl string, httpClient *http.Client, config *Config) (*Client, error) { _, err := url.Parse(wsdl) if err != nil { return nil, err @@ -36,6 +47,7 @@ func SoapClient(wsdl string, httpClient *http.Client) (*Client, error) { c := &Client{ wsdl: wsdl, + config: config, HTTPClient: httpClient, AutoAction: false, } @@ -62,6 +74,7 @@ type Client struct { onRequest sync.WaitGroup onDefinitionsRefresh sync.WaitGroup wsdl string + config *Config } // Call call's the method m with Params p @@ -191,6 +204,14 @@ func (p *process) doRequest(url string) ([]byte, error) { return nil, err } + if p.Client.config != nil && p.Client.config.Dump { + dump, err := httputil.DumpRequestOut(req, true) + if err != nil { + return nil, err + } + fmt.Printf("Request:\n%v\n----\n", string(dump)) + } + if p.Client.Username != "" && p.Client.Password != "" { req.SetBasicAuth(p.Client.Username, p.Client.Password) } @@ -207,6 +228,14 @@ func (p *process) doRequest(url string) ([]byte, error) { } defer resp.Body.Close() + if p.Client.config != nil && p.Client.config.Dump { + dump, err := httputil.DumpResponse(resp, true) + if err != nil { + return nil, err + } + fmt.Printf("Response:\n%v\n----\n", string(dump)) + } + return ioutil.ReadAll(resp.Body) } diff --git a/soap_test.go b/soap_test.go index 3361db6..56037ab 100644 --- a/soap_test.go +++ b/soap_test.go @@ -102,7 +102,10 @@ var ( ) func TestClient_Call(t *testing.T) { - soap, err := SoapClient("http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl", nil) + soap, err := SoapClientWithConfig("http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl", + nil, + &Config{Dump: true}, + ) if err != nil { t.Errorf("error not expected: %s", err) }