Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added configure request callback function, #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions generator/header_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ package {{.}}
// Do not modify
// Copyright (c) 2015, Hooklift. All rights reserved.
import (
"net/http"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like an unused import, are you using gofmt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is required for the operations https://github.com/notzippy/gowsdl/blob/add_soap_header/generator/operations_tmpl.go#L38

I gofmt the soap.go file, appears I have an extra line in this file though..

"encoding/xml"
"time"


gowsdl "github.com/hooklift/gowsdl/generator"
{{/*range .Imports*/}}
{{/*.*/}}
Expand Down
4 changes: 2 additions & 2 deletions generator/operations_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ var opsTmpl = `
// {{range .Faults}}
// - {{.Name}} {{.Doc}}{{end}}{{end}}
{{if ne .Doc ""}}/* {{.Doc}} */{{end}}
func (service *{{$portType}}) {{makePublic .Name | replaceReservedWords}} ({{if ne $requestType ""}}request *{{$requestType}}{{end}}) (*{{$responseType}}, error) {
func (service *{{$portType}}) {{makePublic .Name | replaceReservedWords}} ({{if ne $requestType ""}}request *{{$requestType}}{{end}}, header *gowsdl.SoapHeader, configureRequest func(*http.Request)) (*{{$responseType}}, error) {
response := &{{$responseType}}{}
err := service.client.Call("{{$soapAction}}", {{if ne $requestType ""}}request{{else}}nil{{end}}, response)
err := service.client.Call("{{$soapAction}}", {{if ne $requestType ""}}request{{else}}nil{{end}}, response, header, configureRequest)
if err != nil {
return nil, err
}
Expand Down
29 changes: 19 additions & 10 deletions generator/soap.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"bytes"
"crypto/tls"
"encoding/xml"
"gopkg.in/inconshreveable/log15.v2"
"io/ioutil"
"net/http"

"gopkg.in/inconshreveable/log15.v2"
"net/http/httputil"
)

var Log = log15.New()
Expand All @@ -20,9 +20,9 @@ func init() {
}

type SoapEnvelope struct {
XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"`
//Header SoapHeader `xml:"http://schemas.xmlsoap.org/soap/envelope/ Header,omitempty"`
Body SoapBody `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"`
Header *SoapHeader `xml:"http://schemas.xmlsoap.org/soap/envelope/ Header,omitempty"`
Body SoapBody `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
}

type SoapHeader struct {
Expand Down Expand Up @@ -57,9 +57,9 @@ func NewSoapClient(url string, tls bool) *SoapClient {
}
}

func (s *SoapClient) Call(soapAction string, request, response interface{}) error {
func (s *SoapClient) Call(soapAction string, request, response interface{}, header *SoapHeader, configureRequest func(*http.Request)) error {
envelope := SoapEnvelope{
//Header: SoapHeader{},
Header: header,
}

if request != nil {
Expand All @@ -73,23 +73,30 @@ func (s *SoapClient) Call(soapAction string, request, response interface{}) erro
buffer := &bytes.Buffer{}

encoder := xml.NewEncoder(buffer)
//encoder.Indent(" ", " ")
encoder.Indent(" ", " ")

err := encoder.Encode(envelope)
if err == nil {
err = encoder.Flush()
}
Log.Debug("request", "envelope", log15.Lazy{func() string { return buffer.String() }})
if err != nil {
return err
}

req, err := http.NewRequest("POST", s.url, buffer)

req.Header.Add("Content-Type", "text/xml; charset=\"utf-8\"")
if soapAction != "" {
req.Header.Add("SOAPAction", soapAction)
}
req.Header.Set("User-Agent", "gowsdl/0.1")

if configureRequest != nil {
configureRequest(req)
}

Log.Debug("request", "request", req,
"Header", log15.Lazy{func() string { r, _ := httputil.DumpRequestOut(req, true); return string(r) }},
)

tr := &http.Transport{
TLSClientConfig: &tls.Config{
Expand All @@ -101,6 +108,7 @@ func (s *SoapClient) Call(soapAction string, request, response interface{}) erro
client := &http.Client{Transport: tr}
res, err := client.Do(req)
if err != nil {
Log.Debug("Client error", "err", err)
return err
}
defer res.Body.Close()
Expand All @@ -110,6 +118,7 @@ func (s *SoapClient) Call(soapAction string, request, response interface{}) erro
Log.Warn("empty response")
return nil
}
Log.Debug("Raw response", "url", s.url, "rawbody", log15.Lazy{func() string { return string(rawbody) }})

respEnvelope := &SoapEnvelope{}

Expand Down