-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
45 lines (40 loc) · 1.25 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 gonvs
import (
"encoding/json"
"errors"
"reflect"
"strconv"
)
func (c *Client) sendRequest(methodName string, resultPointer interface{}, requestData []interface{}) error {
requestFields := []json.RawMessage{}
for i := 0; i < len(requestData); i++ {
fieldData := requestData[i]
switch fieldData.(type) {
default:
return errors.New("unknown field value: " + reflect.ValueOf(fieldData).String())
case string:
requestFields = append(requestFields, wrapJSONParam(fieldData.(string)))
case int:
requestFields = append(requestFields, json.RawMessage(strconv.Itoa(fieldData.(int))))
case int64:
requestFields = append(requestFields, json.RawMessage(
strconv.FormatInt(fieldData.(int64), 10)),
)
case ValueType:
requestFields = append(requestFields, wrapJSONParam(string(fieldData.(ValueType))))
}
}
response, err := c.RPC.RawRequest(methodName, requestFields)
if err != nil {
return errors.New("failed to get entrys: " + err.Error())
}
jsonBytes, err := response.MarshalJSON()
if err != nil {
return errors.New("failed to json encode client response: " + err.Error())
}
err = json.Unmarshal(jsonBytes, resultPointer)
if err != nil {
return errors.New("failed to decode json response: " + err.Error())
}
return nil
}