-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.go
123 lines (101 loc) · 3.07 KB
/
util.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
122
123
package aoscxgo
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
)
// A custom error struct
type RequestError struct {
StatusCode string
Err error
}
// A custom error string
func (r *RequestError) Error() string {
return fmt.Sprintf("Status : %v\nError %v", r.StatusCode, r.Err)
}
// delete performs DELETE to the given URL and returns the response.
func delete(client *Client, url string) *http.Response {
req, _ := http.NewRequest("DELETE", url, nil)
req.Header.Set("accept", "*/*")
req.Close = false
req.AddCookie(client.Cookie)
res, err := client.Transport.RoundTrip(req)
//Handle Error
if err != nil {
log.Fatalf("An Error Occured %v %v", err, res.Status)
}
return res
}
// get performs GET to the given URL and returns the data response.
func get(client *Client, url string) (*http.Response, map[string]interface{}) {
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("accept", "*/*")
req.Header.Set("x-csrf-token", client.Csrf)
req.Close = false
req.AddCookie(client.Cookie)
res, err := client.Transport.RoundTrip(req)
if err != nil {
fmt.Println(err)
}
body := make(map[string]interface{})
err = json.NewDecoder(res.Body).Decode(&body)
return res, body
}
// get performs GET to the given URL and returns the data response.
func get_accept_text(client *Client, url string) (*http.Response, error) {
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("Accept", "text/plain")
// req.Header.Set("Content-Type", "text/plain")
req.Header.Set("x-csrf-token", client.Csrf)
req.Close = false
req.AddCookie(client.Cookie)
res, err := client.Transport.RoundTrip(req)
if err != nil {
fmt.Println(err)
}
return res, err
}
// get performs GET to the given URL and returns the data response.
func post(client *Client, url string, json_body *bytes.Buffer) *http.Response {
req, _ := http.NewRequest("POST", url, json_body)
req.Header.Set("Content-Type", "text/plain")
req.Header.Set("x-csrf-token", client.Csrf)
req.Close = false
req.AddCookie(client.Cookie)
res, err := client.Transport.RoundTrip(req)
//Handle Error
if err != nil {
log.Fatalf("An Error Occured %v %v", err, res.Status)
}
return res
}
// put performs PUT to the given URL and returns the response.
func put(client *Client, url string, json_body *bytes.Buffer) *http.Response {
req, _ := http.NewRequest("PUT", url, json_body)
req.Header.Set("accept", "*/*")
req.Header.Set("x-csrf-token", client.Csrf)
req.Close = false
req.AddCookie(client.Cookie)
res, err := client.Transport.RoundTrip(req)
//Handle Error
if err != nil {
log.Fatalf("An Error Occured %v %v", err, res.Status)
}
return res
}
// patch performs PATCH to the given URL and returns the response.
func patch(client *Client, url string, json_body *bytes.Buffer) *http.Response {
req, _ := http.NewRequest("PATCH", url, json_body)
req.Header.Set("accept", "*/*")
req.Header.Set("x-csrf-token", client.Csrf)
req.Close = false
req.AddCookie(client.Cookie)
res, err := client.Transport.RoundTrip(req)
//Handle Error
if err != nil {
log.Fatalf("An Error Occured %v %v", err, res.Status)
}
return res
}