gosoap

package module
v1.7.2 Latest Latest
Warning

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

Go to latest
Published: May 7, 2024 License: MIT Imports: 18 Imported by: 0

README

Go Soap Build Status GoDoc Go Report Card Coverage Status patreon Known Vulnerabilities

package to help with SOAP integrations (client)

Install
go get github.com/tiaguinho/gosoap
Examples
Basic use
package main

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

	"github.com/tiaguinho/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"

	"github.com/tiaguinho/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 AddCustomEnvelopeAttr added in v1.7.2

func AddCustomEnvelopeAttr(key string, value string)

func GetPayloadFromError

func GetPayloadFromError(err error) []byte

GetPayloadFromError returns the payload of a ErrorWithPayload

func IsFault

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

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

SetCustomEnvelope define customizated envelope

Types

type ArrayParams

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

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 orderedmap.OrderedMap) (res *Response, err error)

Call call's the method m with Params p

func (*Client) CallByStruct

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

CallByStruct call's by struct

func (*Client) Do

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

Do Process Soap Request

func (*Client) SetWSDL

func (c *Client) SetWSDL(wsdl string)

SetWSDL set WSDL url

type Config

type Config struct {
	Dump   bool
	Logger DumpLogger
}

Config config the Client

type DumpLogger

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

type ErrorWithPayload

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

ErrorWithPayload error payload schema

type Fault

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

Fault response Fault implements Stringer interface

func (*Fault) String

func (f *Fault) String() string

type FaultError

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

FaultError implements error interface

func (FaultError) Error

func (e FaultError) Error() string

type HeaderParams

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

type Request struct {
	Method string
	Params orderedmap.OrderedMap
}

Request Soap Request

func NewRequest

func NewRequest(m string, p orderedmap.OrderedMap) *Request

func NewRequestByStruct

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

NewRequestByStruct create a new request using builder

type RequestStruct

type RequestStruct interface {
	SoapBuildRequest() *Request
}

RequestStruct soap request interface

type Response

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

Response Soap Response

func (*Response) Unmarshal

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

Unmarshal get the body and unmarshal into the interface

type SliceParams

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

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

SoapHeader struct

type SoapParams

type SoapParams interface{}

Jump to

Keyboard shortcuts

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