xmlrpc

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

README

#xmlrpc

Most code and doc fork from https://code.google.com/p/go-xmlrpc/

Package xmlrpc provides a rudimentary interface for sending and receiving XML-RPC requests.

Procedures can be provided by any objects registered with the server. An XML-RPC server is:

    type SomeObject struct {
        size int
    }

    func (so *SomeObject) GetSize() int { return so.size }
    func (so *SomeObject) SetSize(size int) { so.size = size }
    h := xmlrpc.NewHandler()
    h.Register(&SomeObject{}, nil, false)

    http.Handle("/rpc", h)
    http.ListenAndServe(":2345", nil)

This will add 'GetSize' and 'SetSize' to the server.

The second parameter of the Register method is a name mapping function. This mapping function takes a method name as a parameter and can return "" to ignore a method or return a transformed string.

As an example, this function will only accept methods starting with 'RPC'. It will replace the initial "RPC" with "xmlrpc." and convert the first character after 'RPC' to lowercase, so "GetSize" would be ignored and "RPCGetSize" would be registered as "xmlrpc.getSize":

    func clientMapper(methodname string) string {
        if !strings.HasPrefix(methodname, "RPC") {
            return ""
        }

        var buf bytes.Buffer
        
        buf.WriteString("xmlrpc.")
        r, n := utf8.DecodeRuneInString(methodname[3:])
        buf.WriteRune(unicode.ToLower(r))
        buf.WriteString(methodname[3+n:])

        return buf.String()
    }

Clients are created with xmlrpc.NewClient(host string, int port):

    client, err := xmlrpc.NewClient("localhost", 2345)
    if err != nil {
        fmt.Fprintf(os.Stderr, "Cannot create XML-RPC client: %v\n", err)
        return
    }

Remote procedure calls are made using client.RPCCall, whose parameters are the name of the remote procedure along with any needed parameters:

    reply, cerr, fault := client.RPCCall("SetThing", 123, "abc")
    if cerr != nil {
        fmt.Fprintf(os.Stderr, "Cannot call SetThing: %v\n", cerr)
        return
    } else if fault != nil {
        fmt.Fprintf(os.Stderr, "Exception from SetThing: %v\n", fault)
        return
    }

    fmt.Printf("SetThing(123, \"abc\") returned %v\n", reply)

(Note that parameters are optional so client.RPCCall("foo") is valid code.)

You also can register a function using RegFunc(f interface, name string, padParams bool) Which will register f, with name, if name is "", then use name of f, check the demo

Documentation

Index

Constants

View Source
const ISO8601_LAYOUT = "20060102T15:04:05"

Variables

This section is empty.

Functions

func Marshal

func Marshal(w io.Writer, methodName string, args ...interface{}) error

Write a local data object as an XML-RPC request

Types

type Client

type Client struct {
	http.Client
	// contains filtered or unexported fields
}

XML-RPC client data

func NewClient

func NewClient(address string) (*Client, error)

connect to a remote XML-RPC server

func NewClient(host string, port int) (*Client, error) {
   address := fmt.Sprintf("http://%s:%d/RPC2", host, port)

func (*Client) RPCCall

func (c *Client) RPCCall(methodName string,
	args ...interface{}) (interface{}, error, *Fault)

call a procedure on a remote XML-RPC server

type DFT

type DFT []interface{}

type DICT

type DICT map[string]interface{}

python dict, a map can have different type value

type Fault

type Fault struct {
	Code int
	Msg  string
}

A Fault represents an error or exception in the procedure call being run on the remote machine

func NewFault

func NewFault(code int, msg string) *Fault

func Unmarshal

func Unmarshal(r io.Reader) (string, interface{}, error, *Fault)

Translate an XML stream into a local data object

func UnmarshalString

func UnmarshalString(s string) (string, interface{}, error, *Fault)

Translate an XML string into a local data object

func (*Fault) String

func (f *Fault) String() string

Return a string representation of the XML-RPC fault

type Handler

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

Map from XML-RPC procedure names to Go methods

func NewHandler

func NewHandler() *Handler

create a new handler mapping XML-RPC procedure names to Go methods

func (*Handler) GetMethodList

func (h *Handler) GetMethodList() (ks []string)

help for debug

func (*Handler) RegFunc

func (h *Handler) RegFunc(f interface{}, name string, dft DFT) error

register a func, if name is "", then use func name

func (*Handler) Register

func (h *Handler) Register(obj interface{}, mapper func(string) string,
	padParams bool) error

register all methods associated with the Go object, passing them through the name mapper if one is supplied

The name mapper can return "" to ignore a method or transform the name as desired

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(resp http.ResponseWriter, req *http.Request)

handle an XML-RPC request

func (*Handler) SetLogf

func (h *Handler) SetLogf(logf func(*http.Request, int, string))

type LIST

type LIST []interface{}

python list, a list can have different type item

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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