Skip to content

Commit

Permalink
Merge pull request #45 from mhewedy/mhewedy-patch-1
Browse files Browse the repository at this point in the history
Enable dumping http request/response for better debugging V2
  • Loading branch information
tiaguinho authored Jun 29, 2020
2 parents 49b2a7e + e4d1790 commit cb69547
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
29 changes: 29 additions & 0 deletions soap.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"sync"
Expand All @@ -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
Expand All @@ -36,6 +47,7 @@ func SoapClient(wsdl string, httpClient *http.Client) (*Client, error) {

c := &Client{
wsdl: wsdl,
config: config,
HTTPClient: httpClient,
AutoAction: false,
}
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}

Expand Down
5 changes: 4 additions & 1 deletion soap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit cb69547

Please sign in to comment.