xmlrpc

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2020 License: MIT Imports: 15 Imported by: 5

README

XML-RPC Client for Go

This is an implementation of client-side part of XML-RPC protocol in Go.

Usage

Add dependency to your project:

go get -u alexejk.io/go-xmlrpc

Use it by creating an *xmlrpc.Client and firing RPC method calls with Call().

package main

import(
    "fmt"

    "alexejk.io/go-xmlrpc"
)

func main() {
    client, _ := xmlrpc.NewClient("https://bugzilla.mozilla.org/xmlrpc.cgi")

    result := &struct {
        BugzillaVersion struct {
            Version string
        }
    }{}

    _ = client.Call("Bugzilla.version", nil, result)
    fmt.Printf("Version: %s\n", result.BugzillaVersion.Version)
}

If you want to customize any aspect of http.Client used to perform requests, use NewClientWithHttpClient instead. By defailt, an http.DefaultClient is used.

Argument encoding

Arguments to the remote RPC method are passed on as a *struct. This struct is encoded into XML-RPC types based on following rules:

  • Order of fields in struct type matters - fields are taken in the order they are defined on the type.
  • Numbers are to be specified as int (encoded as <int>) or float64 (encoded as <double>)
  • Both pointer and value references are accepted (pointers are followed to actual values)
Response decoding

Response is decoded following similar rules to argument encoding.

  • Order of fields is important.
  • Outer struct should contain exported field for each response parameter.

Documentation

Overview

This package includes everything that is required to perform XML-RPC requests by utilizing familiar rpc.Client interface.

The simplest use-case is creating a client towards an endpoint and making calls:

c, _ := NewClient("https://bugzilla.mozilla.org/xmlrpc.cgi")

resp := &struct {
	BugzillaVersion struct {
		Version string
	}
}{}

err = c.Call("Bugzilla.version", nil, resp)
fmt.Printf("Version: %s\n", resp.BugzillaVersion.Version)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	*rpc.Client
}

Client is responsible for making calls to RPC services with help of underlying rpc.Client.

func NewClient

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

NewClient creates a Client with http.DefaultClient. If provided endpoint is not valid, an error is returned.

func NewClientWithHttpClient

func NewClientWithHttpClient(endpoint string, httpClient *http.Client) (*Client, error)

NewClientWithHttpClient allows customization of http.Client used to make RPC calls. If provided endpoint is not valid, an error is returned.

type Codec

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

Codec implements methods required by rpc.ClientCodec In this implementation Codec is the one performing actual RPC requests with http.Client.

func NewCodec

func NewCodec(endpoint *url.URL, httpClient *http.Client) *Codec

NewCodec creates a new Codec bound to provided endpoint. Provided client will be used to perform RPC requests.

func (*Codec) Close

func (c *Codec) Close() error

func (*Codec) ReadResponseBody

func (c *Codec) ReadResponseBody(v interface{}) error

func (*Codec) ReadResponseHeader

func (c *Codec) ReadResponseHeader(resp *rpc.Response) error

func (*Codec) WriteRequest

func (c *Codec) WriteRequest(req *rpc.Request, args interface{}) error

type Decoder added in v0.1.1

type Decoder interface {
	DecodeRaw(body []byte, v interface{}) error
	Decode(response *Response, v interface{}) error
	DecodeFault(response *Response) *Fault
}

Decoder implementations provide mechanisms for parsing of XML-RPC responses to native data-types.

type Encoder added in v0.1.1

type Encoder interface {
	Encode(w io.Writer, methodName string, args interface{}) error
}

Encoder implementations are responsible for handling encoding of XML-RPC requests to the proper wire format.

type Fault

type Fault struct {
	// Code provides numerical failure code
	Code int
	// String includes more detailed information about the fault, such as error name and cause
	String string
}

Fault is a wrapper for XML-RPC fault object

func (*Fault) Error

func (f *Fault) Error() string

type Response added in v0.1.1

type Response struct {
	Params []ResponseParam `xml:"params>param"`
	Fault  *ResponseFault  `xml:"fault,omitempty"`
}

Response is the basic parsed object of the XML-RPC response body. While it's not convenient to use this object directly - it contains all the information needed to unmarshal into other data-types.

func NewResponse added in v0.1.1

func NewResponse(body []byte) (*Response, error)

NewResponse creates a Response object from XML body. It relies on XML Unmarshaler and if it fails - error is returned.

type ResponseFault added in v0.1.1

type ResponseFault struct {
	Value ResponseValue `xml:"value"`
}

type ResponseParam added in v0.1.1

type ResponseParam struct {
	Value ResponseValue `xml:"value"`
}

type ResponseStructMember added in v0.1.1

type ResponseStructMember struct {
	Name  string        `xml:"name"`
	Value ResponseValue `xml:"value"`
}

type ResponseValue added in v0.1.1

type ResponseValue struct {
	Array    []*ResponseValue        `xml:"array>data>value"`
	Struct   []*ResponseStructMember `xml:"struct>member"`
	String   string                  `xml:"string"`
	Int      string                  `xml:"int"`
	Int4     string                  `xml:"i4"`
	Double   string                  `xml:"double"`
	Boolean  string                  `xml:"boolean"`
	DateTime string                  `xml:"dateTime.iso8601"`
	Base64   string                  `xml:"base64"`
}

type StdDecoder added in v0.1.1

type StdDecoder struct{}

StdDecoder is the default implementation of the Decoder interface.

func (*StdDecoder) Decode added in v0.1.1

func (d *StdDecoder) Decode(response *Response, v interface{}) error

func (*StdDecoder) DecodeFault added in v0.1.1

func (d *StdDecoder) DecodeFault(response *Response) *Fault

func (*StdDecoder) DecodeRaw added in v0.1.1

func (d *StdDecoder) DecodeRaw(body []byte, v interface{}) error

type StdEncoder added in v0.1.1

type StdEncoder struct{}

StdEncoder is the default implementation of Encoder interface.

func (*StdEncoder) Encode added in v0.1.1

func (e *StdEncoder) Encode(w io.Writer, methodName string, args interface{}) error

Jump to

Keyboard shortcuts

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