forked from abcdsxg/go-wordpress-xmlrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
73 lines (61 loc) · 1.87 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package xmlrpc
import (
"io/ioutil"
"log"
"net/http"
"github.com/abcdsxg/go-wordpress-xmlrpc/wordpress"
"github.com/kolo/xmlrpc"
)
type HttpRT struct {
http.RoundTripper
}
// NewHttpRT return a http.RoundTripper can print the request body
func NewHttpRT(t http.RoundTripper) http.RoundTripper {
return &HttpRT{t}
}
// RoundTrip implement a http.RoundTripper can print the request body
func (t HttpRT) RoundTrip(req *http.Request) (*http.Response, error) {
// you can customize to get more control over connection options
// example: print the request body
b, err := req.GetBody()
if err != nil {
log.Println(err)
}
r, err := ioutil.ReadAll(b)
if err != nil {
log.Println(err)
}
log.Println(string(r))
return t.RoundTripper.RoundTrip(req)
}
// Client Packaging the xmlrpc client
type Client struct {
*xmlrpc.Client
UserInfo
}
// UserInfo wordpress's username and password
type UserInfo struct {
Username string
Password string
}
// NewDefaultClient default implement a http.RoundTripper can print the request body
func NewDefaultClient(url string, info UserInfo) (*Client, error) {
t := NewHttpRT(http.DefaultTransport)
c, err := xmlrpc.NewClient(url, t)
return &Client{Client: c, UserInfo: info}, err
}
// NewClient without http.RoundTripper
func NewClient(url string, info UserInfo) (*Client, error) {
c, err := xmlrpc.NewClient(url, nil)
return &Client{Client: c, UserInfo: info}, err
}
// NewCustomizeClient you can Customize your http.RoundTripper
func NewCustomizeClient(url string, t http.RoundTripper, info UserInfo) (*Client, error) {
c, err := xmlrpc.NewClient(url, t)
return &Client{Client: c, UserInfo: info}, err
}
// Call abstract to proxy xmlrpc call
func (c *Client) Call(baseCall wordpress.BaseCall) (result interface{}, err error) {
err = c.Client.Call(baseCall.GetMethord(), baseCall.GetArgs(c.Username, c.Password), &result)
return result, err
}