gosoap

package module
v1.4.9 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2020 License: MIT Imports: 15 Imported by: 0

README

Go Soap Build Status GoDoc Go Report Card Coverage Status patreon

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)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetPayloadFromError added in v1.4.5

func GetPayloadFromError(err error) []byte

GetPayloadFromError returns the payload of a ErrorWithPayload

func IsFault added in v1.4.5

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.4.5

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

SetCustomEnvelope define customizated envelope

Types

type ArrayParams added in v1.4.5

type ArrayParams [][2]interface{}

type Client

type Client struct {
	HTTPClient   *http.Client
	AutoAction   bool
	URL          string
	HeaderName   string
	HeaderParams HeaderParams
	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.4.5

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.4.5

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

CallByStruct call's by struct

func (*Client) Do added in v1.4.5

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

Do Process Soap Request

func (*Client) SetWSDL added in v1.4.5

func (c *Client) SetWSDL(wsdl string)

SetWSDL set WSDL url

type Config added in v1.4.5

type Config struct {
	Dump   bool
	Logger DumpLogger
}

Config config the Client

type DumpLogger added in v1.4.5

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

type ErrorWithPayload added in v1.4.5

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.4.5

func (f *Fault) String() string

type FaultError added in v1.4.5

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

FaultError implements error interface

func (FaultError) Error added in v1.4.5

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{}

type Request added in v1.4.5

type Request struct {
	Method string
	Params SoapParams
}

Request Soap Request

func NewRequest added in v1.4.5

func NewRequest(m string, p SoapParams) *Request

func NewRequestByStruct added in v1.4.5

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

NewRequestByStruct create a new request using builder

type RequestStruct added in v1.4.5

type RequestStruct interface {
	SoapBuildRequest() *Request
}

RequestStruct soap request interface

type Response added in v1.4.5

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

Response Soap Response

func (*Response) Unmarshal added in v1.4.5

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

Unmarshal get the body and unmarshal into the 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.4.5

type SoapParams interface{}

Params type is used to set the params in soap request

Jump to

Keyboard shortcuts

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