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

Refactor soap client #107

Merged
merged 5 commits into from
Oct 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions example/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package example

import (
"crypto/tls"
"log"
"time"

"github.com/hooklift/gowsdl/example/gen"
"github.com/hooklift/gowsdl/soap"
)

func ExampleBasicUsage() {
client := soap.NewClient("http://svc.asmx")
service := gen.NewStockQuotePortType(client)
reply, err := service.GetLastTradePrice(&gen.TradePriceRequest{})
if err != nil {
log.Fatalf("could't get trade prices: %v", err)
}
log.Println(reply)
}

func ExampleWithOptions() {
client := soap.NewClient(
"http://svc.asmx",
soap.WithTimeout(time.Second*5),
soap.WithBasicAuth("usr", "psw"),
soap.WithTLS(&tls.Config{InsecureSkipVerify: true}),
)
service := gen.NewStockQuotePortType(client)
reply, err := service.GetLastTradePrice(&gen.TradePriceRequest{})
if err != nil {
log.Fatalf("could't get trade prices: %v", err)
}
log.Println(reply)
}
48 changes: 48 additions & 0 deletions example/gen/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package gen

import (
"encoding/xml"
"time"

"github.com/hooklift/gowsdl/soap"
)

// against "unused imports"
var _ time.Time
var _ xml.Name

type TradePriceRequest struct {
XMLName xml.Name `xml:"http://example.com/stockquote.xsd TradePriceRequest"`

TickerSymbol string `xml:"tickerSymbol,omitempty"`
}

type TradePrice struct {
XMLName xml.Name `xml:"http://example.com/stockquote.xsd TradePrice"`

Price float32 `xml:"price,omitempty"`
}

type StockQuotePortType interface {
GetLastTradePrice(request *TradePriceRequest) (*TradePrice, error)
}

type stockQuotePortType struct {
client *soap.Client
}

func NewStockQuotePortType(client *soap.Client) StockQuotePortType {
return &stockQuotePortType{
client: client,
}
}

func (service *stockQuotePortType) GetLastTradePrice(request *TradePriceRequest) (*TradePrice, error) {
response := new(TradePrice)
err := service.client.Call("http://example.com/GetLastTradePrice", request, response)
if err != nil {
return nil, err
}

return response, nil
}
27 changes: 11 additions & 16 deletions gowsdl.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,6 @@ func (g *GoWSDL) Start() (map[string][]byte, error) {
log.Println(err)
}

gocode["soap"], err = g.genSOAPClient()
if err != nil {
log.Println(err)
}

return gocode, nil
}

Expand Down Expand Up @@ -287,6 +282,7 @@ func (g *GoWSDL) genOperations() ([]byte, error) {
"stripns": stripns,
"replaceReservedWords": replaceReservedWords,
"makePublic": g.makePublicFn,
"makePrivate": makePrivate,
"findType": g.findType,
"findSOAPAction": g.findSOAPAction,
"findServiceAddress": g.findServiceAddress,
Expand Down Expand Up @@ -322,17 +318,6 @@ func (g *GoWSDL) genHeader() ([]byte, error) {
return data.Bytes(), nil
}

func (g *GoWSDL) genSOAPClient() ([]byte, error) {
data := new(bytes.Buffer)
tmpl := template.Must(template.New("soapclient").Parse(soapTmpl))
err := tmpl.Execute(data, g.pkg)
if err != nil {
return nil, err
}

return data.Bytes(), nil
}

var reservedWords = map[string]string{
"break": "break_",
"default": "default_",
Expand Down Expand Up @@ -533,6 +518,16 @@ func makePublic(identifier string) string {
return string(field)
}

func makePrivate(identifier string) string {
field := []rune(identifier)
if len(field) == 0 {
return identifier
}

field[0] = unicode.ToLower(field[0])
return string(field)
}

func comment(text string) string {
lines := strings.Split(text, "\n")

Expand Down
15 changes: 0 additions & 15 deletions gowsdl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,6 @@ func TestVboxGeneratesWithoutSyntaxErrors(t *testing.T) {
}
}

func TestSOAPHeaderGeneratesWithoutErrors(t *testing.T) {
g, err := NewGoWSDL("fixtures/ferry.wsdl", "myservice", false, true)
if err != nil {
t.Error(err)
}

resp, err := g.Start()
if err != nil {
t.Error(err)
}
if !strings.Contains(string(resp["operations"]), "SetHeader") {
t.Error("SetHeader method should be generated in the service operation")
}
}

func TestEnumerationsGeneratedCorrectly(t *testing.T) {
enumStringTest := func(t *testing.T, fixtureWsdl string, varName string, typeName string, enumString string) {
g, err := NewGoWSDL("fixtures/"+fixtureWsdl, "myservice", false, true)
Expand Down
8 changes: 1 addition & 7 deletions header_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,9 @@ var headerTmpl = `
package {{.}}

import (
"bytes"
"crypto/tls"
"encoding/xml"
"io/ioutil"
"log"
"math/rand"
"net"
"net/http"
"time"
"github.com/hooklift/gowsdl/soap"

{{/*range .Imports*/}}
{{/*.*/}}
Expand Down
64 changes: 25 additions & 39 deletions operations_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,42 @@ package gowsdl

var opsTmpl = `
{{range .}}
{{$portType := .Name | makePublic}}
type {{$portType}} struct {
client *SOAPClient
{{$privateType := .Name | makePrivate}}
{{$exportType := .Name | makePublic}}

type {{$exportType}} interface {
{{range .Operations}}
{{$faults := len .Faults}}
{{$soapAction := findSOAPAction .Name $privateType}}
{{$requestType := findType .Input.Message | replaceReservedWords | makePublic}}
{{$responseType := findType .Output.Message | replaceReservedWords | makePublic}}

{{/*if ne $soapAction ""*/}}
{{if gt $faults 0}}
// Error can be either of the following types:
// {{range .Faults}}
// - {{.Name}} {{.Doc}}{{end}}{{end}}
{{if ne .Doc ""}}/* {{.Doc}} */{{end}}
{{makePublic .Name | replaceReservedWords}} ({{if ne $requestType ""}}request *{{$requestType}}{{end}}) (*{{$responseType}}, error)
{{/*end*/}}
{{end}}
}

func New{{$portType}}(url string, tls bool, auth *BasicAuth) *{{$portType}} {
if url == "" {
url = {{findServiceAddress .Name | printf "%q"}}
}
client := NewSOAPClient(url, tls, auth)

return &{{$portType}}{
client: client,
}
type {{$privateType}} struct {
client *soap.Client
}

func New{{$portType}}WithTLSConfig(url string, tlsCfg *tls.Config, auth *BasicAuth) *{{$portType}} {
if url == "" {
url = {{findServiceAddress .Name | printf "%q"}}
}
client := NewSOAPClientWithTLSConfig(url, tlsCfg, auth)

return &{{$portType}}{
func New{{$exportType}}(client *soap.Client) {{$exportType}} {
return &{{$privateType}}{
client: client,
}
}

func (service *{{$portType}}) AddHeader(header interface{}) {
service.client.AddHeader(header)
}

// Backwards-compatible function: use AddHeader instead
func (service *{{$portType}}) SetHeader(header interface{}) {
service.client.AddHeader(header)
}

{{range .Operations}}
{{$faults := len .Faults}}
{{$requestType := findType .Input.Message | replaceReservedWords | makePublic}}
{{$soapAction := findSOAPAction .Name $portType}}
{{$soapAction := findSOAPAction .Name $privateType}}
{{$responseType := findType .Output.Message | replaceReservedWords | makePublic}}

{{/*if ne $soapAction ""*/}}
{{if gt $faults 0}}
// Error can be either of the following types:
// {{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 *{{$privateType}}) {{makePublic .Name | replaceReservedWords}} ({{if ne $requestType ""}}request *{{$requestType}}{{end}}) (*{{$responseType}}, error) {
response := new({{$responseType}})
err := service.client.Call("{{$soapAction}}", {{if ne $requestType ""}}request{{else}}nil{{end}}, response)
if err != nil {
Expand All @@ -63,7 +50,6 @@ var opsTmpl = `

return response, nil
}
{{/*end*/}}
{{end}}
{{end}}
`
Loading