-
Notifications
You must be signed in to change notification settings - Fork 75
/
client.go
115 lines (103 loc) · 2.88 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) 2020-2021 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/decred/politeia/politeiad/api/v1/identity"
"github.com/decred/politeia/util"
)
// Client provides a client for interacting with the politeiad API.
type Client struct {
rpcHost string
rpcCert string
rpcUser string
rpcPass string
http *http.Client
pid *identity.PublicIdentity
}
// ErrorReply represents the request body that is returned from politeaid when
// an error occurs. PluginID will only be populated if the error occurred
// during execution of a plugin command.
type ErrorReply struct {
PluginID string `json:"pluginid"`
ErrorCode uint32 `json:"errorcode"`
ErrorContext string `json:"errorcontext"`
}
// RespError represents a politeiad response error. A RespError is returned
// anytime the politeiad response is not a 200.
type RespError struct {
HTTPCode int
ErrorReply ErrorReply
}
// Error satisfies the error interface.
func (e RespError) Error() string {
if e.ErrorReply.PluginID != "" {
return fmt.Sprintf("politeiad plugin error: %v %v %v",
e.HTTPCode, e.ErrorReply.PluginID, e.ErrorReply.ErrorCode)
}
return fmt.Sprintf("politeiad error: %v %v",
e.HTTPCode, e.ErrorReply.ErrorCode)
}
// makeReq makes a politeiad http request to the method and route provided,
// serializing the provided object as the request body, and returning a byte
// slice of the response body. A RespError is returned if politeiad responds
// with anything other than a 200 http status code.
func (c *Client) makeReq(ctx context.Context, method, api, route string, v interface{}) ([]byte, error) {
// Serialize body
var (
reqBody []byte
err error
)
if v != nil {
reqBody, err = json.Marshal(v)
if err != nil {
return nil, err
}
}
// Send request
fullRoute := c.rpcHost + api + route
req, err := http.NewRequestWithContext(ctx, method,
fullRoute, bytes.NewReader(reqBody))
if err != nil {
return nil, err
}
req.SetBasicAuth(c.rpcUser, c.rpcPass)
r, err := c.http.Do(req)
if err != nil {
return nil, err
}
defer r.Body.Close()
// Handle reply
if r.StatusCode != http.StatusOK {
var e ErrorReply
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&e); err != nil {
return nil, fmt.Errorf("status code %v: %v", r.StatusCode, err)
}
return nil, RespError{
HTTPCode: r.StatusCode,
ErrorReply: e,
}
}
return util.RespBody(r), nil
}
// New returns a new politeiad client.
func New(rpcHost, rpcCert, rpcUser, rpcPass string, pid *identity.PublicIdentity) (*Client, error) {
h, err := util.NewHTTPClient(false, rpcCert)
if err != nil {
return nil, err
}
return &Client{
rpcHost: rpcHost,
rpcCert: rpcCert,
rpcUser: rpcUser,
rpcPass: rpcPass,
http: h,
pid: pid,
}, nil
}