-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinternals.go
97 lines (76 loc) · 1.96 KB
/
internals.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
package goidoit
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
// Debug function to enable debug output
func Debug(v bool) {
debug = v
}
// SkipTLSVerify disable SSL/TLS verification for self signed certificates
func SkipTLSVerify(v bool) {
insecure = v
}
// TypeAssertResult is a generic type assert function
func TypeAssertResult(data Response) (GenericResponse, error) {
ret := GenericResponse{Jsonrpc: data.Jsonrpc, Error: data.Error}
ret.Error.Data = ""
if data.Error.Data != nil {
ret.Error.Data = data.Error.Data.(map[string]interface{})
}
if data.Result != nil {
switch data.Result.(type) {
case []interface{}:
results := data.Result.([]interface{})
for i := range results {
ret.Result = append(ret.Result, results[i].(map[string]interface{}))
}
case interface{}:
ret.Result = append(ret.Result, data.Result.(map[string]interface{}))
}
}
return ret, nil
}
// getID increments request id's
func getID() int {
id++
return id
}
// GetParams is used to append nessesary parameters to user provided one
func GetParams(a API, parameters interface{}) interface{} {
var params map[string]interface{}
apikey := APIkey{a.APIkey}
jsonParameters, err := json.Marshal(parameters)
if err != nil {
log.Fatal("JSON ERROR: ", err)
}
json.Unmarshal(jsonParameters, ¶ms)
jsonAPIkey, err := json.Marshal(apikey)
if err != nil {
log.Fatal("JSON ERROR: ", err)
}
json.Unmarshal(jsonAPIkey, ¶ms)
return params
}
// ParseResponse parses json response
func ParseResponse(resp *http.Response) Response {
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal("PARSING ERROR: ", err)
}
// logging
debugPrint("----> # Response # <----\n%s\n", string(data))
var ret Response
_ = json.Unmarshal(data, &ret)
return ret
}
// debugPrint used for request/response debugging
func debugPrint(format string, a ...interface{}) (n int, err error) {
if debug {
return fmt.Printf(format, a...)
}
return 0, nil
}