gosoap

package module
v1.5.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 7, 2023 License: MIT Imports: 14 Imported by: 0

README

Go Soap

package to help with SOAP integrations (client)

Install
go get siteminds.dev/gosoap
Configuration Options

gosoap can be configured when creating the SOAP client by using SoapClientWithConfig() instead of the regular SoapClient() method. This method takes an additional gosoap.Config{} struct that has the following configuration options:

  • Dump (boolean): this makes gosoap dump the raw HTTP request and response in the log (useful for debugging)
  • Logger (DumpLogger): Logger takes any type that implements the gosoap.DumpLogger interface. This is useful for wrapping Dump logs into your own logging solution (e.g. zap, logrus, zerolog, etc.)
  • PrefixOperation (boolean): by default gosoap generates an 'operation' root element in the SOAP Body, containing a xmlns namespace attribute. When setting PrefixOperation = true, the xmlns attribute is not added, allowing you to manually prefix the operation name in the gosoap.Call() with your own namespace prefix. You can add your own namespace prefix using gosoap.SetCustomEnvelope() (see example below).
  • DisableRoot (boolean): when set to true, this makes gosoap skip generating the operation root element entirely, allowing full control of the SOAP Body.
  • Endpoint (string): by default the first location of the first service port found in the WSDL is used. By defining the endpoint url in the configuration, this configured endpoint will be used instead of the one from the WSDL.
Examples
Basic use
package main

import (
	"encoding/xml"
	"log"
	"net/http"
	"time"

	"siteminds.dev/gosoap"
)

// GetIPLocationResponse will hold the Soap response
type GetIPLocationResponse struct {
	GetIPLocationResult string `xml:"GetIpLocationResult"`
}

// GetIPLocationResult will
type GetIPLocationResult struct {
	XMLName xml.Name `xml:"GeoIP"`
	Country string   `xml:"Country"`
	State   string   `xml:"State"`
}

var (
	r GetIPLocationResponse
)

func main() {
	httpClient := &http.Client{
		Timeout: 1500 * time.Millisecond,
	}
	soap, err := gosoap.SoapClient("http://wsgeoip.lavasoft.com/ipservice.asmx?WSDL", httpClient)
	if err != nil {
		log.Fatalf("SoapClient error: %s", err)
	}

	// Use gosoap.ArrayParams to support fixed position params
	params := gosoap.Params{
		"sIp": "8.8.8.8",
	}

	res, err := soap.Call("GetIpLocation", params)
	if err != nil {
		log.Fatalf("Call error: %s", err)
	}

	res.Unmarshal(&r)

	// GetIpLocationResult will be a string. We need to parse it to XML
	result := GetIPLocationResult{}
	err = xml.Unmarshal([]byte(r.GetIPLocationResult), &result)
	if err != nil {
		log.Fatalf("xml.Unmarshal error: %s", err)
	}

	if result.Country != "US" {
		log.Fatalf("error: %+v", r)
	}

	log.Println("Country: ", result.Country)
	log.Println("State: ", result.State)
}
Set Custom Envelope Attributes
package main

import (
	"encoding/xml"
	"log"
	"net/http"
	"time"

	"siteminds.dev/gosoap"
)

// GetIPLocationResponse will hold the Soap response
type GetIPLocationResponse struct {
	GetIPLocationResult string `xml:"GetIpLocationResult"`
}

// GetIPLocationResult will
type GetIPLocationResult struct {
	XMLName xml.Name `xml:"GeoIP"`
	Country string   `xml:"Country"`
	State   string   `xml:"State"`
}

var (
	r GetIPLocationResponse
)

func main() {
	httpClient := &http.Client{
		Timeout: 1500 * time.Millisecond,
	}
	// set custom envelope
    gosoap.SetCustomEnvelope("soapenv", map[string]string{
		"xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/",
		"xmlns:tem": "http://tempuri.org/",
    })

	soap, err := gosoap.SoapClient("http://wsgeoip.lavasoft.com/ipservice.asmx?WSDL", httpClient)
	if err != nil {
		log.Fatalf("SoapClient error: %s", err)
	}

	// Use gosoap.ArrayParams to support fixed position params
	params := gosoap.Params{
		"sIp": "8.8.8.8",
	}

	res, err := soap.Call("GetIpLocation", params)
	if err != nil {
		log.Fatalf("Call error: %s", err)
	}

	res.Unmarshal(&r)

	// GetIpLocationResult will be a string. We need to parse it to XML
	result := GetIPLocationResult{}
	err = xml.Unmarshal([]byte(r.GetIPLocationResult), &result)
	if err != nil {
		log.Fatalf("xml.Unmarshal error: %s", err)
	}

	if result.Country != "US" {
		log.Fatalf("error: %+v", r)
	}

	log.Println("Country: ", result.Country)
	log.Println("State: ", result.State)
}
Set Header params
	soap.HeaderParams = gosoap.SliceParams{
		xml.StartElement{
			Name: xml.Name{
				Space: "auth",
				Local: "Login",
			},
		},
		"user",
		xml.EndElement{
			Name: xml.Name{
				Space: "auth",
				Local: "Login",
			},
		},
		xml.StartElement{
			Name: xml.Name{
				Space: "auth",
				Local: "Password",
			},
		},
		"P@ssw0rd",
		xml.EndElement{
			Name: xml.Name{
				Space: "auth",
				Local: "Password",
			},
		},
	}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetPayloadFromError added in v1.5.0

func GetPayloadFromError(err error) []byte

GetPayloadFromError returns the payload of a ErrorWithPayload

func IsFault added in v1.5.0

func IsFault(err error) bool

IsFault returns whether the given error is a fault error or not.

IsFault will return false when the error could not be typecasted to FaultError, because every fault error should have it's dynamic type as FaultError.

func SetCustomEnvelope added in v1.5.0

func SetCustomEnvelope(prefix string, attrs map[string]string)

SetCustomEnvelope define customizated envelope

Types

type ArrayParams added in v1.5.0

type ArrayParams [][2]interface{}

type Client

type Client struct {
	HTTPClient   *http.Client
	AutoAction   bool
	URL          string
	HeaderName   string
	HeaderParams SoapParams
	Definitions  *wsdlDefinitions
	// Must be set before first request otherwise has no effect, minimum is 15 minutes.
	RefreshDefinitionsAfter time.Duration
	Username                string
	Password                string
	// contains filtered or unexported fields
}

Client struct hold all the information about WSDL, request and response of the server

func SoapClient

func SoapClient(wsdl string, httpClient *http.Client) (*Client, error)

SoapClient return new *Client to handle the requests with the WSDL

func SoapClientWithConfig added in v1.5.0

func SoapClientWithConfig(wsdl string, httpClient *http.Client, config *Config) (*Client, error)

SoapClientWithConfig return new *Client to handle the requests with the WSDL

func (*Client) Call

func (c *Client) Call(m string, p SoapParams) (res *Response, err error)

Call call's the method m with Params p

func (*Client) CallByStruct added in v1.5.0

func (c *Client) CallByStruct(s RequestStruct) (res *Response, err error)

CallByStruct call's by struct

func (*Client) Do added in v1.5.0

func (c *Client) Do(req *Request) (res *Response, err error)

Do Process Soap Request

func (*Client) SetWSDL added in v1.5.0

func (c *Client) SetWSDL(wsdl string)

SetWSDL set WSDL url

type Config added in v1.5.0

type Config struct {
	Dump            bool
	Logger          DumpLogger
	PrefixOperation bool
	DisableRoot     bool
	Endpoint        string
}

Config config the Client

type DumpLogger added in v1.5.0

type DumpLogger interface {
	LogRequest(method string, dump []byte)
	LogResponse(method string, dump []byte)
}

type ErrorWithPayload added in v1.5.0

type ErrorWithPayload struct {
	Payload []byte
	// contains filtered or unexported fields
}

ErrorWithPayload error payload schema

type Fault added in v1.1.0

type Fault struct {
	Code        string `xml:"faultcode"`
	Description string `xml:"faultstring"`
	Detail      string `xml:"detail"`
}

Fault response Fault implements Stringer interface

func (*Fault) String added in v1.5.0

func (f *Fault) String() string

type FaultError added in v1.5.0

type FaultError struct {
	// contains filtered or unexported fields
}

FaultError implements error interface

func (FaultError) Error added in v1.5.0

func (e FaultError) Error() string

type HeaderParams added in v1.2.0

type HeaderParams map[string]interface{}

HeaderParams holds params specific to the header

type Params

type Params map[string]interface{}

Params type is used to set the params in soap request

type Request added in v1.5.0

type Request struct {
	Method string
	Params SoapParams
}

Request Soap Request

func NewRequest added in v1.5.0

func NewRequest(m string, p SoapParams) *Request

func NewRequestByStruct added in v1.5.0

func NewRequestByStruct(s RequestStruct) (*Request, error)

NewRequestByStruct create a new request using builder

type RequestStruct added in v1.5.0

type RequestStruct interface {
	SoapBuildRequest() *Request
}

RequestStruct soap request interface

type Response added in v1.5.0

type Response struct {
	Body    []byte
	Header  []byte
	Payload []byte
}

Response Soap Response

func (*Response) Unmarshal added in v1.5.0

func (r *Response) Unmarshal(v interface{}) error

Unmarshal get the body and unmarshal into the interface

type SliceParams added in v1.5.0

type SliceParams []interface{}

type SoapBody

type SoapBody struct {
	XMLName  struct{} `xml:"Body"`
	Contents []byte   `xml:",innerxml"`
}

SoapBody struct

type SoapEnvelope

type SoapEnvelope struct {
	XMLName struct{} `xml:"Envelope"`
	Header  SoapHeader
	Body    SoapBody
}

SoapEnvelope struct

type SoapHeader added in v1.2.0

type SoapHeader struct {
	XMLName  struct{} `xml:"Header"`
	Contents []byte   `xml:",innerxml"`
}

SoapHeader struct

type SoapParams added in v1.5.0

type SoapParams interface{}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL