soap

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

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

Go to latest
Published: Mar 27, 2023 License: MIT Imports: 15 Imported by: 0

README

SOAP is dead - long live SOAP

First of all do not write SOAP services if you can avoid it! It is over.

If you can not avoid it this package might help.

Service

package main

import (
	"encoding/xml"
	"fmt"
	"net/http"

	"github.com/foomo/soap"
)

// FooRequest a simple request
type FooRequest struct {
	XMLName xml.Name `xml:"fooRequest"`
	Foo     string
}

// FooResponse a simple response
type FooResponse struct {
	Bar string
}

// RunServer run a little demo server
func RunServer() {
	soapServer := soap.NewServer()
	soapServer.HandleOperation(
		// SOAPAction
		"operationFoo",
		// tagname of soap body content
		"fooRequest",
		// RequestFactoryFunc - give the server sth. to unmarshal the request into
		func() interface{} {
			return &FooRequest{}
		},
		// OperationHandlerFunc - do something
		func(request interface{}, w http.ResponseWriter, httpRequest *http.Request) (response interface{}, err error) {
			fooRequest := request.(*FooRequest)
			fooResponse := &FooResponse{
				Bar: "Hello " + fooRequest.Foo,
			}
			response = fooResponse
			return
		},
	)
	err := soapServer.ListenAndServe(":8080")
	fmt.Println("exiting with error", err)
}

func main() {
	RunServer()
}

Client

package main

import (
	"encoding/xml"
	"log"

	"github.com/foomo/soap"
)

// FooRequest a simple request
type FooRequest struct {
	XMLName xml.Name `xml:"fooRequest"`
	Foo     string
}

// FooResponse a simple response
type FooResponse struct {
	Bar string
}

func main() {
	soap.Verbose = true
	client := soap.NewClient("http://127.0.0.1:8080/", nil, nil)
	response := &FooResponse{}
	httpResponse, err := client.Call("operationFoo", &FooRequest{Foo: "hello i am foo"}, response)
	if err != nil {
		panic(err)
	}
	log.Println(response.Bar, httpResponse.Status)
}

Documentation

Index

Constants

View Source
const (
	SoapVersion11 = "1.1"
	SoapVersion12 = "1.2"

	SoapContentType11 = "text/xml; charset=utf-8"
	SoapContentType12 = "application/soap+xml; charset=utf-8"

	NamespaceSoap11 = "http://schemas.xmlsoap.org/soap/envelope/"
	NamespaceSoap12 = "http://www.w3.org/2003/05/soap-envelope"
)

Variables

View Source
var ClientDialTimeout = time.Duration(30 * time.Second)

ClientDialTimeout default timeout 30s

View Source
var UserAgent = "go-soap-0.1"

UserAgent is the default user agent

View Source
var Verbose = false

Verbose be verbose

Functions

func LogJSON

func LogJSON(v interface{})

Types

type BasicAuth

type BasicAuth struct {
	Login    string
	Password string
}

BasicAuth credentials for the client

type Body

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

	Fault               *Fault      `xml:",omitempty"`
	Content             interface{} `xml:",omitempty"`
	SOAPBodyContentType string      `xml:"-"`
}

Body type

func (*Body) UnmarshalXML

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

UnmarshalXML implement xml.Unmarshaler

type Client

type Client struct {
	Marshaller  XMLMarshaller
	ContentType string
	SoapVersion string
	// contains filtered or unexported fields
}

Client generic SOAP client

func NewClient

func NewClient(url string, auth *BasicAuth, tr *http.Transport) *Client

NewClient constructor. SOAP 1.1 is used by default. Switch to SOAP 1.2 with UseSoap12()

func (*Client) Call

func (c *Client) Call(soapAction string, request, response interface{}) (httpResponse *http.Response, err error)

Call make a SOAP call

func (*Client) UseSoap11

func (c *Client) UseSoap11()

func (*Client) UseSoap12

func (c *Client) UseSoap12()

type Envelope

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

Envelope type

type Fault

type Fault 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"`
}

Fault type

func (*Fault) Error

func (f *Fault) Error() string
type Header struct {
	XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Header"`

	Header interface{}
}

Header type

type OperationHandlerFunc

type OperationHandlerFunc func(request interface{}, w http.ResponseWriter, httpRequest *http.Request) (response interface{}, err error)

OperationHandlerFunc runs the actual business logic - request is whatever you constructed in RequestFactoryFunc

type RequestFactoryFunc

type RequestFactoryFunc func() interface{}

RequestFactoryFunc constructs a request object for OperationHandlerFunc

type Server

type Server struct {
	Marshaller  XMLMarshaller
	ContentType string
	SoapVersion string
	// contains filtered or unexported fields
}

Server a SOAP server, which can be run standalone or used as a http.HandlerFunc

func NewServer

func NewServer() *Server

NewServer construct a new SOAP server

func (*Server) ListenAndServe

func (s *Server) ListenAndServe(addr string) error

ListenAndServe run standalone

func (*Server) RegisterHandler

func (s *Server) RegisterHandler(path string, action string, messageType string, requestFactory RequestFactoryFunc, operationHandlerFunc OperationHandlerFunc)

RegisterHandler register to handle an operation

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Server) UseSoap11

func (s *Server) UseSoap11()

func (*Server) UseSoap12

func (s *Server) UseSoap12()

func (*Server) WriteHeader

func (s *Server) WriteHeader(w http.ResponseWriter, code int)

WriteHeader first sets header like content-type and then writes the header

type XMLMarshaller

type XMLMarshaller interface {
	Marshal(v interface{}) ([]byte, error)
	Unmarshal(xml []byte, v interface{}) error
}

XMLMarshaller lets you inject your favourite custom xml implementation

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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