-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinternal.go
121 lines (100 loc) · 2.18 KB
/
internal.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
116
117
118
119
120
121
package gomailtrain
import (
"crypto/tls"
"errors"
"log"
"net/http"
"net/url"
"reflect"
"strconv"
"strings"
)
// NewAPI constructor
func NewAPI(uri string, token string) (*API, error) {
if len(uri) == 0 || len(token) == 0 {
return nil, errors.New("url or token not set")
}
u, err := url.ParseRequestURI(uri)
if err != nil {
return nil, err
}
a := new(API)
a.endPoint = u
a.token = token
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: false},
}
a.client = &http.Client{Transport: tr}
return a, nil
}
// NewAPIWithClient create a new API instance using an existing HTTP client
func NewAPIWithClient(uri string, client *http.Client) (*API, error) {
if len(uri) == 0 {
return nil, errors.New("url not set")
}
u, err := url.ParseRequestURI(uri)
if err != nil {
return nil, err
}
a := new(API)
a.endPoint = u
a.client = client
return a, nil
}
// VerifyTLS to enable disable certificate checks
func (a *API) VerifyTLS(set bool) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: !set},
}
a.client = &http.Client{Transport: tr}
}
// DebugFlag is the global debugging variable
var DebugFlag = false
// SetDebug enables debug output
func SetDebug(state bool) {
DebugFlag = state
}
// Debug outputs debug messages
func Debug(msg interface{}) {
if DebugFlag {
log.Printf("%+v\n", msg)
}
}
func createData(d interface{}) url.Values {
data := url.Values{}
typeOf := reflect.TypeOf(d)
valueOf := reflect.ValueOf(d)
for i := 0; i < valueOf.NumField(); i++ {
pname := strings.Split(typeOf.Field(i).Tag.Get("json"), ",")[0]
if pname == "-" {
continue
}
switch typeOf.Field(i).Type.Kind() {
case reflect.Bool:
if isInList(brokenValues, pname) {
if valueOf.Field(i).Bool() {
data.Set(pname, "yes")
}
} else {
b := "0"
if valueOf.Field(i).Bool() {
b = "1"
}
data.Set(pname, b)
}
case reflect.Int:
data.Set(pname, strconv.FormatInt(valueOf.Field(i).Int(), 10))
case reflect.String:
data.Set(pname, valueOf.Field(i).String())
}
}
return data
}
func isInList(list []string, e string) bool {
for _, v := range list {
if v == e {
return true
}
}
return false
}