goap

package module
v0.0.0-...-c52db2e Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2017 License: MIT Imports: 6 Imported by: 0

README

Goap

It's another golang soap client.

Usage

First define request body and response body structure :

type addRequest struct {
    XMLName xml.Name `xml:"http://tempuri.org/ Add"`

    IntA int `xml:"intA"`
    IntB int `xml:"intB"`
}

type addResponse struct {
    XMLName xml.Name `xml:"http://tempuri.org/ AddResponse"`

    AddResult int `xml:"AddResult"`
}

Then you can use goap to send your request and process response :

response := addResponse{}
err := goap.Call(
    "http://www.dneonline.com/calculator.asmx",  // service url
    "http://tempuri.org/Add", // soap action
    nil, // soap request headers (optional)
    addRequest{IntA: 10, IntB: 15}, // request
    nil, // soap response headers(optional)
    &response) // soap response pointer

if err != nil {
    panic(err)
}

log.Println("add result is", response.AddResult)

Thanks

This project inspired by Zarinpal go example by sijad

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DebugMode log request/response if set true
	DebugMode = false

	// DefaultRequestBuilder create simple post method
	DefaultRequestBuilder = func(method string, url string, body io.Reader) (*http.Request, error) {
		req, err := http.NewRequest(http.MethodPost, url, body)
		if err != nil {
			return nil, err
		}
		req.Header.Add("Content-Type", "text/xml; charset=\"utf-8\"")
		req.Header.Set("User-Agent", userAgent)
		return req, err
	}

	// DefaultClient used by default
	DefaultClient = &Client{
		HTTPClient:     http.DefaultClient,
		RequestBuilder: DefaultRequestBuilder,
	}
)

Functions

func Call

func Call(url string, action string, requestHeaders, request, responseHeaders, response interface{}) error

Call soap action

Types

type Client

type Client struct {
	// HTTPClient used for send/recieve soap request
	HTTPClient     *http.Client
	RequestBuilder func(method string, url string, body io.Reader) (*http.Request, error)
}

Client handle soap

func (*Client) Call

func (c *Client) Call(url string, action string, requestHeaders, request, responseHeaders, response interface{}) error

Call soap action

type SOAPBody

type SOAPBody struct {
	XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`

	Fault   *SOAPFault  `xml:",omitempty"`
	Content interface{} `xml:",omitempty"`
}

SOAPBody entity

func (*SOAPBody) UnmarshalXML

func (b *SOAPBody) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML unmarshals a given XML

type SOAPEnvelope

type SOAPEnvelope struct {
	XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"`

	Header SOAPHeader
	Body   SOAPBody
}

SOAPEnvelope entity

type SOAPFault

type SOAPFault struct {
	XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Fault"`

	Code   string `xml:"faultcode,omitempty"`
	String string `xml:"faultstring,omitempty"`
	Actor  string `xml:"faultactor,omitempty"`
	Detail string `xml:"detail,omitempty"`
}

SOAPFault entity

func (SOAPFault) Error

func (f SOAPFault) Error() string

Error returns the error of the SOAPFault

type SOAPHeader

type SOAPHeader struct {
	XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Header"`

	Header interface{}
}

SOAPHeader entity

Jump to

Keyboard shortcuts

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