generated from alexejk/go-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
client.go
39 lines (30 loc) · 935 Bytes
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package xmlrpc
import (
"fmt"
"net/http"
"net/rpc"
"net/url"
)
// Client is responsible for making calls to RPC services with help of underlying rpc.Client.
type Client struct {
*rpc.Client
}
// NewClient creates a Client with http.DefaultClient.
// If provided endpoint is not valid, an error is returned.
func NewClient(endpoint string) (*Client, error) {
return NewClientWithHttpClient(endpoint, http.DefaultClient)
}
// NewClientWithHttpClient allows customization of http.Client used to make RPC calls.
// If provided endpoint is not valid, an error is returned.
func NewClientWithHttpClient(endpoint string, httpClient *http.Client) (*Client, error) {
// Parse Endpoint URL
endpointUrl, err := url.Parse(endpoint)
if err != nil {
return nil, fmt.Errorf("invalid endpoint url: %w", err)
}
codec := NewCodec(endpointUrl, httpClient)
c := &Client{
Client: rpc.NewClientWithCodec(codec),
}
return c, nil
}