forked from FracKenA/op5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.go
80 lines (61 loc) · 1.58 KB
/
query.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
package op5
import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
)
type HostResult []struct {
Host
}
func GetQuery(server Server, query string, columns string) ([]byte, string, int, error) {
var returnedError APIResponseError
customTransport := http.DefaultTransport.(*http.Transport).Clone()
parameters := url.Values{}
if query != "" {
parameters.Add("query", query)
}
if columns != ""{
parameters.Add("columns", columns)
}
myurl, err := url.Parse("https://" + server.Host + "/api/filter/query")
if err != nil {
fmt.Println(err)
}
myurl.RawQuery = parameters.Encode()
fmt.Println(myurl)
req, err := http.NewRequest("GET",myurl.String(), nil)
if err != nil {
fmt.Printf("Error%s\n", err)
}
req.SetBasicAuth(server.Username, server.Password)
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
if server.AllowInsecure == true {
customTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
} else {
customTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: false}
}
op5client := http.Client{Transport: customTransport, Timeout: 15 * time.Second}
response, err := op5client.Do(req)
if err != nil {
fmt.Printf("Error %s\n", err)
}
defer func() {
err := response.Body.Close()
if err != nil {
fmt.Printf("Error %s\n", err)
}
}()
e, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("Error %s\n", err)
}
status := response.Status
code := response.StatusCode
err = json.Unmarshal(e, &returnedError)
return e, status, code, err
}