2012-09-18 17:09:38 +04:00
|
|
|
package xmlrpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/rpc"
|
2012-09-18 22:43:02 +04:00
|
|
|
"reflect"
|
2012-09-18 17:09:38 +04:00
|
|
|
)
|
|
|
|
|
2012-09-19 10:04:01 +04:00
|
|
|
// clientCodec is rpc.ClientCodec interface implementation.
|
2012-09-18 17:09:38 +04:00
|
|
|
type clientCodec struct {
|
2012-09-19 10:04:01 +04:00
|
|
|
// url presents url of xmlrpc service
|
|
|
|
url string
|
|
|
|
|
|
|
|
// httpClient works with HTTP protocol
|
|
|
|
httpClient *http.Client
|
2012-09-18 17:09:38 +04:00
|
|
|
|
2012-09-19 10:04:01 +04:00
|
|
|
// responses presents map of active requests. It is required to return request id, that
|
|
|
|
// rpc.Client can mark them as done.
|
|
|
|
responses map[uint64]*http.Response
|
|
|
|
|
|
|
|
// responseBody holds response body of last request.
|
2012-09-18 17:09:38 +04:00
|
|
|
responseBody []byte
|
2012-09-18 23:31:11 +04:00
|
|
|
|
2012-09-19 10:04:01 +04:00
|
|
|
// ready presents channel, that is used to link request and it`s response.
|
2012-09-18 23:31:11 +04:00
|
|
|
ready chan uint64
|
2012-09-18 17:09:38 +04:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:47:01 +04:00
|
|
|
func (codec *clientCodec) WriteRequest(request *rpc.Request, params interface{}) (err error) {
|
|
|
|
httpRequest, err := newRequest(codec.url, request.ServiceMethod, params)
|
2012-09-18 17:09:38 +04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2012-09-19 10:04:01 +04:00
|
|
|
var httpResponse *http.Response
|
|
|
|
httpResponse, err = codec.httpClient.Do(httpRequest)
|
2012-09-18 17:09:38 +04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2012-09-19 10:04:01 +04:00
|
|
|
codec.responses[request.Seq] = httpResponse
|
2012-09-18 23:31:11 +04:00
|
|
|
codec.ready <- request.Seq
|
|
|
|
|
|
|
|
return nil
|
2012-09-18 17:09:38 +04:00
|
|
|
}
|
|
|
|
|
2012-09-19 10:04:01 +04:00
|
|
|
func (codec *clientCodec) ReadResponseHeader(response *rpc.Response) (err error) {
|
2012-09-18 23:31:11 +04:00
|
|
|
seq := <-codec.ready
|
2012-09-19 10:04:01 +04:00
|
|
|
httpResponse := codec.responses[seq]
|
2012-09-18 17:09:38 +04:00
|
|
|
|
|
|
|
codec.responseBody, err = ioutil.ReadAll(httpResponse.Body)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
httpResponse.Body.Close()
|
|
|
|
|
2012-09-18 23:31:11 +04:00
|
|
|
response.Seq = seq
|
2012-09-19 10:04:01 +04:00
|
|
|
delete(codec.responses, seq)
|
2012-09-18 23:31:11 +04:00
|
|
|
|
2012-09-18 17:09:38 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (codec *clientCodec) ReadResponseBody(body interface{}) (err error) {
|
2012-09-18 22:43:02 +04:00
|
|
|
var result interface{}
|
|
|
|
result, err = parseResponse(codec.responseBody)
|
2012-09-18 17:09:38 +04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2012-09-18 22:43:02 +04:00
|
|
|
v := reflect.ValueOf(body)
|
|
|
|
|
|
|
|
if v.Kind() == reflect.Ptr {
|
|
|
|
v = v.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
v.Set(reflect.ValueOf(result))
|
|
|
|
|
2012-09-18 17:09:38 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (codec *clientCodec) Close() error {
|
2012-09-19 10:04:01 +04:00
|
|
|
transport := codec.httpClient.Transport.(*http.Transport)
|
|
|
|
transport.CloseIdleConnections()
|
|
|
|
return nil
|
2012-09-18 17:09:38 +04:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:47:01 +04:00
|
|
|
// NewClient returns instance of rpc.Client object, that is used to send request to xmlrpc service.
|
|
|
|
func NewClient(url string, transport *http.Transport) (*rpc.Client, error) {
|
|
|
|
if transport == nil {
|
|
|
|
transport = &http.Transport{}
|
|
|
|
}
|
|
|
|
|
2012-09-19 10:04:01 +04:00
|
|
|
httpClient := &http.Client{ Transport: transport }
|
2012-09-18 17:09:38 +04:00
|
|
|
|
2012-09-18 23:31:11 +04:00
|
|
|
codec := clientCodec{
|
2012-09-19 10:04:01 +04:00
|
|
|
url: url,
|
|
|
|
httpClient: httpClient,
|
2012-09-18 23:31:11 +04:00
|
|
|
ready: make(chan uint64),
|
2012-09-19 10:04:01 +04:00
|
|
|
responses: make(map[uint64]*http.Response),
|
2012-09-18 23:31:11 +04:00
|
|
|
}
|
2012-09-19 12:47:01 +04:00
|
|
|
|
2012-09-18 17:09:38 +04:00
|
|
|
return rpc.NewClientWithCodec(&codec), nil
|
|
|
|
}
|