-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathidoit.go
89 lines (76 loc) · 1.93 KB
/
idoit.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
package goidoit
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
)
// Request implements basic idoit requests
func (a API) Request(method string, parameters interface{}) (Response, error) {
var params = GetParams(a, parameters)
id = getID()
data := Request{
Version: "2.0",
Method: method,
Params: params,
ID: id,
}
dataJSON, err := json.Marshal(data)
if err != nil {
fmt.Println("JSON ERROR: ", err)
return Response{}, err
}
// logging
debugPrint("----> # Request # <----\n%s\n", string(dataJSON))
req, err := http.NewRequest("POST", a.URL, bytes.NewBuffer(dataJSON))
if err != nil {
fmt.Println("REQUEST ERROR: ", err)
return Response{}, err
}
req.Header.Add("content-type", "application/json")
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: false},
}
if insecure {
tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}
client := &http.Client{Transport: tr}
// use X-RPC auth or fallback to API-Key only
if method == "idoit.login" {
req.Header["X-RPC-Auth-Username"] = []string{a.Username}
req.Header["X-RPC-Auth-Password"] = []string{a.Password}
} else {
if a.IsLoggedIn() {
req.Header["X-RPC-Auth-Session"] = []string{a.SessionID}
}
}
resp, err := client.Do(req)
if err != nil {
fmt.Println("REQUEST ERROR: ", err)
return Response{}, err
}
var ret = ParseResponse(resp)
return ret, nil
}
// Search implements the idoit.search method
func (a *API) Search(query string) (GenericResponse, error) {
params := struct {
Query string `json:"q"`
}{query}
data, err := a.Request("idoit.search", ¶ms)
if err != nil {
return GenericResponse{}, err
}
return TypeAssertResult(data)
}
// Version implements idoit.version method
func (a *API) Version() (GenericResponse, error) {
data, err := a.Request("idoit.version", nil)
if err != nil {
return GenericResponse{}, err
}
return TypeAssertResult(data)
}