generated from alexejk/go-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclient.go
45 lines (37 loc) · 1.06 KB
/
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
40
41
42
43
44
45
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
codec *Codec
}
// NewClient creates a Client with http.DefaultClient.
// If provided endpoint is not valid, an error is returned.
func NewClient(endpoint string, opts ...Option) (*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, http.DefaultClient)
c := &Client{
codec: codec,
Client: rpc.NewClientWithCodec(codec),
}
// Apply options
for _, opt := range opts {
opt(c)
}
return c, nil
}
// NewCustomClient allows customization of http.Client used to make RPC calls.
// If provided endpoint is not valid, an error is returned.
// Deprecated: prefer using NewClient with HttpClient Option
func NewCustomClient(endpoint string, httpClient *http.Client) (*Client, error) {
return NewClient(endpoint, HttpClient(httpClient))
}